blob: 586c7391a066554b023e0e6db8e6f12b615164ea [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
Pierre Ossmand129bce2006-03-24 03:18:17 -080052static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053053static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053054static void sdhci_tuning_timer(unsigned long data);
Kevin Liu52983382013-01-31 11:31:37 +080055static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
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);
Adrian Hunterf0710a52013-05-06 12:17:32 +030060static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
61static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030062#else
63static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
64{
65 return 0;
66}
67static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
68{
69 return 0;
70}
Adrian Hunterf0710a52013-05-06 12:17:32 +030071static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
72{
73}
74static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
75{
76}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030077#endif
78
Pierre Ossmand129bce2006-03-24 03:18:17 -080079static void sdhci_dumpregs(struct sdhci_host *host)
80{
Girish K Sa3c76eb2011-10-11 11:44:09 +053081 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070082 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080083
Girish K Sa3c76eb2011-10-11 11:44:09 +053084 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030085 sdhci_readl(host, SDHCI_DMA_ADDRESS),
86 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053087 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030088 sdhci_readw(host, SDHCI_BLOCK_SIZE),
89 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053090 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030091 sdhci_readl(host, SDHCI_ARGUMENT),
92 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053093 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030094 sdhci_readl(host, SDHCI_PRESENT_STATE),
95 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053096 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030097 sdhci_readb(host, SDHCI_POWER_CONTROL),
98 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053099 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300100 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
101 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530102 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300103 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
104 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530105 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readl(host, SDHCI_INT_ENABLE),
107 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530108 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300109 sdhci_readw(host, SDHCI_ACMD12_ERR),
110 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530111 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300112 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500113 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530114 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500115 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300116 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530117 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530118 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800119
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100120 if (host->flags & SDHCI_USE_ADMA)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530121 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100122 readl(host->ioaddr + SDHCI_ADMA_ERROR),
123 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
124
Girish K Sa3c76eb2011-10-11 11:44:09 +0530125 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800126}
127
128/*****************************************************************************\
129 * *
130 * Low level functions *
131 * *
132\*****************************************************************************/
133
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300134static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
135{
Russell King5b4f1f62014-04-25 12:57:02 +0100136 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300137
Adrian Hunterc79396c2011-12-27 15:48:42 +0200138 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100139 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300140 return;
141
Russell King5b4f1f62014-04-25 12:57:02 +0100142 if (enable) {
143 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
144 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800145
Russell King5b4f1f62014-04-25 12:57:02 +0100146 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
147 SDHCI_INT_CARD_INSERT;
148 } else {
149 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
150 }
Russell Kingb537f942014-04-25 12:56:01 +0100151
152 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
153 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300154}
155
156static void sdhci_enable_card_detection(struct sdhci_host *host)
157{
158 sdhci_set_card_detection(host, true);
159}
160
161static void sdhci_disable_card_detection(struct sdhci_host *host)
162{
163 sdhci_set_card_detection(host, false);
164}
165
Russell King03231f92014-04-25 12:57:12 +0100166void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800167{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700168 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800169
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300170 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800171
Adrian Hunterf0710a52013-05-06 12:17:32 +0300172 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800173 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300174 /* Reset-all turns off SD Bus Power */
175 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
176 sdhci_runtime_pm_bus_off(host);
177 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800178
Pierre Ossmane16514d82006-06-30 02:22:24 -0700179 /* Wait max 100 ms */
180 timeout = 100;
181
182 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300183 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700184 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530185 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700186 mmc_hostname(host->mmc), (int)mask);
187 sdhci_dumpregs(host);
188 return;
189 }
190 timeout--;
191 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800192 }
Russell King03231f92014-04-25 12:57:12 +0100193}
194EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300195
Russell King03231f92014-04-25 12:57:12 +0100196static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
197{
198 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
199 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
200 SDHCI_CARD_PRESENT))
201 return;
202 }
203
204 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800205
Russell Kingda91a8f2014-04-25 13:00:12 +0100206 if (mask & SDHCI_RESET_ALL) {
207 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
208 if (host->ops->enable_dma)
209 host->ops->enable_dma(host);
210 }
211
212 /* Resetting the controller clears many */
213 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800214 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800215}
216
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800217static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
218
219static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800220{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800221 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100222 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800223 else
Russell King03231f92014-04-25 12:57:12 +0100224 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800225
Russell Kingb537f942014-04-25 12:56:01 +0100226 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
227 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
228 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
229 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
230 SDHCI_INT_RESPONSE;
231
232 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
233 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800234
235 if (soft) {
236 /* force clock reconfiguration */
237 host->clock = 0;
238 sdhci_set_ios(host->mmc, &host->mmc->ios);
239 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300240}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800241
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300242static void sdhci_reinit(struct sdhci_host *host)
243{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800244 sdhci_init(host, 0);
Aaron Lub67c6b42012-06-29 16:17:31 +0800245 /*
246 * Retuning stuffs are affected by different cards inserted and only
247 * applicable to UHS-I cards. So reset these fields to their initial
248 * value when card is removed.
249 */
Aaron Lu973905f2012-07-04 13:29:09 +0800250 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
251 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
252
Aaron Lub67c6b42012-06-29 16:17:31 +0800253 del_timer_sync(&host->tuning_timer);
254 host->flags &= ~SDHCI_NEEDS_RETUNING;
255 host->mmc->max_blk_count =
256 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
257 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300258 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800259}
260
261static void sdhci_activate_led(struct sdhci_host *host)
262{
263 u8 ctrl;
264
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300265 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800266 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300267 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800268}
269
270static void sdhci_deactivate_led(struct sdhci_host *host)
271{
272 u8 ctrl;
273
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300274 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800275 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300276 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800277}
278
Pierre Ossmanf9134312008-12-21 17:01:48 +0100279#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100280static void sdhci_led_control(struct led_classdev *led,
281 enum led_brightness brightness)
282{
283 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
284 unsigned long flags;
285
286 spin_lock_irqsave(&host->lock, flags);
287
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300288 if (host->runtime_suspended)
289 goto out;
290
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100291 if (brightness == LED_OFF)
292 sdhci_deactivate_led(host);
293 else
294 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300295out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100296 spin_unlock_irqrestore(&host->lock, flags);
297}
298#endif
299
Pierre Ossmand129bce2006-03-24 03:18:17 -0800300/*****************************************************************************\
301 * *
302 * Core functions *
303 * *
304\*****************************************************************************/
305
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100306static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307{
Pierre Ossman76591502008-07-21 00:32:11 +0200308 unsigned long flags;
309 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700310 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200311 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800312
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800314
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100315 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200316 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800317
Pierre Ossman76591502008-07-21 00:32:11 +0200318 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800319
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100320 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200321 if (!sg_miter_next(&host->sg_miter))
322 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800323
Pierre Ossman76591502008-07-21 00:32:11 +0200324 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800325
Pierre Ossman76591502008-07-21 00:32:11 +0200326 blksize -= len;
327 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200328
Pierre Ossman76591502008-07-21 00:32:11 +0200329 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800330
Pierre Ossman76591502008-07-21 00:32:11 +0200331 while (len) {
332 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300333 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200334 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800335 }
Pierre Ossman76591502008-07-21 00:32:11 +0200336
337 *buf = scratch & 0xFF;
338
339 buf++;
340 scratch >>= 8;
341 chunk--;
342 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800343 }
344 }
Pierre Ossman76591502008-07-21 00:32:11 +0200345
346 sg_miter_stop(&host->sg_miter);
347
348 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100349}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800350
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100351static void sdhci_write_block_pio(struct sdhci_host *host)
352{
Pierre Ossman76591502008-07-21 00:32:11 +0200353 unsigned long flags;
354 size_t blksize, len, chunk;
355 u32 scratch;
356 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100357
358 DBG("PIO writing\n");
359
360 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200361 chunk = 0;
362 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100363
Pierre Ossman76591502008-07-21 00:32:11 +0200364 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100365
366 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200367 if (!sg_miter_next(&host->sg_miter))
368 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100369
Pierre Ossman76591502008-07-21 00:32:11 +0200370 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200371
Pierre Ossman76591502008-07-21 00:32:11 +0200372 blksize -= len;
373 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100374
Pierre Ossman76591502008-07-21 00:32:11 +0200375 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100376
Pierre Ossman76591502008-07-21 00:32:11 +0200377 while (len) {
378 scratch |= (u32)*buf << (chunk * 8);
379
380 buf++;
381 chunk++;
382 len--;
383
384 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300385 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200386 chunk = 0;
387 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100388 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100389 }
390 }
Pierre Ossman76591502008-07-21 00:32:11 +0200391
392 sg_miter_stop(&host->sg_miter);
393
394 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100395}
396
397static void sdhci_transfer_pio(struct sdhci_host *host)
398{
399 u32 mask;
400
401 BUG_ON(!host->data);
402
Pierre Ossman76591502008-07-21 00:32:11 +0200403 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100404 return;
405
406 if (host->data->flags & MMC_DATA_READ)
407 mask = SDHCI_DATA_AVAILABLE;
408 else
409 mask = SDHCI_SPACE_AVAILABLE;
410
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200411 /*
412 * Some controllers (JMicron JMB38x) mess up the buffer bits
413 * for transfers < 4 bytes. As long as it is just one block,
414 * we can ignore the bits.
415 */
416 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
417 (host->data->blocks == 1))
418 mask = ~0;
419
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300420 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300421 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
422 udelay(100);
423
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100424 if (host->data->flags & MMC_DATA_READ)
425 sdhci_read_block_pio(host);
426 else
427 sdhci_write_block_pio(host);
428
Pierre Ossman76591502008-07-21 00:32:11 +0200429 host->blocks--;
430 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100431 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100432 }
433
434 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800435}
436
Pierre Ossman2134a922008-06-28 18:28:51 +0200437static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
438{
439 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800440 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200441}
442
443static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
444{
Cong Wang482fce92011-11-27 13:27:00 +0800445 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200446 local_irq_restore(*flags);
447}
448
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200449static void sdhci_adma_write_desc(void *desc, u32 addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800450{
Ben Dooks9e506f32010-03-05 13:43:29 -0800451 __le32 *dataddr = (__le32 __force *)(desc + 4);
452 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800453
Ben Dooks9e506f32010-03-05 13:43:29 -0800454 /* SDHCI specification says ADMA descriptors should be 4 byte
455 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800456
Ben Dooks9e506f32010-03-05 13:43:29 -0800457 cmdlen[0] = cpu_to_le16(cmd);
458 cmdlen[1] = cpu_to_le16(len);
459
460 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800461}
462
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200463static void sdhci_adma_mark_end(void *desc)
464{
465 u8 *dma_desc = desc;
466
467 dma_desc[0] |= 0x2; /* end */
468}
469
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200470static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200471 struct mmc_data *data)
472{
473 int direction;
474
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200475 void *desc;
476 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200477 dma_addr_t addr;
478 dma_addr_t align_addr;
479 int len, offset;
480
481 struct scatterlist *sg;
482 int i;
483 char *buffer;
484 unsigned long flags;
485
486 /*
487 * The spec does not specify endianness of descriptor table.
488 * We currently guess that it is LE.
489 */
490
491 if (data->flags & MMC_DATA_READ)
492 direction = DMA_FROM_DEVICE;
493 else
494 direction = DMA_TO_DEVICE;
495
Pierre Ossman2134a922008-06-28 18:28:51 +0200496 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200497 host->align_buffer, host->align_buffer_sz, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700498 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200499 goto fail;
Adrian Hunter76fe3792014-11-04 12:42:42 +0200500 BUG_ON(host->align_addr & host->align_mask);
Pierre Ossman2134a922008-06-28 18:28:51 +0200501
502 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
503 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200504 if (host->sg_count == 0)
505 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200506
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200507 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200508 align = host->align_buffer;
509
510 align_addr = host->align_addr;
511
512 for_each_sg(data->sg, sg, host->sg_count, i) {
513 addr = sg_dma_address(sg);
514 len = sg_dma_len(sg);
515
516 /*
517 * The SDHCI specification states that ADMA
518 * addresses must be 32-bit aligned. If they
519 * aren't, then we use a bounce buffer for
520 * the (up to three) bytes that screw up the
521 * alignment.
522 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200523 offset = (host->align_sz - (addr & host->align_mask)) &
524 host->align_mask;
Pierre Ossman2134a922008-06-28 18:28:51 +0200525 if (offset) {
526 if (data->flags & MMC_DATA_WRITE) {
527 buffer = sdhci_kmap_atomic(sg, &flags);
Adrian Hunter8be78c62014-11-04 12:42:36 +0200528 WARN_ON(((long)buffer & (PAGE_SIZE - 1)) >
529 (PAGE_SIZE - offset));
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 */
Adrian Hunter08621b12014-11-04 12:42:38 +0200535 sdhci_adma_write_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200536
537 BUG_ON(offset > 65536);
538
Adrian Hunter76fe3792014-11-04 12:42:42 +0200539 align += host->align_sz;
540 align_addr += host->align_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200541
Adrian Hunter76fe3792014-11-04 12:42:42 +0200542 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200543
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 */
Adrian Hunter08621b12014-11-04 12:42:38 +0200551 sdhci_adma_write_desc(desc, addr, len, 0x21);
Adrian Hunter76fe3792014-11-04 12:42:42 +0200552 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200553
554 /*
555 * If this triggers then we have a calculation bug
556 * somewhere. :/
557 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200558 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200559 }
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 */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200565 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200566 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200567 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700568 }
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 */
Adrian Hunter08621b12014-11-04 12:42:38 +0200575 sdhci_adma_write_desc(desc, 0, 0, 0x3);
Thomas Abraham70764a92010-05-26 14:42:04 -0700576 }
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),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200583 host->align_addr, host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200584 }
585
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200586 return 0;
587
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200588unmap_align:
589 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200590 host->align_buffer_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200591fail:
592 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200593}
594
595static void sdhci_adma_table_post(struct sdhci_host *host,
596 struct mmc_data *data)
597{
598 int direction;
599
600 struct scatterlist *sg;
601 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200602 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200603 char *buffer;
604 unsigned long flags;
Russell Kingde0b65a2014-04-25 12:58:29 +0100605 bool has_unaligned;
Pierre Ossman2134a922008-06-28 18:28:51 +0200606
607 if (data->flags & MMC_DATA_READ)
608 direction = DMA_FROM_DEVICE;
609 else
610 direction = DMA_TO_DEVICE;
611
Pierre Ossman2134a922008-06-28 18:28:51 +0200612 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200613 host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200614
Russell Kingde0b65a2014-04-25 12:58:29 +0100615 /* Do a quick scan of the SG list for any unaligned mappings */
616 has_unaligned = false;
617 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter76fe3792014-11-04 12:42:42 +0200618 if (sg_dma_address(sg) & host->align_mask) {
Russell Kingde0b65a2014-04-25 12:58:29 +0100619 has_unaligned = true;
620 break;
621 }
622
623 if (has_unaligned && data->flags & MMC_DATA_READ) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200624 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
625 data->sg_len, direction);
626
627 align = host->align_buffer;
628
629 for_each_sg(data->sg, sg, host->sg_count, i) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200630 if (sg_dma_address(sg) & host->align_mask) {
631 size = host->align_sz -
632 (sg_dma_address(sg) & host->align_mask);
Pierre Ossman2134a922008-06-28 18:28:51 +0200633
634 buffer = sdhci_kmap_atomic(sg, &flags);
Adrian Hunter8be78c62014-11-04 12:42:36 +0200635 WARN_ON(((long)buffer & (PAGE_SIZE - 1)) >
636 (PAGE_SIZE - size));
Pierre Ossman2134a922008-06-28 18:28:51 +0200637 memcpy(buffer, align, size);
638 sdhci_kunmap_atomic(buffer, &flags);
639
Adrian Hunter76fe3792014-11-04 12:42:42 +0200640 align += host->align_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200641 }
642 }
643 }
644
645 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
646 data->sg_len, direction);
647}
648
Andrei Warkentina3c77782011-04-11 16:13:42 -0500649static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800650{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700651 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500652 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700653 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800654
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200655 /*
656 * If the host controller provides us with an incorrect timeout
657 * value, just skip the check and use 0xE. The hardware may take
658 * longer to time out, but that's much better than having a too-short
659 * timeout value.
660 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200661 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200662 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200663
Andrei Warkentina3c77782011-04-11 16:13:42 -0500664 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100665 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500666 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800667
Andrei Warkentina3c77782011-04-11 16:13:42 -0500668 /* timeout in us */
669 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100670 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300671 else {
672 target_timeout = data->timeout_ns / 1000;
673 if (host->clock)
674 target_timeout += data->timeout_clks / host->clock;
675 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700676
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700677 /*
678 * Figure out needed cycles.
679 * We do this in steps in order to fit inside a 32 bit int.
680 * The first step is the minimum timeout, which will have a
681 * minimum resolution of 6 bits:
682 * (1) 2^13*1000 > 2^22,
683 * (2) host->timeout_clk < 2^16
684 * =>
685 * (1) / (2) > 2^6
686 */
687 count = 0;
688 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
689 while (current_timeout < target_timeout) {
690 count++;
691 current_timeout <<= 1;
692 if (count >= 0xF)
693 break;
694 }
695
696 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400697 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
698 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700699 count = 0xE;
700 }
701
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200702 return count;
703}
704
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300705static void sdhci_set_transfer_irqs(struct sdhci_host *host)
706{
707 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
708 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
709
710 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100711 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300712 else
Russell Kingb537f942014-04-25 12:56:01 +0100713 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
714
715 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
716 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300717}
718
Aisheng Dongb45e6682014-08-27 15:26:29 +0800719static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200720{
721 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800722
723 if (host->ops->set_timeout) {
724 host->ops->set_timeout(host, cmd);
725 } else {
726 count = sdhci_calc_timeout(host, cmd);
727 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
728 }
729}
730
731static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
732{
Pierre Ossman2134a922008-06-28 18:28:51 +0200733 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500734 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200735 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200736
737 WARN_ON(host->data);
738
Aisheng Dongb45e6682014-08-27 15:26:29 +0800739 if (data || (cmd->flags & MMC_RSP_BUSY))
740 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500741
742 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200743 return;
744
745 /* Sanity checks */
746 BUG_ON(data->blksz * data->blocks > 524288);
747 BUG_ON(data->blksz > host->mmc->max_blk_size);
748 BUG_ON(data->blocks > 65535);
749
750 host->data = data;
751 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400752 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200753
Richard Röjforsa13abc72009-09-22 16:45:30 -0700754 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100755 host->flags |= SDHCI_REQ_USE_DMA;
756
Pierre Ossman2134a922008-06-28 18:28:51 +0200757 /*
758 * FIXME: This doesn't account for merging when mapping the
759 * scatterlist.
760 */
761 if (host->flags & SDHCI_REQ_USE_DMA) {
762 int broken, i;
763 struct scatterlist *sg;
764
765 broken = 0;
766 if (host->flags & SDHCI_USE_ADMA) {
767 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
768 broken = 1;
769 } else {
770 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
771 broken = 1;
772 }
773
774 if (unlikely(broken)) {
775 for_each_sg(data->sg, sg, data->sg_len, i) {
776 if (sg->length & 0x3) {
777 DBG("Reverting to PIO because of "
778 "transfer size (%d)\n",
779 sg->length);
780 host->flags &= ~SDHCI_REQ_USE_DMA;
781 break;
782 }
783 }
784 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100785 }
786
787 /*
788 * The assumption here being that alignment is the same after
789 * translation to device address space.
790 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200791 if (host->flags & SDHCI_REQ_USE_DMA) {
792 int broken, i;
793 struct scatterlist *sg;
794
795 broken = 0;
796 if (host->flags & SDHCI_USE_ADMA) {
797 /*
798 * As we use 3 byte chunks to work around
799 * alignment problems, we need to check this
800 * quirk.
801 */
802 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
803 broken = 1;
804 } else {
805 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
806 broken = 1;
807 }
808
809 if (unlikely(broken)) {
810 for_each_sg(data->sg, sg, data->sg_len, i) {
811 if (sg->offset & 0x3) {
812 DBG("Reverting to PIO because of "
813 "bad alignment\n");
814 host->flags &= ~SDHCI_REQ_USE_DMA;
815 break;
816 }
817 }
818 }
819 }
820
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200821 if (host->flags & SDHCI_REQ_USE_DMA) {
822 if (host->flags & SDHCI_USE_ADMA) {
823 ret = sdhci_adma_table_pre(host, data);
824 if (ret) {
825 /*
826 * This only happens when someone fed
827 * us an invalid request.
828 */
829 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200830 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200831 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300832 sdhci_writel(host, host->adma_addr,
833 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200834 }
835 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300836 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200837
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300838 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200839 data->sg, data->sg_len,
840 (data->flags & MMC_DATA_READ) ?
841 DMA_FROM_DEVICE :
842 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300843 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200844 /*
845 * This only happens when someone fed
846 * us an invalid request.
847 */
848 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200849 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200850 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200851 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300852 sdhci_writel(host, sg_dma_address(data->sg),
853 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200854 }
855 }
856 }
857
Pierre Ossman2134a922008-06-28 18:28:51 +0200858 /*
859 * Always adjust the DMA selection as some controllers
860 * (e.g. JMicron) can't do PIO properly when the selection
861 * is ADMA.
862 */
863 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300864 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200865 ctrl &= ~SDHCI_CTRL_DMA_MASK;
866 if ((host->flags & SDHCI_REQ_USE_DMA) &&
867 (host->flags & SDHCI_USE_ADMA))
868 ctrl |= SDHCI_CTRL_ADMA32;
869 else
870 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300871 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100872 }
873
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200874 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200875 int flags;
876
877 flags = SG_MITER_ATOMIC;
878 if (host->data->flags & MMC_DATA_READ)
879 flags |= SG_MITER_TO_SG;
880 else
881 flags |= SG_MITER_FROM_SG;
882 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200883 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800884 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700885
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300886 sdhci_set_transfer_irqs(host);
887
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400888 /* Set the DMA boundary value and block size */
889 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
890 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300891 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700892}
893
894static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500895 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700896{
897 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500898 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700899
Dong Aisheng2b558c12013-10-30 22:09:48 +0800900 if (data == NULL) {
901 /* clear Auto CMD settings for no data CMDs */
902 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
903 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
904 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700905 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800906 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700907
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200908 WARN_ON(!host->data);
909
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700910 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500911 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
912 mode |= SDHCI_TRNS_MULTI;
913 /*
914 * If we are sending CMD23, CMD12 never gets sent
915 * on successful completion (so no Auto-CMD12).
916 */
917 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
918 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500919 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
920 mode |= SDHCI_TRNS_AUTO_CMD23;
921 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
922 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700923 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500924
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700925 if (data->flags & MMC_DATA_READ)
926 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100927 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700928 mode |= SDHCI_TRNS_DMA;
929
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300930 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800931}
932
933static void sdhci_finish_data(struct sdhci_host *host)
934{
935 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800936
937 BUG_ON(!host->data);
938
939 data = host->data;
940 host->data = NULL;
941
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100942 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200943 if (host->flags & SDHCI_USE_ADMA)
944 sdhci_adma_table_post(host, data);
945 else {
946 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
947 data->sg_len, (data->flags & MMC_DATA_READ) ?
948 DMA_FROM_DEVICE : DMA_TO_DEVICE);
949 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800950 }
951
952 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200953 * The specification states that the block count register must
954 * be updated, but it does not specify at what point in the
955 * data flow. That makes the register entirely useless to read
956 * back so we have to assume that nothing made it to the card
957 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800958 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200959 if (data->error)
960 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800961 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200962 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800963
Andrei Warkentine89d4562011-05-23 15:06:37 -0500964 /*
965 * Need to send CMD12 if -
966 * a) open-ended multiblock transfer (no CMD23)
967 * b) error in multiblock transfer
968 */
969 if (data->stop &&
970 (data->error ||
971 !host->mrq->sbc)) {
972
Pierre Ossmand129bce2006-03-24 03:18:17 -0800973 /*
974 * The controller needs a reset of internal state machines
975 * upon error conditions.
976 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200977 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100978 sdhci_do_reset(host, SDHCI_RESET_CMD);
979 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800980 }
981
982 sdhci_send_command(host, data->stop);
983 } else
984 tasklet_schedule(&host->finish_tasklet);
985}
986
Dong Aishengc0e551292013-09-13 19:11:31 +0800987void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800988{
989 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700990 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700991 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800992
993 WARN_ON(host->cmd);
994
Pierre Ossmand129bce2006-03-24 03:18:17 -0800995 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700996 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700997
998 mask = SDHCI_CMD_INHIBIT;
999 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1000 mask |= SDHCI_DATA_INHIBIT;
1001
1002 /* We shouldn't wait for data inihibit for stop commands, even
1003 though they might use busy signaling */
1004 if (host->mrq->data && (cmd == host->mrq->data->stop))
1005 mask &= ~SDHCI_DATA_INHIBIT;
1006
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001007 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001008 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301009 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001010 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001011 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001012 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001013 tasklet_schedule(&host->finish_tasklet);
1014 return;
1015 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001016 timeout--;
1017 mdelay(1);
1018 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001019
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001020 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001021 if (!cmd->data && cmd->busy_timeout > 9000)
1022 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001023 else
1024 timeout += 10 * HZ;
1025 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001026
1027 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001028 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001029
Andrei Warkentina3c77782011-04-11 16:13:42 -05001030 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001031
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001032 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033
Andrei Warkentine89d4562011-05-23 15:06:37 -05001034 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001035
Pierre Ossmand129bce2006-03-24 03:18:17 -08001036 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301037 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001038 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001039 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001040 tasklet_schedule(&host->finish_tasklet);
1041 return;
1042 }
1043
1044 if (!(cmd->flags & MMC_RSP_PRESENT))
1045 flags = SDHCI_CMD_RESP_NONE;
1046 else if (cmd->flags & MMC_RSP_136)
1047 flags = SDHCI_CMD_RESP_LONG;
1048 else if (cmd->flags & MMC_RSP_BUSY)
1049 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1050 else
1051 flags = SDHCI_CMD_RESP_SHORT;
1052
1053 if (cmd->flags & MMC_RSP_CRC)
1054 flags |= SDHCI_CMD_CRC;
1055 if (cmd->flags & MMC_RSP_OPCODE)
1056 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301057
1058 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301059 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1060 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001061 flags |= SDHCI_CMD_DATA;
1062
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001063 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001064}
Dong Aishengc0e551292013-09-13 19:11:31 +08001065EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001066
1067static void sdhci_finish_command(struct sdhci_host *host)
1068{
1069 int i;
1070
1071 BUG_ON(host->cmd == NULL);
1072
1073 if (host->cmd->flags & MMC_RSP_PRESENT) {
1074 if (host->cmd->flags & MMC_RSP_136) {
1075 /* CRC is stripped so we need to do some shifting. */
1076 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001077 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001078 SDHCI_RESPONSE + (3-i)*4) << 8;
1079 if (i != 3)
1080 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001081 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001082 SDHCI_RESPONSE + (3-i)*4-1);
1083 }
1084 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001085 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001086 }
1087 }
1088
Pierre Ossman17b04292007-07-22 22:18:46 +02001089 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001090
Andrei Warkentine89d4562011-05-23 15:06:37 -05001091 /* Finished CMD23, now send actual command. */
1092 if (host->cmd == host->mrq->sbc) {
1093 host->cmd = NULL;
1094 sdhci_send_command(host, host->mrq->cmd);
1095 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001096
Andrei Warkentine89d4562011-05-23 15:06:37 -05001097 /* Processed actual command. */
1098 if (host->data && host->data_early)
1099 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001100
Andrei Warkentine89d4562011-05-23 15:06:37 -05001101 if (!host->cmd->data)
1102 tasklet_schedule(&host->finish_tasklet);
1103
1104 host->cmd = NULL;
1105 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001106}
1107
Kevin Liu52983382013-01-31 11:31:37 +08001108static u16 sdhci_get_preset_value(struct sdhci_host *host)
1109{
Russell Kingd975f122014-04-25 12:59:31 +01001110 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001111
Russell Kingd975f122014-04-25 12:59:31 +01001112 switch (host->timing) {
1113 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001114 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1115 break;
Russell Kingd975f122014-04-25 12:59:31 +01001116 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001117 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1118 break;
Russell Kingd975f122014-04-25 12:59:31 +01001119 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001120 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1121 break;
Russell Kingd975f122014-04-25 12:59:31 +01001122 case MMC_TIMING_UHS_SDR104:
1123 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001124 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1125 break;
Russell Kingd975f122014-04-25 12:59:31 +01001126 case MMC_TIMING_UHS_DDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001127 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1128 break;
1129 default:
1130 pr_warn("%s: Invalid UHS-I mode selected\n",
1131 mmc_hostname(host->mmc));
1132 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1133 break;
1134 }
1135 return preset;
1136}
1137
Russell King17710592014-04-25 12:58:55 +01001138void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001139{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301140 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001141 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301142 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001143 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001144
Russell King1650d0c2014-04-25 12:58:50 +01001145 host->mmc->actual_clock = 0;
1146
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001147 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001148
1149 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001150 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001151
Zhangfei Gao85105c52010-08-06 07:10:01 +08001152 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001153 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001154 u16 pre_val;
1155
1156 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1157 pre_val = sdhci_get_preset_value(host);
1158 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1159 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1160 if (host->clk_mul &&
1161 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1162 clk = SDHCI_PROG_CLOCK_MODE;
1163 real_div = div + 1;
1164 clk_mul = host->clk_mul;
1165 } else {
1166 real_div = max_t(int, 1, div << 1);
1167 }
1168 goto clock_set;
1169 }
1170
Arindam Nathc3ed3872011-05-05 12:19:06 +05301171 /*
1172 * Check if the Host Controller supports Programmable Clock
1173 * Mode.
1174 */
1175 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001176 for (div = 1; div <= 1024; div++) {
1177 if ((host->max_clk * host->clk_mul / div)
1178 <= clock)
1179 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001180 }
Kevin Liu52983382013-01-31 11:31:37 +08001181 /*
1182 * Set Programmable Clock Mode in the Clock
1183 * Control register.
1184 */
1185 clk = SDHCI_PROG_CLOCK_MODE;
1186 real_div = div;
1187 clk_mul = host->clk_mul;
1188 div--;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301189 } else {
1190 /* Version 3.00 divisors must be a multiple of 2. */
1191 if (host->max_clk <= clock)
1192 div = 1;
1193 else {
1194 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1195 div += 2) {
1196 if ((host->max_clk / div) <= clock)
1197 break;
1198 }
1199 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001200 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301201 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001202 }
1203 } else {
1204 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001205 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001206 if ((host->max_clk / div) <= clock)
1207 break;
1208 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001209 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301210 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001211 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001212
Kevin Liu52983382013-01-31 11:31:37 +08001213clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001214 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001215 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301216 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001217 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1218 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001219 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001220 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001221
Chris Ball27f6cb12009-09-22 16:45:31 -07001222 /* Wait max 20 ms */
1223 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001224 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001225 & SDHCI_CLOCK_INT_STABLE)) {
1226 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301227 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001228 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001229 sdhci_dumpregs(host);
1230 return;
1231 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001232 timeout--;
1233 mdelay(1);
1234 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001235
1236 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001237 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001238}
Russell King17710592014-04-25 12:58:55 +01001239EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001240
Russell King24fbb3c2014-04-25 13:00:06 +01001241static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1242 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001243{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001244 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001245 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001246
Tim Kryger52221612014-06-25 00:25:34 -07001247 if (!IS_ERR(mmc->supply.vmmc)) {
1248 spin_unlock_irq(&host->lock);
Markus Mayer4e743f12014-07-03 13:27:42 -07001249 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Tim Kryger52221612014-06-25 00:25:34 -07001250 spin_lock_irq(&host->lock);
1251 return;
1252 }
1253
Russell King24fbb3c2014-04-25 13:00:06 +01001254 if (mode != MMC_POWER_OFF) {
1255 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001256 case MMC_VDD_165_195:
1257 pwr = SDHCI_POWER_180;
1258 break;
1259 case MMC_VDD_29_30:
1260 case MMC_VDD_30_31:
1261 pwr = SDHCI_POWER_300;
1262 break;
1263 case MMC_VDD_32_33:
1264 case MMC_VDD_33_34:
1265 pwr = SDHCI_POWER_330;
1266 break;
1267 default:
1268 BUG();
1269 }
1270 }
1271
1272 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001273 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001274
Pierre Ossmanae628902009-05-03 20:45:03 +02001275 host->pwr = pwr;
1276
1277 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001278 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001279 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1280 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001281 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001282 } else {
1283 /*
1284 * Spec says that we should clear the power reg before setting
1285 * a new value. Some controllers don't seem to like this though.
1286 */
1287 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1288 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001289
Russell Kinge921a8b2014-04-25 13:00:01 +01001290 /*
1291 * At least the Marvell CaFe chip gets confused if we set the
1292 * voltage and set turn on power at the same time, so set the
1293 * voltage first.
1294 */
1295 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1296 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001297
Russell Kinge921a8b2014-04-25 13:00:01 +01001298 pwr |= SDHCI_POWER_ON;
1299
Pierre Ossmanae628902009-05-03 20:45:03 +02001300 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1301
Russell Kinge921a8b2014-04-25 13:00:01 +01001302 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1303 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001304
Russell Kinge921a8b2014-04-25 13:00:01 +01001305 /*
1306 * Some controllers need an extra 10ms delay of 10ms before
1307 * they can apply clock after applying power
1308 */
1309 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1310 mdelay(10);
1311 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001312}
1313
Pierre Ossmand129bce2006-03-24 03:18:17 -08001314/*****************************************************************************\
1315 * *
1316 * MMC callbacks *
1317 * *
1318\*****************************************************************************/
1319
1320static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1321{
1322 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001323 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001324 unsigned long flags;
Aaron Lu473b0952012-07-03 17:27:49 +08001325 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001326
1327 host = mmc_priv(mmc);
1328
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001329 sdhci_runtime_pm_get(host);
1330
Pierre Ossmand129bce2006-03-24 03:18:17 -08001331 spin_lock_irqsave(&host->lock, flags);
1332
1333 WARN_ON(host->mrq != NULL);
1334
Pierre Ossmanf9134312008-12-21 17:01:48 +01001335#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001336 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001337#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001338
1339 /*
1340 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1341 * requests if Auto-CMD12 is enabled.
1342 */
1343 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001344 if (mrq->stop) {
1345 mrq->data->stop = NULL;
1346 mrq->stop = NULL;
1347 }
1348 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001349
1350 host->mrq = mrq;
1351
Shawn Guo505a8682012-12-11 15:23:42 +08001352 /*
1353 * Firstly check card presence from cd-gpio. The return could
1354 * be one of the following possibilities:
1355 * negative: cd-gpio is not available
1356 * zero: cd-gpio is used, and card is removed
1357 * one: cd-gpio is used, and card is present
1358 */
1359 present = mmc_gpio_get_cd(host->mmc);
1360 if (present < 0) {
1361 /* If polling, assume that the card is always present. */
1362 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1363 present = 1;
1364 else
1365 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1366 SDHCI_CARD_PRESENT;
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +08001367 }
1368
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001369 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001370 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001371 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301372 } else {
1373 u32 present_state;
1374
1375 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1376 /*
1377 * Check if the re-tuning timer has already expired and there
Yi Sun7756a96d2014-09-09 02:13:59 +00001378 * is no on-going data transfer and DAT0 is not busy. If so,
1379 * we need to execute tuning procedure before sending command.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301380 */
1381 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
Yi Sun7756a96d2014-09-09 02:13:59 +00001382 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ)) &&
1383 (present_state & SDHCI_DATA_0_LVL_MASK)) {
Chris Ball14efd952012-11-05 14:29:49 -05001384 if (mmc->card) {
1385 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1386 tuning_opcode =
1387 mmc->card->type == MMC_TYPE_MMC ?
1388 MMC_SEND_TUNING_BLOCK_HS200 :
1389 MMC_SEND_TUNING_BLOCK;
Chuansheng Liu63c21182013-11-05 14:52:45 +08001390
1391 /* Here we need to set the host->mrq to NULL,
1392 * in case the pending finish_tasklet
1393 * finishes it incorrectly.
1394 */
1395 host->mrq = NULL;
1396
Chris Ball14efd952012-11-05 14:29:49 -05001397 spin_unlock_irqrestore(&host->lock, flags);
1398 sdhci_execute_tuning(mmc, tuning_opcode);
1399 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301400
Chris Ball14efd952012-11-05 14:29:49 -05001401 /* Restore original mmc_request structure */
1402 host->mrq = mrq;
1403 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301404 }
1405
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001406 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001407 sdhci_send_command(host, mrq->sbc);
1408 else
1409 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301410 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001411
Pierre Ossman5f25a662006-10-04 02:15:39 -07001412 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001413 spin_unlock_irqrestore(&host->lock, flags);
1414}
1415
Russell King2317f562014-04-25 12:57:07 +01001416void sdhci_set_bus_width(struct sdhci_host *host, int width)
1417{
1418 u8 ctrl;
1419
1420 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1421 if (width == MMC_BUS_WIDTH_8) {
1422 ctrl &= ~SDHCI_CTRL_4BITBUS;
1423 if (host->version >= SDHCI_SPEC_300)
1424 ctrl |= SDHCI_CTRL_8BITBUS;
1425 } else {
1426 if (host->version >= SDHCI_SPEC_300)
1427 ctrl &= ~SDHCI_CTRL_8BITBUS;
1428 if (width == MMC_BUS_WIDTH_4)
1429 ctrl |= SDHCI_CTRL_4BITBUS;
1430 else
1431 ctrl &= ~SDHCI_CTRL_4BITBUS;
1432 }
1433 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1434}
1435EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1436
Russell King96d7b782014-04-25 12:59:26 +01001437void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1438{
1439 u16 ctrl_2;
1440
1441 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1442 /* Select Bus Speed Mode for host */
1443 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1444 if ((timing == MMC_TIMING_MMC_HS200) ||
1445 (timing == MMC_TIMING_UHS_SDR104))
1446 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1447 else if (timing == MMC_TIMING_UHS_SDR12)
1448 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1449 else if (timing == MMC_TIMING_UHS_SDR25)
1450 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1451 else if (timing == MMC_TIMING_UHS_SDR50)
1452 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1453 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1454 (timing == MMC_TIMING_MMC_DDR52))
1455 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1456 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1457}
1458EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1459
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001460static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001461{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001462 unsigned long flags;
1463 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001464 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001465
Pierre Ossmand129bce2006-03-24 03:18:17 -08001466 spin_lock_irqsave(&host->lock, flags);
1467
Adrian Hunterceb61432011-12-27 15:48:41 +02001468 if (host->flags & SDHCI_DEVICE_DEAD) {
1469 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001470 if (!IS_ERR(mmc->supply.vmmc) &&
1471 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001472 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001473 return;
1474 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001475
Pierre Ossmand129bce2006-03-24 03:18:17 -08001476 /*
1477 * Reset the chip on each power off.
1478 * Should clear out any weird states.
1479 */
1480 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001481 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001482 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001483 }
1484
Kevin Liu52983382013-01-31 11:31:37 +08001485 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001486 (ios->power_mode == MMC_POWER_UP) &&
1487 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001488 sdhci_enable_preset_value(host, false);
1489
Russell King373073e2014-04-25 12:58:45 +01001490 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001491 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001492 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001493
1494 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1495 host->clock) {
1496 host->timeout_clk = host->mmc->actual_clock ?
1497 host->mmc->actual_clock / 1000 :
1498 host->clock / 1000;
1499 host->mmc->max_busy_timeout =
1500 host->ops->get_max_timeout_count ?
1501 host->ops->get_max_timeout_count(host) :
1502 1 << 27;
1503 host->mmc->max_busy_timeout /= host->timeout_clk;
1504 }
Russell King373073e2014-04-25 12:58:45 +01001505 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001506
Russell King24fbb3c2014-04-25 13:00:06 +01001507 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001508
Philip Rakity643a81f2010-09-23 08:24:32 -07001509 if (host->ops->platform_send_init_74_clocks)
1510 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1511
Russell King2317f562014-04-25 12:57:07 +01001512 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001513
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001514 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001515
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001516 if ((ios->timing == MMC_TIMING_SD_HS ||
1517 ios->timing == MMC_TIMING_MMC_HS)
1518 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001519 ctrl |= SDHCI_CTRL_HISPD;
1520 else
1521 ctrl &= ~SDHCI_CTRL_HISPD;
1522
Arindam Nathd6d50a12011-05-05 12:18:59 +05301523 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301524 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301525
1526 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301527 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001528 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301529 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301530 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1531 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001532 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301533 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301534
Russell Kingda91a8f2014-04-25 13:00:12 +01001535 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301536 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301537 /*
1538 * We only need to set Driver Strength if the
1539 * preset value enable is not set.
1540 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001541 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301542 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1543 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1544 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1545 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1546 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1547
1548 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301549 } else {
1550 /*
1551 * According to SDHC Spec v3.00, if the Preset Value
1552 * Enable in the Host Control 2 register is set, we
1553 * need to reset SD Clock Enable before changing High
1554 * Speed Enable to avoid generating clock gliches.
1555 */
Arindam Nath758535c2011-05-05 12:19:00 +05301556
1557 /* Reset SD Clock Enable */
1558 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1559 clk &= ~SDHCI_CLOCK_CARD_EN;
1560 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1561
1562 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1563
1564 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001565 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301566 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301567
Arindam Nath49c468f2011-05-05 12:19:01 +05301568 /* Reset SD Clock Enable */
1569 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1570 clk &= ~SDHCI_CLOCK_CARD_EN;
1571 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1572
Russell King96d7b782014-04-25 12:59:26 +01001573 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001574 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301575
Kevin Liu52983382013-01-31 11:31:37 +08001576 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1577 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1578 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1579 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1580 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1581 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1582 u16 preset;
1583
1584 sdhci_enable_preset_value(host, true);
1585 preset = sdhci_get_preset_value(host);
1586 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1587 >> SDHCI_PRESET_DRV_SHIFT;
1588 }
1589
Arindam Nath49c468f2011-05-05 12:19:01 +05301590 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001591 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301592 } else
1593 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301594
Leandro Dorileob8352262007-07-25 23:47:04 +02001595 /*
1596 * Some (ENE) controllers go apeshit on some ios operation,
1597 * signalling timeout and CRC errors even on CMD0. Resetting
1598 * it on each ios seems to solve the problem.
1599 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001600 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001601 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001602
Pierre Ossman5f25a662006-10-04 02:15:39 -07001603 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001604 spin_unlock_irqrestore(&host->lock, flags);
1605}
1606
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001607static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1608{
1609 struct sdhci_host *host = mmc_priv(mmc);
1610
1611 sdhci_runtime_pm_get(host);
1612 sdhci_do_set_ios(host, ios);
1613 sdhci_runtime_pm_put(host);
1614}
1615
Kevin Liu94144a42013-02-28 17:35:53 +08001616static int sdhci_do_get_cd(struct sdhci_host *host)
1617{
1618 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1619
1620 if (host->flags & SDHCI_DEVICE_DEAD)
1621 return 0;
1622
1623 /* If polling/nonremovable, assume that the card is always present. */
1624 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1625 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1626 return 1;
1627
1628 /* Try slot gpio detect */
1629 if (!IS_ERR_VALUE(gpio_cd))
1630 return !!gpio_cd;
1631
1632 /* Host native card detect */
1633 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1634}
1635
1636static int sdhci_get_cd(struct mmc_host *mmc)
1637{
1638 struct sdhci_host *host = mmc_priv(mmc);
1639 int ret;
1640
1641 sdhci_runtime_pm_get(host);
1642 ret = sdhci_do_get_cd(host);
1643 sdhci_runtime_pm_put(host);
1644 return ret;
1645}
1646
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001647static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001648{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001649 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001650 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001651
Pierre Ossmand129bce2006-03-24 03:18:17 -08001652 spin_lock_irqsave(&host->lock, flags);
1653
Pierre Ossman1e728592008-04-16 19:13:13 +02001654 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001655 is_readonly = 0;
1656 else if (host->ops->get_ro)
1657 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001658 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001659 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1660 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001661
1662 spin_unlock_irqrestore(&host->lock, flags);
1663
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001664 /* This quirk needs to be replaced by a callback-function later */
1665 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1666 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001667}
1668
Takashi Iwai82b0e232011-04-21 20:26:38 +02001669#define SAMPLE_COUNT 5
1670
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001671static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001672{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001673 int i, ro_count;
1674
Takashi Iwai82b0e232011-04-21 20:26:38 +02001675 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001676 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001677
1678 ro_count = 0;
1679 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001680 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001681 if (++ro_count > SAMPLE_COUNT / 2)
1682 return 1;
1683 }
1684 msleep(30);
1685 }
1686 return 0;
1687}
1688
Adrian Hunter20758b62011-08-29 16:42:12 +03001689static void sdhci_hw_reset(struct mmc_host *mmc)
1690{
1691 struct sdhci_host *host = mmc_priv(mmc);
1692
1693 if (host->ops && host->ops->hw_reset)
1694 host->ops->hw_reset(host);
1695}
1696
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001697static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001698{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001699 struct sdhci_host *host = mmc_priv(mmc);
1700 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001701
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001702 sdhci_runtime_pm_get(host);
1703 ret = sdhci_do_get_ro(host);
1704 sdhci_runtime_pm_put(host);
1705 return ret;
1706}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001707
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001708static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1709{
Russell Kingbe138552014-04-25 12:55:56 +01001710 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001711 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001712 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001713 else
Russell Kingb537f942014-04-25 12:56:01 +01001714 host->ier &= ~SDHCI_INT_CARD_INT;
1715
1716 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1717 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001718 mmiowb();
1719 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001720}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001721
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001722static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1723{
1724 struct sdhci_host *host = mmc_priv(mmc);
1725 unsigned long flags;
1726
Russell Kingef104332014-04-25 12:55:41 +01001727 sdhci_runtime_pm_get(host);
1728
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001729 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001730 if (enable)
1731 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1732 else
1733 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1734
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001735 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001736 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001737
1738 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001739}
1740
Philip Rakity6231f3d2012-07-23 15:56:23 -07001741static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001742 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001743{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001744 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001745 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001746 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001747
1748 /*
1749 * Signal Voltage Switching is only applicable for Host Controllers
1750 * v3.00 and above.
1751 */
1752 if (host->version < SDHCI_SPEC_300)
1753 return 0;
1754
Philip Rakity6231f3d2012-07-23 15:56:23 -07001755 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001756
Fabio Estevam21f59982013-02-14 10:35:03 -02001757 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001758 case MMC_SIGNAL_VOLTAGE_330:
1759 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1760 ctrl &= ~SDHCI_CTRL_VDD_180;
1761 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1762
Tim Kryger3a48edc2014-06-13 10:13:56 -07001763 if (!IS_ERR(mmc->supply.vqmmc)) {
1764 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1765 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001766 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001767 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1768 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001769 return -EIO;
1770 }
1771 }
1772 /* Wait for 5ms */
1773 usleep_range(5000, 5500);
1774
1775 /* 3.3V regulator output should be stable within 5 ms */
1776 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1777 if (!(ctrl & SDHCI_CTRL_VDD_180))
1778 return 0;
1779
Joe Perches66061102014-09-12 14:56:56 -07001780 pr_warn("%s: 3.3V regulator output did not became stable\n",
1781 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001782
1783 return -EAGAIN;
1784 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001785 if (!IS_ERR(mmc->supply.vqmmc)) {
1786 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001787 1700000, 1950000);
1788 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001789 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1790 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001791 return -EIO;
1792 }
1793 }
1794
1795 /*
1796 * Enable 1.8V Signal Enable in the Host Control2
1797 * register
1798 */
1799 ctrl |= SDHCI_CTRL_VDD_180;
1800 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1801
Kevin Liu20b92a32012-12-17 19:29:26 +08001802 /* 1.8V regulator output should be stable within 5 ms */
1803 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1804 if (ctrl & SDHCI_CTRL_VDD_180)
1805 return 0;
1806
Joe Perches66061102014-09-12 14:56:56 -07001807 pr_warn("%s: 1.8V regulator output did not became stable\n",
1808 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001809
1810 return -EAGAIN;
1811 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001812 if (!IS_ERR(mmc->supply.vqmmc)) {
1813 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1814 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001815 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001816 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1817 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001818 return -EIO;
1819 }
1820 }
1821 return 0;
1822 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301823 /* No signal voltage switch required */
1824 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001825 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301826}
1827
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001828static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001829 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001830{
1831 struct sdhci_host *host = mmc_priv(mmc);
1832 int err;
1833
1834 if (host->version < SDHCI_SPEC_300)
1835 return 0;
1836 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001837 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001838 sdhci_runtime_pm_put(host);
1839 return err;
1840}
1841
Kevin Liu20b92a32012-12-17 19:29:26 +08001842static int sdhci_card_busy(struct mmc_host *mmc)
1843{
1844 struct sdhci_host *host = mmc_priv(mmc);
1845 u32 present_state;
1846
1847 sdhci_runtime_pm_get(host);
1848 /* Check whether DAT[3:0] is 0000 */
1849 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1850 sdhci_runtime_pm_put(host);
1851
1852 return !(present_state & SDHCI_DATA_LVL_MASK);
1853}
1854
Girish K S069c9f12012-01-06 09:56:39 +05301855static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301856{
Russell King4b6f37d2014-04-25 12:59:36 +01001857 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301858 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301859 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301860 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001861 unsigned long flags;
Arindam Nathb513ea22011-05-05 12:19:04 +05301862
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001863 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001864 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301865
Arindam Nathb513ea22011-05-05 12:19:04 +05301866 /*
Girish K S069c9f12012-01-06 09:56:39 +05301867 * The Host Controller needs tuning only in case of SDR104 mode
1868 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301869 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301870 * If the Host Controller supports the HS200 mode then the
1871 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301872 */
Russell King4b6f37d2014-04-25 12:59:36 +01001873 switch (host->timing) {
1874 case MMC_TIMING_MMC_HS200:
1875 case MMC_TIMING_UHS_SDR104:
1876 break;
Girish K S069c9f12012-01-06 09:56:39 +05301877
Russell King4b6f37d2014-04-25 12:59:36 +01001878 case MMC_TIMING_UHS_SDR50:
1879 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1880 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1881 break;
1882 /* FALLTHROUGH */
1883
1884 default:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001885 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001886 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301887 return 0;
1888 }
1889
Dong Aisheng45251812013-09-13 19:11:30 +08001890 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001891 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001892 err = host->ops->platform_execute_tuning(host, opcode);
1893 sdhci_runtime_pm_put(host);
1894 return err;
1895 }
1896
Russell King4b6f37d2014-04-25 12:59:36 +01001897 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1898 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Arindam Nathb513ea22011-05-05 12:19:04 +05301899 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1900
1901 /*
1902 * As per the Host Controller spec v3.00, tuning command
1903 * generates Buffer Read Ready interrupt, so enable that.
1904 *
1905 * Note: The spec clearly says that when tuning sequence
1906 * is being performed, the controller does not generate
1907 * interrupts other than Buffer Read Ready interrupt. But
1908 * to make sure we don't hit a controller bug, we _only_
1909 * enable Buffer Read Ready interrupt here.
1910 */
Russell Kingb537f942014-04-25 12:56:01 +01001911 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1912 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301913
1914 /*
1915 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1916 * of loops reaches 40 times or a timeout of 150ms occurs.
1917 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301918 do {
1919 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001920 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301921
Girish K S069c9f12012-01-06 09:56:39 +05301922 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301923 cmd.arg = 0;
1924 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1925 cmd.retries = 0;
1926 cmd.data = NULL;
1927 cmd.error = 0;
1928
Al Cooper7ce45e92014-05-09 11:34:07 -04001929 if (tuning_loop_counter-- == 0)
1930 break;
1931
Arindam Nathb513ea22011-05-05 12:19:04 +05301932 mrq.cmd = &cmd;
1933 host->mrq = &mrq;
1934
1935 /*
1936 * In response to CMD19, the card sends 64 bytes of tuning
1937 * block to the Host Controller. So we set the block size
1938 * to 64 here.
1939 */
Girish K S069c9f12012-01-06 09:56:39 +05301940 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1941 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1942 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1943 SDHCI_BLOCK_SIZE);
1944 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1945 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1946 SDHCI_BLOCK_SIZE);
1947 } else {
1948 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1949 SDHCI_BLOCK_SIZE);
1950 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301951
1952 /*
1953 * The tuning block is sent by the card to the host controller.
1954 * So we set the TRNS_READ bit in the Transfer Mode register.
1955 * This also takes care of setting DMA Enable and Multi Block
1956 * Select in the same register to 0.
1957 */
1958 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1959
1960 sdhci_send_command(host, &cmd);
1961
1962 host->cmd = NULL;
1963 host->mrq = NULL;
1964
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001965 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301966 /* Wait for Buffer Read Ready interrupt */
1967 wait_event_interruptible_timeout(host->buf_ready_int,
1968 (host->tuning_done == 1),
1969 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001970 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301971
1972 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301973 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301974 "Buffer Read Ready interrupt during tuning "
1975 "procedure, falling back to fixed sampling "
1976 "clock\n");
1977 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1978 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1979 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1980 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1981
1982 err = -EIO;
1983 goto out;
1984 }
1985
1986 host->tuning_done = 0;
1987
1988 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07001989
1990 /* eMMC spec does not require a delay between tuning cycles */
1991 if (opcode == MMC_SEND_TUNING_BLOCK)
1992 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05301993 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1994
1995 /*
1996 * The Host Driver has exhausted the maximum number of loops allowed,
1997 * so use fixed sampling frequency.
1998 */
Al Cooper7ce45e92014-05-09 11:34:07 -04001999 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302000 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2001 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002002 }
2003 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
2004 pr_info(DRIVER_NAME ": Tuning procedure"
2005 " failed, falling back to fixed sampling"
2006 " clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002007 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302008 }
2009
2010out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302011 /*
2012 * If this is the very first time we are here, we start the retuning
2013 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2014 * flag won't be set, we check this condition before actually starting
2015 * the timer.
2016 */
2017 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2018 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
Aaron Lu973905f2012-07-04 13:29:09 +08002019 host->flags |= SDHCI_USING_RETUNING_TIMER;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302020 mod_timer(&host->tuning_timer, jiffies +
2021 host->tuning_count * HZ);
2022 /* Tuning mode 1 limits the maximum data length to 4MB */
2023 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
Arend van Spriel2bc02482014-01-04 13:51:26 +01002024 } else if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302025 host->flags &= ~SDHCI_NEEDS_RETUNING;
2026 /* Reload the new initial value for timer */
Arend van Spriel2bc02482014-01-04 13:51:26 +01002027 mod_timer(&host->tuning_timer, jiffies +
2028 host->tuning_count * HZ);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302029 }
2030
2031 /*
2032 * In case tuning fails, host controllers which support re-tuning can
2033 * try tuning again at a later time, when the re-tuning timer expires.
2034 * So for these controllers, we return 0. Since there might be other
2035 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08002036 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2037 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302038 */
Aaron Lu973905f2012-07-04 13:29:09 +08002039 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302040 err = 0;
2041
Russell Kingb537f942014-04-25 12:56:01 +01002042 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2043 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002044 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002045 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302046
2047 return err;
2048}
2049
Kevin Liu52983382013-01-31 11:31:37 +08002050
2051static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302052{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302053 /* Host Controller v3.00 defines preset value registers */
2054 if (host->version < SDHCI_SPEC_300)
2055 return;
2056
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302057 /*
2058 * We only enable or disable Preset Value if they are not already
2059 * enabled or disabled respectively. Otherwise, we bail out.
2060 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002061 if (host->preset_enabled != enable) {
2062 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2063
2064 if (enable)
2065 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2066 else
2067 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2068
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302069 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002070
2071 if (enable)
2072 host->flags |= SDHCI_PV_ENABLED;
2073 else
2074 host->flags &= ~SDHCI_PV_ENABLED;
2075
2076 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302077 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002078}
2079
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002080static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002081{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002082 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002083 unsigned long flags;
2084
Christian Daudt722e1282013-06-20 14:26:36 -07002085 /* First check if client has provided their own card event */
2086 if (host->ops->card_event)
2087 host->ops->card_event(host);
2088
Pierre Ossmand129bce2006-03-24 03:18:17 -08002089 spin_lock_irqsave(&host->lock, flags);
2090
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002091 /* Check host->mrq first in case we are runtime suspended */
Shawn Guo9668d762013-06-09 19:49:24 +08002092 if (host->mrq && !sdhci_do_get_cd(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302093 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002094 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302095 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002096 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002097
Russell King03231f92014-04-25 12:57:12 +01002098 sdhci_do_reset(host, SDHCI_RESET_CMD);
2099 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002100
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002101 host->mrq->cmd->error = -ENOMEDIUM;
2102 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002103 }
2104
2105 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002106}
2107
2108static const struct mmc_host_ops sdhci_ops = {
2109 .request = sdhci_request,
2110 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002111 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002112 .get_ro = sdhci_get_ro,
2113 .hw_reset = sdhci_hw_reset,
2114 .enable_sdio_irq = sdhci_enable_sdio_irq,
2115 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2116 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002117 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002118 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002119};
2120
2121/*****************************************************************************\
2122 * *
2123 * Tasklets *
2124 * *
2125\*****************************************************************************/
2126
Pierre Ossmand129bce2006-03-24 03:18:17 -08002127static void sdhci_tasklet_finish(unsigned long param)
2128{
2129 struct sdhci_host *host;
2130 unsigned long flags;
2131 struct mmc_request *mrq;
2132
2133 host = (struct sdhci_host*)param;
2134
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002135 spin_lock_irqsave(&host->lock, flags);
2136
Chris Ball0c9c99a2011-04-27 17:35:31 -04002137 /*
2138 * If this tasklet gets rescheduled while running, it will
2139 * be run again afterwards but without any active request.
2140 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002141 if (!host->mrq) {
2142 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002143 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002144 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002145
2146 del_timer(&host->timer);
2147
2148 mrq = host->mrq;
2149
Pierre Ossmand129bce2006-03-24 03:18:17 -08002150 /*
2151 * The controller needs a reset of internal state machines
2152 * upon error conditions.
2153 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002154 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002155 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002156 (mrq->sbc && mrq->sbc->error) ||
2157 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2158 (mrq->data->stop && mrq->data->stop->error))) ||
2159 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002160
2161 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002162 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002163 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002164 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002165
2166 /* Spec says we should do both at the same time, but Ricoh
2167 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002168 sdhci_do_reset(host, SDHCI_RESET_CMD);
2169 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002170 }
2171
2172 host->mrq = NULL;
2173 host->cmd = NULL;
2174 host->data = NULL;
2175
Pierre Ossmanf9134312008-12-21 17:01:48 +01002176#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002177 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002178#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002179
Pierre Ossman5f25a662006-10-04 02:15:39 -07002180 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002181 spin_unlock_irqrestore(&host->lock, flags);
2182
2183 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002184 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002185}
2186
2187static void sdhci_timeout_timer(unsigned long data)
2188{
2189 struct sdhci_host *host;
2190 unsigned long flags;
2191
2192 host = (struct sdhci_host*)data;
2193
2194 spin_lock_irqsave(&host->lock, flags);
2195
2196 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302197 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002198 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002199 sdhci_dumpregs(host);
2200
2201 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002202 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002203 sdhci_finish_data(host);
2204 } else {
2205 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002206 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002207 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002208 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002209
2210 tasklet_schedule(&host->finish_tasklet);
2211 }
2212 }
2213
Pierre Ossman5f25a662006-10-04 02:15:39 -07002214 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002215 spin_unlock_irqrestore(&host->lock, flags);
2216}
2217
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302218static void sdhci_tuning_timer(unsigned long data)
2219{
2220 struct sdhci_host *host;
2221 unsigned long flags;
2222
2223 host = (struct sdhci_host *)data;
2224
2225 spin_lock_irqsave(&host->lock, flags);
2226
2227 host->flags |= SDHCI_NEEDS_RETUNING;
2228
2229 spin_unlock_irqrestore(&host->lock, flags);
2230}
2231
Pierre Ossmand129bce2006-03-24 03:18:17 -08002232/*****************************************************************************\
2233 * *
2234 * Interrupt handling *
2235 * *
2236\*****************************************************************************/
2237
Adrian Hunter61541392014-09-24 10:27:27 +03002238static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002239{
2240 BUG_ON(intmask == 0);
2241
2242 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302243 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002244 "though no command operation was in progress.\n",
2245 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002246 sdhci_dumpregs(host);
2247 return;
2248 }
2249
Pierre Ossman43b58b32007-07-25 23:15:27 +02002250 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002251 host->cmd->error = -ETIMEDOUT;
2252 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2253 SDHCI_INT_INDEX))
2254 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002255
Pierre Ossmane8095172008-07-25 01:09:08 +02002256 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002257 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002258 return;
2259 }
2260
2261 /*
2262 * The host can send and interrupt when the busy state has
2263 * ended, allowing us to wait without wasting CPU cycles.
2264 * Unfortunately this is overloaded on the "data complete"
2265 * interrupt, so we need to take some care when handling
2266 * it.
2267 *
2268 * Note: The 1.0 specification is a bit ambiguous about this
2269 * feature so there might be some problems with older
2270 * controllers.
2271 */
2272 if (host->cmd->flags & MMC_RSP_BUSY) {
2273 if (host->cmd->data)
2274 DBG("Cannot wait for busy signal when also "
2275 "doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002276 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2277 && !host->busy_handle) {
2278 /* Mark that command complete before busy is ended */
2279 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002280 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002281 }
Ben Dooksf9454052009-02-20 20:33:08 +03002282
2283 /* The controller does not support the end-of-busy IRQ,
2284 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002285 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2286 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2287 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002288 }
2289
2290 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002291 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002292}
2293
George G. Davis0957c332010-02-18 12:32:12 -05002294#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002295static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002296{
2297 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002298 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002299 __le32 *dma;
2300 __le16 *len;
2301 u8 attr;
2302
2303 sdhci_dumpregs(host);
2304
2305 while (true) {
2306 dma = (__le32 *)(desc + 4);
2307 len = (__le16 *)(desc + 2);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002308 attr = *(u8 *)desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002309
2310 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2311 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2312
Adrian Hunter76fe3792014-11-04 12:42:42 +02002313 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002314
2315 if (attr & 2)
2316 break;
2317 }
2318}
2319#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002320static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002321#endif
2322
Pierre Ossmand129bce2006-03-24 03:18:17 -08002323static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2324{
Girish K S069c9f12012-01-06 09:56:39 +05302325 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002326 BUG_ON(intmask == 0);
2327
Arindam Nathb513ea22011-05-05 12:19:04 +05302328 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2329 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302330 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2331 if (command == MMC_SEND_TUNING_BLOCK ||
2332 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302333 host->tuning_done = 1;
2334 wake_up(&host->buf_ready_int);
2335 return;
2336 }
2337 }
2338
Pierre Ossmand129bce2006-03-24 03:18:17 -08002339 if (!host->data) {
2340 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002341 * The "data complete" interrupt is also used to
2342 * indicate that a busy state has ended. See comment
2343 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002344 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002345 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002346 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2347 host->cmd->error = -ETIMEDOUT;
2348 tasklet_schedule(&host->finish_tasklet);
2349 return;
2350 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002351 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002352 /*
2353 * Some cards handle busy-end interrupt
2354 * before the command completed, so make
2355 * sure we do things in the proper order.
2356 */
2357 if (host->busy_handle)
2358 sdhci_finish_command(host);
2359 else
2360 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002361 return;
2362 }
2363 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002364
Girish K Sa3c76eb2011-10-11 11:44:09 +05302365 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002366 "though no data operation was in progress.\n",
2367 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002368 sdhci_dumpregs(host);
2369
2370 return;
2371 }
2372
2373 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002374 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002375 else if (intmask & SDHCI_INT_DATA_END_BIT)
2376 host->data->error = -EILSEQ;
2377 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2378 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2379 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002380 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002381 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302382 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002383 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002384 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002385 if (host->ops->adma_workaround)
2386 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002387 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002388
Pierre Ossman17b04292007-07-22 22:18:46 +02002389 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002390 sdhci_finish_data(host);
2391 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002392 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002393 sdhci_transfer_pio(host);
2394
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002395 /*
2396 * We currently don't do anything fancy with DMA
2397 * boundaries, but as we can't disable the feature
2398 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002399 *
2400 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2401 * should return a valid address to continue from, but as
2402 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002403 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002404 if (intmask & SDHCI_INT_DMA_END) {
2405 u32 dmastart, dmanow;
2406 dmastart = sg_dma_address(host->data->sg);
2407 dmanow = dmastart + host->data->bytes_xfered;
2408 /*
2409 * Force update to the next DMA block boundary.
2410 */
2411 dmanow = (dmanow &
2412 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2413 SDHCI_DEFAULT_BOUNDARY_SIZE;
2414 host->data->bytes_xfered = dmanow - dmastart;
2415 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2416 " next 0x%08x\n",
2417 mmc_hostname(host->mmc), dmastart,
2418 host->data->bytes_xfered, dmanow);
2419 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2420 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002421
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002422 if (intmask & SDHCI_INT_DATA_END) {
2423 if (host->cmd) {
2424 /*
2425 * Data managed to finish before the
2426 * command completed. Make sure we do
2427 * things in the proper order.
2428 */
2429 host->data_early = 1;
2430 } else {
2431 sdhci_finish_data(host);
2432 }
2433 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002434 }
2435}
2436
David Howells7d12e782006-10-05 14:55:46 +01002437static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002438{
Russell King781e9892014-04-25 12:55:46 +01002439 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002440 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002441 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002442 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002443
2444 spin_lock(&host->lock);
2445
Russell Kingbe138552014-04-25 12:55:56 +01002446 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002447 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002448 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002449 }
2450
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002451 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002452 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002453 result = IRQ_NONE;
2454 goto out;
2455 }
2456
Russell King41005002014-04-25 12:55:36 +01002457 do {
2458 /* Clear selected interrupts. */
2459 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2460 SDHCI_INT_BUS_POWER);
2461 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002462
Russell King41005002014-04-25 12:55:36 +01002463 DBG("*** %s got interrupt: 0x%08x\n",
2464 mmc_hostname(host->mmc), intmask);
2465
2466 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2467 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2468 SDHCI_CARD_PRESENT;
2469
2470 /*
2471 * There is a observation on i.mx esdhc. INSERT
2472 * bit will be immediately set again when it gets
2473 * cleared, if a card is inserted. We have to mask
2474 * the irq to prevent interrupt storm which will
2475 * freeze the system. And the REMOVE gets the
2476 * same situation.
2477 *
2478 * More testing are needed here to ensure it works
2479 * for other platforms though.
2480 */
Russell Kingb537f942014-04-25 12:56:01 +01002481 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2482 SDHCI_INT_CARD_REMOVE);
2483 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2484 SDHCI_INT_CARD_INSERT;
2485 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2486 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002487
2488 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2489 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002490
2491 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2492 SDHCI_INT_CARD_REMOVE);
2493 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002494 }
2495
2496 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002497 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2498 &intmask);
Russell King41005002014-04-25 12:55:36 +01002499
2500 if (intmask & SDHCI_INT_DATA_MASK)
2501 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2502
2503 if (intmask & SDHCI_INT_BUS_POWER)
2504 pr_err("%s: Card is consuming too much power!\n",
2505 mmc_hostname(host->mmc));
2506
Russell King781e9892014-04-25 12:55:46 +01002507 if (intmask & SDHCI_INT_CARD_INT) {
2508 sdhci_enable_sdio_irq_nolock(host, false);
2509 host->thread_isr |= SDHCI_INT_CARD_INT;
2510 result = IRQ_WAKE_THREAD;
2511 }
Russell King41005002014-04-25 12:55:36 +01002512
2513 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2514 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2515 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2516 SDHCI_INT_CARD_INT);
2517
2518 if (intmask) {
2519 unexpected |= intmask;
2520 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2521 }
2522
Russell King781e9892014-04-25 12:55:46 +01002523 if (result == IRQ_NONE)
2524 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002525
2526 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002527 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002528out:
2529 spin_unlock(&host->lock);
2530
Alexander Stein6379b232012-03-14 09:52:10 +01002531 if (unexpected) {
2532 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2533 mmc_hostname(host->mmc), unexpected);
2534 sdhci_dumpregs(host);
2535 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002536
Pierre Ossmand129bce2006-03-24 03:18:17 -08002537 return result;
2538}
2539
Russell King781e9892014-04-25 12:55:46 +01002540static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2541{
2542 struct sdhci_host *host = dev_id;
2543 unsigned long flags;
2544 u32 isr;
2545
2546 spin_lock_irqsave(&host->lock, flags);
2547 isr = host->thread_isr;
2548 host->thread_isr = 0;
2549 spin_unlock_irqrestore(&host->lock, flags);
2550
Russell King3560db82014-04-25 12:55:51 +01002551 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2552 sdhci_card_event(host->mmc);
2553 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2554 }
2555
Russell King781e9892014-04-25 12:55:46 +01002556 if (isr & SDHCI_INT_CARD_INT) {
2557 sdio_run_irqs(host->mmc);
2558
2559 spin_lock_irqsave(&host->lock, flags);
2560 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2561 sdhci_enable_sdio_irq_nolock(host, true);
2562 spin_unlock_irqrestore(&host->lock, flags);
2563 }
2564
2565 return isr ? IRQ_HANDLED : IRQ_NONE;
2566}
2567
Pierre Ossmand129bce2006-03-24 03:18:17 -08002568/*****************************************************************************\
2569 * *
2570 * Suspend/resume *
2571 * *
2572\*****************************************************************************/
2573
2574#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002575void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2576{
2577 u8 val;
2578 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2579 | SDHCI_WAKE_ON_INT;
2580
2581 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2582 val |= mask ;
2583 /* Avoid fake wake up */
2584 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2585 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2586 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2587}
2588EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2589
Fabio Estevam0b10f472014-08-30 14:53:13 -03002590static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002591{
2592 u8 val;
2593 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2594 | SDHCI_WAKE_ON_INT;
2595
2596 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2597 val &= ~mask;
2598 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2599}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002600
Manuel Lauss29495aa2011-11-03 11:09:45 +01002601int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002602{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002603 sdhci_disable_card_detection(host);
2604
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302605 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002606 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002607 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302608 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302609 }
2610
Kevin Liuad080d72013-01-05 17:21:33 +08002611 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002612 host->ier = 0;
2613 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2614 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002615 free_irq(host->irq, host);
2616 } else {
2617 sdhci_enable_irq_wakeups(host);
2618 enable_irq_wake(host->irq);
2619 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002620 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002621}
2622
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002623EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002624
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002625int sdhci_resume_host(struct sdhci_host *host)
2626{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002627 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002628
Richard Röjforsa13abc72009-09-22 16:45:30 -07002629 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002630 if (host->ops->enable_dma)
2631 host->ops->enable_dma(host);
2632 }
2633
Kevin Liuad080d72013-01-05 17:21:33 +08002634 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell King781e9892014-04-25 12:55:46 +01002635 ret = request_threaded_irq(host->irq, sdhci_irq,
2636 sdhci_thread_irq, IRQF_SHARED,
2637 mmc_hostname(host->mmc), host);
Kevin Liuad080d72013-01-05 17:21:33 +08002638 if (ret)
2639 return ret;
2640 } else {
2641 sdhci_disable_irq_wakeups(host);
2642 disable_irq_wake(host->irq);
2643 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002644
Adrian Hunter6308d292012-02-07 14:48:54 +02002645 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2646 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2647 /* Card keeps power but host controller does not */
2648 sdhci_init(host, 0);
2649 host->pwr = 0;
2650 host->clock = 0;
2651 sdhci_do_set_ios(host, &host->mmc->ios);
2652 } else {
2653 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2654 mmiowb();
2655 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002656
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002657 sdhci_enable_card_detection(host);
2658
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302659 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002660 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302661 host->flags |= SDHCI_NEEDS_RETUNING;
2662
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002663 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002664}
2665
2666EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002667#endif /* CONFIG_PM */
2668
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002669#ifdef CONFIG_PM_RUNTIME
2670
2671static int sdhci_runtime_pm_get(struct sdhci_host *host)
2672{
2673 return pm_runtime_get_sync(host->mmc->parent);
2674}
2675
2676static int sdhci_runtime_pm_put(struct sdhci_host *host)
2677{
2678 pm_runtime_mark_last_busy(host->mmc->parent);
2679 return pm_runtime_put_autosuspend(host->mmc->parent);
2680}
2681
Adrian Hunterf0710a52013-05-06 12:17:32 +03002682static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2683{
2684 if (host->runtime_suspended || host->bus_on)
2685 return;
2686 host->bus_on = true;
2687 pm_runtime_get_noresume(host->mmc->parent);
2688}
2689
2690static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2691{
2692 if (host->runtime_suspended || !host->bus_on)
2693 return;
2694 host->bus_on = false;
2695 pm_runtime_put_noidle(host->mmc->parent);
2696}
2697
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002698int sdhci_runtime_suspend_host(struct sdhci_host *host)
2699{
2700 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002701
2702 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002703 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002704 del_timer_sync(&host->tuning_timer);
2705 host->flags &= ~SDHCI_NEEDS_RETUNING;
2706 }
2707
2708 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002709 host->ier &= SDHCI_INT_CARD_INT;
2710 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2711 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002712 spin_unlock_irqrestore(&host->lock, flags);
2713
Russell King781e9892014-04-25 12:55:46 +01002714 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002715
2716 spin_lock_irqsave(&host->lock, flags);
2717 host->runtime_suspended = true;
2718 spin_unlock_irqrestore(&host->lock, flags);
2719
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002720 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002721}
2722EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2723
2724int sdhci_runtime_resume_host(struct sdhci_host *host)
2725{
2726 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002727 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002728
2729 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2730 if (host->ops->enable_dma)
2731 host->ops->enable_dma(host);
2732 }
2733
2734 sdhci_init(host, 0);
2735
2736 /* Force clock and power re-program */
2737 host->pwr = 0;
2738 host->clock = 0;
2739 sdhci_do_set_ios(host, &host->mmc->ios);
2740
2741 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002742 if ((host_flags & SDHCI_PV_ENABLED) &&
2743 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2744 spin_lock_irqsave(&host->lock, flags);
2745 sdhci_enable_preset_value(host, true);
2746 spin_unlock_irqrestore(&host->lock, flags);
2747 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002748
2749 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002750 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002751 host->flags |= SDHCI_NEEDS_RETUNING;
2752
2753 spin_lock_irqsave(&host->lock, flags);
2754
2755 host->runtime_suspended = false;
2756
2757 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002758 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002759 sdhci_enable_sdio_irq_nolock(host, true);
2760
2761 /* Enable Card Detection */
2762 sdhci_enable_card_detection(host);
2763
2764 spin_unlock_irqrestore(&host->lock, flags);
2765
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002766 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002767}
2768EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2769
2770#endif
2771
Pierre Ossmand129bce2006-03-24 03:18:17 -08002772/*****************************************************************************\
2773 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002774 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002775 * *
2776\*****************************************************************************/
2777
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002778struct sdhci_host *sdhci_alloc_host(struct device *dev,
2779 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002780{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002781 struct mmc_host *mmc;
2782 struct sdhci_host *host;
2783
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002784 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002785
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002786 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002787 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002788 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002789
2790 host = mmc_priv(mmc);
2791 host->mmc = mmc;
2792
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002793 return host;
2794}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002795
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002796EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002797
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002798int sdhci_add_host(struct sdhci_host *host)
2799{
2800 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002801 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302802 u32 max_current_caps;
2803 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002804 unsigned int override_timeout_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002805 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002806
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002807 WARN_ON(host == NULL);
2808 if (host == NULL)
2809 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002810
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002811 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002812
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002813 if (debug_quirks)
2814 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002815 if (debug_quirks2)
2816 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002817
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002818 override_timeout_clk = host->timeout_clk;
2819
Russell King03231f92014-04-25 12:57:12 +01002820 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002821
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002822 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002823 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2824 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002825 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302826 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002827 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002828 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002829 }
2830
Arindam Nathf2119df2011-05-05 12:18:57 +05302831 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002832 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002833
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002834 if (host->version >= SDHCI_SPEC_300)
2835 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2836 host->caps1 :
2837 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302838
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002839 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002840 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302841 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002842 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002843 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002844 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002845
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002846 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002847 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002848 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002849 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002850 }
2851
Arindam Nathf2119df2011-05-05 12:18:57 +05302852 if ((host->version >= SDHCI_SPEC_200) &&
2853 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002854 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002855
2856 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2857 (host->flags & SDHCI_USE_ADMA)) {
2858 DBG("Disabling ADMA as it is marked broken\n");
2859 host->flags &= ~SDHCI_USE_ADMA;
2860 }
2861
Richard Röjforsa13abc72009-09-22 16:45:30 -07002862 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002863 if (host->ops->enable_dma) {
2864 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07002865 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002866 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002867 host->flags &=
2868 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002869 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002870 }
2871 }
2872
Pierre Ossman2134a922008-06-28 18:28:51 +02002873 if (host->flags & SDHCI_USE_ADMA) {
2874 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002875 * The DMA descriptor table size is calculated as the maximum
2876 * number of segments times 2, to allow for an alignment
2877 * descriptor for each segment, plus 1 for a nop end descriptor,
2878 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002879 */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02002880 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) * 8;
2881 host->align_buffer_sz = SDHCI_MAX_SEGS * 4;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002882 host->desc_sz = 8;
2883 host->align_sz = 4;
2884 host->align_mask = 3;
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002885 host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +02002886 host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002887 &host->adma_addr,
2888 GFP_KERNEL);
Adrian Hunter76fe3792014-11-04 12:42:42 +02002889 host->align_buffer = kmalloc(host->align_buffer_sz, GFP_KERNEL);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002890 if (!host->adma_table || !host->align_buffer) {
Adrian Hunter76fe3792014-11-04 12:42:42 +02002891 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002892 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02002893 kfree(host->align_buffer);
Joe Perches66061102014-09-12 14:56:56 -07002894 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002895 mmc_hostname(mmc));
2896 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002897 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01002898 host->align_buffer = NULL;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002899 } else if (host->adma_addr & host->align_mask) {
Joe Perches66061102014-09-12 14:56:56 -07002900 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2901 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002902 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002903 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002904 host->adma_table, host->adma_addr);
Russell Kingd1e49f72014-04-25 12:58:34 +01002905 kfree(host->align_buffer);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002906 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01002907 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02002908 }
2909 }
2910
Pierre Ossman76591502008-07-21 00:32:11 +02002911 /*
2912 * If we use DMA, then it's up to the caller to set the DMA
2913 * mask, but PIO does not need the hw shim so we set a new
2914 * mask here in that case.
2915 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002916 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002917 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07002918 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02002919 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002920
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002921 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302922 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002923 >> SDHCI_CLOCK_BASE_SHIFT;
2924 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302925 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002926 >> SDHCI_CLOCK_BASE_SHIFT;
2927
Pierre Ossmand129bce2006-03-24 03:18:17 -08002928 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002929 if (host->max_clk == 0 || host->quirks &
2930 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002931 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302932 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002933 "frequency.\n", mmc_hostname(mmc));
2934 return -ENODEV;
2935 }
2936 host->max_clk = host->ops->get_max_clock(host);
2937 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002938
2939 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302940 * In case of Host Controller v3.00, find out whether clock
2941 * multiplier is supported.
2942 */
2943 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2944 SDHCI_CLOCK_MUL_SHIFT;
2945
2946 /*
2947 * In case the value in Clock Multiplier is 0, then programmable
2948 * clock mode is not supported, otherwise the actual clock
2949 * multiplier is one more than the value of Clock Multiplier
2950 * in the Capabilities Register.
2951 */
2952 if (host->clk_mul)
2953 host->clk_mul += 1;
2954
2955 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002956 * Set host parameters.
2957 */
2958 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302959 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002960 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002961 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302962 else if (host->version >= SDHCI_SPEC_300) {
2963 if (host->clk_mul) {
2964 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2965 mmc->f_max = host->max_clk * host->clk_mul;
2966 } else
2967 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2968 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002969 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002970
Aisheng Dong28aab052014-08-27 15:26:31 +08002971 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2972 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
2973 SDHCI_TIMEOUT_CLK_SHIFT;
2974 if (host->timeout_clk == 0) {
2975 if (host->ops->get_timeout_clock) {
2976 host->timeout_clk =
2977 host->ops->get_timeout_clock(host);
2978 } else {
2979 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
2980 mmc_hostname(mmc));
2981 return -ENODEV;
2982 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03002983 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03002984
Aisheng Dong28aab052014-08-27 15:26:31 +08002985 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2986 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002987
Aisheng Dong28aab052014-08-27 15:26:31 +08002988 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08002989 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08002990 mmc->max_busy_timeout /= host->timeout_clk;
2991 }
Adrian Hunter58d12462011-06-28 17:16:03 +03002992
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002993 if (override_timeout_clk)
2994 host->timeout_clk = override_timeout_clk;
2995
Andrei Warkentine89d4562011-05-23 15:06:37 -05002996 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01002997 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05002998
2999 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3000 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003001
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003002 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003003 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003004 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003005 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003006 host->flags |= SDHCI_AUTO_CMD23;
3007 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3008 } else {
3009 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3010 }
3011
Philip Rakity15ec4462010-11-19 16:48:39 -05003012 /*
3013 * A controller may support 8-bit width, but the board itself
3014 * might not have the pins brought out. Boards that support
3015 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3016 * their platform code before calling sdhci_add_host(), and we
3017 * won't assume 8-bit width for hosts without that CAP.
3018 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003019 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003020 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003021
Jerry Huang63ef5d82012-10-25 13:47:19 +08003022 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3023 mmc->caps &= ~MMC_CAP_CMD23;
3024
Arindam Nathf2119df2011-05-05 12:18:57 +05303025 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003026 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003027
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003028 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Markus Mayer4e743f12014-07-03 13:27:42 -07003029 !(mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003030 mmc->caps |= MMC_CAP_NEEDS_POLL;
3031
Tim Kryger3a48edc2014-06-13 10:13:56 -07003032 /* If there are external regulators, get them */
3033 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3034 return -EPROBE_DEFER;
3035
Philip Rakity6231f3d2012-07-23 15:56:23 -07003036 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003037 if (!IS_ERR(mmc->supply.vqmmc)) {
3038 ret = regulator_enable(mmc->supply.vqmmc);
3039 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3040 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003041 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3042 SDHCI_SUPPORT_SDR50 |
3043 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003044 if (ret) {
3045 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3046 mmc_hostname(mmc), ret);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003047 mmc->supply.vqmmc = NULL;
Chris Balla3361ab2013-03-11 17:51:53 -04003048 }
Kevin Liu8363c372012-11-17 17:55:51 -05003049 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003050
Daniel Drake6a661802012-11-25 13:01:19 -05003051 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3052 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3053 SDHCI_SUPPORT_DDR50);
3054
Al Cooper4188bba2012-03-16 15:54:17 -04003055 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3056 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3057 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303058 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3059
3060 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003061 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303062 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003063 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3064 * field can be promoted to support HS200.
3065 */
Chuanxiao.Dongadc82852014-08-19 11:02:41 +08003066 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200)) {
David Cohen13868bf2013-10-29 10:58:26 -07003067 mmc->caps2 |= MMC_CAP2_HS200;
Chuanxiao.Dongadc82852014-08-19 11:02:41 +08003068 if (IS_ERR(mmc->supply.vqmmc) ||
3069 !regulator_is_supported_voltage
3070 (mmc->supply.vqmmc, 1100000, 1300000))
3071 mmc->caps2 &= ~MMC_CAP2_HS200_1_2V_SDR;
3072 }
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003073 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303074 mmc->caps |= MMC_CAP_UHS_SDR50;
3075
Micky Ching9107ebb2014-02-21 18:40:35 +08003076 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3077 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303078 mmc->caps |= MMC_CAP_UHS_DDR50;
3079
Girish K S069c9f12012-01-06 09:56:39 +05303080 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303081 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3082 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3083
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003084 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303085 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003086 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303087
Arindam Nathd6d50a12011-05-05 12:18:59 +05303088 /* Driver Type(s) (A, C, D) supported by the host */
3089 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3090 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3091 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3092 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3093 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3094 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3095
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303096 /* Initial value for re-tuning timer count */
3097 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3098 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3099
3100 /*
3101 * In case Re-tuning Timer is not disabled, the actual value of
3102 * re-tuning timer will be 2 ^ (n - 1).
3103 */
3104 if (host->tuning_count)
3105 host->tuning_count = 1 << (host->tuning_count - 1);
3106
3107 /* Re-tuning mode supported by the Host Controller */
3108 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3109 SDHCI_RETUNING_MODE_SHIFT;
3110
Takashi Iwai8f230f42010-12-08 10:04:30 +01003111 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003112
Arindam Nathf2119df2011-05-05 12:18:57 +05303113 /*
3114 * According to SD Host Controller spec v3.00, if the Host System
3115 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3116 * the value is meaningful only if Voltage Support in the Capabilities
3117 * register is set. The actual current value is 4 times the register
3118 * value.
3119 */
3120 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003121 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003122 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003123 if (curr > 0) {
3124
3125 /* convert to SDHCI_MAX_CURRENT format */
3126 curr = curr/1000; /* convert to mA */
3127 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3128
3129 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3130 max_current_caps =
3131 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3132 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3133 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3134 }
3135 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303136
3137 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003138 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303139
Aaron Lu55c46652012-07-04 13:31:48 +08003140 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303141 SDHCI_MAX_CURRENT_330_MASK) >>
3142 SDHCI_MAX_CURRENT_330_SHIFT) *
3143 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303144 }
3145 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003146 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303147
Aaron Lu55c46652012-07-04 13:31:48 +08003148 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303149 SDHCI_MAX_CURRENT_300_MASK) >>
3150 SDHCI_MAX_CURRENT_300_SHIFT) *
3151 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303152 }
3153 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003154 ocr_avail |= MMC_VDD_165_195;
3155
Aaron Lu55c46652012-07-04 13:31:48 +08003156 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303157 SDHCI_MAX_CURRENT_180_MASK) >>
3158 SDHCI_MAX_CURRENT_180_SHIFT) *
3159 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303160 }
3161
Tim Kryger52221612014-06-25 00:25:34 -07003162 /* If OCR set by external regulators, use it instead */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003163 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003164 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003165
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003166 if (host->ocr_mask)
Tim Kryger3a48edc2014-06-13 10:13:56 -07003167 ocr_avail &= host->ocr_mask;
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003168
Takashi Iwai8f230f42010-12-08 10:04:30 +01003169 mmc->ocr_avail = ocr_avail;
3170 mmc->ocr_avail_sdio = ocr_avail;
3171 if (host->ocr_avail_sdio)
3172 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3173 mmc->ocr_avail_sd = ocr_avail;
3174 if (host->ocr_avail_sd)
3175 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3176 else /* normal SD controllers don't support 1.8V */
3177 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3178 mmc->ocr_avail_mmc = ocr_avail;
3179 if (host->ocr_avail_mmc)
3180 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003181
3182 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303183 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003184 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003185 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003186 }
3187
Pierre Ossmand129bce2006-03-24 03:18:17 -08003188 spin_lock_init(&host->lock);
3189
3190 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003191 * Maximum number of segments. Depends on if the hardware
3192 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003193 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003194 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003195 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003196 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003197 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003198 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003199 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003200
3201 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003202 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01003203 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08003204 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003205 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003206
3207 /*
3208 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003209 * of bytes. When doing hardware scatter/gather, each entry cannot
3210 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003211 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003212 if (host->flags & SDHCI_USE_ADMA) {
3213 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3214 mmc->max_seg_size = 65535;
3215 else
3216 mmc->max_seg_size = 65536;
3217 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003218 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003219 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003220
3221 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003222 * Maximum block size. This varies from controller to controller and
3223 * is specified in the capabilities register.
3224 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003225 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3226 mmc->max_blk_size = 2;
3227 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303228 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003229 SDHCI_MAX_BLOCK_SHIFT;
3230 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003231 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3232 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003233 mmc->max_blk_size = 0;
3234 }
3235 }
3236
3237 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003238
3239 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003240 * Maximum block count.
3241 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003242 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003243
3244 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003245 * Init tasklets.
3246 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003247 tasklet_init(&host->finish_tasklet,
3248 sdhci_tasklet_finish, (unsigned long)host);
3249
Al Viroe4cad1b2006-10-10 22:47:07 +01003250 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003251
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303252 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303253 init_waitqueue_head(&host->buf_ready_int);
3254
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303255 /* Initialize re-tuning timer */
3256 init_timer(&host->tuning_timer);
3257 host->tuning_timer.data = (unsigned long)host;
3258 host->tuning_timer.function = sdhci_tuning_timer;
3259 }
3260
Shawn Guo2af502c2013-07-05 14:38:55 +08003261 sdhci_init(host, 0);
3262
Russell King781e9892014-04-25 12:55:46 +01003263 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3264 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003265 if (ret) {
3266 pr_err("%s: Failed to request IRQ %d: %d\n",
3267 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003268 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003269 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003270
Pierre Ossmand129bce2006-03-24 03:18:17 -08003271#ifdef CONFIG_MMC_DEBUG
3272 sdhci_dumpregs(host);
3273#endif
3274
Pierre Ossmanf9134312008-12-21 17:01:48 +01003275#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003276 snprintf(host->led_name, sizeof(host->led_name),
3277 "%s::", mmc_hostname(mmc));
3278 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003279 host->led.brightness = LED_OFF;
3280 host->led.default_trigger = mmc_hostname(mmc);
3281 host->led.brightness_set = sdhci_led_control;
3282
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003283 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003284 if (ret) {
3285 pr_err("%s: Failed to register LED device: %d\n",
3286 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003287 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003288 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003289#endif
3290
Pierre Ossman5f25a662006-10-04 02:15:39 -07003291 mmiowb();
3292
Pierre Ossmand129bce2006-03-24 03:18:17 -08003293 mmc_add_host(mmc);
3294
Girish K Sa3c76eb2011-10-11 11:44:09 +05303295 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003296 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003297 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3298 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003299
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003300 sdhci_enable_card_detection(host);
3301
Pierre Ossmand129bce2006-03-24 03:18:17 -08003302 return 0;
3303
Pierre Ossmanf9134312008-12-21 17:01:48 +01003304#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003305reset:
Russell King03231f92014-04-25 12:57:12 +01003306 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003307 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3308 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003309 free_irq(host->irq, host);
3310#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003311untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003312 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003313
3314 return ret;
3315}
3316
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003317EXPORT_SYMBOL_GPL(sdhci_add_host);
3318
Pierre Ossman1e728592008-04-16 19:13:13 +02003319void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003320{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003321 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003322 unsigned long flags;
3323
3324 if (dead) {
3325 spin_lock_irqsave(&host->lock, flags);
3326
3327 host->flags |= SDHCI_DEVICE_DEAD;
3328
3329 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303330 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003331 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003332
3333 host->mrq->cmd->error = -ENOMEDIUM;
3334 tasklet_schedule(&host->finish_tasklet);
3335 }
3336
3337 spin_unlock_irqrestore(&host->lock, flags);
3338 }
3339
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003340 sdhci_disable_card_detection(host);
3341
Markus Mayer4e743f12014-07-03 13:27:42 -07003342 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003343
Pierre Ossmanf9134312008-12-21 17:01:48 +01003344#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003345 led_classdev_unregister(&host->led);
3346#endif
3347
Pierre Ossman1e728592008-04-16 19:13:13 +02003348 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003349 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003350
Russell Kingb537f942014-04-25 12:56:01 +01003351 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3352 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003353 free_irq(host->irq, host);
3354
3355 del_timer_sync(&host->timer);
3356
Pierre Ossmand129bce2006-03-24 03:18:17 -08003357 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003358
Tim Kryger3a48edc2014-06-13 10:13:56 -07003359 if (!IS_ERR(mmc->supply.vqmmc))
3360 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003361
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003362 if (host->adma_table)
Adrian Hunter76fe3792014-11-04 12:42:42 +02003363 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003364 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003365 kfree(host->align_buffer);
3366
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003367 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003368 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003369}
3370
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003371EXPORT_SYMBOL_GPL(sdhci_remove_host);
3372
3373void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003374{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003375 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003376}
3377
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003378EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003379
3380/*****************************************************************************\
3381 * *
3382 * Driver init/exit *
3383 * *
3384\*****************************************************************************/
3385
3386static int __init sdhci_drv_init(void)
3387{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303388 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003389 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303390 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003391
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003392 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003393}
3394
3395static void __exit sdhci_drv_exit(void)
3396{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003397}
3398
3399module_init(sdhci_drv_init);
3400module_exit(sdhci_drv_exit);
3401
Pierre Ossmandf673b22006-06-30 02:22:31 -07003402module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003403module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003404
Pierre Ossman32710e82009-04-08 20:14:54 +02003405MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003406MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003407MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003408
Pierre Ossmandf673b22006-06-30 02:22:31 -07003409MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003410MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");