blob: ec093490a24f379caecb5b52ac74621097e355f8 [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{
Adrian Hunter05452302014-11-04 12:42:45 +0200451 struct sdhci_adma2_32_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800452
Adrian Hunter05452302014-11-04 12:42:45 +0200453 dma_desc->cmd = cpu_to_le16(cmd);
454 dma_desc->len = cpu_to_le16(len);
455 dma_desc->addr = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800456}
457
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200458static void sdhci_adma_mark_end(void *desc)
459{
Adrian Hunter05452302014-11-04 12:42:45 +0200460 struct sdhci_adma2_32_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200461
Adrian Hunter05452302014-11-04 12:42:45 +0200462 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200463}
464
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200465static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200466 struct mmc_data *data)
467{
468 int direction;
469
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200470 void *desc;
471 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200472 dma_addr_t addr;
473 dma_addr_t align_addr;
474 int len, offset;
475
476 struct scatterlist *sg;
477 int i;
478 char *buffer;
479 unsigned long flags;
480
481 /*
482 * The spec does not specify endianness of descriptor table.
483 * We currently guess that it is LE.
484 */
485
486 if (data->flags & MMC_DATA_READ)
487 direction = DMA_FROM_DEVICE;
488 else
489 direction = DMA_TO_DEVICE;
490
Pierre Ossman2134a922008-06-28 18:28:51 +0200491 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200492 host->align_buffer, host->align_buffer_sz, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700493 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200494 goto fail;
Adrian Hunter76fe3792014-11-04 12:42:42 +0200495 BUG_ON(host->align_addr & host->align_mask);
Pierre Ossman2134a922008-06-28 18:28:51 +0200496
497 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
498 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200499 if (host->sg_count == 0)
500 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200501
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200502 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200503 align = host->align_buffer;
504
505 align_addr = host->align_addr;
506
507 for_each_sg(data->sg, sg, host->sg_count, i) {
508 addr = sg_dma_address(sg);
509 len = sg_dma_len(sg);
510
511 /*
512 * The SDHCI specification states that ADMA
513 * addresses must be 32-bit aligned. If they
514 * aren't, then we use a bounce buffer for
515 * the (up to three) bytes that screw up the
516 * alignment.
517 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200518 offset = (host->align_sz - (addr & host->align_mask)) &
519 host->align_mask;
Pierre Ossman2134a922008-06-28 18:28:51 +0200520 if (offset) {
521 if (data->flags & MMC_DATA_WRITE) {
522 buffer = sdhci_kmap_atomic(sg, &flags);
Adrian Hunter8be78c62014-11-04 12:42:36 +0200523 WARN_ON(((long)buffer & (PAGE_SIZE - 1)) >
524 (PAGE_SIZE - offset));
Pierre Ossman2134a922008-06-28 18:28:51 +0200525 memcpy(align, buffer, offset);
526 sdhci_kunmap_atomic(buffer, &flags);
527 }
528
Ben Dooks118cd172010-03-05 13:43:26 -0800529 /* tran, valid */
Adrian Hunter739d46d2014-11-04 12:42:44 +0200530 sdhci_adma_write_desc(desc, align_addr, offset,
531 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200532
533 BUG_ON(offset > 65536);
534
Adrian Hunter76fe3792014-11-04 12:42:42 +0200535 align += host->align_sz;
536 align_addr += host->align_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200537
Adrian Hunter76fe3792014-11-04 12:42:42 +0200538 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200539
540 addr += offset;
541 len -= offset;
542 }
543
Pierre Ossman2134a922008-06-28 18:28:51 +0200544 BUG_ON(len > 65536);
545
Ben Dooks118cd172010-03-05 13:43:26 -0800546 /* tran, valid */
Adrian Hunter739d46d2014-11-04 12:42:44 +0200547 sdhci_adma_write_desc(desc, addr, len, ADMA2_TRAN_VALID);
Adrian Hunter76fe3792014-11-04 12:42:42 +0200548 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200549
550 /*
551 * If this triggers then we have a calculation bug
552 * somewhere. :/
553 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200554 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200555 }
556
Thomas Abraham70764a92010-05-26 14:42:04 -0700557 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
558 /*
559 * Mark the last descriptor as the terminating descriptor
560 */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200561 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200562 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200563 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700564 }
565 } else {
566 /*
567 * Add a terminating entry.
568 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200569
Thomas Abraham70764a92010-05-26 14:42:04 -0700570 /* nop, end, valid */
Adrian Hunter739d46d2014-11-04 12:42:44 +0200571 sdhci_adma_write_desc(desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700572 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200573
574 /*
575 * Resync align buffer as we might have changed it.
576 */
577 if (data->flags & MMC_DATA_WRITE) {
578 dma_sync_single_for_device(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200579 host->align_addr, host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200580 }
581
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200582 return 0;
583
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200584unmap_align:
585 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200586 host->align_buffer_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200587fail:
588 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200589}
590
591static void sdhci_adma_table_post(struct sdhci_host *host,
592 struct mmc_data *data)
593{
594 int direction;
595
596 struct scatterlist *sg;
597 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200598 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200599 char *buffer;
600 unsigned long flags;
Russell Kingde0b65a2014-04-25 12:58:29 +0100601 bool has_unaligned;
Pierre Ossman2134a922008-06-28 18:28:51 +0200602
603 if (data->flags & MMC_DATA_READ)
604 direction = DMA_FROM_DEVICE;
605 else
606 direction = DMA_TO_DEVICE;
607
Pierre Ossman2134a922008-06-28 18:28:51 +0200608 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200609 host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200610
Russell Kingde0b65a2014-04-25 12:58:29 +0100611 /* Do a quick scan of the SG list for any unaligned mappings */
612 has_unaligned = false;
613 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter76fe3792014-11-04 12:42:42 +0200614 if (sg_dma_address(sg) & host->align_mask) {
Russell Kingde0b65a2014-04-25 12:58:29 +0100615 has_unaligned = true;
616 break;
617 }
618
619 if (has_unaligned && data->flags & MMC_DATA_READ) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200620 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
621 data->sg_len, direction);
622
623 align = host->align_buffer;
624
625 for_each_sg(data->sg, sg, host->sg_count, i) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200626 if (sg_dma_address(sg) & host->align_mask) {
627 size = host->align_sz -
628 (sg_dma_address(sg) & host->align_mask);
Pierre Ossman2134a922008-06-28 18:28:51 +0200629
630 buffer = sdhci_kmap_atomic(sg, &flags);
Adrian Hunter8be78c62014-11-04 12:42:36 +0200631 WARN_ON(((long)buffer & (PAGE_SIZE - 1)) >
632 (PAGE_SIZE - size));
Pierre Ossman2134a922008-06-28 18:28:51 +0200633 memcpy(buffer, align, size);
634 sdhci_kunmap_atomic(buffer, &flags);
635
Adrian Hunter76fe3792014-11-04 12:42:42 +0200636 align += host->align_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200637 }
638 }
639 }
640
641 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
642 data->sg_len, direction);
643}
644
Andrei Warkentina3c77782011-04-11 16:13:42 -0500645static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800646{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700647 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500648 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700649 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800650
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200651 /*
652 * If the host controller provides us with an incorrect timeout
653 * value, just skip the check and use 0xE. The hardware may take
654 * longer to time out, but that's much better than having a too-short
655 * timeout value.
656 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200657 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200658 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200659
Andrei Warkentina3c77782011-04-11 16:13:42 -0500660 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100661 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500662 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800663
Andrei Warkentina3c77782011-04-11 16:13:42 -0500664 /* timeout in us */
665 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100666 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300667 else {
668 target_timeout = data->timeout_ns / 1000;
669 if (host->clock)
670 target_timeout += data->timeout_clks / host->clock;
671 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700672
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700673 /*
674 * Figure out needed cycles.
675 * We do this in steps in order to fit inside a 32 bit int.
676 * The first step is the minimum timeout, which will have a
677 * minimum resolution of 6 bits:
678 * (1) 2^13*1000 > 2^22,
679 * (2) host->timeout_clk < 2^16
680 * =>
681 * (1) / (2) > 2^6
682 */
683 count = 0;
684 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
685 while (current_timeout < target_timeout) {
686 count++;
687 current_timeout <<= 1;
688 if (count >= 0xF)
689 break;
690 }
691
692 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400693 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
694 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700695 count = 0xE;
696 }
697
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200698 return count;
699}
700
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300701static void sdhci_set_transfer_irqs(struct sdhci_host *host)
702{
703 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
704 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
705
706 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100707 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300708 else
Russell Kingb537f942014-04-25 12:56:01 +0100709 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
710
711 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
712 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300713}
714
Aisheng Dongb45e6682014-08-27 15:26:29 +0800715static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200716{
717 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800718
719 if (host->ops->set_timeout) {
720 host->ops->set_timeout(host, cmd);
721 } else {
722 count = sdhci_calc_timeout(host, cmd);
723 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
724 }
725}
726
727static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
728{
Pierre Ossman2134a922008-06-28 18:28:51 +0200729 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500730 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200731 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200732
733 WARN_ON(host->data);
734
Aisheng Dongb45e6682014-08-27 15:26:29 +0800735 if (data || (cmd->flags & MMC_RSP_BUSY))
736 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500737
738 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200739 return;
740
741 /* Sanity checks */
742 BUG_ON(data->blksz * data->blocks > 524288);
743 BUG_ON(data->blksz > host->mmc->max_blk_size);
744 BUG_ON(data->blocks > 65535);
745
746 host->data = data;
747 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400748 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200749
Richard Röjforsa13abc72009-09-22 16:45:30 -0700750 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100751 host->flags |= SDHCI_REQ_USE_DMA;
752
Pierre Ossman2134a922008-06-28 18:28:51 +0200753 /*
754 * FIXME: This doesn't account for merging when mapping the
755 * scatterlist.
756 */
757 if (host->flags & SDHCI_REQ_USE_DMA) {
758 int broken, i;
759 struct scatterlist *sg;
760
761 broken = 0;
762 if (host->flags & SDHCI_USE_ADMA) {
763 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
764 broken = 1;
765 } else {
766 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
767 broken = 1;
768 }
769
770 if (unlikely(broken)) {
771 for_each_sg(data->sg, sg, data->sg_len, i) {
772 if (sg->length & 0x3) {
773 DBG("Reverting to PIO because of "
774 "transfer size (%d)\n",
775 sg->length);
776 host->flags &= ~SDHCI_REQ_USE_DMA;
777 break;
778 }
779 }
780 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100781 }
782
783 /*
784 * The assumption here being that alignment is the same after
785 * translation to device address space.
786 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200787 if (host->flags & SDHCI_REQ_USE_DMA) {
788 int broken, i;
789 struct scatterlist *sg;
790
791 broken = 0;
792 if (host->flags & SDHCI_USE_ADMA) {
793 /*
794 * As we use 3 byte chunks to work around
795 * alignment problems, we need to check this
796 * quirk.
797 */
798 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
799 broken = 1;
800 } else {
801 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
802 broken = 1;
803 }
804
805 if (unlikely(broken)) {
806 for_each_sg(data->sg, sg, data->sg_len, i) {
807 if (sg->offset & 0x3) {
808 DBG("Reverting to PIO because of "
809 "bad alignment\n");
810 host->flags &= ~SDHCI_REQ_USE_DMA;
811 break;
812 }
813 }
814 }
815 }
816
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200817 if (host->flags & SDHCI_REQ_USE_DMA) {
818 if (host->flags & SDHCI_USE_ADMA) {
819 ret = sdhci_adma_table_pre(host, data);
820 if (ret) {
821 /*
822 * This only happens when someone fed
823 * us an invalid request.
824 */
825 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200826 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200827 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300828 sdhci_writel(host, host->adma_addr,
829 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200830 }
831 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300832 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200833
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300834 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200835 data->sg, data->sg_len,
836 (data->flags & MMC_DATA_READ) ?
837 DMA_FROM_DEVICE :
838 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300839 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200840 /*
841 * This only happens when someone fed
842 * us an invalid request.
843 */
844 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200845 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200846 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200847 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300848 sdhci_writel(host, sg_dma_address(data->sg),
849 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200850 }
851 }
852 }
853
Pierre Ossman2134a922008-06-28 18:28:51 +0200854 /*
855 * Always adjust the DMA selection as some controllers
856 * (e.g. JMicron) can't do PIO properly when the selection
857 * is ADMA.
858 */
859 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300860 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200861 ctrl &= ~SDHCI_CTRL_DMA_MASK;
862 if ((host->flags & SDHCI_REQ_USE_DMA) &&
863 (host->flags & SDHCI_USE_ADMA))
864 ctrl |= SDHCI_CTRL_ADMA32;
865 else
866 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300867 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100868 }
869
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200870 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200871 int flags;
872
873 flags = SG_MITER_ATOMIC;
874 if (host->data->flags & MMC_DATA_READ)
875 flags |= SG_MITER_TO_SG;
876 else
877 flags |= SG_MITER_FROM_SG;
878 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200879 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800880 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700881
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300882 sdhci_set_transfer_irqs(host);
883
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400884 /* Set the DMA boundary value and block size */
885 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
886 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300887 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700888}
889
890static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500891 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700892{
893 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500894 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700895
Dong Aisheng2b558c12013-10-30 22:09:48 +0800896 if (data == NULL) {
897 /* clear Auto CMD settings for no data CMDs */
898 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
899 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
900 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700901 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800902 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700903
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200904 WARN_ON(!host->data);
905
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700906 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500907 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
908 mode |= SDHCI_TRNS_MULTI;
909 /*
910 * If we are sending CMD23, CMD12 never gets sent
911 * on successful completion (so no Auto-CMD12).
912 */
913 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
914 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500915 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
916 mode |= SDHCI_TRNS_AUTO_CMD23;
917 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
918 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700919 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500920
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700921 if (data->flags & MMC_DATA_READ)
922 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100923 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700924 mode |= SDHCI_TRNS_DMA;
925
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300926 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800927}
928
929static void sdhci_finish_data(struct sdhci_host *host)
930{
931 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800932
933 BUG_ON(!host->data);
934
935 data = host->data;
936 host->data = NULL;
937
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100938 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200939 if (host->flags & SDHCI_USE_ADMA)
940 sdhci_adma_table_post(host, data);
941 else {
942 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
943 data->sg_len, (data->flags & MMC_DATA_READ) ?
944 DMA_FROM_DEVICE : DMA_TO_DEVICE);
945 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800946 }
947
948 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200949 * The specification states that the block count register must
950 * be updated, but it does not specify at what point in the
951 * data flow. That makes the register entirely useless to read
952 * back so we have to assume that nothing made it to the card
953 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800954 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200955 if (data->error)
956 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800957 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200958 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800959
Andrei Warkentine89d4562011-05-23 15:06:37 -0500960 /*
961 * Need to send CMD12 if -
962 * a) open-ended multiblock transfer (no CMD23)
963 * b) error in multiblock transfer
964 */
965 if (data->stop &&
966 (data->error ||
967 !host->mrq->sbc)) {
968
Pierre Ossmand129bce2006-03-24 03:18:17 -0800969 /*
970 * The controller needs a reset of internal state machines
971 * upon error conditions.
972 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200973 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100974 sdhci_do_reset(host, SDHCI_RESET_CMD);
975 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800976 }
977
978 sdhci_send_command(host, data->stop);
979 } else
980 tasklet_schedule(&host->finish_tasklet);
981}
982
Dong Aishengc0e551292013-09-13 19:11:31 +0800983void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800984{
985 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700986 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700987 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800988
989 WARN_ON(host->cmd);
990
Pierre Ossmand129bce2006-03-24 03:18:17 -0800991 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700992 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700993
994 mask = SDHCI_CMD_INHIBIT;
995 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
996 mask |= SDHCI_DATA_INHIBIT;
997
998 /* We shouldn't wait for data inihibit for stop commands, even
999 though they might use busy signaling */
1000 if (host->mrq->data && (cmd == host->mrq->data->stop))
1001 mask &= ~SDHCI_DATA_INHIBIT;
1002
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001003 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001004 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301005 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001006 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001007 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001008 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009 tasklet_schedule(&host->finish_tasklet);
1010 return;
1011 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001012 timeout--;
1013 mdelay(1);
1014 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001015
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001016 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001017 if (!cmd->data && cmd->busy_timeout > 9000)
1018 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001019 else
1020 timeout += 10 * HZ;
1021 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001022
1023 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001024 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001025
Andrei Warkentina3c77782011-04-11 16:13:42 -05001026 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001027
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001028 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001029
Andrei Warkentine89d4562011-05-23 15:06:37 -05001030 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001031
Pierre Ossmand129bce2006-03-24 03:18:17 -08001032 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301033 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001034 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001035 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001036 tasklet_schedule(&host->finish_tasklet);
1037 return;
1038 }
1039
1040 if (!(cmd->flags & MMC_RSP_PRESENT))
1041 flags = SDHCI_CMD_RESP_NONE;
1042 else if (cmd->flags & MMC_RSP_136)
1043 flags = SDHCI_CMD_RESP_LONG;
1044 else if (cmd->flags & MMC_RSP_BUSY)
1045 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1046 else
1047 flags = SDHCI_CMD_RESP_SHORT;
1048
1049 if (cmd->flags & MMC_RSP_CRC)
1050 flags |= SDHCI_CMD_CRC;
1051 if (cmd->flags & MMC_RSP_OPCODE)
1052 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301053
1054 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301055 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1056 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057 flags |= SDHCI_CMD_DATA;
1058
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001059 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001060}
Dong Aishengc0e551292013-09-13 19:11:31 +08001061EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001062
1063static void sdhci_finish_command(struct sdhci_host *host)
1064{
1065 int i;
1066
1067 BUG_ON(host->cmd == NULL);
1068
1069 if (host->cmd->flags & MMC_RSP_PRESENT) {
1070 if (host->cmd->flags & MMC_RSP_136) {
1071 /* CRC is stripped so we need to do some shifting. */
1072 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001073 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001074 SDHCI_RESPONSE + (3-i)*4) << 8;
1075 if (i != 3)
1076 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001077 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001078 SDHCI_RESPONSE + (3-i)*4-1);
1079 }
1080 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001081 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001082 }
1083 }
1084
Pierre Ossman17b04292007-07-22 22:18:46 +02001085 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001086
Andrei Warkentine89d4562011-05-23 15:06:37 -05001087 /* Finished CMD23, now send actual command. */
1088 if (host->cmd == host->mrq->sbc) {
1089 host->cmd = NULL;
1090 sdhci_send_command(host, host->mrq->cmd);
1091 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001092
Andrei Warkentine89d4562011-05-23 15:06:37 -05001093 /* Processed actual command. */
1094 if (host->data && host->data_early)
1095 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001096
Andrei Warkentine89d4562011-05-23 15:06:37 -05001097 if (!host->cmd->data)
1098 tasklet_schedule(&host->finish_tasklet);
1099
1100 host->cmd = NULL;
1101 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001102}
1103
Kevin Liu52983382013-01-31 11:31:37 +08001104static u16 sdhci_get_preset_value(struct sdhci_host *host)
1105{
Russell Kingd975f122014-04-25 12:59:31 +01001106 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001107
Russell Kingd975f122014-04-25 12:59:31 +01001108 switch (host->timing) {
1109 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001110 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1111 break;
Russell Kingd975f122014-04-25 12:59:31 +01001112 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001113 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1114 break;
Russell Kingd975f122014-04-25 12:59:31 +01001115 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001116 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1117 break;
Russell Kingd975f122014-04-25 12:59:31 +01001118 case MMC_TIMING_UHS_SDR104:
1119 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001120 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1121 break;
Russell Kingd975f122014-04-25 12:59:31 +01001122 case MMC_TIMING_UHS_DDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001123 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1124 break;
1125 default:
1126 pr_warn("%s: Invalid UHS-I mode selected\n",
1127 mmc_hostname(host->mmc));
1128 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1129 break;
1130 }
1131 return preset;
1132}
1133
Russell King17710592014-04-25 12:58:55 +01001134void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001135{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301136 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001137 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301138 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001139 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140
Russell King1650d0c2014-04-25 12:58:50 +01001141 host->mmc->actual_clock = 0;
1142
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001143 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001144
1145 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001146 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001147
Zhangfei Gao85105c52010-08-06 07:10:01 +08001148 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001149 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001150 u16 pre_val;
1151
1152 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1153 pre_val = sdhci_get_preset_value(host);
1154 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1155 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1156 if (host->clk_mul &&
1157 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1158 clk = SDHCI_PROG_CLOCK_MODE;
1159 real_div = div + 1;
1160 clk_mul = host->clk_mul;
1161 } else {
1162 real_div = max_t(int, 1, div << 1);
1163 }
1164 goto clock_set;
1165 }
1166
Arindam Nathc3ed3872011-05-05 12:19:06 +05301167 /*
1168 * Check if the Host Controller supports Programmable Clock
1169 * Mode.
1170 */
1171 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001172 for (div = 1; div <= 1024; div++) {
1173 if ((host->max_clk * host->clk_mul / div)
1174 <= clock)
1175 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001176 }
Kevin Liu52983382013-01-31 11:31:37 +08001177 /*
1178 * Set Programmable Clock Mode in the Clock
1179 * Control register.
1180 */
1181 clk = SDHCI_PROG_CLOCK_MODE;
1182 real_div = div;
1183 clk_mul = host->clk_mul;
1184 div--;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301185 } else {
1186 /* Version 3.00 divisors must be a multiple of 2. */
1187 if (host->max_clk <= clock)
1188 div = 1;
1189 else {
1190 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1191 div += 2) {
1192 if ((host->max_clk / div) <= clock)
1193 break;
1194 }
1195 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001196 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301197 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001198 }
1199 } else {
1200 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001201 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001202 if ((host->max_clk / div) <= clock)
1203 break;
1204 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001205 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301206 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001207 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001208
Kevin Liu52983382013-01-31 11:31:37 +08001209clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001210 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001211 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301212 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001213 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1214 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001215 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001216 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001217
Chris Ball27f6cb12009-09-22 16:45:31 -07001218 /* Wait max 20 ms */
1219 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001220 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001221 & SDHCI_CLOCK_INT_STABLE)) {
1222 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301223 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001224 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001225 sdhci_dumpregs(host);
1226 return;
1227 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001228 timeout--;
1229 mdelay(1);
1230 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001231
1232 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001233 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001234}
Russell King17710592014-04-25 12:58:55 +01001235EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001236
Russell King24fbb3c2014-04-25 13:00:06 +01001237static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1238 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001239{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001240 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001241 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001242
Tim Kryger52221612014-06-25 00:25:34 -07001243 if (!IS_ERR(mmc->supply.vmmc)) {
1244 spin_unlock_irq(&host->lock);
Markus Mayer4e743f12014-07-03 13:27:42 -07001245 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Tim Kryger52221612014-06-25 00:25:34 -07001246 spin_lock_irq(&host->lock);
1247 return;
1248 }
1249
Russell King24fbb3c2014-04-25 13:00:06 +01001250 if (mode != MMC_POWER_OFF) {
1251 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001252 case MMC_VDD_165_195:
1253 pwr = SDHCI_POWER_180;
1254 break;
1255 case MMC_VDD_29_30:
1256 case MMC_VDD_30_31:
1257 pwr = SDHCI_POWER_300;
1258 break;
1259 case MMC_VDD_32_33:
1260 case MMC_VDD_33_34:
1261 pwr = SDHCI_POWER_330;
1262 break;
1263 default:
1264 BUG();
1265 }
1266 }
1267
1268 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001269 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001270
Pierre Ossmanae628902009-05-03 20:45:03 +02001271 host->pwr = pwr;
1272
1273 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001274 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001275 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1276 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001277 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001278 } else {
1279 /*
1280 * Spec says that we should clear the power reg before setting
1281 * a new value. Some controllers don't seem to like this though.
1282 */
1283 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1284 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001285
Russell Kinge921a8b2014-04-25 13:00:01 +01001286 /*
1287 * At least the Marvell CaFe chip gets confused if we set the
1288 * voltage and set turn on power at the same time, so set the
1289 * voltage first.
1290 */
1291 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1292 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001293
Russell Kinge921a8b2014-04-25 13:00:01 +01001294 pwr |= SDHCI_POWER_ON;
1295
Pierre Ossmanae628902009-05-03 20:45:03 +02001296 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1297
Russell Kinge921a8b2014-04-25 13:00:01 +01001298 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1299 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001300
Russell Kinge921a8b2014-04-25 13:00:01 +01001301 /*
1302 * Some controllers need an extra 10ms delay of 10ms before
1303 * they can apply clock after applying power
1304 */
1305 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1306 mdelay(10);
1307 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001308}
1309
Pierre Ossmand129bce2006-03-24 03:18:17 -08001310/*****************************************************************************\
1311 * *
1312 * MMC callbacks *
1313 * *
1314\*****************************************************************************/
1315
1316static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1317{
1318 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001319 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001320 unsigned long flags;
Aaron Lu473b0952012-07-03 17:27:49 +08001321 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001322
1323 host = mmc_priv(mmc);
1324
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001325 sdhci_runtime_pm_get(host);
1326
Pierre Ossmand129bce2006-03-24 03:18:17 -08001327 spin_lock_irqsave(&host->lock, flags);
1328
1329 WARN_ON(host->mrq != NULL);
1330
Pierre Ossmanf9134312008-12-21 17:01:48 +01001331#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001332 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001333#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001334
1335 /*
1336 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1337 * requests if Auto-CMD12 is enabled.
1338 */
1339 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001340 if (mrq->stop) {
1341 mrq->data->stop = NULL;
1342 mrq->stop = NULL;
1343 }
1344 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001345
1346 host->mrq = mrq;
1347
Shawn Guo505a8682012-12-11 15:23:42 +08001348 /*
1349 * Firstly check card presence from cd-gpio. The return could
1350 * be one of the following possibilities:
1351 * negative: cd-gpio is not available
1352 * zero: cd-gpio is used, and card is removed
1353 * one: cd-gpio is used, and card is present
1354 */
1355 present = mmc_gpio_get_cd(host->mmc);
1356 if (present < 0) {
1357 /* If polling, assume that the card is always present. */
1358 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1359 present = 1;
1360 else
1361 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1362 SDHCI_CARD_PRESENT;
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +08001363 }
1364
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001365 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001366 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001367 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301368 } else {
1369 u32 present_state;
1370
1371 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1372 /*
1373 * Check if the re-tuning timer has already expired and there
Yi Sun7756a96d2014-09-09 02:13:59 +00001374 * is no on-going data transfer and DAT0 is not busy. If so,
1375 * we need to execute tuning procedure before sending command.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301376 */
1377 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
Yi Sun7756a96d2014-09-09 02:13:59 +00001378 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ)) &&
1379 (present_state & SDHCI_DATA_0_LVL_MASK)) {
Chris Ball14efd952012-11-05 14:29:49 -05001380 if (mmc->card) {
1381 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1382 tuning_opcode =
1383 mmc->card->type == MMC_TYPE_MMC ?
1384 MMC_SEND_TUNING_BLOCK_HS200 :
1385 MMC_SEND_TUNING_BLOCK;
Chuansheng Liu63c21182013-11-05 14:52:45 +08001386
1387 /* Here we need to set the host->mrq to NULL,
1388 * in case the pending finish_tasklet
1389 * finishes it incorrectly.
1390 */
1391 host->mrq = NULL;
1392
Chris Ball14efd952012-11-05 14:29:49 -05001393 spin_unlock_irqrestore(&host->lock, flags);
1394 sdhci_execute_tuning(mmc, tuning_opcode);
1395 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301396
Chris Ball14efd952012-11-05 14:29:49 -05001397 /* Restore original mmc_request structure */
1398 host->mrq = mrq;
1399 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301400 }
1401
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001402 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001403 sdhci_send_command(host, mrq->sbc);
1404 else
1405 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301406 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001407
Pierre Ossman5f25a662006-10-04 02:15:39 -07001408 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001409 spin_unlock_irqrestore(&host->lock, flags);
1410}
1411
Russell King2317f562014-04-25 12:57:07 +01001412void sdhci_set_bus_width(struct sdhci_host *host, int width)
1413{
1414 u8 ctrl;
1415
1416 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1417 if (width == MMC_BUS_WIDTH_8) {
1418 ctrl &= ~SDHCI_CTRL_4BITBUS;
1419 if (host->version >= SDHCI_SPEC_300)
1420 ctrl |= SDHCI_CTRL_8BITBUS;
1421 } else {
1422 if (host->version >= SDHCI_SPEC_300)
1423 ctrl &= ~SDHCI_CTRL_8BITBUS;
1424 if (width == MMC_BUS_WIDTH_4)
1425 ctrl |= SDHCI_CTRL_4BITBUS;
1426 else
1427 ctrl &= ~SDHCI_CTRL_4BITBUS;
1428 }
1429 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1430}
1431EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1432
Russell King96d7b782014-04-25 12:59:26 +01001433void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1434{
1435 u16 ctrl_2;
1436
1437 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1438 /* Select Bus Speed Mode for host */
1439 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1440 if ((timing == MMC_TIMING_MMC_HS200) ||
1441 (timing == MMC_TIMING_UHS_SDR104))
1442 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1443 else if (timing == MMC_TIMING_UHS_SDR12)
1444 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1445 else if (timing == MMC_TIMING_UHS_SDR25)
1446 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1447 else if (timing == MMC_TIMING_UHS_SDR50)
1448 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1449 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1450 (timing == MMC_TIMING_MMC_DDR52))
1451 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1452 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1453}
1454EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1455
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001456static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001457{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001458 unsigned long flags;
1459 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001460 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001461
Pierre Ossmand129bce2006-03-24 03:18:17 -08001462 spin_lock_irqsave(&host->lock, flags);
1463
Adrian Hunterceb61432011-12-27 15:48:41 +02001464 if (host->flags & SDHCI_DEVICE_DEAD) {
1465 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001466 if (!IS_ERR(mmc->supply.vmmc) &&
1467 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001468 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001469 return;
1470 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001471
Pierre Ossmand129bce2006-03-24 03:18:17 -08001472 /*
1473 * Reset the chip on each power off.
1474 * Should clear out any weird states.
1475 */
1476 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001477 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001478 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001479 }
1480
Kevin Liu52983382013-01-31 11:31:37 +08001481 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001482 (ios->power_mode == MMC_POWER_UP) &&
1483 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001484 sdhci_enable_preset_value(host, false);
1485
Russell King373073e2014-04-25 12:58:45 +01001486 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001487 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001488 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001489
1490 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1491 host->clock) {
1492 host->timeout_clk = host->mmc->actual_clock ?
1493 host->mmc->actual_clock / 1000 :
1494 host->clock / 1000;
1495 host->mmc->max_busy_timeout =
1496 host->ops->get_max_timeout_count ?
1497 host->ops->get_max_timeout_count(host) :
1498 1 << 27;
1499 host->mmc->max_busy_timeout /= host->timeout_clk;
1500 }
Russell King373073e2014-04-25 12:58:45 +01001501 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001502
Russell King24fbb3c2014-04-25 13:00:06 +01001503 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001504
Philip Rakity643a81f2010-09-23 08:24:32 -07001505 if (host->ops->platform_send_init_74_clocks)
1506 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1507
Russell King2317f562014-04-25 12:57:07 +01001508 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001509
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001510 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001511
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001512 if ((ios->timing == MMC_TIMING_SD_HS ||
1513 ios->timing == MMC_TIMING_MMC_HS)
1514 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001515 ctrl |= SDHCI_CTRL_HISPD;
1516 else
1517 ctrl &= ~SDHCI_CTRL_HISPD;
1518
Arindam Nathd6d50a12011-05-05 12:18:59 +05301519 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301520 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301521
1522 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301523 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001524 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301525 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301526 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1527 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001528 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301529 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301530
Russell Kingda91a8f2014-04-25 13:00:12 +01001531 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301532 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301533 /*
1534 * We only need to set Driver Strength if the
1535 * preset value enable is not set.
1536 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001537 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301538 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1539 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1540 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1541 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1542 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1543
1544 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301545 } else {
1546 /*
1547 * According to SDHC Spec v3.00, if the Preset Value
1548 * Enable in the Host Control 2 register is set, we
1549 * need to reset SD Clock Enable before changing High
1550 * Speed Enable to avoid generating clock gliches.
1551 */
Arindam Nath758535c2011-05-05 12:19:00 +05301552
1553 /* Reset SD Clock Enable */
1554 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1555 clk &= ~SDHCI_CLOCK_CARD_EN;
1556 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1557
1558 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1559
1560 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001561 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301562 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301563
Arindam Nath49c468f2011-05-05 12:19:01 +05301564 /* Reset SD Clock Enable */
1565 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1566 clk &= ~SDHCI_CLOCK_CARD_EN;
1567 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1568
Russell King96d7b782014-04-25 12:59:26 +01001569 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001570 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301571
Kevin Liu52983382013-01-31 11:31:37 +08001572 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1573 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1574 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1575 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1576 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1577 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1578 u16 preset;
1579
1580 sdhci_enable_preset_value(host, true);
1581 preset = sdhci_get_preset_value(host);
1582 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1583 >> SDHCI_PRESET_DRV_SHIFT;
1584 }
1585
Arindam Nath49c468f2011-05-05 12:19:01 +05301586 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001587 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301588 } else
1589 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301590
Leandro Dorileob8352262007-07-25 23:47:04 +02001591 /*
1592 * Some (ENE) controllers go apeshit on some ios operation,
1593 * signalling timeout and CRC errors even on CMD0. Resetting
1594 * it on each ios seems to solve the problem.
1595 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001596 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001597 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001598
Pierre Ossman5f25a662006-10-04 02:15:39 -07001599 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001600 spin_unlock_irqrestore(&host->lock, flags);
1601}
1602
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001603static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1604{
1605 struct sdhci_host *host = mmc_priv(mmc);
1606
1607 sdhci_runtime_pm_get(host);
1608 sdhci_do_set_ios(host, ios);
1609 sdhci_runtime_pm_put(host);
1610}
1611
Kevin Liu94144a42013-02-28 17:35:53 +08001612static int sdhci_do_get_cd(struct sdhci_host *host)
1613{
1614 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1615
1616 if (host->flags & SDHCI_DEVICE_DEAD)
1617 return 0;
1618
1619 /* If polling/nonremovable, assume that the card is always present. */
1620 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1621 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1622 return 1;
1623
1624 /* Try slot gpio detect */
1625 if (!IS_ERR_VALUE(gpio_cd))
1626 return !!gpio_cd;
1627
1628 /* Host native card detect */
1629 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1630}
1631
1632static int sdhci_get_cd(struct mmc_host *mmc)
1633{
1634 struct sdhci_host *host = mmc_priv(mmc);
1635 int ret;
1636
1637 sdhci_runtime_pm_get(host);
1638 ret = sdhci_do_get_cd(host);
1639 sdhci_runtime_pm_put(host);
1640 return ret;
1641}
1642
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001643static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001644{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001645 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001646 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001647
Pierre Ossmand129bce2006-03-24 03:18:17 -08001648 spin_lock_irqsave(&host->lock, flags);
1649
Pierre Ossman1e728592008-04-16 19:13:13 +02001650 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001651 is_readonly = 0;
1652 else if (host->ops->get_ro)
1653 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001654 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001655 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1656 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001657
1658 spin_unlock_irqrestore(&host->lock, flags);
1659
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001660 /* This quirk needs to be replaced by a callback-function later */
1661 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1662 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001663}
1664
Takashi Iwai82b0e232011-04-21 20:26:38 +02001665#define SAMPLE_COUNT 5
1666
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001667static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001668{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001669 int i, ro_count;
1670
Takashi Iwai82b0e232011-04-21 20:26:38 +02001671 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001672 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001673
1674 ro_count = 0;
1675 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001676 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001677 if (++ro_count > SAMPLE_COUNT / 2)
1678 return 1;
1679 }
1680 msleep(30);
1681 }
1682 return 0;
1683}
1684
Adrian Hunter20758b62011-08-29 16:42:12 +03001685static void sdhci_hw_reset(struct mmc_host *mmc)
1686{
1687 struct sdhci_host *host = mmc_priv(mmc);
1688
1689 if (host->ops && host->ops->hw_reset)
1690 host->ops->hw_reset(host);
1691}
1692
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001693static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001694{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001695 struct sdhci_host *host = mmc_priv(mmc);
1696 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001697
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001698 sdhci_runtime_pm_get(host);
1699 ret = sdhci_do_get_ro(host);
1700 sdhci_runtime_pm_put(host);
1701 return ret;
1702}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001703
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001704static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1705{
Russell Kingbe138552014-04-25 12:55:56 +01001706 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001707 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001708 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001709 else
Russell Kingb537f942014-04-25 12:56:01 +01001710 host->ier &= ~SDHCI_INT_CARD_INT;
1711
1712 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1713 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001714 mmiowb();
1715 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001716}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001717
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001718static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1719{
1720 struct sdhci_host *host = mmc_priv(mmc);
1721 unsigned long flags;
1722
Russell Kingef104332014-04-25 12:55:41 +01001723 sdhci_runtime_pm_get(host);
1724
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001725 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001726 if (enable)
1727 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1728 else
1729 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1730
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001731 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001732 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001733
1734 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001735}
1736
Philip Rakity6231f3d2012-07-23 15:56:23 -07001737static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001738 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001739{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001740 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001741 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001742 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001743
1744 /*
1745 * Signal Voltage Switching is only applicable for Host Controllers
1746 * v3.00 and above.
1747 */
1748 if (host->version < SDHCI_SPEC_300)
1749 return 0;
1750
Philip Rakity6231f3d2012-07-23 15:56:23 -07001751 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001752
Fabio Estevam21f59982013-02-14 10:35:03 -02001753 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001754 case MMC_SIGNAL_VOLTAGE_330:
1755 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1756 ctrl &= ~SDHCI_CTRL_VDD_180;
1757 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1758
Tim Kryger3a48edc2014-06-13 10:13:56 -07001759 if (!IS_ERR(mmc->supply.vqmmc)) {
1760 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1761 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001762 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001763 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1764 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001765 return -EIO;
1766 }
1767 }
1768 /* Wait for 5ms */
1769 usleep_range(5000, 5500);
1770
1771 /* 3.3V regulator output should be stable within 5 ms */
1772 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1773 if (!(ctrl & SDHCI_CTRL_VDD_180))
1774 return 0;
1775
Joe Perches66061102014-09-12 14:56:56 -07001776 pr_warn("%s: 3.3V regulator output did not became stable\n",
1777 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001778
1779 return -EAGAIN;
1780 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001781 if (!IS_ERR(mmc->supply.vqmmc)) {
1782 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001783 1700000, 1950000);
1784 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001785 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1786 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001787 return -EIO;
1788 }
1789 }
1790
1791 /*
1792 * Enable 1.8V Signal Enable in the Host Control2
1793 * register
1794 */
1795 ctrl |= SDHCI_CTRL_VDD_180;
1796 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1797
Kevin Liu20b92a32012-12-17 19:29:26 +08001798 /* 1.8V regulator output should be stable within 5 ms */
1799 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1800 if (ctrl & SDHCI_CTRL_VDD_180)
1801 return 0;
1802
Joe Perches66061102014-09-12 14:56:56 -07001803 pr_warn("%s: 1.8V regulator output did not became stable\n",
1804 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001805
1806 return -EAGAIN;
1807 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001808 if (!IS_ERR(mmc->supply.vqmmc)) {
1809 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1810 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001811 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001812 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1813 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001814 return -EIO;
1815 }
1816 }
1817 return 0;
1818 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301819 /* No signal voltage switch required */
1820 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001821 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301822}
1823
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001824static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001825 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001826{
1827 struct sdhci_host *host = mmc_priv(mmc);
1828 int err;
1829
1830 if (host->version < SDHCI_SPEC_300)
1831 return 0;
1832 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001833 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001834 sdhci_runtime_pm_put(host);
1835 return err;
1836}
1837
Kevin Liu20b92a32012-12-17 19:29:26 +08001838static int sdhci_card_busy(struct mmc_host *mmc)
1839{
1840 struct sdhci_host *host = mmc_priv(mmc);
1841 u32 present_state;
1842
1843 sdhci_runtime_pm_get(host);
1844 /* Check whether DAT[3:0] is 0000 */
1845 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1846 sdhci_runtime_pm_put(host);
1847
1848 return !(present_state & SDHCI_DATA_LVL_MASK);
1849}
1850
Girish K S069c9f12012-01-06 09:56:39 +05301851static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301852{
Russell King4b6f37d2014-04-25 12:59:36 +01001853 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301854 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301855 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301856 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001857 unsigned long flags;
Arindam Nathb513ea22011-05-05 12:19:04 +05301858
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001859 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001860 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301861
Arindam Nathb513ea22011-05-05 12:19:04 +05301862 /*
Girish K S069c9f12012-01-06 09:56:39 +05301863 * The Host Controller needs tuning only in case of SDR104 mode
1864 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301865 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301866 * If the Host Controller supports the HS200 mode then the
1867 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301868 */
Russell King4b6f37d2014-04-25 12:59:36 +01001869 switch (host->timing) {
1870 case MMC_TIMING_MMC_HS200:
1871 case MMC_TIMING_UHS_SDR104:
1872 break;
Girish K S069c9f12012-01-06 09:56:39 +05301873
Russell King4b6f37d2014-04-25 12:59:36 +01001874 case MMC_TIMING_UHS_SDR50:
1875 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1876 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1877 break;
1878 /* FALLTHROUGH */
1879
1880 default:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001881 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001882 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301883 return 0;
1884 }
1885
Dong Aisheng45251812013-09-13 19:11:30 +08001886 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001887 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001888 err = host->ops->platform_execute_tuning(host, opcode);
1889 sdhci_runtime_pm_put(host);
1890 return err;
1891 }
1892
Russell King4b6f37d2014-04-25 12:59:36 +01001893 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1894 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Arindam Nathb513ea22011-05-05 12:19:04 +05301895 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1896
1897 /*
1898 * As per the Host Controller spec v3.00, tuning command
1899 * generates Buffer Read Ready interrupt, so enable that.
1900 *
1901 * Note: The spec clearly says that when tuning sequence
1902 * is being performed, the controller does not generate
1903 * interrupts other than Buffer Read Ready interrupt. But
1904 * to make sure we don't hit a controller bug, we _only_
1905 * enable Buffer Read Ready interrupt here.
1906 */
Russell Kingb537f942014-04-25 12:56:01 +01001907 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1908 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301909
1910 /*
1911 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1912 * of loops reaches 40 times or a timeout of 150ms occurs.
1913 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301914 do {
1915 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001916 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301917
Girish K S069c9f12012-01-06 09:56:39 +05301918 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301919 cmd.arg = 0;
1920 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1921 cmd.retries = 0;
1922 cmd.data = NULL;
1923 cmd.error = 0;
1924
Al Cooper7ce45e92014-05-09 11:34:07 -04001925 if (tuning_loop_counter-- == 0)
1926 break;
1927
Arindam Nathb513ea22011-05-05 12:19:04 +05301928 mrq.cmd = &cmd;
1929 host->mrq = &mrq;
1930
1931 /*
1932 * In response to CMD19, the card sends 64 bytes of tuning
1933 * block to the Host Controller. So we set the block size
1934 * to 64 here.
1935 */
Girish K S069c9f12012-01-06 09:56:39 +05301936 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1937 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1938 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1939 SDHCI_BLOCK_SIZE);
1940 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1941 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1942 SDHCI_BLOCK_SIZE);
1943 } else {
1944 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1945 SDHCI_BLOCK_SIZE);
1946 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301947
1948 /*
1949 * The tuning block is sent by the card to the host controller.
1950 * So we set the TRNS_READ bit in the Transfer Mode register.
1951 * This also takes care of setting DMA Enable and Multi Block
1952 * Select in the same register to 0.
1953 */
1954 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1955
1956 sdhci_send_command(host, &cmd);
1957
1958 host->cmd = NULL;
1959 host->mrq = NULL;
1960
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001961 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301962 /* Wait for Buffer Read Ready interrupt */
1963 wait_event_interruptible_timeout(host->buf_ready_int,
1964 (host->tuning_done == 1),
1965 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001966 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301967
1968 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301969 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301970 "Buffer Read Ready interrupt during tuning "
1971 "procedure, falling back to fixed sampling "
1972 "clock\n");
1973 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1974 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1975 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1976 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1977
1978 err = -EIO;
1979 goto out;
1980 }
1981
1982 host->tuning_done = 0;
1983
1984 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07001985
1986 /* eMMC spec does not require a delay between tuning cycles */
1987 if (opcode == MMC_SEND_TUNING_BLOCK)
1988 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05301989 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1990
1991 /*
1992 * The Host Driver has exhausted the maximum number of loops allowed,
1993 * so use fixed sampling frequency.
1994 */
Al Cooper7ce45e92014-05-09 11:34:07 -04001995 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05301996 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1997 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04001998 }
1999 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
2000 pr_info(DRIVER_NAME ": Tuning procedure"
2001 " failed, falling back to fixed sampling"
2002 " clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002003 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302004 }
2005
2006out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302007 /*
2008 * If this is the very first time we are here, we start the retuning
2009 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2010 * flag won't be set, we check this condition before actually starting
2011 * the timer.
2012 */
2013 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2014 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
Aaron Lu973905f2012-07-04 13:29:09 +08002015 host->flags |= SDHCI_USING_RETUNING_TIMER;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302016 mod_timer(&host->tuning_timer, jiffies +
2017 host->tuning_count * HZ);
2018 /* Tuning mode 1 limits the maximum data length to 4MB */
2019 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
Arend van Spriel2bc02482014-01-04 13:51:26 +01002020 } else if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302021 host->flags &= ~SDHCI_NEEDS_RETUNING;
2022 /* Reload the new initial value for timer */
Arend van Spriel2bc02482014-01-04 13:51:26 +01002023 mod_timer(&host->tuning_timer, jiffies +
2024 host->tuning_count * HZ);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302025 }
2026
2027 /*
2028 * In case tuning fails, host controllers which support re-tuning can
2029 * try tuning again at a later time, when the re-tuning timer expires.
2030 * So for these controllers, we return 0. Since there might be other
2031 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08002032 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2033 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302034 */
Aaron Lu973905f2012-07-04 13:29:09 +08002035 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302036 err = 0;
2037
Russell Kingb537f942014-04-25 12:56:01 +01002038 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2039 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002040 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002041 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302042
2043 return err;
2044}
2045
Kevin Liu52983382013-01-31 11:31:37 +08002046
2047static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302048{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302049 /* Host Controller v3.00 defines preset value registers */
2050 if (host->version < SDHCI_SPEC_300)
2051 return;
2052
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302053 /*
2054 * We only enable or disable Preset Value if they are not already
2055 * enabled or disabled respectively. Otherwise, we bail out.
2056 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002057 if (host->preset_enabled != enable) {
2058 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2059
2060 if (enable)
2061 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2062 else
2063 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2064
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302065 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002066
2067 if (enable)
2068 host->flags |= SDHCI_PV_ENABLED;
2069 else
2070 host->flags &= ~SDHCI_PV_ENABLED;
2071
2072 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302073 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002074}
2075
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002076static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002077{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002078 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002079 unsigned long flags;
2080
Christian Daudt722e1282013-06-20 14:26:36 -07002081 /* First check if client has provided their own card event */
2082 if (host->ops->card_event)
2083 host->ops->card_event(host);
2084
Pierre Ossmand129bce2006-03-24 03:18:17 -08002085 spin_lock_irqsave(&host->lock, flags);
2086
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002087 /* Check host->mrq first in case we are runtime suspended */
Shawn Guo9668d762013-06-09 19:49:24 +08002088 if (host->mrq && !sdhci_do_get_cd(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302089 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002090 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302091 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002092 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002093
Russell King03231f92014-04-25 12:57:12 +01002094 sdhci_do_reset(host, SDHCI_RESET_CMD);
2095 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002096
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002097 host->mrq->cmd->error = -ENOMEDIUM;
2098 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002099 }
2100
2101 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002102}
2103
2104static const struct mmc_host_ops sdhci_ops = {
2105 .request = sdhci_request,
2106 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002107 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002108 .get_ro = sdhci_get_ro,
2109 .hw_reset = sdhci_hw_reset,
2110 .enable_sdio_irq = sdhci_enable_sdio_irq,
2111 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2112 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002113 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002114 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002115};
2116
2117/*****************************************************************************\
2118 * *
2119 * Tasklets *
2120 * *
2121\*****************************************************************************/
2122
Pierre Ossmand129bce2006-03-24 03:18:17 -08002123static void sdhci_tasklet_finish(unsigned long param)
2124{
2125 struct sdhci_host *host;
2126 unsigned long flags;
2127 struct mmc_request *mrq;
2128
2129 host = (struct sdhci_host*)param;
2130
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002131 spin_lock_irqsave(&host->lock, flags);
2132
Chris Ball0c9c99a2011-04-27 17:35:31 -04002133 /*
2134 * If this tasklet gets rescheduled while running, it will
2135 * be run again afterwards but without any active request.
2136 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002137 if (!host->mrq) {
2138 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002139 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002140 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002141
2142 del_timer(&host->timer);
2143
2144 mrq = host->mrq;
2145
Pierre Ossmand129bce2006-03-24 03:18:17 -08002146 /*
2147 * The controller needs a reset of internal state machines
2148 * upon error conditions.
2149 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002150 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002151 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002152 (mrq->sbc && mrq->sbc->error) ||
2153 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2154 (mrq->data->stop && mrq->data->stop->error))) ||
2155 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002156
2157 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002158 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002159 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002160 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002161
2162 /* Spec says we should do both at the same time, but Ricoh
2163 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002164 sdhci_do_reset(host, SDHCI_RESET_CMD);
2165 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002166 }
2167
2168 host->mrq = NULL;
2169 host->cmd = NULL;
2170 host->data = NULL;
2171
Pierre Ossmanf9134312008-12-21 17:01:48 +01002172#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002173 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002174#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002175
Pierre Ossman5f25a662006-10-04 02:15:39 -07002176 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002177 spin_unlock_irqrestore(&host->lock, flags);
2178
2179 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002180 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002181}
2182
2183static void sdhci_timeout_timer(unsigned long data)
2184{
2185 struct sdhci_host *host;
2186 unsigned long flags;
2187
2188 host = (struct sdhci_host*)data;
2189
2190 spin_lock_irqsave(&host->lock, flags);
2191
2192 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302193 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002194 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002195 sdhci_dumpregs(host);
2196
2197 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002198 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002199 sdhci_finish_data(host);
2200 } else {
2201 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002202 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002203 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002204 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002205
2206 tasklet_schedule(&host->finish_tasklet);
2207 }
2208 }
2209
Pierre Ossman5f25a662006-10-04 02:15:39 -07002210 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002211 spin_unlock_irqrestore(&host->lock, flags);
2212}
2213
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302214static void sdhci_tuning_timer(unsigned long data)
2215{
2216 struct sdhci_host *host;
2217 unsigned long flags;
2218
2219 host = (struct sdhci_host *)data;
2220
2221 spin_lock_irqsave(&host->lock, flags);
2222
2223 host->flags |= SDHCI_NEEDS_RETUNING;
2224
2225 spin_unlock_irqrestore(&host->lock, flags);
2226}
2227
Pierre Ossmand129bce2006-03-24 03:18:17 -08002228/*****************************************************************************\
2229 * *
2230 * Interrupt handling *
2231 * *
2232\*****************************************************************************/
2233
Adrian Hunter61541392014-09-24 10:27:27 +03002234static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002235{
2236 BUG_ON(intmask == 0);
2237
2238 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302239 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002240 "though no command operation was in progress.\n",
2241 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002242 sdhci_dumpregs(host);
2243 return;
2244 }
2245
Pierre Ossman43b58b32007-07-25 23:15:27 +02002246 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002247 host->cmd->error = -ETIMEDOUT;
2248 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2249 SDHCI_INT_INDEX))
2250 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002251
Pierre Ossmane8095172008-07-25 01:09:08 +02002252 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002253 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002254 return;
2255 }
2256
2257 /*
2258 * The host can send and interrupt when the busy state has
2259 * ended, allowing us to wait without wasting CPU cycles.
2260 * Unfortunately this is overloaded on the "data complete"
2261 * interrupt, so we need to take some care when handling
2262 * it.
2263 *
2264 * Note: The 1.0 specification is a bit ambiguous about this
2265 * feature so there might be some problems with older
2266 * controllers.
2267 */
2268 if (host->cmd->flags & MMC_RSP_BUSY) {
2269 if (host->cmd->data)
2270 DBG("Cannot wait for busy signal when also "
2271 "doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002272 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2273 && !host->busy_handle) {
2274 /* Mark that command complete before busy is ended */
2275 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002276 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002277 }
Ben Dooksf9454052009-02-20 20:33:08 +03002278
2279 /* The controller does not support the end-of-busy IRQ,
2280 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002281 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2282 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2283 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002284 }
2285
2286 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002287 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002288}
2289
George G. Davis0957c332010-02-18 12:32:12 -05002290#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002291static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002292{
2293 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002294 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002295
2296 sdhci_dumpregs(host);
2297
2298 while (true) {
Adrian Hunter05452302014-11-04 12:42:45 +02002299 struct sdhci_adma2_32_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002300
2301 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
Adrian Hunter05452302014-11-04 12:42:45 +02002302 name, desc, le32_to_cpu(dma_desc->addr),
2303 le16_to_cpu(dma_desc->len),
2304 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002305
Adrian Hunter76fe3792014-11-04 12:42:42 +02002306 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002307
Adrian Hunter05452302014-11-04 12:42:45 +02002308 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002309 break;
2310 }
2311}
2312#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002313static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002314#endif
2315
Pierre Ossmand129bce2006-03-24 03:18:17 -08002316static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2317{
Girish K S069c9f12012-01-06 09:56:39 +05302318 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002319 BUG_ON(intmask == 0);
2320
Arindam Nathb513ea22011-05-05 12:19:04 +05302321 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2322 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302323 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2324 if (command == MMC_SEND_TUNING_BLOCK ||
2325 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302326 host->tuning_done = 1;
2327 wake_up(&host->buf_ready_int);
2328 return;
2329 }
2330 }
2331
Pierre Ossmand129bce2006-03-24 03:18:17 -08002332 if (!host->data) {
2333 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002334 * The "data complete" interrupt is also used to
2335 * indicate that a busy state has ended. See comment
2336 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002337 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002338 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002339 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2340 host->cmd->error = -ETIMEDOUT;
2341 tasklet_schedule(&host->finish_tasklet);
2342 return;
2343 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002344 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002345 /*
2346 * Some cards handle busy-end interrupt
2347 * before the command completed, so make
2348 * sure we do things in the proper order.
2349 */
2350 if (host->busy_handle)
2351 sdhci_finish_command(host);
2352 else
2353 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002354 return;
2355 }
2356 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002357
Girish K Sa3c76eb2011-10-11 11:44:09 +05302358 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002359 "though no data operation was in progress.\n",
2360 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002361 sdhci_dumpregs(host);
2362
2363 return;
2364 }
2365
2366 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002367 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002368 else if (intmask & SDHCI_INT_DATA_END_BIT)
2369 host->data->error = -EILSEQ;
2370 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2371 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2372 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002373 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002374 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302375 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002376 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002377 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002378 if (host->ops->adma_workaround)
2379 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002380 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002381
Pierre Ossman17b04292007-07-22 22:18:46 +02002382 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002383 sdhci_finish_data(host);
2384 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002385 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002386 sdhci_transfer_pio(host);
2387
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002388 /*
2389 * We currently don't do anything fancy with DMA
2390 * boundaries, but as we can't disable the feature
2391 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002392 *
2393 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2394 * should return a valid address to continue from, but as
2395 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002396 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002397 if (intmask & SDHCI_INT_DMA_END) {
2398 u32 dmastart, dmanow;
2399 dmastart = sg_dma_address(host->data->sg);
2400 dmanow = dmastart + host->data->bytes_xfered;
2401 /*
2402 * Force update to the next DMA block boundary.
2403 */
2404 dmanow = (dmanow &
2405 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2406 SDHCI_DEFAULT_BOUNDARY_SIZE;
2407 host->data->bytes_xfered = dmanow - dmastart;
2408 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2409 " next 0x%08x\n",
2410 mmc_hostname(host->mmc), dmastart,
2411 host->data->bytes_xfered, dmanow);
2412 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2413 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002414
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002415 if (intmask & SDHCI_INT_DATA_END) {
2416 if (host->cmd) {
2417 /*
2418 * Data managed to finish before the
2419 * command completed. Make sure we do
2420 * things in the proper order.
2421 */
2422 host->data_early = 1;
2423 } else {
2424 sdhci_finish_data(host);
2425 }
2426 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002427 }
2428}
2429
David Howells7d12e782006-10-05 14:55:46 +01002430static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002431{
Russell King781e9892014-04-25 12:55:46 +01002432 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002433 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002434 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002435 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002436
2437 spin_lock(&host->lock);
2438
Russell Kingbe138552014-04-25 12:55:56 +01002439 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002440 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002441 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002442 }
2443
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002444 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002445 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002446 result = IRQ_NONE;
2447 goto out;
2448 }
2449
Russell King41005002014-04-25 12:55:36 +01002450 do {
2451 /* Clear selected interrupts. */
2452 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2453 SDHCI_INT_BUS_POWER);
2454 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002455
Russell King41005002014-04-25 12:55:36 +01002456 DBG("*** %s got interrupt: 0x%08x\n",
2457 mmc_hostname(host->mmc), intmask);
2458
2459 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2460 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2461 SDHCI_CARD_PRESENT;
2462
2463 /*
2464 * There is a observation on i.mx esdhc. INSERT
2465 * bit will be immediately set again when it gets
2466 * cleared, if a card is inserted. We have to mask
2467 * the irq to prevent interrupt storm which will
2468 * freeze the system. And the REMOVE gets the
2469 * same situation.
2470 *
2471 * More testing are needed here to ensure it works
2472 * for other platforms though.
2473 */
Russell Kingb537f942014-04-25 12:56:01 +01002474 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2475 SDHCI_INT_CARD_REMOVE);
2476 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2477 SDHCI_INT_CARD_INSERT;
2478 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2479 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002480
2481 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2482 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002483
2484 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2485 SDHCI_INT_CARD_REMOVE);
2486 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002487 }
2488
2489 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002490 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2491 &intmask);
Russell King41005002014-04-25 12:55:36 +01002492
2493 if (intmask & SDHCI_INT_DATA_MASK)
2494 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2495
2496 if (intmask & SDHCI_INT_BUS_POWER)
2497 pr_err("%s: Card is consuming too much power!\n",
2498 mmc_hostname(host->mmc));
2499
Russell King781e9892014-04-25 12:55:46 +01002500 if (intmask & SDHCI_INT_CARD_INT) {
2501 sdhci_enable_sdio_irq_nolock(host, false);
2502 host->thread_isr |= SDHCI_INT_CARD_INT;
2503 result = IRQ_WAKE_THREAD;
2504 }
Russell King41005002014-04-25 12:55:36 +01002505
2506 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2507 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2508 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2509 SDHCI_INT_CARD_INT);
2510
2511 if (intmask) {
2512 unexpected |= intmask;
2513 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2514 }
2515
Russell King781e9892014-04-25 12:55:46 +01002516 if (result == IRQ_NONE)
2517 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002518
2519 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002520 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002521out:
2522 spin_unlock(&host->lock);
2523
Alexander Stein6379b232012-03-14 09:52:10 +01002524 if (unexpected) {
2525 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2526 mmc_hostname(host->mmc), unexpected);
2527 sdhci_dumpregs(host);
2528 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002529
Pierre Ossmand129bce2006-03-24 03:18:17 -08002530 return result;
2531}
2532
Russell King781e9892014-04-25 12:55:46 +01002533static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2534{
2535 struct sdhci_host *host = dev_id;
2536 unsigned long flags;
2537 u32 isr;
2538
2539 spin_lock_irqsave(&host->lock, flags);
2540 isr = host->thread_isr;
2541 host->thread_isr = 0;
2542 spin_unlock_irqrestore(&host->lock, flags);
2543
Russell King3560db82014-04-25 12:55:51 +01002544 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2545 sdhci_card_event(host->mmc);
2546 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2547 }
2548
Russell King781e9892014-04-25 12:55:46 +01002549 if (isr & SDHCI_INT_CARD_INT) {
2550 sdio_run_irqs(host->mmc);
2551
2552 spin_lock_irqsave(&host->lock, flags);
2553 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2554 sdhci_enable_sdio_irq_nolock(host, true);
2555 spin_unlock_irqrestore(&host->lock, flags);
2556 }
2557
2558 return isr ? IRQ_HANDLED : IRQ_NONE;
2559}
2560
Pierre Ossmand129bce2006-03-24 03:18:17 -08002561/*****************************************************************************\
2562 * *
2563 * Suspend/resume *
2564 * *
2565\*****************************************************************************/
2566
2567#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002568void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2569{
2570 u8 val;
2571 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2572 | SDHCI_WAKE_ON_INT;
2573
2574 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2575 val |= mask ;
2576 /* Avoid fake wake up */
2577 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2578 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2579 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2580}
2581EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2582
Fabio Estevam0b10f472014-08-30 14:53:13 -03002583static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002584{
2585 u8 val;
2586 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2587 | SDHCI_WAKE_ON_INT;
2588
2589 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2590 val &= ~mask;
2591 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2592}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002593
Manuel Lauss29495aa2011-11-03 11:09:45 +01002594int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002595{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002596 sdhci_disable_card_detection(host);
2597
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302598 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002599 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002600 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302601 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302602 }
2603
Kevin Liuad080d72013-01-05 17:21:33 +08002604 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002605 host->ier = 0;
2606 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2607 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002608 free_irq(host->irq, host);
2609 } else {
2610 sdhci_enable_irq_wakeups(host);
2611 enable_irq_wake(host->irq);
2612 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002613 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002614}
2615
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002616EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002617
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002618int sdhci_resume_host(struct sdhci_host *host)
2619{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002620 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002621
Richard Röjforsa13abc72009-09-22 16:45:30 -07002622 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002623 if (host->ops->enable_dma)
2624 host->ops->enable_dma(host);
2625 }
2626
Kevin Liuad080d72013-01-05 17:21:33 +08002627 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell King781e9892014-04-25 12:55:46 +01002628 ret = request_threaded_irq(host->irq, sdhci_irq,
2629 sdhci_thread_irq, IRQF_SHARED,
2630 mmc_hostname(host->mmc), host);
Kevin Liuad080d72013-01-05 17:21:33 +08002631 if (ret)
2632 return ret;
2633 } else {
2634 sdhci_disable_irq_wakeups(host);
2635 disable_irq_wake(host->irq);
2636 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002637
Adrian Hunter6308d292012-02-07 14:48:54 +02002638 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2639 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2640 /* Card keeps power but host controller does not */
2641 sdhci_init(host, 0);
2642 host->pwr = 0;
2643 host->clock = 0;
2644 sdhci_do_set_ios(host, &host->mmc->ios);
2645 } else {
2646 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2647 mmiowb();
2648 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002649
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002650 sdhci_enable_card_detection(host);
2651
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302652 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002653 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302654 host->flags |= SDHCI_NEEDS_RETUNING;
2655
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002656 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002657}
2658
2659EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002660#endif /* CONFIG_PM */
2661
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002662#ifdef CONFIG_PM_RUNTIME
2663
2664static int sdhci_runtime_pm_get(struct sdhci_host *host)
2665{
2666 return pm_runtime_get_sync(host->mmc->parent);
2667}
2668
2669static int sdhci_runtime_pm_put(struct sdhci_host *host)
2670{
2671 pm_runtime_mark_last_busy(host->mmc->parent);
2672 return pm_runtime_put_autosuspend(host->mmc->parent);
2673}
2674
Adrian Hunterf0710a52013-05-06 12:17:32 +03002675static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2676{
2677 if (host->runtime_suspended || host->bus_on)
2678 return;
2679 host->bus_on = true;
2680 pm_runtime_get_noresume(host->mmc->parent);
2681}
2682
2683static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2684{
2685 if (host->runtime_suspended || !host->bus_on)
2686 return;
2687 host->bus_on = false;
2688 pm_runtime_put_noidle(host->mmc->parent);
2689}
2690
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002691int sdhci_runtime_suspend_host(struct sdhci_host *host)
2692{
2693 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002694
2695 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002696 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002697 del_timer_sync(&host->tuning_timer);
2698 host->flags &= ~SDHCI_NEEDS_RETUNING;
2699 }
2700
2701 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002702 host->ier &= SDHCI_INT_CARD_INT;
2703 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2704 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002705 spin_unlock_irqrestore(&host->lock, flags);
2706
Russell King781e9892014-04-25 12:55:46 +01002707 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002708
2709 spin_lock_irqsave(&host->lock, flags);
2710 host->runtime_suspended = true;
2711 spin_unlock_irqrestore(&host->lock, flags);
2712
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002713 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002714}
2715EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2716
2717int sdhci_runtime_resume_host(struct sdhci_host *host)
2718{
2719 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002720 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002721
2722 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2723 if (host->ops->enable_dma)
2724 host->ops->enable_dma(host);
2725 }
2726
2727 sdhci_init(host, 0);
2728
2729 /* Force clock and power re-program */
2730 host->pwr = 0;
2731 host->clock = 0;
2732 sdhci_do_set_ios(host, &host->mmc->ios);
2733
2734 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002735 if ((host_flags & SDHCI_PV_ENABLED) &&
2736 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2737 spin_lock_irqsave(&host->lock, flags);
2738 sdhci_enable_preset_value(host, true);
2739 spin_unlock_irqrestore(&host->lock, flags);
2740 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002741
2742 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002743 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002744 host->flags |= SDHCI_NEEDS_RETUNING;
2745
2746 spin_lock_irqsave(&host->lock, flags);
2747
2748 host->runtime_suspended = false;
2749
2750 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002751 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002752 sdhci_enable_sdio_irq_nolock(host, true);
2753
2754 /* Enable Card Detection */
2755 sdhci_enable_card_detection(host);
2756
2757 spin_unlock_irqrestore(&host->lock, flags);
2758
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002759 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002760}
2761EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2762
2763#endif
2764
Pierre Ossmand129bce2006-03-24 03:18:17 -08002765/*****************************************************************************\
2766 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002767 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002768 * *
2769\*****************************************************************************/
2770
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002771struct sdhci_host *sdhci_alloc_host(struct device *dev,
2772 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002773{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002774 struct mmc_host *mmc;
2775 struct sdhci_host *host;
2776
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002777 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002778
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002779 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002780 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002781 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002782
2783 host = mmc_priv(mmc);
2784 host->mmc = mmc;
2785
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002786 return host;
2787}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002788
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002789EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002790
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002791int sdhci_add_host(struct sdhci_host *host)
2792{
2793 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002794 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302795 u32 max_current_caps;
2796 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002797 unsigned int override_timeout_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002798 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002799
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002800 WARN_ON(host == NULL);
2801 if (host == NULL)
2802 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002803
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002804 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002805
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002806 if (debug_quirks)
2807 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002808 if (debug_quirks2)
2809 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002810
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002811 override_timeout_clk = host->timeout_clk;
2812
Russell King03231f92014-04-25 12:57:12 +01002813 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002814
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002815 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002816 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2817 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002818 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302819 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002820 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002821 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002822 }
2823
Arindam Nathf2119df2011-05-05 12:18:57 +05302824 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002825 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002826
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002827 if (host->version >= SDHCI_SPEC_300)
2828 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2829 host->caps1 :
2830 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302831
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002832 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002833 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302834 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002835 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002836 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002837 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002838
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002839 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002840 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002841 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002842 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002843 }
2844
Arindam Nathf2119df2011-05-05 12:18:57 +05302845 if ((host->version >= SDHCI_SPEC_200) &&
2846 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002847 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002848
2849 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2850 (host->flags & SDHCI_USE_ADMA)) {
2851 DBG("Disabling ADMA as it is marked broken\n");
2852 host->flags &= ~SDHCI_USE_ADMA;
2853 }
2854
Richard Röjforsa13abc72009-09-22 16:45:30 -07002855 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002856 if (host->ops->enable_dma) {
2857 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07002858 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002859 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002860 host->flags &=
2861 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002862 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002863 }
2864 }
2865
Pierre Ossman2134a922008-06-28 18:28:51 +02002866 if (host->flags & SDHCI_USE_ADMA) {
2867 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002868 * The DMA descriptor table size is calculated as the maximum
2869 * number of segments times 2, to allow for an alignment
2870 * descriptor for each segment, plus 1 for a nop end descriptor,
2871 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002872 */
Adrian Hunter739d46d2014-11-04 12:42:44 +02002873 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2874 SDHCI_ADMA2_32_DESC_SZ;
2875 host->align_buffer_sz = SDHCI_MAX_SEGS *
2876 SDHCI_ADMA2_32_ALIGN;
2877 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
2878 host->align_sz = SDHCI_ADMA2_32_ALIGN;
2879 host->align_mask = SDHCI_ADMA2_32_ALIGN - 1;
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002880 host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +02002881 host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002882 &host->adma_addr,
2883 GFP_KERNEL);
Adrian Hunter76fe3792014-11-04 12:42:42 +02002884 host->align_buffer = kmalloc(host->align_buffer_sz, GFP_KERNEL);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002885 if (!host->adma_table || !host->align_buffer) {
Adrian Hunter76fe3792014-11-04 12:42:42 +02002886 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002887 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02002888 kfree(host->align_buffer);
Joe Perches66061102014-09-12 14:56:56 -07002889 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002890 mmc_hostname(mmc));
2891 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002892 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01002893 host->align_buffer = NULL;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002894 } else if (host->adma_addr & host->align_mask) {
Joe Perches66061102014-09-12 14:56:56 -07002895 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2896 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002897 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002898 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002899 host->adma_table, host->adma_addr);
Russell Kingd1e49f72014-04-25 12:58:34 +01002900 kfree(host->align_buffer);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002901 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01002902 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02002903 }
2904 }
2905
Pierre Ossman76591502008-07-21 00:32:11 +02002906 /*
2907 * If we use DMA, then it's up to the caller to set the DMA
2908 * mask, but PIO does not need the hw shim so we set a new
2909 * mask here in that case.
2910 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002911 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002912 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07002913 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02002914 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002915
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002916 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302917 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002918 >> SDHCI_CLOCK_BASE_SHIFT;
2919 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302920 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002921 >> SDHCI_CLOCK_BASE_SHIFT;
2922
Pierre Ossmand129bce2006-03-24 03:18:17 -08002923 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002924 if (host->max_clk == 0 || host->quirks &
2925 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002926 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302927 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002928 "frequency.\n", mmc_hostname(mmc));
2929 return -ENODEV;
2930 }
2931 host->max_clk = host->ops->get_max_clock(host);
2932 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002933
2934 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302935 * In case of Host Controller v3.00, find out whether clock
2936 * multiplier is supported.
2937 */
2938 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2939 SDHCI_CLOCK_MUL_SHIFT;
2940
2941 /*
2942 * In case the value in Clock Multiplier is 0, then programmable
2943 * clock mode is not supported, otherwise the actual clock
2944 * multiplier is one more than the value of Clock Multiplier
2945 * in the Capabilities Register.
2946 */
2947 if (host->clk_mul)
2948 host->clk_mul += 1;
2949
2950 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002951 * Set host parameters.
2952 */
2953 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302954 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002955 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002956 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302957 else if (host->version >= SDHCI_SPEC_300) {
2958 if (host->clk_mul) {
2959 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2960 mmc->f_max = host->max_clk * host->clk_mul;
2961 } else
2962 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2963 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002964 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002965
Aisheng Dong28aab052014-08-27 15:26:31 +08002966 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2967 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
2968 SDHCI_TIMEOUT_CLK_SHIFT;
2969 if (host->timeout_clk == 0) {
2970 if (host->ops->get_timeout_clock) {
2971 host->timeout_clk =
2972 host->ops->get_timeout_clock(host);
2973 } else {
2974 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
2975 mmc_hostname(mmc));
2976 return -ENODEV;
2977 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03002978 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03002979
Aisheng Dong28aab052014-08-27 15:26:31 +08002980 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2981 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002982
Aisheng Dong28aab052014-08-27 15:26:31 +08002983 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08002984 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08002985 mmc->max_busy_timeout /= host->timeout_clk;
2986 }
Adrian Hunter58d12462011-06-28 17:16:03 +03002987
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002988 if (override_timeout_clk)
2989 host->timeout_clk = override_timeout_clk;
2990
Andrei Warkentine89d4562011-05-23 15:06:37 -05002991 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01002992 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05002993
2994 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2995 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002996
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002997 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002998 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002999 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003000 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003001 host->flags |= SDHCI_AUTO_CMD23;
3002 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3003 } else {
3004 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3005 }
3006
Philip Rakity15ec4462010-11-19 16:48:39 -05003007 /*
3008 * A controller may support 8-bit width, but the board itself
3009 * might not have the pins brought out. Boards that support
3010 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3011 * their platform code before calling sdhci_add_host(), and we
3012 * won't assume 8-bit width for hosts without that CAP.
3013 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003014 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003015 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003016
Jerry Huang63ef5d82012-10-25 13:47:19 +08003017 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3018 mmc->caps &= ~MMC_CAP_CMD23;
3019
Arindam Nathf2119df2011-05-05 12:18:57 +05303020 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003021 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003022
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003023 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Markus Mayer4e743f12014-07-03 13:27:42 -07003024 !(mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003025 mmc->caps |= MMC_CAP_NEEDS_POLL;
3026
Tim Kryger3a48edc2014-06-13 10:13:56 -07003027 /* If there are external regulators, get them */
3028 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3029 return -EPROBE_DEFER;
3030
Philip Rakity6231f3d2012-07-23 15:56:23 -07003031 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003032 if (!IS_ERR(mmc->supply.vqmmc)) {
3033 ret = regulator_enable(mmc->supply.vqmmc);
3034 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3035 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003036 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3037 SDHCI_SUPPORT_SDR50 |
3038 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003039 if (ret) {
3040 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3041 mmc_hostname(mmc), ret);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003042 mmc->supply.vqmmc = NULL;
Chris Balla3361ab2013-03-11 17:51:53 -04003043 }
Kevin Liu8363c372012-11-17 17:55:51 -05003044 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003045
Daniel Drake6a661802012-11-25 13:01:19 -05003046 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3047 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3048 SDHCI_SUPPORT_DDR50);
3049
Al Cooper4188bba2012-03-16 15:54:17 -04003050 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3051 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3052 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303053 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3054
3055 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003056 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303057 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003058 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3059 * field can be promoted to support HS200.
3060 */
Chuanxiao.Dongadc82852014-08-19 11:02:41 +08003061 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200)) {
David Cohen13868bf2013-10-29 10:58:26 -07003062 mmc->caps2 |= MMC_CAP2_HS200;
Chuanxiao.Dongadc82852014-08-19 11:02:41 +08003063 if (IS_ERR(mmc->supply.vqmmc) ||
3064 !regulator_is_supported_voltage
3065 (mmc->supply.vqmmc, 1100000, 1300000))
3066 mmc->caps2 &= ~MMC_CAP2_HS200_1_2V_SDR;
3067 }
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003068 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303069 mmc->caps |= MMC_CAP_UHS_SDR50;
3070
Micky Ching9107ebb2014-02-21 18:40:35 +08003071 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3072 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303073 mmc->caps |= MMC_CAP_UHS_DDR50;
3074
Girish K S069c9f12012-01-06 09:56:39 +05303075 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303076 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3077 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3078
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003079 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303080 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003081 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303082
Arindam Nathd6d50a12011-05-05 12:18:59 +05303083 /* Driver Type(s) (A, C, D) supported by the host */
3084 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3085 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3086 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3087 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3088 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3089 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3090
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303091 /* Initial value for re-tuning timer count */
3092 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3093 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3094
3095 /*
3096 * In case Re-tuning Timer is not disabled, the actual value of
3097 * re-tuning timer will be 2 ^ (n - 1).
3098 */
3099 if (host->tuning_count)
3100 host->tuning_count = 1 << (host->tuning_count - 1);
3101
3102 /* Re-tuning mode supported by the Host Controller */
3103 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3104 SDHCI_RETUNING_MODE_SHIFT;
3105
Takashi Iwai8f230f42010-12-08 10:04:30 +01003106 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003107
Arindam Nathf2119df2011-05-05 12:18:57 +05303108 /*
3109 * According to SD Host Controller spec v3.00, if the Host System
3110 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3111 * the value is meaningful only if Voltage Support in the Capabilities
3112 * register is set. The actual current value is 4 times the register
3113 * value.
3114 */
3115 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003116 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003117 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003118 if (curr > 0) {
3119
3120 /* convert to SDHCI_MAX_CURRENT format */
3121 curr = curr/1000; /* convert to mA */
3122 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3123
3124 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3125 max_current_caps =
3126 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3127 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3128 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3129 }
3130 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303131
3132 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003133 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303134
Aaron Lu55c46652012-07-04 13:31:48 +08003135 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303136 SDHCI_MAX_CURRENT_330_MASK) >>
3137 SDHCI_MAX_CURRENT_330_SHIFT) *
3138 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303139 }
3140 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003141 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303142
Aaron Lu55c46652012-07-04 13:31:48 +08003143 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303144 SDHCI_MAX_CURRENT_300_MASK) >>
3145 SDHCI_MAX_CURRENT_300_SHIFT) *
3146 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303147 }
3148 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003149 ocr_avail |= MMC_VDD_165_195;
3150
Aaron Lu55c46652012-07-04 13:31:48 +08003151 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303152 SDHCI_MAX_CURRENT_180_MASK) >>
3153 SDHCI_MAX_CURRENT_180_SHIFT) *
3154 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303155 }
3156
Tim Kryger52221612014-06-25 00:25:34 -07003157 /* If OCR set by external regulators, use it instead */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003158 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003159 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003160
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003161 if (host->ocr_mask)
Tim Kryger3a48edc2014-06-13 10:13:56 -07003162 ocr_avail &= host->ocr_mask;
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003163
Takashi Iwai8f230f42010-12-08 10:04:30 +01003164 mmc->ocr_avail = ocr_avail;
3165 mmc->ocr_avail_sdio = ocr_avail;
3166 if (host->ocr_avail_sdio)
3167 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3168 mmc->ocr_avail_sd = ocr_avail;
3169 if (host->ocr_avail_sd)
3170 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3171 else /* normal SD controllers don't support 1.8V */
3172 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3173 mmc->ocr_avail_mmc = ocr_avail;
3174 if (host->ocr_avail_mmc)
3175 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003176
3177 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303178 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003179 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003180 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003181 }
3182
Pierre Ossmand129bce2006-03-24 03:18:17 -08003183 spin_lock_init(&host->lock);
3184
3185 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003186 * Maximum number of segments. Depends on if the hardware
3187 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003188 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003189 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003190 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003191 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003192 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003193 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003194 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003195
3196 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003197 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01003198 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08003199 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003200 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003201
3202 /*
3203 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003204 * of bytes. When doing hardware scatter/gather, each entry cannot
3205 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003206 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003207 if (host->flags & SDHCI_USE_ADMA) {
3208 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3209 mmc->max_seg_size = 65535;
3210 else
3211 mmc->max_seg_size = 65536;
3212 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003213 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003214 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003215
3216 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003217 * Maximum block size. This varies from controller to controller and
3218 * is specified in the capabilities register.
3219 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003220 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3221 mmc->max_blk_size = 2;
3222 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303223 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003224 SDHCI_MAX_BLOCK_SHIFT;
3225 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003226 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3227 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003228 mmc->max_blk_size = 0;
3229 }
3230 }
3231
3232 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003233
3234 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003235 * Maximum block count.
3236 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003237 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003238
3239 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003240 * Init tasklets.
3241 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003242 tasklet_init(&host->finish_tasklet,
3243 sdhci_tasklet_finish, (unsigned long)host);
3244
Al Viroe4cad1b2006-10-10 22:47:07 +01003245 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003246
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303247 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303248 init_waitqueue_head(&host->buf_ready_int);
3249
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303250 /* Initialize re-tuning timer */
3251 init_timer(&host->tuning_timer);
3252 host->tuning_timer.data = (unsigned long)host;
3253 host->tuning_timer.function = sdhci_tuning_timer;
3254 }
3255
Shawn Guo2af502c2013-07-05 14:38:55 +08003256 sdhci_init(host, 0);
3257
Russell King781e9892014-04-25 12:55:46 +01003258 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3259 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003260 if (ret) {
3261 pr_err("%s: Failed to request IRQ %d: %d\n",
3262 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003263 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003264 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003265
Pierre Ossmand129bce2006-03-24 03:18:17 -08003266#ifdef CONFIG_MMC_DEBUG
3267 sdhci_dumpregs(host);
3268#endif
3269
Pierre Ossmanf9134312008-12-21 17:01:48 +01003270#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003271 snprintf(host->led_name, sizeof(host->led_name),
3272 "%s::", mmc_hostname(mmc));
3273 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003274 host->led.brightness = LED_OFF;
3275 host->led.default_trigger = mmc_hostname(mmc);
3276 host->led.brightness_set = sdhci_led_control;
3277
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003278 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003279 if (ret) {
3280 pr_err("%s: Failed to register LED device: %d\n",
3281 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003282 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003283 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003284#endif
3285
Pierre Ossman5f25a662006-10-04 02:15:39 -07003286 mmiowb();
3287
Pierre Ossmand129bce2006-03-24 03:18:17 -08003288 mmc_add_host(mmc);
3289
Girish K Sa3c76eb2011-10-11 11:44:09 +05303290 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003291 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003292 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3293 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003294
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003295 sdhci_enable_card_detection(host);
3296
Pierre Ossmand129bce2006-03-24 03:18:17 -08003297 return 0;
3298
Pierre Ossmanf9134312008-12-21 17:01:48 +01003299#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003300reset:
Russell King03231f92014-04-25 12:57:12 +01003301 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003302 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3303 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003304 free_irq(host->irq, host);
3305#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003306untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003307 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003308
3309 return ret;
3310}
3311
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003312EXPORT_SYMBOL_GPL(sdhci_add_host);
3313
Pierre Ossman1e728592008-04-16 19:13:13 +02003314void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003315{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003316 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003317 unsigned long flags;
3318
3319 if (dead) {
3320 spin_lock_irqsave(&host->lock, flags);
3321
3322 host->flags |= SDHCI_DEVICE_DEAD;
3323
3324 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303325 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003326 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003327
3328 host->mrq->cmd->error = -ENOMEDIUM;
3329 tasklet_schedule(&host->finish_tasklet);
3330 }
3331
3332 spin_unlock_irqrestore(&host->lock, flags);
3333 }
3334
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003335 sdhci_disable_card_detection(host);
3336
Markus Mayer4e743f12014-07-03 13:27:42 -07003337 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003338
Pierre Ossmanf9134312008-12-21 17:01:48 +01003339#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003340 led_classdev_unregister(&host->led);
3341#endif
3342
Pierre Ossman1e728592008-04-16 19:13:13 +02003343 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003344 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003345
Russell Kingb537f942014-04-25 12:56:01 +01003346 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3347 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003348 free_irq(host->irq, host);
3349
3350 del_timer_sync(&host->timer);
3351
Pierre Ossmand129bce2006-03-24 03:18:17 -08003352 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003353
Tim Kryger3a48edc2014-06-13 10:13:56 -07003354 if (!IS_ERR(mmc->supply.vqmmc))
3355 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003356
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003357 if (host->adma_table)
Adrian Hunter76fe3792014-11-04 12:42:42 +02003358 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003359 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003360 kfree(host->align_buffer);
3361
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003362 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003363 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003364}
3365
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003366EXPORT_SYMBOL_GPL(sdhci_remove_host);
3367
3368void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003369{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003370 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003371}
3372
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003373EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003374
3375/*****************************************************************************\
3376 * *
3377 * Driver init/exit *
3378 * *
3379\*****************************************************************************/
3380
3381static int __init sdhci_drv_init(void)
3382{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303383 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003384 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303385 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003386
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003387 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003388}
3389
3390static void __exit sdhci_drv_exit(void)
3391{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003392}
3393
3394module_init(sdhci_drv_init);
3395module_exit(sdhci_drv_exit);
3396
Pierre Ossmandf673b22006-06-30 02:22:31 -07003397module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003398module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003399
Pierre Ossman32710e82009-04-08 20:14:54 +02003400MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003401MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003402MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003403
Pierre Ossmandf673b22006-06-30 02:22:31 -07003404MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003405MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");