blob: b5e22b8c4885c67209b7e6646f27c6155f29331e [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
Russell Kingd1e49f72014-04-25 12:58:34 +010047#define ADMA_SIZE ((128 * 2 + 1) * 4)
48
Pierre Ossmandf673b22006-06-30 02:22:31 -070049static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030050static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070051
Pierre Ossmand129bce2006-03-24 03:18:17 -080052static void sdhci_finish_data(struct sdhci_host *);
53
Pierre Ossmand129bce2006-03-24 03:18:17 -080054static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053055static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053056static void sdhci_tuning_timer(unsigned long data);
Kevin Liu52983382013-01-31 11:31:37 +080057static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080058
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030059#ifdef CONFIG_PM_RUNTIME
60static int sdhci_runtime_pm_get(struct sdhci_host *host);
61static int sdhci_runtime_pm_put(struct sdhci_host *host);
Adrian Hunterf0710a52013-05-06 12:17:32 +030062static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
63static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030064#else
65static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
66{
67 return 0;
68}
69static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
70{
71 return 0;
72}
Adrian Hunterf0710a52013-05-06 12:17:32 +030073static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
74{
75}
76static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
77{
78}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030079#endif
80
Pierre Ossmand129bce2006-03-24 03:18:17 -080081static void sdhci_dumpregs(struct sdhci_host *host)
82{
Girish K Sa3c76eb2011-10-11 11:44:09 +053083 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070084 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080085
Girish K Sa3c76eb2011-10-11 11:44:09 +053086 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030087 sdhci_readl(host, SDHCI_DMA_ADDRESS),
88 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053089 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030090 sdhci_readw(host, SDHCI_BLOCK_SIZE),
91 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053092 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030093 sdhci_readl(host, SDHCI_ARGUMENT),
94 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053095 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030096 sdhci_readl(host, SDHCI_PRESENT_STATE),
97 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053098 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030099 sdhci_readb(host, SDHCI_POWER_CONTROL),
100 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530101 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300102 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
103 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530104 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300105 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
106 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530107 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300108 sdhci_readl(host, SDHCI_INT_ENABLE),
109 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530110 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300111 sdhci_readw(host, SDHCI_ACMD12_ERR),
112 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530113 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300114 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500115 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530116 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500117 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300118 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530119 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530120 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800121
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100122 if (host->flags & SDHCI_USE_ADMA)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530123 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100124 readl(host->ioaddr + SDHCI_ADMA_ERROR),
125 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
126
Girish K Sa3c76eb2011-10-11 11:44:09 +0530127 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800128}
129
130/*****************************************************************************\
131 * *
132 * Low level functions *
133 * *
134\*****************************************************************************/
135
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300136static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
137{
Russell King5b4f1f62014-04-25 12:57:02 +0100138 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300139
Adrian Hunterc79396c2011-12-27 15:48:42 +0200140 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100141 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300142 return;
143
Russell King5b4f1f62014-04-25 12:57:02 +0100144 if (enable) {
145 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
146 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800147
Russell King5b4f1f62014-04-25 12:57:02 +0100148 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
149 SDHCI_INT_CARD_INSERT;
150 } else {
151 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
152 }
Russell Kingb537f942014-04-25 12:56:01 +0100153
154 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
155 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300156}
157
158static void sdhci_enable_card_detection(struct sdhci_host *host)
159{
160 sdhci_set_card_detection(host, true);
161}
162
163static void sdhci_disable_card_detection(struct sdhci_host *host)
164{
165 sdhci_set_card_detection(host, false);
166}
167
Russell King03231f92014-04-25 12:57:12 +0100168void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800169{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700170 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800171
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300172 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800173
Adrian Hunterf0710a52013-05-06 12:17:32 +0300174 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800175 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300176 /* Reset-all turns off SD Bus Power */
177 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
178 sdhci_runtime_pm_bus_off(host);
179 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800180
Pierre Ossmane16514d82006-06-30 02:22:24 -0700181 /* Wait max 100 ms */
182 timeout = 100;
183
184 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300185 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700186 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530187 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700188 mmc_hostname(host->mmc), (int)mask);
189 sdhci_dumpregs(host);
190 return;
191 }
192 timeout--;
193 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800194 }
Russell King03231f92014-04-25 12:57:12 +0100195}
196EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300197
Russell King03231f92014-04-25 12:57:12 +0100198static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
199{
200 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
201 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
202 SDHCI_CARD_PRESENT))
203 return;
204 }
205
206 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800207
Russell Kingda91a8f2014-04-25 13:00:12 +0100208 if (mask & SDHCI_RESET_ALL) {
209 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
210 if (host->ops->enable_dma)
211 host->ops->enable_dma(host);
212 }
213
214 /* Resetting the controller clears many */
215 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800216 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800217}
218
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800219static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
220
221static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800223 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100224 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800225 else
Russell King03231f92014-04-25 12:57:12 +0100226 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800227
Russell Kingb537f942014-04-25 12:56:01 +0100228 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
229 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
230 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
231 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
232 SDHCI_INT_RESPONSE;
233
234 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
235 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800236
237 if (soft) {
238 /* force clock reconfiguration */
239 host->clock = 0;
240 sdhci_set_ios(host->mmc, &host->mmc->ios);
241 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300242}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800243
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300244static void sdhci_reinit(struct sdhci_host *host)
245{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800246 sdhci_init(host, 0);
Aaron Lub67c6b42012-06-29 16:17:31 +0800247 /*
248 * Retuning stuffs are affected by different cards inserted and only
249 * applicable to UHS-I cards. So reset these fields to their initial
250 * value when card is removed.
251 */
Aaron Lu973905f2012-07-04 13:29:09 +0800252 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
253 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
254
Aaron Lub67c6b42012-06-29 16:17:31 +0800255 del_timer_sync(&host->tuning_timer);
256 host->flags &= ~SDHCI_NEEDS_RETUNING;
257 host->mmc->max_blk_count =
258 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
259 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300260 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261}
262
263static void sdhci_activate_led(struct sdhci_host *host)
264{
265 u8 ctrl;
266
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300267 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800268 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300269 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800270}
271
272static void sdhci_deactivate_led(struct sdhci_host *host)
273{
274 u8 ctrl;
275
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300276 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800277 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300278 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800279}
280
Pierre Ossmanf9134312008-12-21 17:01:48 +0100281#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100282static void sdhci_led_control(struct led_classdev *led,
283 enum led_brightness brightness)
284{
285 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
286 unsigned long flags;
287
288 spin_lock_irqsave(&host->lock, flags);
289
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300290 if (host->runtime_suspended)
291 goto out;
292
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100293 if (brightness == LED_OFF)
294 sdhci_deactivate_led(host);
295 else
296 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300297out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100298 spin_unlock_irqrestore(&host->lock, flags);
299}
300#endif
301
Pierre Ossmand129bce2006-03-24 03:18:17 -0800302/*****************************************************************************\
303 * *
304 * Core functions *
305 * *
306\*****************************************************************************/
307
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100308static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800309{
Pierre Ossman76591502008-07-21 00:32:11 +0200310 unsigned long flags;
311 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700312 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200313 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800314
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100315 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800316
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100317 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200318 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800319
Pierre Ossman76591502008-07-21 00:32:11 +0200320 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800321
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100322 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200323 if (!sg_miter_next(&host->sg_miter))
324 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800325
Pierre Ossman76591502008-07-21 00:32:11 +0200326 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800327
Pierre Ossman76591502008-07-21 00:32:11 +0200328 blksize -= len;
329 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200330
Pierre Ossman76591502008-07-21 00:32:11 +0200331 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800332
Pierre Ossman76591502008-07-21 00:32:11 +0200333 while (len) {
334 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300335 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200336 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800337 }
Pierre Ossman76591502008-07-21 00:32:11 +0200338
339 *buf = scratch & 0xFF;
340
341 buf++;
342 scratch >>= 8;
343 chunk--;
344 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800345 }
346 }
Pierre Ossman76591502008-07-21 00:32:11 +0200347
348 sg_miter_stop(&host->sg_miter);
349
350 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100351}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800352
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100353static void sdhci_write_block_pio(struct sdhci_host *host)
354{
Pierre Ossman76591502008-07-21 00:32:11 +0200355 unsigned long flags;
356 size_t blksize, len, chunk;
357 u32 scratch;
358 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100359
360 DBG("PIO writing\n");
361
362 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200363 chunk = 0;
364 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100365
Pierre Ossman76591502008-07-21 00:32:11 +0200366 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100367
368 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200369 if (!sg_miter_next(&host->sg_miter))
370 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100371
Pierre Ossman76591502008-07-21 00:32:11 +0200372 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200373
Pierre Ossman76591502008-07-21 00:32:11 +0200374 blksize -= len;
375 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100376
Pierre Ossman76591502008-07-21 00:32:11 +0200377 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100378
Pierre Ossman76591502008-07-21 00:32:11 +0200379 while (len) {
380 scratch |= (u32)*buf << (chunk * 8);
381
382 buf++;
383 chunk++;
384 len--;
385
386 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300387 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200388 chunk = 0;
389 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100390 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100391 }
392 }
Pierre Ossman76591502008-07-21 00:32:11 +0200393
394 sg_miter_stop(&host->sg_miter);
395
396 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100397}
398
399static void sdhci_transfer_pio(struct sdhci_host *host)
400{
401 u32 mask;
402
403 BUG_ON(!host->data);
404
Pierre Ossman76591502008-07-21 00:32:11 +0200405 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100406 return;
407
408 if (host->data->flags & MMC_DATA_READ)
409 mask = SDHCI_DATA_AVAILABLE;
410 else
411 mask = SDHCI_SPACE_AVAILABLE;
412
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200413 /*
414 * Some controllers (JMicron JMB38x) mess up the buffer bits
415 * for transfers < 4 bytes. As long as it is just one block,
416 * we can ignore the bits.
417 */
418 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
419 (host->data->blocks == 1))
420 mask = ~0;
421
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300422 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300423 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
424 udelay(100);
425
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100426 if (host->data->flags & MMC_DATA_READ)
427 sdhci_read_block_pio(host);
428 else
429 sdhci_write_block_pio(host);
430
Pierre Ossman76591502008-07-21 00:32:11 +0200431 host->blocks--;
432 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100433 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100434 }
435
436 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800437}
438
Pierre Ossman2134a922008-06-28 18:28:51 +0200439static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
440{
441 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800442 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200443}
444
445static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
446{
Cong Wang482fce92011-11-27 13:27:00 +0800447 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200448 local_irq_restore(*flags);
449}
450
Ben Dooks118cd172010-03-05 13:43:26 -0800451static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
452{
Ben Dooks9e506f32010-03-05 13:43:29 -0800453 __le32 *dataddr = (__le32 __force *)(desc + 4);
454 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800455
Ben Dooks9e506f32010-03-05 13:43:29 -0800456 /* SDHCI specification says ADMA descriptors should be 4 byte
457 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800458
Ben Dooks9e506f32010-03-05 13:43:29 -0800459 cmdlen[0] = cpu_to_le16(cmd);
460 cmdlen[1] = cpu_to_le16(len);
461
462 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800463}
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
470 u8 *desc;
471 u8 *align;
472 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),
492 host->align_buffer, 128 * 4, 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;
Pierre Ossman2134a922008-06-28 18:28:51 +0200495 BUG_ON(host->align_addr & 0x3);
496
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
502 desc = host->adma_desc;
503 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 */
518 offset = (4 - (addr & 0x3)) & 0x3;
519 if (offset) {
520 if (data->flags & MMC_DATA_WRITE) {
521 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200522 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200523 memcpy(align, buffer, offset);
524 sdhci_kunmap_atomic(buffer, &flags);
525 }
526
Ben Dooks118cd172010-03-05 13:43:26 -0800527 /* tran, valid */
528 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200529
530 BUG_ON(offset > 65536);
531
Pierre Ossman2134a922008-06-28 18:28:51 +0200532 align += 4;
533 align_addr += 4;
534
535 desc += 8;
536
537 addr += offset;
538 len -= offset;
539 }
540
Pierre Ossman2134a922008-06-28 18:28:51 +0200541 BUG_ON(len > 65536);
542
Ben Dooks118cd172010-03-05 13:43:26 -0800543 /* tran, valid */
544 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200545 desc += 8;
546
547 /*
548 * If this triggers then we have a calculation bug
549 * somewhere. :/
550 */
Russell Kingd1e49f72014-04-25 12:58:34 +0100551 WARN_ON((desc - host->adma_desc) > ADMA_SIZE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200552 }
553
Thomas Abraham70764a92010-05-26 14:42:04 -0700554 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
555 /*
556 * Mark the last descriptor as the terminating descriptor
557 */
558 if (desc != host->adma_desc) {
559 desc -= 8;
560 desc[0] |= 0x2; /* end */
561 }
562 } else {
563 /*
564 * Add a terminating entry.
565 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200566
Thomas Abraham70764a92010-05-26 14:42:04 -0700567 /* nop, end, valid */
568 sdhci_set_adma_desc(desc, 0, 0, 0x3);
569 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200570
571 /*
572 * Resync align buffer as we might have changed it.
573 */
574 if (data->flags & MMC_DATA_WRITE) {
575 dma_sync_single_for_device(mmc_dev(host->mmc),
576 host->align_addr, 128 * 4, direction);
577 }
578
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200579 return 0;
580
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200581unmap_align:
582 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
583 128 * 4, direction);
584fail:
585 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200586}
587
588static void sdhci_adma_table_post(struct sdhci_host *host,
589 struct mmc_data *data)
590{
591 int direction;
592
593 struct scatterlist *sg;
594 int i, size;
595 u8 *align;
596 char *buffer;
597 unsigned long flags;
Russell Kingde0b65a2014-04-25 12:58:29 +0100598 bool has_unaligned;
Pierre Ossman2134a922008-06-28 18:28:51 +0200599
600 if (data->flags & MMC_DATA_READ)
601 direction = DMA_FROM_DEVICE;
602 else
603 direction = DMA_TO_DEVICE;
604
Pierre Ossman2134a922008-06-28 18:28:51 +0200605 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
606 128 * 4, direction);
607
Russell Kingde0b65a2014-04-25 12:58:29 +0100608 /* Do a quick scan of the SG list for any unaligned mappings */
609 has_unaligned = false;
610 for_each_sg(data->sg, sg, host->sg_count, i)
611 if (sg_dma_address(sg) & 3) {
612 has_unaligned = true;
613 break;
614 }
615
616 if (has_unaligned && data->flags & MMC_DATA_READ) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200617 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
618 data->sg_len, direction);
619
620 align = host->align_buffer;
621
622 for_each_sg(data->sg, sg, host->sg_count, i) {
623 if (sg_dma_address(sg) & 0x3) {
624 size = 4 - (sg_dma_address(sg) & 0x3);
625
626 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200627 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200628 memcpy(buffer, align, size);
629 sdhci_kunmap_atomic(buffer, &flags);
630
631 align += 4;
632 }
633 }
634 }
635
636 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
637 data->sg_len, direction);
638}
639
Andrei Warkentina3c77782011-04-11 16:13:42 -0500640static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800641{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700642 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500643 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700644 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800645
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200646 /*
647 * If the host controller provides us with an incorrect timeout
648 * value, just skip the check and use 0xE. The hardware may take
649 * longer to time out, but that's much better than having a too-short
650 * timeout value.
651 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200652 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200653 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200654
Andrei Warkentina3c77782011-04-11 16:13:42 -0500655 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100656 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500657 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800658
Andrei Warkentina3c77782011-04-11 16:13:42 -0500659 /* timeout in us */
660 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100661 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300662 else {
663 target_timeout = data->timeout_ns / 1000;
664 if (host->clock)
665 target_timeout += data->timeout_clks / host->clock;
666 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700667
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700668 /*
669 * Figure out needed cycles.
670 * We do this in steps in order to fit inside a 32 bit int.
671 * The first step is the minimum timeout, which will have a
672 * minimum resolution of 6 bits:
673 * (1) 2^13*1000 > 2^22,
674 * (2) host->timeout_clk < 2^16
675 * =>
676 * (1) / (2) > 2^6
677 */
678 count = 0;
679 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
680 while (current_timeout < target_timeout) {
681 count++;
682 current_timeout <<= 1;
683 if (count >= 0xF)
684 break;
685 }
686
687 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400688 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
689 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700690 count = 0xE;
691 }
692
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200693 return count;
694}
695
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300696static void sdhci_set_transfer_irqs(struct sdhci_host *host)
697{
698 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
699 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
700
701 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100702 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300703 else
Russell Kingb537f942014-04-25 12:56:01 +0100704 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
705
706 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
707 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300708}
709
Aisheng Dongb45e6682014-08-27 15:26:29 +0800710static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200711{
712 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800713
714 if (host->ops->set_timeout) {
715 host->ops->set_timeout(host, cmd);
716 } else {
717 count = sdhci_calc_timeout(host, cmd);
718 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
719 }
720}
721
722static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
723{
Pierre Ossman2134a922008-06-28 18:28:51 +0200724 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500725 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200726 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200727
728 WARN_ON(host->data);
729
Aisheng Dongb45e6682014-08-27 15:26:29 +0800730 if (data || (cmd->flags & MMC_RSP_BUSY))
731 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500732
733 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200734 return;
735
736 /* Sanity checks */
737 BUG_ON(data->blksz * data->blocks > 524288);
738 BUG_ON(data->blksz > host->mmc->max_blk_size);
739 BUG_ON(data->blocks > 65535);
740
741 host->data = data;
742 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400743 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200744
Richard Röjforsa13abc72009-09-22 16:45:30 -0700745 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100746 host->flags |= SDHCI_REQ_USE_DMA;
747
Pierre Ossman2134a922008-06-28 18:28:51 +0200748 /*
749 * FIXME: This doesn't account for merging when mapping the
750 * scatterlist.
751 */
752 if (host->flags & SDHCI_REQ_USE_DMA) {
753 int broken, i;
754 struct scatterlist *sg;
755
756 broken = 0;
757 if (host->flags & SDHCI_USE_ADMA) {
758 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
759 broken = 1;
760 } else {
761 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
762 broken = 1;
763 }
764
765 if (unlikely(broken)) {
766 for_each_sg(data->sg, sg, data->sg_len, i) {
767 if (sg->length & 0x3) {
768 DBG("Reverting to PIO because of "
769 "transfer size (%d)\n",
770 sg->length);
771 host->flags &= ~SDHCI_REQ_USE_DMA;
772 break;
773 }
774 }
775 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100776 }
777
778 /*
779 * The assumption here being that alignment is the same after
780 * translation to device address space.
781 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200782 if (host->flags & SDHCI_REQ_USE_DMA) {
783 int broken, i;
784 struct scatterlist *sg;
785
786 broken = 0;
787 if (host->flags & SDHCI_USE_ADMA) {
788 /*
789 * As we use 3 byte chunks to work around
790 * alignment problems, we need to check this
791 * quirk.
792 */
793 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
794 broken = 1;
795 } else {
796 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
797 broken = 1;
798 }
799
800 if (unlikely(broken)) {
801 for_each_sg(data->sg, sg, data->sg_len, i) {
802 if (sg->offset & 0x3) {
803 DBG("Reverting to PIO because of "
804 "bad alignment\n");
805 host->flags &= ~SDHCI_REQ_USE_DMA;
806 break;
807 }
808 }
809 }
810 }
811
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200812 if (host->flags & SDHCI_REQ_USE_DMA) {
813 if (host->flags & SDHCI_USE_ADMA) {
814 ret = sdhci_adma_table_pre(host, data);
815 if (ret) {
816 /*
817 * This only happens when someone fed
818 * us an invalid request.
819 */
820 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200821 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200822 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300823 sdhci_writel(host, host->adma_addr,
824 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200825 }
826 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300827 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200828
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300829 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200830 data->sg, data->sg_len,
831 (data->flags & MMC_DATA_READ) ?
832 DMA_FROM_DEVICE :
833 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300834 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200835 /*
836 * This only happens when someone fed
837 * us an invalid request.
838 */
839 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200840 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200841 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200842 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300843 sdhci_writel(host, sg_dma_address(data->sg),
844 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200845 }
846 }
847 }
848
Pierre Ossman2134a922008-06-28 18:28:51 +0200849 /*
850 * Always adjust the DMA selection as some controllers
851 * (e.g. JMicron) can't do PIO properly when the selection
852 * is ADMA.
853 */
854 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300855 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200856 ctrl &= ~SDHCI_CTRL_DMA_MASK;
857 if ((host->flags & SDHCI_REQ_USE_DMA) &&
858 (host->flags & SDHCI_USE_ADMA))
859 ctrl |= SDHCI_CTRL_ADMA32;
860 else
861 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300862 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100863 }
864
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200865 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200866 int flags;
867
868 flags = SG_MITER_ATOMIC;
869 if (host->data->flags & MMC_DATA_READ)
870 flags |= SG_MITER_TO_SG;
871 else
872 flags |= SG_MITER_FROM_SG;
873 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200874 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800875 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700876
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300877 sdhci_set_transfer_irqs(host);
878
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400879 /* Set the DMA boundary value and block size */
880 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
881 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300882 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700883}
884
885static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500886 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700887{
888 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500889 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700890
Dong Aisheng2b558c12013-10-30 22:09:48 +0800891 if (data == NULL) {
892 /* clear Auto CMD settings for no data CMDs */
893 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
894 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
895 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700896 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800897 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700898
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200899 WARN_ON(!host->data);
900
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700901 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500902 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
903 mode |= SDHCI_TRNS_MULTI;
904 /*
905 * If we are sending CMD23, CMD12 never gets sent
906 * on successful completion (so no Auto-CMD12).
907 */
908 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
909 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500910 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
911 mode |= SDHCI_TRNS_AUTO_CMD23;
912 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
913 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700914 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500915
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700916 if (data->flags & MMC_DATA_READ)
917 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100918 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700919 mode |= SDHCI_TRNS_DMA;
920
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300921 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800922}
923
924static void sdhci_finish_data(struct sdhci_host *host)
925{
926 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800927
928 BUG_ON(!host->data);
929
930 data = host->data;
931 host->data = NULL;
932
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100933 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200934 if (host->flags & SDHCI_USE_ADMA)
935 sdhci_adma_table_post(host, data);
936 else {
937 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
938 data->sg_len, (data->flags & MMC_DATA_READ) ?
939 DMA_FROM_DEVICE : DMA_TO_DEVICE);
940 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800941 }
942
943 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200944 * The specification states that the block count register must
945 * be updated, but it does not specify at what point in the
946 * data flow. That makes the register entirely useless to read
947 * back so we have to assume that nothing made it to the card
948 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800949 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200950 if (data->error)
951 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800952 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200953 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800954
Andrei Warkentine89d4562011-05-23 15:06:37 -0500955 /*
956 * Need to send CMD12 if -
957 * a) open-ended multiblock transfer (no CMD23)
958 * b) error in multiblock transfer
959 */
960 if (data->stop &&
961 (data->error ||
962 !host->mrq->sbc)) {
963
Pierre Ossmand129bce2006-03-24 03:18:17 -0800964 /*
965 * The controller needs a reset of internal state machines
966 * upon error conditions.
967 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200968 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100969 sdhci_do_reset(host, SDHCI_RESET_CMD);
970 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800971 }
972
973 sdhci_send_command(host, data->stop);
974 } else
975 tasklet_schedule(&host->finish_tasklet);
976}
977
Dong Aishengc0e551292013-09-13 19:11:31 +0800978void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800979{
980 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700981 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700982 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983
984 WARN_ON(host->cmd);
985
Pierre Ossmand129bce2006-03-24 03:18:17 -0800986 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700987 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700988
989 mask = SDHCI_CMD_INHIBIT;
990 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
991 mask |= SDHCI_DATA_INHIBIT;
992
993 /* We shouldn't wait for data inihibit for stop commands, even
994 though they might use busy signaling */
995 if (host->mrq->data && (cmd == host->mrq->data->stop))
996 mask &= ~SDHCI_DATA_INHIBIT;
997
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300998 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700999 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301000 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001001 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001002 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001003 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001004 tasklet_schedule(&host->finish_tasklet);
1005 return;
1006 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001007 timeout--;
1008 mdelay(1);
1009 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001010
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001011 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001012 if (!cmd->data && cmd->busy_timeout > 9000)
1013 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001014 else
1015 timeout += 10 * HZ;
1016 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001017
1018 host->cmd = cmd;
1019
Andrei Warkentina3c77782011-04-11 16:13:42 -05001020 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001021
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001022 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001023
Andrei Warkentine89d4562011-05-23 15:06:37 -05001024 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001025
Pierre Ossmand129bce2006-03-24 03:18:17 -08001026 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301027 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001028 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001029 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030 tasklet_schedule(&host->finish_tasklet);
1031 return;
1032 }
1033
1034 if (!(cmd->flags & MMC_RSP_PRESENT))
1035 flags = SDHCI_CMD_RESP_NONE;
1036 else if (cmd->flags & MMC_RSP_136)
1037 flags = SDHCI_CMD_RESP_LONG;
1038 else if (cmd->flags & MMC_RSP_BUSY)
1039 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1040 else
1041 flags = SDHCI_CMD_RESP_SHORT;
1042
1043 if (cmd->flags & MMC_RSP_CRC)
1044 flags |= SDHCI_CMD_CRC;
1045 if (cmd->flags & MMC_RSP_OPCODE)
1046 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301047
1048 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301049 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1050 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001051 flags |= SDHCI_CMD_DATA;
1052
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001053 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001054}
Dong Aishengc0e551292013-09-13 19:11:31 +08001055EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001056
1057static void sdhci_finish_command(struct sdhci_host *host)
1058{
1059 int i;
1060
1061 BUG_ON(host->cmd == NULL);
1062
1063 if (host->cmd->flags & MMC_RSP_PRESENT) {
1064 if (host->cmd->flags & MMC_RSP_136) {
1065 /* CRC is stripped so we need to do some shifting. */
1066 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001067 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001068 SDHCI_RESPONSE + (3-i)*4) << 8;
1069 if (i != 3)
1070 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001071 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072 SDHCI_RESPONSE + (3-i)*4-1);
1073 }
1074 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001075 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001076 }
1077 }
1078
Pierre Ossman17b04292007-07-22 22:18:46 +02001079 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001080
Andrei Warkentine89d4562011-05-23 15:06:37 -05001081 /* Finished CMD23, now send actual command. */
1082 if (host->cmd == host->mrq->sbc) {
1083 host->cmd = NULL;
1084 sdhci_send_command(host, host->mrq->cmd);
1085 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001086
Andrei Warkentine89d4562011-05-23 15:06:37 -05001087 /* Processed actual command. */
1088 if (host->data && host->data_early)
1089 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001090
Andrei Warkentine89d4562011-05-23 15:06:37 -05001091 if (!host->cmd->data)
1092 tasklet_schedule(&host->finish_tasklet);
1093
1094 host->cmd = NULL;
1095 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001096}
1097
Kevin Liu52983382013-01-31 11:31:37 +08001098static u16 sdhci_get_preset_value(struct sdhci_host *host)
1099{
Russell Kingd975f122014-04-25 12:59:31 +01001100 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001101
Russell Kingd975f122014-04-25 12:59:31 +01001102 switch (host->timing) {
1103 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001104 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1105 break;
Russell Kingd975f122014-04-25 12:59:31 +01001106 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001107 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1108 break;
Russell Kingd975f122014-04-25 12:59:31 +01001109 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001110 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1111 break;
Russell Kingd975f122014-04-25 12:59:31 +01001112 case MMC_TIMING_UHS_SDR104:
1113 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001114 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1115 break;
Russell Kingd975f122014-04-25 12:59:31 +01001116 case MMC_TIMING_UHS_DDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001117 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1118 break;
1119 default:
1120 pr_warn("%s: Invalid UHS-I mode selected\n",
1121 mmc_hostname(host->mmc));
1122 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1123 break;
1124 }
1125 return preset;
1126}
1127
Russell King17710592014-04-25 12:58:55 +01001128void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001129{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301130 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001131 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301132 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001133 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001134
Russell King1650d0c2014-04-25 12:58:50 +01001135 host->mmc->actual_clock = 0;
1136
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001137 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001138
1139 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001140 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001141
Zhangfei Gao85105c52010-08-06 07:10:01 +08001142 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001143 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001144 u16 pre_val;
1145
1146 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1147 pre_val = sdhci_get_preset_value(host);
1148 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1149 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1150 if (host->clk_mul &&
1151 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1152 clk = SDHCI_PROG_CLOCK_MODE;
1153 real_div = div + 1;
1154 clk_mul = host->clk_mul;
1155 } else {
1156 real_div = max_t(int, 1, div << 1);
1157 }
1158 goto clock_set;
1159 }
1160
Arindam Nathc3ed3872011-05-05 12:19:06 +05301161 /*
1162 * Check if the Host Controller supports Programmable Clock
1163 * Mode.
1164 */
1165 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001166 for (div = 1; div <= 1024; div++) {
1167 if ((host->max_clk * host->clk_mul / div)
1168 <= clock)
1169 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001170 }
Kevin Liu52983382013-01-31 11:31:37 +08001171 /*
1172 * Set Programmable Clock Mode in the Clock
1173 * Control register.
1174 */
1175 clk = SDHCI_PROG_CLOCK_MODE;
1176 real_div = div;
1177 clk_mul = host->clk_mul;
1178 div--;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301179 } else {
1180 /* Version 3.00 divisors must be a multiple of 2. */
1181 if (host->max_clk <= clock)
1182 div = 1;
1183 else {
1184 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1185 div += 2) {
1186 if ((host->max_clk / div) <= clock)
1187 break;
1188 }
1189 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001190 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301191 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001192 }
1193 } else {
1194 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001195 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001196 if ((host->max_clk / div) <= clock)
1197 break;
1198 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001199 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301200 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001201 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001202
Kevin Liu52983382013-01-31 11:31:37 +08001203clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001204 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001205 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301206 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001207 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1208 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001209 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001210 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001211
Chris Ball27f6cb12009-09-22 16:45:31 -07001212 /* Wait max 20 ms */
1213 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001214 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001215 & SDHCI_CLOCK_INT_STABLE)) {
1216 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301217 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001218 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001219 sdhci_dumpregs(host);
1220 return;
1221 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001222 timeout--;
1223 mdelay(1);
1224 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001225
1226 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001227 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001228}
Russell King17710592014-04-25 12:58:55 +01001229EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001230
Russell King24fbb3c2014-04-25 13:00:06 +01001231static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1232 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001233{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001234 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001235 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001236
Tim Kryger52221612014-06-25 00:25:34 -07001237 if (!IS_ERR(mmc->supply.vmmc)) {
1238 spin_unlock_irq(&host->lock);
Markus Mayer4e743f12014-07-03 13:27:42 -07001239 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Tim Kryger52221612014-06-25 00:25:34 -07001240 spin_lock_irq(&host->lock);
1241 return;
1242 }
1243
Russell King24fbb3c2014-04-25 13:00:06 +01001244 if (mode != MMC_POWER_OFF) {
1245 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001246 case MMC_VDD_165_195:
1247 pwr = SDHCI_POWER_180;
1248 break;
1249 case MMC_VDD_29_30:
1250 case MMC_VDD_30_31:
1251 pwr = SDHCI_POWER_300;
1252 break;
1253 case MMC_VDD_32_33:
1254 case MMC_VDD_33_34:
1255 pwr = SDHCI_POWER_330;
1256 break;
1257 default:
1258 BUG();
1259 }
1260 }
1261
1262 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001263 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001264
Pierre Ossmanae628902009-05-03 20:45:03 +02001265 host->pwr = pwr;
1266
1267 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001268 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001269 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1270 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001271 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001272 } else {
1273 /*
1274 * Spec says that we should clear the power reg before setting
1275 * a new value. Some controllers don't seem to like this though.
1276 */
1277 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1278 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001279
Russell Kinge921a8b2014-04-25 13:00:01 +01001280 /*
1281 * At least the Marvell CaFe chip gets confused if we set the
1282 * voltage and set turn on power at the same time, so set the
1283 * voltage first.
1284 */
1285 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1286 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001287
Russell Kinge921a8b2014-04-25 13:00:01 +01001288 pwr |= SDHCI_POWER_ON;
1289
Pierre Ossmanae628902009-05-03 20:45:03 +02001290 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1291
Russell Kinge921a8b2014-04-25 13:00:01 +01001292 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1293 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001294
Russell Kinge921a8b2014-04-25 13:00:01 +01001295 /*
1296 * Some controllers need an extra 10ms delay of 10ms before
1297 * they can apply clock after applying power
1298 */
1299 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1300 mdelay(10);
1301 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001302}
1303
Pierre Ossmand129bce2006-03-24 03:18:17 -08001304/*****************************************************************************\
1305 * *
1306 * MMC callbacks *
1307 * *
1308\*****************************************************************************/
1309
1310static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1311{
1312 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001313 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001314 unsigned long flags;
Aaron Lu473b0952012-07-03 17:27:49 +08001315 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001316
1317 host = mmc_priv(mmc);
1318
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001319 sdhci_runtime_pm_get(host);
1320
Pierre Ossmand129bce2006-03-24 03:18:17 -08001321 spin_lock_irqsave(&host->lock, flags);
1322
1323 WARN_ON(host->mrq != NULL);
1324
Pierre Ossmanf9134312008-12-21 17:01:48 +01001325#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001326 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001327#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001328
1329 /*
1330 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1331 * requests if Auto-CMD12 is enabled.
1332 */
1333 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001334 if (mrq->stop) {
1335 mrq->data->stop = NULL;
1336 mrq->stop = NULL;
1337 }
1338 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001339
1340 host->mrq = mrq;
1341
Shawn Guo505a8682012-12-11 15:23:42 +08001342 /*
1343 * Firstly check card presence from cd-gpio. The return could
1344 * be one of the following possibilities:
1345 * negative: cd-gpio is not available
1346 * zero: cd-gpio is used, and card is removed
1347 * one: cd-gpio is used, and card is present
1348 */
1349 present = mmc_gpio_get_cd(host->mmc);
1350 if (present < 0) {
1351 /* If polling, assume that the card is always present. */
1352 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1353 present = 1;
1354 else
1355 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1356 SDHCI_CARD_PRESENT;
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +08001357 }
1358
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001359 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001360 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001361 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301362 } else {
1363 u32 present_state;
1364
1365 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1366 /*
1367 * Check if the re-tuning timer has already expired and there
1368 * is no on-going data transfer. If so, we need to execute
1369 * tuning procedure before sending command.
1370 */
1371 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1372 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
Chris Ball14efd952012-11-05 14:29:49 -05001373 if (mmc->card) {
1374 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1375 tuning_opcode =
1376 mmc->card->type == MMC_TYPE_MMC ?
1377 MMC_SEND_TUNING_BLOCK_HS200 :
1378 MMC_SEND_TUNING_BLOCK;
Chuansheng Liu63c21182013-11-05 14:52:45 +08001379
1380 /* Here we need to set the host->mrq to NULL,
1381 * in case the pending finish_tasklet
1382 * finishes it incorrectly.
1383 */
1384 host->mrq = NULL;
1385
Chris Ball14efd952012-11-05 14:29:49 -05001386 spin_unlock_irqrestore(&host->lock, flags);
1387 sdhci_execute_tuning(mmc, tuning_opcode);
1388 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301389
Chris Ball14efd952012-11-05 14:29:49 -05001390 /* Restore original mmc_request structure */
1391 host->mrq = mrq;
1392 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301393 }
1394
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001395 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001396 sdhci_send_command(host, mrq->sbc);
1397 else
1398 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301399 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001400
Pierre Ossman5f25a662006-10-04 02:15:39 -07001401 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001402 spin_unlock_irqrestore(&host->lock, flags);
1403}
1404
Russell King2317f562014-04-25 12:57:07 +01001405void sdhci_set_bus_width(struct sdhci_host *host, int width)
1406{
1407 u8 ctrl;
1408
1409 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1410 if (width == MMC_BUS_WIDTH_8) {
1411 ctrl &= ~SDHCI_CTRL_4BITBUS;
1412 if (host->version >= SDHCI_SPEC_300)
1413 ctrl |= SDHCI_CTRL_8BITBUS;
1414 } else {
1415 if (host->version >= SDHCI_SPEC_300)
1416 ctrl &= ~SDHCI_CTRL_8BITBUS;
1417 if (width == MMC_BUS_WIDTH_4)
1418 ctrl |= SDHCI_CTRL_4BITBUS;
1419 else
1420 ctrl &= ~SDHCI_CTRL_4BITBUS;
1421 }
1422 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1423}
1424EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1425
Russell King96d7b782014-04-25 12:59:26 +01001426void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1427{
1428 u16 ctrl_2;
1429
1430 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1431 /* Select Bus Speed Mode for host */
1432 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1433 if ((timing == MMC_TIMING_MMC_HS200) ||
1434 (timing == MMC_TIMING_UHS_SDR104))
1435 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1436 else if (timing == MMC_TIMING_UHS_SDR12)
1437 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1438 else if (timing == MMC_TIMING_UHS_SDR25)
1439 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1440 else if (timing == MMC_TIMING_UHS_SDR50)
1441 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1442 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1443 (timing == MMC_TIMING_MMC_DDR52))
1444 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1445 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1446}
1447EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1448
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001449static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001450{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001451 unsigned long flags;
1452 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001453 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001454
Pierre Ossmand129bce2006-03-24 03:18:17 -08001455 spin_lock_irqsave(&host->lock, flags);
1456
Adrian Hunterceb61432011-12-27 15:48:41 +02001457 if (host->flags & SDHCI_DEVICE_DEAD) {
1458 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001459 if (!IS_ERR(mmc->supply.vmmc) &&
1460 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001461 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001462 return;
1463 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001464
Pierre Ossmand129bce2006-03-24 03:18:17 -08001465 /*
1466 * Reset the chip on each power off.
1467 * Should clear out any weird states.
1468 */
1469 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001470 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001471 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001472 }
1473
Kevin Liu52983382013-01-31 11:31:37 +08001474 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001475 (ios->power_mode == MMC_POWER_UP) &&
1476 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001477 sdhci_enable_preset_value(host, false);
1478
Russell King373073e2014-04-25 12:58:45 +01001479 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001480 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001481 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001482
1483 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1484 host->clock) {
1485 host->timeout_clk = host->mmc->actual_clock ?
1486 host->mmc->actual_clock / 1000 :
1487 host->clock / 1000;
1488 host->mmc->max_busy_timeout =
1489 host->ops->get_max_timeout_count ?
1490 host->ops->get_max_timeout_count(host) :
1491 1 << 27;
1492 host->mmc->max_busy_timeout /= host->timeout_clk;
1493 }
Russell King373073e2014-04-25 12:58:45 +01001494 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001495
Russell King24fbb3c2014-04-25 13:00:06 +01001496 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001497
Philip Rakity643a81f2010-09-23 08:24:32 -07001498 if (host->ops->platform_send_init_74_clocks)
1499 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1500
Russell King2317f562014-04-25 12:57:07 +01001501 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001502
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001503 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001504
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001505 if ((ios->timing == MMC_TIMING_SD_HS ||
1506 ios->timing == MMC_TIMING_MMC_HS)
1507 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001508 ctrl |= SDHCI_CTRL_HISPD;
1509 else
1510 ctrl &= ~SDHCI_CTRL_HISPD;
1511
Arindam Nathd6d50a12011-05-05 12:18:59 +05301512 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301513 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301514
1515 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301516 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001517 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301518 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301519 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1520 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001521 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301522 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301523
Russell Kingda91a8f2014-04-25 13:00:12 +01001524 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301525 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301526 /*
1527 * We only need to set Driver Strength if the
1528 * preset value enable is not set.
1529 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001530 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301531 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1532 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1533 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1534 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1535 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1536
1537 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301538 } else {
1539 /*
1540 * According to SDHC Spec v3.00, if the Preset Value
1541 * Enable in the Host Control 2 register is set, we
1542 * need to reset SD Clock Enable before changing High
1543 * Speed Enable to avoid generating clock gliches.
1544 */
Arindam Nath758535c2011-05-05 12:19:00 +05301545
1546 /* Reset SD Clock Enable */
1547 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1548 clk &= ~SDHCI_CLOCK_CARD_EN;
1549 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1550
1551 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1552
1553 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001554 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301555 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301556
Arindam Nath49c468f2011-05-05 12:19:01 +05301557 /* Reset SD Clock Enable */
1558 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1559 clk &= ~SDHCI_CLOCK_CARD_EN;
1560 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1561
Russell King96d7b782014-04-25 12:59:26 +01001562 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001563 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301564
Kevin Liu52983382013-01-31 11:31:37 +08001565 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1566 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1567 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1568 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1569 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1570 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1571 u16 preset;
1572
1573 sdhci_enable_preset_value(host, true);
1574 preset = sdhci_get_preset_value(host);
1575 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1576 >> SDHCI_PRESET_DRV_SHIFT;
1577 }
1578
Arindam Nath49c468f2011-05-05 12:19:01 +05301579 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001580 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301581 } else
1582 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301583
Leandro Dorileob8352262007-07-25 23:47:04 +02001584 /*
1585 * Some (ENE) controllers go apeshit on some ios operation,
1586 * signalling timeout and CRC errors even on CMD0. Resetting
1587 * it on each ios seems to solve the problem.
1588 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001589 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001590 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001591
Pierre Ossman5f25a662006-10-04 02:15:39 -07001592 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001593 spin_unlock_irqrestore(&host->lock, flags);
1594}
1595
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001596static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1597{
1598 struct sdhci_host *host = mmc_priv(mmc);
1599
1600 sdhci_runtime_pm_get(host);
1601 sdhci_do_set_ios(host, ios);
1602 sdhci_runtime_pm_put(host);
1603}
1604
Kevin Liu94144a42013-02-28 17:35:53 +08001605static int sdhci_do_get_cd(struct sdhci_host *host)
1606{
1607 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1608
1609 if (host->flags & SDHCI_DEVICE_DEAD)
1610 return 0;
1611
1612 /* If polling/nonremovable, assume that the card is always present. */
1613 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1614 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1615 return 1;
1616
1617 /* Try slot gpio detect */
1618 if (!IS_ERR_VALUE(gpio_cd))
1619 return !!gpio_cd;
1620
1621 /* Host native card detect */
1622 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1623}
1624
1625static int sdhci_get_cd(struct mmc_host *mmc)
1626{
1627 struct sdhci_host *host = mmc_priv(mmc);
1628 int ret;
1629
1630 sdhci_runtime_pm_get(host);
1631 ret = sdhci_do_get_cd(host);
1632 sdhci_runtime_pm_put(host);
1633 return ret;
1634}
1635
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001636static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001637{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001638 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001639 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001640
Pierre Ossmand129bce2006-03-24 03:18:17 -08001641 spin_lock_irqsave(&host->lock, flags);
1642
Pierre Ossman1e728592008-04-16 19:13:13 +02001643 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001644 is_readonly = 0;
1645 else if (host->ops->get_ro)
1646 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001647 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001648 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1649 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001650
1651 spin_unlock_irqrestore(&host->lock, flags);
1652
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001653 /* This quirk needs to be replaced by a callback-function later */
1654 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1655 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001656}
1657
Takashi Iwai82b0e232011-04-21 20:26:38 +02001658#define SAMPLE_COUNT 5
1659
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001660static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001661{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001662 int i, ro_count;
1663
Takashi Iwai82b0e232011-04-21 20:26:38 +02001664 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001665 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001666
1667 ro_count = 0;
1668 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001669 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001670 if (++ro_count > SAMPLE_COUNT / 2)
1671 return 1;
1672 }
1673 msleep(30);
1674 }
1675 return 0;
1676}
1677
Adrian Hunter20758b62011-08-29 16:42:12 +03001678static void sdhci_hw_reset(struct mmc_host *mmc)
1679{
1680 struct sdhci_host *host = mmc_priv(mmc);
1681
1682 if (host->ops && host->ops->hw_reset)
1683 host->ops->hw_reset(host);
1684}
1685
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001686static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001687{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001688 struct sdhci_host *host = mmc_priv(mmc);
1689 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001690
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001691 sdhci_runtime_pm_get(host);
1692 ret = sdhci_do_get_ro(host);
1693 sdhci_runtime_pm_put(host);
1694 return ret;
1695}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001696
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001697static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1698{
Russell Kingbe138552014-04-25 12:55:56 +01001699 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001700 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001701 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001702 else
Russell Kingb537f942014-04-25 12:56:01 +01001703 host->ier &= ~SDHCI_INT_CARD_INT;
1704
1705 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1706 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001707 mmiowb();
1708 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001709}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001710
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001711static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1712{
1713 struct sdhci_host *host = mmc_priv(mmc);
1714 unsigned long flags;
1715
Russell Kingef104332014-04-25 12:55:41 +01001716 sdhci_runtime_pm_get(host);
1717
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001718 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001719 if (enable)
1720 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1721 else
1722 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1723
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001724 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001725 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001726
1727 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001728}
1729
Philip Rakity6231f3d2012-07-23 15:56:23 -07001730static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001731 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001732{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001733 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001734 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001735 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001736
1737 /*
1738 * Signal Voltage Switching is only applicable for Host Controllers
1739 * v3.00 and above.
1740 */
1741 if (host->version < SDHCI_SPEC_300)
1742 return 0;
1743
Philip Rakity6231f3d2012-07-23 15:56:23 -07001744 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001745
Fabio Estevam21f59982013-02-14 10:35:03 -02001746 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001747 case MMC_SIGNAL_VOLTAGE_330:
1748 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1749 ctrl &= ~SDHCI_CTRL_VDD_180;
1750 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1751
Tim Kryger3a48edc2014-06-13 10:13:56 -07001752 if (!IS_ERR(mmc->supply.vqmmc)) {
1753 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1754 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001755 if (ret) {
1756 pr_warning("%s: Switching to 3.3V signalling voltage "
Markus Mayer4e743f12014-07-03 13:27:42 -07001757 " failed\n", mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001758 return -EIO;
1759 }
1760 }
1761 /* Wait for 5ms */
1762 usleep_range(5000, 5500);
1763
1764 /* 3.3V regulator output should be stable within 5 ms */
1765 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1766 if (!(ctrl & SDHCI_CTRL_VDD_180))
1767 return 0;
1768
1769 pr_warning("%s: 3.3V regulator output did not became stable\n",
Markus Mayer4e743f12014-07-03 13:27:42 -07001770 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001771
1772 return -EAGAIN;
1773 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001774 if (!IS_ERR(mmc->supply.vqmmc)) {
1775 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001776 1700000, 1950000);
1777 if (ret) {
1778 pr_warning("%s: Switching to 1.8V signalling voltage "
Markus Mayer4e743f12014-07-03 13:27:42 -07001779 " failed\n", mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001780 return -EIO;
1781 }
1782 }
1783
1784 /*
1785 * Enable 1.8V Signal Enable in the Host Control2
1786 * register
1787 */
1788 ctrl |= SDHCI_CTRL_VDD_180;
1789 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1790
Kevin Liu20b92a32012-12-17 19:29:26 +08001791 /* 1.8V regulator output should be stable within 5 ms */
1792 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1793 if (ctrl & SDHCI_CTRL_VDD_180)
1794 return 0;
1795
1796 pr_warning("%s: 1.8V regulator output did not became stable\n",
Markus Mayer4e743f12014-07-03 13:27:42 -07001797 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001798
1799 return -EAGAIN;
1800 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001801 if (!IS_ERR(mmc->supply.vqmmc)) {
1802 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1803 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001804 if (ret) {
1805 pr_warning("%s: Switching to 1.2V signalling voltage "
Markus Mayer4e743f12014-07-03 13:27:42 -07001806 " failed\n", mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001807 return -EIO;
1808 }
1809 }
1810 return 0;
1811 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301812 /* No signal voltage switch required */
1813 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001814 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301815}
1816
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001817static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001818 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001819{
1820 struct sdhci_host *host = mmc_priv(mmc);
1821 int err;
1822
1823 if (host->version < SDHCI_SPEC_300)
1824 return 0;
1825 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001826 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001827 sdhci_runtime_pm_put(host);
1828 return err;
1829}
1830
Kevin Liu20b92a32012-12-17 19:29:26 +08001831static int sdhci_card_busy(struct mmc_host *mmc)
1832{
1833 struct sdhci_host *host = mmc_priv(mmc);
1834 u32 present_state;
1835
1836 sdhci_runtime_pm_get(host);
1837 /* Check whether DAT[3:0] is 0000 */
1838 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1839 sdhci_runtime_pm_put(host);
1840
1841 return !(present_state & SDHCI_DATA_LVL_MASK);
1842}
1843
Girish K S069c9f12012-01-06 09:56:39 +05301844static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301845{
Russell King4b6f37d2014-04-25 12:59:36 +01001846 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301847 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301848 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301849 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001850 unsigned long flags;
Arindam Nathb513ea22011-05-05 12:19:04 +05301851
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001852 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001853 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301854
Arindam Nathb513ea22011-05-05 12:19:04 +05301855 /*
Girish K S069c9f12012-01-06 09:56:39 +05301856 * The Host Controller needs tuning only in case of SDR104 mode
1857 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301858 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301859 * If the Host Controller supports the HS200 mode then the
1860 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301861 */
Russell King4b6f37d2014-04-25 12:59:36 +01001862 switch (host->timing) {
1863 case MMC_TIMING_MMC_HS200:
1864 case MMC_TIMING_UHS_SDR104:
1865 break;
Girish K S069c9f12012-01-06 09:56:39 +05301866
Russell King4b6f37d2014-04-25 12:59:36 +01001867 case MMC_TIMING_UHS_SDR50:
1868 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1869 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1870 break;
1871 /* FALLTHROUGH */
1872
1873 default:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001874 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001875 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301876 return 0;
1877 }
1878
Dong Aisheng45251812013-09-13 19:11:30 +08001879 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001880 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001881 err = host->ops->platform_execute_tuning(host, opcode);
1882 sdhci_runtime_pm_put(host);
1883 return err;
1884 }
1885
Russell King4b6f37d2014-04-25 12:59:36 +01001886 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1887 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Arindam Nathb513ea22011-05-05 12:19:04 +05301888 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1889
1890 /*
1891 * As per the Host Controller spec v3.00, tuning command
1892 * generates Buffer Read Ready interrupt, so enable that.
1893 *
1894 * Note: The spec clearly says that when tuning sequence
1895 * is being performed, the controller does not generate
1896 * interrupts other than Buffer Read Ready interrupt. But
1897 * to make sure we don't hit a controller bug, we _only_
1898 * enable Buffer Read Ready interrupt here.
1899 */
Russell Kingb537f942014-04-25 12:56:01 +01001900 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1901 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301902
1903 /*
1904 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1905 * of loops reaches 40 times or a timeout of 150ms occurs.
1906 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301907 do {
1908 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001909 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301910
Girish K S069c9f12012-01-06 09:56:39 +05301911 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301912 cmd.arg = 0;
1913 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1914 cmd.retries = 0;
1915 cmd.data = NULL;
1916 cmd.error = 0;
1917
Al Cooper7ce45e92014-05-09 11:34:07 -04001918 if (tuning_loop_counter-- == 0)
1919 break;
1920
Arindam Nathb513ea22011-05-05 12:19:04 +05301921 mrq.cmd = &cmd;
1922 host->mrq = &mrq;
1923
1924 /*
1925 * In response to CMD19, the card sends 64 bytes of tuning
1926 * block to the Host Controller. So we set the block size
1927 * to 64 here.
1928 */
Girish K S069c9f12012-01-06 09:56:39 +05301929 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1930 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1931 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1932 SDHCI_BLOCK_SIZE);
1933 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1934 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1935 SDHCI_BLOCK_SIZE);
1936 } else {
1937 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1938 SDHCI_BLOCK_SIZE);
1939 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301940
1941 /*
1942 * The tuning block is sent by the card to the host controller.
1943 * So we set the TRNS_READ bit in the Transfer Mode register.
1944 * This also takes care of setting DMA Enable and Multi Block
1945 * Select in the same register to 0.
1946 */
1947 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1948
1949 sdhci_send_command(host, &cmd);
1950
1951 host->cmd = NULL;
1952 host->mrq = NULL;
1953
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001954 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301955 /* Wait for Buffer Read Ready interrupt */
1956 wait_event_interruptible_timeout(host->buf_ready_int,
1957 (host->tuning_done == 1),
1958 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001959 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301960
1961 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301962 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301963 "Buffer Read Ready interrupt during tuning "
1964 "procedure, falling back to fixed sampling "
1965 "clock\n");
1966 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1967 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1968 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1969 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1970
1971 err = -EIO;
1972 goto out;
1973 }
1974
1975 host->tuning_done = 0;
1976
1977 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07001978
1979 /* eMMC spec does not require a delay between tuning cycles */
1980 if (opcode == MMC_SEND_TUNING_BLOCK)
1981 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05301982 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1983
1984 /*
1985 * The Host Driver has exhausted the maximum number of loops allowed,
1986 * so use fixed sampling frequency.
1987 */
Al Cooper7ce45e92014-05-09 11:34:07 -04001988 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05301989 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1990 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04001991 }
1992 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
1993 pr_info(DRIVER_NAME ": Tuning procedure"
1994 " failed, falling back to fixed sampling"
1995 " clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08001996 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05301997 }
1998
1999out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302000 /*
2001 * If this is the very first time we are here, we start the retuning
2002 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2003 * flag won't be set, we check this condition before actually starting
2004 * the timer.
2005 */
2006 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2007 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
Aaron Lu973905f2012-07-04 13:29:09 +08002008 host->flags |= SDHCI_USING_RETUNING_TIMER;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302009 mod_timer(&host->tuning_timer, jiffies +
2010 host->tuning_count * HZ);
2011 /* Tuning mode 1 limits the maximum data length to 4MB */
2012 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
Arend van Spriel2bc02482014-01-04 13:51:26 +01002013 } else if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302014 host->flags &= ~SDHCI_NEEDS_RETUNING;
2015 /* Reload the new initial value for timer */
Arend van Spriel2bc02482014-01-04 13:51:26 +01002016 mod_timer(&host->tuning_timer, jiffies +
2017 host->tuning_count * HZ);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302018 }
2019
2020 /*
2021 * In case tuning fails, host controllers which support re-tuning can
2022 * try tuning again at a later time, when the re-tuning timer expires.
2023 * So for these controllers, we return 0. Since there might be other
2024 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08002025 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2026 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302027 */
Aaron Lu973905f2012-07-04 13:29:09 +08002028 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302029 err = 0;
2030
Russell Kingb537f942014-04-25 12:56:01 +01002031 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2032 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002033 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002034 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302035
2036 return err;
2037}
2038
Kevin Liu52983382013-01-31 11:31:37 +08002039
2040static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302041{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302042 /* Host Controller v3.00 defines preset value registers */
2043 if (host->version < SDHCI_SPEC_300)
2044 return;
2045
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302046 /*
2047 * We only enable or disable Preset Value if they are not already
2048 * enabled or disabled respectively. Otherwise, we bail out.
2049 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002050 if (host->preset_enabled != enable) {
2051 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2052
2053 if (enable)
2054 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2055 else
2056 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2057
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302058 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002059
2060 if (enable)
2061 host->flags |= SDHCI_PV_ENABLED;
2062 else
2063 host->flags &= ~SDHCI_PV_ENABLED;
2064
2065 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302066 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002067}
2068
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002069static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002070{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002071 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002072 unsigned long flags;
2073
Christian Daudt722e1282013-06-20 14:26:36 -07002074 /* First check if client has provided their own card event */
2075 if (host->ops->card_event)
2076 host->ops->card_event(host);
2077
Pierre Ossmand129bce2006-03-24 03:18:17 -08002078 spin_lock_irqsave(&host->lock, flags);
2079
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002080 /* Check host->mrq first in case we are runtime suspended */
Shawn Guo9668d762013-06-09 19:49:24 +08002081 if (host->mrq && !sdhci_do_get_cd(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302082 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002083 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302084 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002085 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002086
Russell King03231f92014-04-25 12:57:12 +01002087 sdhci_do_reset(host, SDHCI_RESET_CMD);
2088 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002089
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002090 host->mrq->cmd->error = -ENOMEDIUM;
2091 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002092 }
2093
2094 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002095}
2096
2097static const struct mmc_host_ops sdhci_ops = {
2098 .request = sdhci_request,
2099 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002100 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002101 .get_ro = sdhci_get_ro,
2102 .hw_reset = sdhci_hw_reset,
2103 .enable_sdio_irq = sdhci_enable_sdio_irq,
2104 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2105 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002106 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002107 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002108};
2109
2110/*****************************************************************************\
2111 * *
2112 * Tasklets *
2113 * *
2114\*****************************************************************************/
2115
Pierre Ossmand129bce2006-03-24 03:18:17 -08002116static void sdhci_tasklet_finish(unsigned long param)
2117{
2118 struct sdhci_host *host;
2119 unsigned long flags;
2120 struct mmc_request *mrq;
2121
2122 host = (struct sdhci_host*)param;
2123
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002124 spin_lock_irqsave(&host->lock, flags);
2125
Chris Ball0c9c99a2011-04-27 17:35:31 -04002126 /*
2127 * If this tasklet gets rescheduled while running, it will
2128 * be run again afterwards but without any active request.
2129 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002130 if (!host->mrq) {
2131 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002132 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002133 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002134
2135 del_timer(&host->timer);
2136
2137 mrq = host->mrq;
2138
Pierre Ossmand129bce2006-03-24 03:18:17 -08002139 /*
2140 * The controller needs a reset of internal state machines
2141 * upon error conditions.
2142 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002143 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002144 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002145 (mrq->data && (mrq->data->error ||
2146 (mrq->data->stop && mrq->data->stop->error))) ||
2147 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002148
2149 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002150 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002151 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002152 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002153
2154 /* Spec says we should do both at the same time, but Ricoh
2155 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002156 sdhci_do_reset(host, SDHCI_RESET_CMD);
2157 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002158 }
2159
2160 host->mrq = NULL;
2161 host->cmd = NULL;
2162 host->data = NULL;
2163
Pierre Ossmanf9134312008-12-21 17:01:48 +01002164#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002165 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002166#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002167
Pierre Ossman5f25a662006-10-04 02:15:39 -07002168 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002169 spin_unlock_irqrestore(&host->lock, flags);
2170
2171 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002172 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002173}
2174
2175static void sdhci_timeout_timer(unsigned long data)
2176{
2177 struct sdhci_host *host;
2178 unsigned long flags;
2179
2180 host = (struct sdhci_host*)data;
2181
2182 spin_lock_irqsave(&host->lock, flags);
2183
2184 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302185 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002186 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002187 sdhci_dumpregs(host);
2188
2189 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002190 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002191 sdhci_finish_data(host);
2192 } else {
2193 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002194 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002195 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002196 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002197
2198 tasklet_schedule(&host->finish_tasklet);
2199 }
2200 }
2201
Pierre Ossman5f25a662006-10-04 02:15:39 -07002202 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002203 spin_unlock_irqrestore(&host->lock, flags);
2204}
2205
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302206static void sdhci_tuning_timer(unsigned long data)
2207{
2208 struct sdhci_host *host;
2209 unsigned long flags;
2210
2211 host = (struct sdhci_host *)data;
2212
2213 spin_lock_irqsave(&host->lock, flags);
2214
2215 host->flags |= SDHCI_NEEDS_RETUNING;
2216
2217 spin_unlock_irqrestore(&host->lock, flags);
2218}
2219
Pierre Ossmand129bce2006-03-24 03:18:17 -08002220/*****************************************************************************\
2221 * *
2222 * Interrupt handling *
2223 * *
2224\*****************************************************************************/
2225
2226static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2227{
2228 BUG_ON(intmask == 0);
2229
2230 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302231 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002232 "though no command operation was in progress.\n",
2233 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002234 sdhci_dumpregs(host);
2235 return;
2236 }
2237
Pierre Ossman43b58b32007-07-25 23:15:27 +02002238 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002239 host->cmd->error = -ETIMEDOUT;
2240 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2241 SDHCI_INT_INDEX))
2242 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002243
Pierre Ossmane8095172008-07-25 01:09:08 +02002244 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002245 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002246 return;
2247 }
2248
2249 /*
2250 * The host can send and interrupt when the busy state has
2251 * ended, allowing us to wait without wasting CPU cycles.
2252 * Unfortunately this is overloaded on the "data complete"
2253 * interrupt, so we need to take some care when handling
2254 * it.
2255 *
2256 * Note: The 1.0 specification is a bit ambiguous about this
2257 * feature so there might be some problems with older
2258 * controllers.
2259 */
2260 if (host->cmd->flags & MMC_RSP_BUSY) {
2261 if (host->cmd->data)
2262 DBG("Cannot wait for busy signal when also "
2263 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002264 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002265 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002266
2267 /* The controller does not support the end-of-busy IRQ,
2268 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002269 }
2270
2271 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002272 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002273}
2274
George G. Davis0957c332010-02-18 12:32:12 -05002275#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002276static void sdhci_show_adma_error(struct sdhci_host *host)
2277{
2278 const char *name = mmc_hostname(host->mmc);
2279 u8 *desc = host->adma_desc;
2280 __le32 *dma;
2281 __le16 *len;
2282 u8 attr;
2283
2284 sdhci_dumpregs(host);
2285
2286 while (true) {
2287 dma = (__le32 *)(desc + 4);
2288 len = (__le16 *)(desc + 2);
2289 attr = *desc;
2290
2291 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2292 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2293
2294 desc += 8;
2295
2296 if (attr & 2)
2297 break;
2298 }
2299}
2300#else
2301static void sdhci_show_adma_error(struct sdhci_host *host) { }
2302#endif
2303
Pierre Ossmand129bce2006-03-24 03:18:17 -08002304static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2305{
Girish K S069c9f12012-01-06 09:56:39 +05302306 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002307 BUG_ON(intmask == 0);
2308
Arindam Nathb513ea22011-05-05 12:19:04 +05302309 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2310 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302311 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2312 if (command == MMC_SEND_TUNING_BLOCK ||
2313 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302314 host->tuning_done = 1;
2315 wake_up(&host->buf_ready_int);
2316 return;
2317 }
2318 }
2319
Pierre Ossmand129bce2006-03-24 03:18:17 -08002320 if (!host->data) {
2321 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002322 * The "data complete" interrupt is also used to
2323 * indicate that a busy state has ended. See comment
2324 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002325 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002326 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002327 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2328 host->cmd->error = -ETIMEDOUT;
2329 tasklet_schedule(&host->finish_tasklet);
2330 return;
2331 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002332 if (intmask & SDHCI_INT_DATA_END) {
2333 sdhci_finish_command(host);
2334 return;
2335 }
2336 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002337
Girish K Sa3c76eb2011-10-11 11:44:09 +05302338 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002339 "though no data operation was in progress.\n",
2340 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002341 sdhci_dumpregs(host);
2342
2343 return;
2344 }
2345
2346 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002347 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002348 else if (intmask & SDHCI_INT_DATA_END_BIT)
2349 host->data->error = -EILSEQ;
2350 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2351 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2352 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002353 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002354 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302355 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002356 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002357 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002358 if (host->ops->adma_workaround)
2359 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002360 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002361
Pierre Ossman17b04292007-07-22 22:18:46 +02002362 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002363 sdhci_finish_data(host);
2364 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002365 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002366 sdhci_transfer_pio(host);
2367
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002368 /*
2369 * We currently don't do anything fancy with DMA
2370 * boundaries, but as we can't disable the feature
2371 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002372 *
2373 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2374 * should return a valid address to continue from, but as
2375 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002376 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002377 if (intmask & SDHCI_INT_DMA_END) {
2378 u32 dmastart, dmanow;
2379 dmastart = sg_dma_address(host->data->sg);
2380 dmanow = dmastart + host->data->bytes_xfered;
2381 /*
2382 * Force update to the next DMA block boundary.
2383 */
2384 dmanow = (dmanow &
2385 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2386 SDHCI_DEFAULT_BOUNDARY_SIZE;
2387 host->data->bytes_xfered = dmanow - dmastart;
2388 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2389 " next 0x%08x\n",
2390 mmc_hostname(host->mmc), dmastart,
2391 host->data->bytes_xfered, dmanow);
2392 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2393 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002394
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002395 if (intmask & SDHCI_INT_DATA_END) {
2396 if (host->cmd) {
2397 /*
2398 * Data managed to finish before the
2399 * command completed. Make sure we do
2400 * things in the proper order.
2401 */
2402 host->data_early = 1;
2403 } else {
2404 sdhci_finish_data(host);
2405 }
2406 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002407 }
2408}
2409
David Howells7d12e782006-10-05 14:55:46 +01002410static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002411{
Russell King781e9892014-04-25 12:55:46 +01002412 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002413 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002414 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002415 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002416
2417 spin_lock(&host->lock);
2418
Russell Kingbe138552014-04-25 12:55:56 +01002419 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002420 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002421 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002422 }
2423
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002424 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002425 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002426 result = IRQ_NONE;
2427 goto out;
2428 }
2429
Russell King41005002014-04-25 12:55:36 +01002430 do {
2431 /* Clear selected interrupts. */
2432 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2433 SDHCI_INT_BUS_POWER);
2434 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002435
Russell King41005002014-04-25 12:55:36 +01002436 DBG("*** %s got interrupt: 0x%08x\n",
2437 mmc_hostname(host->mmc), intmask);
2438
2439 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2440 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2441 SDHCI_CARD_PRESENT;
2442
2443 /*
2444 * There is a observation on i.mx esdhc. INSERT
2445 * bit will be immediately set again when it gets
2446 * cleared, if a card is inserted. We have to mask
2447 * the irq to prevent interrupt storm which will
2448 * freeze the system. And the REMOVE gets the
2449 * same situation.
2450 *
2451 * More testing are needed here to ensure it works
2452 * for other platforms though.
2453 */
Russell Kingb537f942014-04-25 12:56:01 +01002454 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2455 SDHCI_INT_CARD_REMOVE);
2456 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2457 SDHCI_INT_CARD_INSERT;
2458 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2459 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002460
2461 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2462 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002463
2464 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2465 SDHCI_INT_CARD_REMOVE);
2466 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002467 }
2468
2469 if (intmask & SDHCI_INT_CMD_MASK)
2470 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
2471
2472 if (intmask & SDHCI_INT_DATA_MASK)
2473 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2474
2475 if (intmask & SDHCI_INT_BUS_POWER)
2476 pr_err("%s: Card is consuming too much power!\n",
2477 mmc_hostname(host->mmc));
2478
Russell King781e9892014-04-25 12:55:46 +01002479 if (intmask & SDHCI_INT_CARD_INT) {
2480 sdhci_enable_sdio_irq_nolock(host, false);
2481 host->thread_isr |= SDHCI_INT_CARD_INT;
2482 result = IRQ_WAKE_THREAD;
2483 }
Russell King41005002014-04-25 12:55:36 +01002484
2485 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2486 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2487 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2488 SDHCI_INT_CARD_INT);
2489
2490 if (intmask) {
2491 unexpected |= intmask;
2492 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2493 }
2494
Russell King781e9892014-04-25 12:55:46 +01002495 if (result == IRQ_NONE)
2496 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002497
2498 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002499 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002500out:
2501 spin_unlock(&host->lock);
2502
Alexander Stein6379b232012-03-14 09:52:10 +01002503 if (unexpected) {
2504 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2505 mmc_hostname(host->mmc), unexpected);
2506 sdhci_dumpregs(host);
2507 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002508
Pierre Ossmand129bce2006-03-24 03:18:17 -08002509 return result;
2510}
2511
Russell King781e9892014-04-25 12:55:46 +01002512static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2513{
2514 struct sdhci_host *host = dev_id;
2515 unsigned long flags;
2516 u32 isr;
2517
2518 spin_lock_irqsave(&host->lock, flags);
2519 isr = host->thread_isr;
2520 host->thread_isr = 0;
2521 spin_unlock_irqrestore(&host->lock, flags);
2522
Russell King3560db82014-04-25 12:55:51 +01002523 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2524 sdhci_card_event(host->mmc);
2525 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2526 }
2527
Russell King781e9892014-04-25 12:55:46 +01002528 if (isr & SDHCI_INT_CARD_INT) {
2529 sdio_run_irqs(host->mmc);
2530
2531 spin_lock_irqsave(&host->lock, flags);
2532 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2533 sdhci_enable_sdio_irq_nolock(host, true);
2534 spin_unlock_irqrestore(&host->lock, flags);
2535 }
2536
2537 return isr ? IRQ_HANDLED : IRQ_NONE;
2538}
2539
Pierre Ossmand129bce2006-03-24 03:18:17 -08002540/*****************************************************************************\
2541 * *
2542 * Suspend/resume *
2543 * *
2544\*****************************************************************************/
2545
2546#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002547void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2548{
2549 u8 val;
2550 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2551 | SDHCI_WAKE_ON_INT;
2552
2553 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2554 val |= mask ;
2555 /* Avoid fake wake up */
2556 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2557 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2558 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2559}
2560EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2561
Fabio Estevam0b10f472014-08-30 14:53:13 -03002562static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002563{
2564 u8 val;
2565 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2566 | SDHCI_WAKE_ON_INT;
2567
2568 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2569 val &= ~mask;
2570 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2571}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002572
Manuel Lauss29495aa2011-11-03 11:09:45 +01002573int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002574{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002575 sdhci_disable_card_detection(host);
2576
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302577 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002578 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002579 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302580 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302581 }
2582
Kevin Liuad080d72013-01-05 17:21:33 +08002583 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002584 host->ier = 0;
2585 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2586 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002587 free_irq(host->irq, host);
2588 } else {
2589 sdhci_enable_irq_wakeups(host);
2590 enable_irq_wake(host->irq);
2591 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002592 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002593}
2594
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002595EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002596
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002597int sdhci_resume_host(struct sdhci_host *host)
2598{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002599 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002600
Richard Röjforsa13abc72009-09-22 16:45:30 -07002601 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002602 if (host->ops->enable_dma)
2603 host->ops->enable_dma(host);
2604 }
2605
Kevin Liuad080d72013-01-05 17:21:33 +08002606 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell King781e9892014-04-25 12:55:46 +01002607 ret = request_threaded_irq(host->irq, sdhci_irq,
2608 sdhci_thread_irq, IRQF_SHARED,
2609 mmc_hostname(host->mmc), host);
Kevin Liuad080d72013-01-05 17:21:33 +08002610 if (ret)
2611 return ret;
2612 } else {
2613 sdhci_disable_irq_wakeups(host);
2614 disable_irq_wake(host->irq);
2615 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002616
Adrian Hunter6308d292012-02-07 14:48:54 +02002617 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2618 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2619 /* Card keeps power but host controller does not */
2620 sdhci_init(host, 0);
2621 host->pwr = 0;
2622 host->clock = 0;
2623 sdhci_do_set_ios(host, &host->mmc->ios);
2624 } else {
2625 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2626 mmiowb();
2627 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002628
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002629 sdhci_enable_card_detection(host);
2630
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302631 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002632 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302633 host->flags |= SDHCI_NEEDS_RETUNING;
2634
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002635 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002636}
2637
2638EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002639#endif /* CONFIG_PM */
2640
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002641#ifdef CONFIG_PM_RUNTIME
2642
2643static int sdhci_runtime_pm_get(struct sdhci_host *host)
2644{
2645 return pm_runtime_get_sync(host->mmc->parent);
2646}
2647
2648static int sdhci_runtime_pm_put(struct sdhci_host *host)
2649{
2650 pm_runtime_mark_last_busy(host->mmc->parent);
2651 return pm_runtime_put_autosuspend(host->mmc->parent);
2652}
2653
Adrian Hunterf0710a52013-05-06 12:17:32 +03002654static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2655{
2656 if (host->runtime_suspended || host->bus_on)
2657 return;
2658 host->bus_on = true;
2659 pm_runtime_get_noresume(host->mmc->parent);
2660}
2661
2662static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2663{
2664 if (host->runtime_suspended || !host->bus_on)
2665 return;
2666 host->bus_on = false;
2667 pm_runtime_put_noidle(host->mmc->parent);
2668}
2669
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002670int sdhci_runtime_suspend_host(struct sdhci_host *host)
2671{
2672 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002673
2674 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002675 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002676 del_timer_sync(&host->tuning_timer);
2677 host->flags &= ~SDHCI_NEEDS_RETUNING;
2678 }
2679
2680 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002681 host->ier &= SDHCI_INT_CARD_INT;
2682 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2683 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002684 spin_unlock_irqrestore(&host->lock, flags);
2685
Russell King781e9892014-04-25 12:55:46 +01002686 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002687
2688 spin_lock_irqsave(&host->lock, flags);
2689 host->runtime_suspended = true;
2690 spin_unlock_irqrestore(&host->lock, flags);
2691
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002692 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002693}
2694EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2695
2696int sdhci_runtime_resume_host(struct sdhci_host *host)
2697{
2698 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002699 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002700
2701 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2702 if (host->ops->enable_dma)
2703 host->ops->enable_dma(host);
2704 }
2705
2706 sdhci_init(host, 0);
2707
2708 /* Force clock and power re-program */
2709 host->pwr = 0;
2710 host->clock = 0;
2711 sdhci_do_set_ios(host, &host->mmc->ios);
2712
2713 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002714 if ((host_flags & SDHCI_PV_ENABLED) &&
2715 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2716 spin_lock_irqsave(&host->lock, flags);
2717 sdhci_enable_preset_value(host, true);
2718 spin_unlock_irqrestore(&host->lock, flags);
2719 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002720
2721 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002722 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002723 host->flags |= SDHCI_NEEDS_RETUNING;
2724
2725 spin_lock_irqsave(&host->lock, flags);
2726
2727 host->runtime_suspended = false;
2728
2729 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002730 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002731 sdhci_enable_sdio_irq_nolock(host, true);
2732
2733 /* Enable Card Detection */
2734 sdhci_enable_card_detection(host);
2735
2736 spin_unlock_irqrestore(&host->lock, flags);
2737
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002738 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002739}
2740EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2741
2742#endif
2743
Pierre Ossmand129bce2006-03-24 03:18:17 -08002744/*****************************************************************************\
2745 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002746 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002747 * *
2748\*****************************************************************************/
2749
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002750struct sdhci_host *sdhci_alloc_host(struct device *dev,
2751 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002752{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002753 struct mmc_host *mmc;
2754 struct sdhci_host *host;
2755
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002756 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002757
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002758 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002759 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002760 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002761
2762 host = mmc_priv(mmc);
2763 host->mmc = mmc;
2764
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002765 return host;
2766}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002767
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002768EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002769
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002770int sdhci_add_host(struct sdhci_host *host)
2771{
2772 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002773 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302774 u32 max_current_caps;
2775 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002776 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002777
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002778 WARN_ON(host == NULL);
2779 if (host == NULL)
2780 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002781
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002782 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002783
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002784 if (debug_quirks)
2785 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002786 if (debug_quirks2)
2787 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002788
Russell King03231f92014-04-25 12:57:12 +01002789 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002790
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002791 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002792 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2793 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002794 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302795 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002796 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002797 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002798 }
2799
Arindam Nathf2119df2011-05-05 12:18:57 +05302800 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002801 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002802
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002803 if (host->version >= SDHCI_SPEC_300)
2804 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2805 host->caps1 :
2806 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302807
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002808 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002809 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302810 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002811 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002812 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002813 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002814
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002815 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002816 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002817 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002818 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002819 }
2820
Arindam Nathf2119df2011-05-05 12:18:57 +05302821 if ((host->version >= SDHCI_SPEC_200) &&
2822 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002823 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002824
2825 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2826 (host->flags & SDHCI_USE_ADMA)) {
2827 DBG("Disabling ADMA as it is marked broken\n");
2828 host->flags &= ~SDHCI_USE_ADMA;
2829 }
2830
Richard Röjforsa13abc72009-09-22 16:45:30 -07002831 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002832 if (host->ops->enable_dma) {
2833 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302834 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002835 "available. Falling back to PIO.\n",
2836 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002837 host->flags &=
2838 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002839 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002840 }
2841 }
2842
Pierre Ossman2134a922008-06-28 18:28:51 +02002843 if (host->flags & SDHCI_USE_ADMA) {
2844 /*
2845 * We need to allocate descriptors for all sg entries
2846 * (128) and potentially one alignment transfer for
2847 * each of those entries.
2848 */
Markus Mayer4e743f12014-07-03 13:27:42 -07002849 host->adma_desc = dma_alloc_coherent(mmc_dev(mmc),
Russell Kingd1e49f72014-04-25 12:58:34 +01002850 ADMA_SIZE, &host->adma_addr,
2851 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02002852 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2853 if (!host->adma_desc || !host->align_buffer) {
Markus Mayer4e743f12014-07-03 13:27:42 -07002854 dma_free_coherent(mmc_dev(mmc), ADMA_SIZE,
Russell Kingd1e49f72014-04-25 12:58:34 +01002855 host->adma_desc, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02002856 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302857 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002858 "buffers. Falling back to standard DMA.\n",
2859 mmc_hostname(mmc));
2860 host->flags &= ~SDHCI_USE_ADMA;
Russell Kingd1e49f72014-04-25 12:58:34 +01002861 host->adma_desc = NULL;
2862 host->align_buffer = NULL;
2863 } else if (host->adma_addr & 3) {
2864 pr_warning("%s: unable to allocate aligned ADMA descriptor\n",
2865 mmc_hostname(mmc));
2866 host->flags &= ~SDHCI_USE_ADMA;
Markus Mayer4e743f12014-07-03 13:27:42 -07002867 dma_free_coherent(mmc_dev(mmc), ADMA_SIZE,
Russell Kingd1e49f72014-04-25 12:58:34 +01002868 host->adma_desc, host->adma_addr);
2869 kfree(host->align_buffer);
2870 host->adma_desc = NULL;
2871 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02002872 }
2873 }
2874
Pierre Ossman76591502008-07-21 00:32:11 +02002875 /*
2876 * If we use DMA, then it's up to the caller to set the DMA
2877 * mask, but PIO does not need the hw shim so we set a new
2878 * mask here in that case.
2879 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002880 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002881 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07002882 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02002883 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002884
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002885 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302886 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002887 >> SDHCI_CLOCK_BASE_SHIFT;
2888 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302889 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002890 >> SDHCI_CLOCK_BASE_SHIFT;
2891
Pierre Ossmand129bce2006-03-24 03:18:17 -08002892 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002893 if (host->max_clk == 0 || host->quirks &
2894 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002895 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302896 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002897 "frequency.\n", mmc_hostname(mmc));
2898 return -ENODEV;
2899 }
2900 host->max_clk = host->ops->get_max_clock(host);
2901 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002902
2903 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302904 * In case of Host Controller v3.00, find out whether clock
2905 * multiplier is supported.
2906 */
2907 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2908 SDHCI_CLOCK_MUL_SHIFT;
2909
2910 /*
2911 * In case the value in Clock Multiplier is 0, then programmable
2912 * clock mode is not supported, otherwise the actual clock
2913 * multiplier is one more than the value of Clock Multiplier
2914 * in the Capabilities Register.
2915 */
2916 if (host->clk_mul)
2917 host->clk_mul += 1;
2918
2919 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002920 * Set host parameters.
2921 */
2922 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302923 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002924 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002925 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302926 else if (host->version >= SDHCI_SPEC_300) {
2927 if (host->clk_mul) {
2928 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2929 mmc->f_max = host->max_clk * host->clk_mul;
2930 } else
2931 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2932 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002933 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002934
Aisheng Dong28aab052014-08-27 15:26:31 +08002935 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2936 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
2937 SDHCI_TIMEOUT_CLK_SHIFT;
2938 if (host->timeout_clk == 0) {
2939 if (host->ops->get_timeout_clock) {
2940 host->timeout_clk =
2941 host->ops->get_timeout_clock(host);
2942 } else {
2943 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
2944 mmc_hostname(mmc));
2945 return -ENODEV;
2946 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03002947 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03002948
Aisheng Dong28aab052014-08-27 15:26:31 +08002949 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2950 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002951
Aisheng Dong28aab052014-08-27 15:26:31 +08002952 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08002953 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08002954 mmc->max_busy_timeout /= host->timeout_clk;
2955 }
Adrian Hunter58d12462011-06-28 17:16:03 +03002956
Andrei Warkentine89d4562011-05-23 15:06:37 -05002957 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01002958 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05002959
2960 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2961 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002962
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002963 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002964 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002965 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002966 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002967 host->flags |= SDHCI_AUTO_CMD23;
2968 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2969 } else {
2970 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2971 }
2972
Philip Rakity15ec4462010-11-19 16:48:39 -05002973 /*
2974 * A controller may support 8-bit width, but the board itself
2975 * might not have the pins brought out. Boards that support
2976 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2977 * their platform code before calling sdhci_add_host(), and we
2978 * won't assume 8-bit width for hosts without that CAP.
2979 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002980 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002981 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002982
Jerry Huang63ef5d82012-10-25 13:47:19 +08002983 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
2984 mmc->caps &= ~MMC_CAP_CMD23;
2985
Arindam Nathf2119df2011-05-05 12:18:57 +05302986 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002987 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002988
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002989 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Markus Mayer4e743f12014-07-03 13:27:42 -07002990 !(mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002991 mmc->caps |= MMC_CAP_NEEDS_POLL;
2992
Tim Kryger3a48edc2014-06-13 10:13:56 -07002993 /* If there are external regulators, get them */
2994 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
2995 return -EPROBE_DEFER;
2996
Philip Rakity6231f3d2012-07-23 15:56:23 -07002997 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07002998 if (!IS_ERR(mmc->supply.vqmmc)) {
2999 ret = regulator_enable(mmc->supply.vqmmc);
3000 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3001 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003002 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3003 SDHCI_SUPPORT_SDR50 |
3004 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003005 if (ret) {
3006 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3007 mmc_hostname(mmc), ret);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003008 mmc->supply.vqmmc = NULL;
Chris Balla3361ab2013-03-11 17:51:53 -04003009 }
Kevin Liu8363c372012-11-17 17:55:51 -05003010 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003011
Daniel Drake6a661802012-11-25 13:01:19 -05003012 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3013 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3014 SDHCI_SUPPORT_DDR50);
3015
Al Cooper4188bba2012-03-16 15:54:17 -04003016 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3017 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3018 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303019 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3020
3021 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003022 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303023 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003024 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3025 * field can be promoted to support HS200.
3026 */
David Cohen13868bf2013-10-29 10:58:26 -07003027 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
3028 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003029 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303030 mmc->caps |= MMC_CAP_UHS_SDR50;
3031
Micky Ching9107ebb2014-02-21 18:40:35 +08003032 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3033 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303034 mmc->caps |= MMC_CAP_UHS_DDR50;
3035
Girish K S069c9f12012-01-06 09:56:39 +05303036 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303037 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3038 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3039
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003040 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303041 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003042 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303043
Arindam Nathd6d50a12011-05-05 12:18:59 +05303044 /* Driver Type(s) (A, C, D) supported by the host */
3045 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3046 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3047 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3048 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3049 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3050 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3051
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303052 /* Initial value for re-tuning timer count */
3053 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3054 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3055
3056 /*
3057 * In case Re-tuning Timer is not disabled, the actual value of
3058 * re-tuning timer will be 2 ^ (n - 1).
3059 */
3060 if (host->tuning_count)
3061 host->tuning_count = 1 << (host->tuning_count - 1);
3062
3063 /* Re-tuning mode supported by the Host Controller */
3064 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3065 SDHCI_RETUNING_MODE_SHIFT;
3066
Takashi Iwai8f230f42010-12-08 10:04:30 +01003067 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003068
Arindam Nathf2119df2011-05-05 12:18:57 +05303069 /*
3070 * According to SD Host Controller spec v3.00, if the Host System
3071 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3072 * the value is meaningful only if Voltage Support in the Capabilities
3073 * register is set. The actual current value is 4 times the register
3074 * value.
3075 */
3076 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003077 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003078 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003079 if (curr > 0) {
3080
3081 /* convert to SDHCI_MAX_CURRENT format */
3082 curr = curr/1000; /* convert to mA */
3083 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3084
3085 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3086 max_current_caps =
3087 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3088 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3089 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3090 }
3091 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303092
3093 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003094 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303095
Aaron Lu55c46652012-07-04 13:31:48 +08003096 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303097 SDHCI_MAX_CURRENT_330_MASK) >>
3098 SDHCI_MAX_CURRENT_330_SHIFT) *
3099 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303100 }
3101 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003102 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303103
Aaron Lu55c46652012-07-04 13:31:48 +08003104 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303105 SDHCI_MAX_CURRENT_300_MASK) >>
3106 SDHCI_MAX_CURRENT_300_SHIFT) *
3107 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303108 }
3109 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003110 ocr_avail |= MMC_VDD_165_195;
3111
Aaron Lu55c46652012-07-04 13:31:48 +08003112 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303113 SDHCI_MAX_CURRENT_180_MASK) >>
3114 SDHCI_MAX_CURRENT_180_SHIFT) *
3115 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303116 }
3117
Tim Kryger52221612014-06-25 00:25:34 -07003118 /* If OCR set by external regulators, use it instead */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003119 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003120 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003121
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003122 if (host->ocr_mask)
Tim Kryger3a48edc2014-06-13 10:13:56 -07003123 ocr_avail &= host->ocr_mask;
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003124
Takashi Iwai8f230f42010-12-08 10:04:30 +01003125 mmc->ocr_avail = ocr_avail;
3126 mmc->ocr_avail_sdio = ocr_avail;
3127 if (host->ocr_avail_sdio)
3128 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3129 mmc->ocr_avail_sd = ocr_avail;
3130 if (host->ocr_avail_sd)
3131 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3132 else /* normal SD controllers don't support 1.8V */
3133 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3134 mmc->ocr_avail_mmc = ocr_avail;
3135 if (host->ocr_avail_mmc)
3136 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003137
3138 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303139 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003140 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003141 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003142 }
3143
Pierre Ossmand129bce2006-03-24 03:18:17 -08003144 spin_lock_init(&host->lock);
3145
3146 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003147 * Maximum number of segments. Depends on if the hardware
3148 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003149 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003150 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003151 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003152 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003153 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003154 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04003155 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003156
3157 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003158 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01003159 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08003160 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003161 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003162
3163 /*
3164 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003165 * of bytes. When doing hardware scatter/gather, each entry cannot
3166 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003167 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003168 if (host->flags & SDHCI_USE_ADMA) {
3169 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3170 mmc->max_seg_size = 65535;
3171 else
3172 mmc->max_seg_size = 65536;
3173 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003174 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003175 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003176
3177 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003178 * Maximum block size. This varies from controller to controller and
3179 * is specified in the capabilities register.
3180 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003181 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3182 mmc->max_blk_size = 2;
3183 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303184 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003185 SDHCI_MAX_BLOCK_SHIFT;
3186 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303187 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003188 "assuming 512 bytes\n", mmc_hostname(mmc));
3189 mmc->max_blk_size = 0;
3190 }
3191 }
3192
3193 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003194
3195 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003196 * Maximum block count.
3197 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003198 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003199
3200 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003201 * Init tasklets.
3202 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003203 tasklet_init(&host->finish_tasklet,
3204 sdhci_tasklet_finish, (unsigned long)host);
3205
Al Viroe4cad1b2006-10-10 22:47:07 +01003206 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003207
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303208 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303209 init_waitqueue_head(&host->buf_ready_int);
3210
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303211 /* Initialize re-tuning timer */
3212 init_timer(&host->tuning_timer);
3213 host->tuning_timer.data = (unsigned long)host;
3214 host->tuning_timer.function = sdhci_tuning_timer;
3215 }
3216
Shawn Guo2af502c2013-07-05 14:38:55 +08003217 sdhci_init(host, 0);
3218
Russell King781e9892014-04-25 12:55:46 +01003219 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3220 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003221 if (ret) {
3222 pr_err("%s: Failed to request IRQ %d: %d\n",
3223 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003224 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003225 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003226
Pierre Ossmand129bce2006-03-24 03:18:17 -08003227#ifdef CONFIG_MMC_DEBUG
3228 sdhci_dumpregs(host);
3229#endif
3230
Pierre Ossmanf9134312008-12-21 17:01:48 +01003231#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003232 snprintf(host->led_name, sizeof(host->led_name),
3233 "%s::", mmc_hostname(mmc));
3234 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003235 host->led.brightness = LED_OFF;
3236 host->led.default_trigger = mmc_hostname(mmc);
3237 host->led.brightness_set = sdhci_led_control;
3238
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003239 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003240 if (ret) {
3241 pr_err("%s: Failed to register LED device: %d\n",
3242 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003243 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003244 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003245#endif
3246
Pierre Ossman5f25a662006-10-04 02:15:39 -07003247 mmiowb();
3248
Pierre Ossmand129bce2006-03-24 03:18:17 -08003249 mmc_add_host(mmc);
3250
Girish K Sa3c76eb2011-10-11 11:44:09 +05303251 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003252 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003253 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3254 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003255
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003256 sdhci_enable_card_detection(host);
3257
Pierre Ossmand129bce2006-03-24 03:18:17 -08003258 return 0;
3259
Pierre Ossmanf9134312008-12-21 17:01:48 +01003260#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003261reset:
Russell King03231f92014-04-25 12:57:12 +01003262 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003263 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3264 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003265 free_irq(host->irq, host);
3266#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003267untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003268 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003269
3270 return ret;
3271}
3272
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003273EXPORT_SYMBOL_GPL(sdhci_add_host);
3274
Pierre Ossman1e728592008-04-16 19:13:13 +02003275void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003276{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003277 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003278 unsigned long flags;
3279
3280 if (dead) {
3281 spin_lock_irqsave(&host->lock, flags);
3282
3283 host->flags |= SDHCI_DEVICE_DEAD;
3284
3285 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303286 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003287 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003288
3289 host->mrq->cmd->error = -ENOMEDIUM;
3290 tasklet_schedule(&host->finish_tasklet);
3291 }
3292
3293 spin_unlock_irqrestore(&host->lock, flags);
3294 }
3295
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003296 sdhci_disable_card_detection(host);
3297
Markus Mayer4e743f12014-07-03 13:27:42 -07003298 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003299
Pierre Ossmanf9134312008-12-21 17:01:48 +01003300#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003301 led_classdev_unregister(&host->led);
3302#endif
3303
Pierre Ossman1e728592008-04-16 19:13:13 +02003304 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003305 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003306
Russell Kingb537f942014-04-25 12:56:01 +01003307 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3308 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003309 free_irq(host->irq, host);
3310
3311 del_timer_sync(&host->timer);
3312
Pierre Ossmand129bce2006-03-24 03:18:17 -08003313 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003314
Tim Kryger3a48edc2014-06-13 10:13:56 -07003315 if (!IS_ERR(mmc->supply.vmmc))
3316 regulator_disable(mmc->supply.vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003317
Tim Kryger3a48edc2014-06-13 10:13:56 -07003318 if (!IS_ERR(mmc->supply.vqmmc))
3319 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003320
Russell Kingd1e49f72014-04-25 12:58:34 +01003321 if (host->adma_desc)
Markus Mayer4e743f12014-07-03 13:27:42 -07003322 dma_free_coherent(mmc_dev(mmc), ADMA_SIZE,
Russell Kingd1e49f72014-04-25 12:58:34 +01003323 host->adma_desc, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003324 kfree(host->align_buffer);
3325
3326 host->adma_desc = NULL;
3327 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003328}
3329
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003330EXPORT_SYMBOL_GPL(sdhci_remove_host);
3331
3332void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003333{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003334 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003335}
3336
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003337EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003338
3339/*****************************************************************************\
3340 * *
3341 * Driver init/exit *
3342 * *
3343\*****************************************************************************/
3344
3345static int __init sdhci_drv_init(void)
3346{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303347 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003348 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303349 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003350
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003351 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003352}
3353
3354static void __exit sdhci_drv_exit(void)
3355{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003356}
3357
3358module_init(sdhci_drv_init);
3359module_exit(sdhci_drv_exit);
3360
Pierre Ossmandf673b22006-06-30 02:22:31 -07003361module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003362module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003363
Pierre Ossman32710e82009-04-08 20:14:54 +02003364MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003365MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003366MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003367
Pierre Ossmandf673b22006-06-30 02:22:31 -07003368MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003369MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");