blob: 07a242675c0ef665e96323802b1985afd229c9cf [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
Andrei Warkentina3c77782011-04-11 16:13:42 -0500710static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200711{
712 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200713 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500714 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200715 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200716
717 WARN_ON(host->data);
718
Andrei Warkentina3c77782011-04-11 16:13:42 -0500719 if (data || (cmd->flags & MMC_RSP_BUSY)) {
720 count = sdhci_calc_timeout(host, cmd);
721 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
722 }
723
724 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200725 return;
726
727 /* Sanity checks */
728 BUG_ON(data->blksz * data->blocks > 524288);
729 BUG_ON(data->blksz > host->mmc->max_blk_size);
730 BUG_ON(data->blocks > 65535);
731
732 host->data = data;
733 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400734 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200735
Richard Röjforsa13abc72009-09-22 16:45:30 -0700736 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100737 host->flags |= SDHCI_REQ_USE_DMA;
738
Pierre Ossman2134a922008-06-28 18:28:51 +0200739 /*
740 * FIXME: This doesn't account for merging when mapping the
741 * scatterlist.
742 */
743 if (host->flags & SDHCI_REQ_USE_DMA) {
744 int broken, i;
745 struct scatterlist *sg;
746
747 broken = 0;
748 if (host->flags & SDHCI_USE_ADMA) {
749 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
750 broken = 1;
751 } else {
752 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
753 broken = 1;
754 }
755
756 if (unlikely(broken)) {
757 for_each_sg(data->sg, sg, data->sg_len, i) {
758 if (sg->length & 0x3) {
759 DBG("Reverting to PIO because of "
760 "transfer size (%d)\n",
761 sg->length);
762 host->flags &= ~SDHCI_REQ_USE_DMA;
763 break;
764 }
765 }
766 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100767 }
768
769 /*
770 * The assumption here being that alignment is the same after
771 * translation to device address space.
772 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200773 if (host->flags & SDHCI_REQ_USE_DMA) {
774 int broken, i;
775 struct scatterlist *sg;
776
777 broken = 0;
778 if (host->flags & SDHCI_USE_ADMA) {
779 /*
780 * As we use 3 byte chunks to work around
781 * alignment problems, we need to check this
782 * quirk.
783 */
784 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
785 broken = 1;
786 } else {
787 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
788 broken = 1;
789 }
790
791 if (unlikely(broken)) {
792 for_each_sg(data->sg, sg, data->sg_len, i) {
793 if (sg->offset & 0x3) {
794 DBG("Reverting to PIO because of "
795 "bad alignment\n");
796 host->flags &= ~SDHCI_REQ_USE_DMA;
797 break;
798 }
799 }
800 }
801 }
802
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200803 if (host->flags & SDHCI_REQ_USE_DMA) {
804 if (host->flags & SDHCI_USE_ADMA) {
805 ret = sdhci_adma_table_pre(host, data);
806 if (ret) {
807 /*
808 * This only happens when someone fed
809 * us an invalid request.
810 */
811 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200812 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200813 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300814 sdhci_writel(host, host->adma_addr,
815 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200816 }
817 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300818 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200819
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300820 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200821 data->sg, data->sg_len,
822 (data->flags & MMC_DATA_READ) ?
823 DMA_FROM_DEVICE :
824 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300825 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200826 /*
827 * This only happens when someone fed
828 * us an invalid request.
829 */
830 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200831 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200832 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200833 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300834 sdhci_writel(host, sg_dma_address(data->sg),
835 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200836 }
837 }
838 }
839
Pierre Ossman2134a922008-06-28 18:28:51 +0200840 /*
841 * Always adjust the DMA selection as some controllers
842 * (e.g. JMicron) can't do PIO properly when the selection
843 * is ADMA.
844 */
845 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300846 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200847 ctrl &= ~SDHCI_CTRL_DMA_MASK;
848 if ((host->flags & SDHCI_REQ_USE_DMA) &&
849 (host->flags & SDHCI_USE_ADMA))
850 ctrl |= SDHCI_CTRL_ADMA32;
851 else
852 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300853 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100854 }
855
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200856 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200857 int flags;
858
859 flags = SG_MITER_ATOMIC;
860 if (host->data->flags & MMC_DATA_READ)
861 flags |= SG_MITER_TO_SG;
862 else
863 flags |= SG_MITER_FROM_SG;
864 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200865 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800866 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700867
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300868 sdhci_set_transfer_irqs(host);
869
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400870 /* Set the DMA boundary value and block size */
871 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
872 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300873 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700874}
875
876static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500877 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700878{
879 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500880 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700881
Dong Aisheng2b558c12013-10-30 22:09:48 +0800882 if (data == NULL) {
883 /* clear Auto CMD settings for no data CMDs */
884 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
885 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
886 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700887 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800888 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700889
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200890 WARN_ON(!host->data);
891
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700892 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500893 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
894 mode |= SDHCI_TRNS_MULTI;
895 /*
896 * If we are sending CMD23, CMD12 never gets sent
897 * on successful completion (so no Auto-CMD12).
898 */
899 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
900 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500901 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
902 mode |= SDHCI_TRNS_AUTO_CMD23;
903 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
904 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700905 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500906
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700907 if (data->flags & MMC_DATA_READ)
908 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100909 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700910 mode |= SDHCI_TRNS_DMA;
911
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300912 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800913}
914
915static void sdhci_finish_data(struct sdhci_host *host)
916{
917 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800918
919 BUG_ON(!host->data);
920
921 data = host->data;
922 host->data = NULL;
923
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100924 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200925 if (host->flags & SDHCI_USE_ADMA)
926 sdhci_adma_table_post(host, data);
927 else {
928 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
929 data->sg_len, (data->flags & MMC_DATA_READ) ?
930 DMA_FROM_DEVICE : DMA_TO_DEVICE);
931 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800932 }
933
934 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200935 * The specification states that the block count register must
936 * be updated, but it does not specify at what point in the
937 * data flow. That makes the register entirely useless to read
938 * back so we have to assume that nothing made it to the card
939 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800940 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200941 if (data->error)
942 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800943 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200944 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800945
Andrei Warkentine89d4562011-05-23 15:06:37 -0500946 /*
947 * Need to send CMD12 if -
948 * a) open-ended multiblock transfer (no CMD23)
949 * b) error in multiblock transfer
950 */
951 if (data->stop &&
952 (data->error ||
953 !host->mrq->sbc)) {
954
Pierre Ossmand129bce2006-03-24 03:18:17 -0800955 /*
956 * The controller needs a reset of internal state machines
957 * upon error conditions.
958 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200959 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100960 sdhci_do_reset(host, SDHCI_RESET_CMD);
961 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800962 }
963
964 sdhci_send_command(host, data->stop);
965 } else
966 tasklet_schedule(&host->finish_tasklet);
967}
968
Dong Aishengc0e551292013-09-13 19:11:31 +0800969void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800970{
971 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700972 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700973 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800974
975 WARN_ON(host->cmd);
976
Pierre Ossmand129bce2006-03-24 03:18:17 -0800977 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700978 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700979
980 mask = SDHCI_CMD_INHIBIT;
981 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
982 mask |= SDHCI_DATA_INHIBIT;
983
984 /* We shouldn't wait for data inihibit for stop commands, even
985 though they might use busy signaling */
986 if (host->mrq->data && (cmd == host->mrq->data->stop))
987 mask &= ~SDHCI_DATA_INHIBIT;
988
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300989 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700990 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530991 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100992 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800993 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200994 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800995 tasklet_schedule(&host->finish_tasklet);
996 return;
997 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700998 timeout--;
999 mdelay(1);
1000 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001001
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001002 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001003 if (!cmd->data && cmd->busy_timeout > 9000)
1004 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001005 else
1006 timeout += 10 * HZ;
1007 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001008
1009 host->cmd = cmd;
1010
Andrei Warkentina3c77782011-04-11 16:13:42 -05001011 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001013 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014
Andrei Warkentine89d4562011-05-23 15:06:37 -05001015 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001016
Pierre Ossmand129bce2006-03-24 03:18:17 -08001017 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301018 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001019 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001020 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001021 tasklet_schedule(&host->finish_tasklet);
1022 return;
1023 }
1024
1025 if (!(cmd->flags & MMC_RSP_PRESENT))
1026 flags = SDHCI_CMD_RESP_NONE;
1027 else if (cmd->flags & MMC_RSP_136)
1028 flags = SDHCI_CMD_RESP_LONG;
1029 else if (cmd->flags & MMC_RSP_BUSY)
1030 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1031 else
1032 flags = SDHCI_CMD_RESP_SHORT;
1033
1034 if (cmd->flags & MMC_RSP_CRC)
1035 flags |= SDHCI_CMD_CRC;
1036 if (cmd->flags & MMC_RSP_OPCODE)
1037 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301038
1039 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301040 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1041 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001042 flags |= SDHCI_CMD_DATA;
1043
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001044 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001045}
Dong Aishengc0e551292013-09-13 19:11:31 +08001046EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047
1048static void sdhci_finish_command(struct sdhci_host *host)
1049{
1050 int i;
1051
1052 BUG_ON(host->cmd == NULL);
1053
1054 if (host->cmd->flags & MMC_RSP_PRESENT) {
1055 if (host->cmd->flags & MMC_RSP_136) {
1056 /* CRC is stripped so we need to do some shifting. */
1057 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001058 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001059 SDHCI_RESPONSE + (3-i)*4) << 8;
1060 if (i != 3)
1061 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001062 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001063 SDHCI_RESPONSE + (3-i)*4-1);
1064 }
1065 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001066 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001067 }
1068 }
1069
Pierre Ossman17b04292007-07-22 22:18:46 +02001070 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001071
Andrei Warkentine89d4562011-05-23 15:06:37 -05001072 /* Finished CMD23, now send actual command. */
1073 if (host->cmd == host->mrq->sbc) {
1074 host->cmd = NULL;
1075 sdhci_send_command(host, host->mrq->cmd);
1076 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001077
Andrei Warkentine89d4562011-05-23 15:06:37 -05001078 /* Processed actual command. */
1079 if (host->data && host->data_early)
1080 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001081
Andrei Warkentine89d4562011-05-23 15:06:37 -05001082 if (!host->cmd->data)
1083 tasklet_schedule(&host->finish_tasklet);
1084
1085 host->cmd = NULL;
1086 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001087}
1088
Kevin Liu52983382013-01-31 11:31:37 +08001089static u16 sdhci_get_preset_value(struct sdhci_host *host)
1090{
Russell Kingd975f122014-04-25 12:59:31 +01001091 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001092
Russell Kingd975f122014-04-25 12:59:31 +01001093 switch (host->timing) {
1094 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001095 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1096 break;
Russell Kingd975f122014-04-25 12:59:31 +01001097 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001098 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1099 break;
Russell Kingd975f122014-04-25 12:59:31 +01001100 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001101 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1102 break;
Russell Kingd975f122014-04-25 12:59:31 +01001103 case MMC_TIMING_UHS_SDR104:
1104 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001105 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1106 break;
Russell Kingd975f122014-04-25 12:59:31 +01001107 case MMC_TIMING_UHS_DDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001108 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1109 break;
1110 default:
1111 pr_warn("%s: Invalid UHS-I mode selected\n",
1112 mmc_hostname(host->mmc));
1113 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1114 break;
1115 }
1116 return preset;
1117}
1118
Russell King17710592014-04-25 12:58:55 +01001119void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001120{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301121 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001122 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301123 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001124 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001125
Russell King1650d0c2014-04-25 12:58:50 +01001126 host->mmc->actual_clock = 0;
1127
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001128 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001129
1130 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001131 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001132
Zhangfei Gao85105c52010-08-06 07:10:01 +08001133 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001134 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001135 u16 pre_val;
1136
1137 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1138 pre_val = sdhci_get_preset_value(host);
1139 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1140 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1141 if (host->clk_mul &&
1142 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1143 clk = SDHCI_PROG_CLOCK_MODE;
1144 real_div = div + 1;
1145 clk_mul = host->clk_mul;
1146 } else {
1147 real_div = max_t(int, 1, div << 1);
1148 }
1149 goto clock_set;
1150 }
1151
Arindam Nathc3ed3872011-05-05 12:19:06 +05301152 /*
1153 * Check if the Host Controller supports Programmable Clock
1154 * Mode.
1155 */
1156 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001157 for (div = 1; div <= 1024; div++) {
1158 if ((host->max_clk * host->clk_mul / div)
1159 <= clock)
1160 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001161 }
Kevin Liu52983382013-01-31 11:31:37 +08001162 /*
1163 * Set Programmable Clock Mode in the Clock
1164 * Control register.
1165 */
1166 clk = SDHCI_PROG_CLOCK_MODE;
1167 real_div = div;
1168 clk_mul = host->clk_mul;
1169 div--;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301170 } else {
1171 /* Version 3.00 divisors must be a multiple of 2. */
1172 if (host->max_clk <= clock)
1173 div = 1;
1174 else {
1175 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1176 div += 2) {
1177 if ((host->max_clk / div) <= clock)
1178 break;
1179 }
1180 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001181 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301182 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001183 }
1184 } else {
1185 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001186 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001187 if ((host->max_clk / div) <= clock)
1188 break;
1189 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001190 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301191 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001192 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001193
Kevin Liu52983382013-01-31 11:31:37 +08001194clock_set:
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001195 if (real_div)
1196 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1197
Arindam Nathc3ed3872011-05-05 12:19:06 +05301198 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001199 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1200 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001201 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001202 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001203
Chris Ball27f6cb12009-09-22 16:45:31 -07001204 /* Wait max 20 ms */
1205 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001206 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001207 & SDHCI_CLOCK_INT_STABLE)) {
1208 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301209 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001210 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001211 sdhci_dumpregs(host);
1212 return;
1213 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001214 timeout--;
1215 mdelay(1);
1216 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001217
1218 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001219 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001220}
Russell King17710592014-04-25 12:58:55 +01001221EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001222
Russell King24fbb3c2014-04-25 13:00:06 +01001223static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1224 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001225{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001226 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001227 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001228
Tim Kryger52221612014-06-25 00:25:34 -07001229 if (!IS_ERR(mmc->supply.vmmc)) {
1230 spin_unlock_irq(&host->lock);
1231 mmc_regulator_set_ocr(host->mmc, mmc->supply.vmmc, vdd);
1232 spin_lock_irq(&host->lock);
1233 return;
1234 }
1235
Russell King24fbb3c2014-04-25 13:00:06 +01001236 if (mode != MMC_POWER_OFF) {
1237 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001238 case MMC_VDD_165_195:
1239 pwr = SDHCI_POWER_180;
1240 break;
1241 case MMC_VDD_29_30:
1242 case MMC_VDD_30_31:
1243 pwr = SDHCI_POWER_300;
1244 break;
1245 case MMC_VDD_32_33:
1246 case MMC_VDD_33_34:
1247 pwr = SDHCI_POWER_330;
1248 break;
1249 default:
1250 BUG();
1251 }
1252 }
1253
1254 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001255 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001256
Pierre Ossmanae628902009-05-03 20:45:03 +02001257 host->pwr = pwr;
1258
1259 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001260 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001261 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1262 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001263 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001264 } else {
1265 /*
1266 * Spec says that we should clear the power reg before setting
1267 * a new value. Some controllers don't seem to like this though.
1268 */
1269 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1270 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001271
Russell Kinge921a8b2014-04-25 13:00:01 +01001272 /*
1273 * At least the Marvell CaFe chip gets confused if we set the
1274 * voltage and set turn on power at the same time, so set the
1275 * voltage first.
1276 */
1277 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1278 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001279
Russell Kinge921a8b2014-04-25 13:00:01 +01001280 pwr |= SDHCI_POWER_ON;
1281
Pierre Ossmanae628902009-05-03 20:45:03 +02001282 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1283
Russell Kinge921a8b2014-04-25 13:00:01 +01001284 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1285 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001286
Russell Kinge921a8b2014-04-25 13:00:01 +01001287 /*
1288 * Some controllers need an extra 10ms delay of 10ms before
1289 * they can apply clock after applying power
1290 */
1291 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1292 mdelay(10);
1293 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001294}
1295
Pierre Ossmand129bce2006-03-24 03:18:17 -08001296/*****************************************************************************\
1297 * *
1298 * MMC callbacks *
1299 * *
1300\*****************************************************************************/
1301
1302static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1303{
1304 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001305 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001306 unsigned long flags;
Aaron Lu473b0952012-07-03 17:27:49 +08001307 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001308
1309 host = mmc_priv(mmc);
1310
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001311 sdhci_runtime_pm_get(host);
1312
Pierre Ossmand129bce2006-03-24 03:18:17 -08001313 spin_lock_irqsave(&host->lock, flags);
1314
1315 WARN_ON(host->mrq != NULL);
1316
Pierre Ossmanf9134312008-12-21 17:01:48 +01001317#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001318 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001319#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001320
1321 /*
1322 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1323 * requests if Auto-CMD12 is enabled.
1324 */
1325 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001326 if (mrq->stop) {
1327 mrq->data->stop = NULL;
1328 mrq->stop = NULL;
1329 }
1330 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001331
1332 host->mrq = mrq;
1333
Shawn Guo505a8682012-12-11 15:23:42 +08001334 /*
1335 * Firstly check card presence from cd-gpio. The return could
1336 * be one of the following possibilities:
1337 * negative: cd-gpio is not available
1338 * zero: cd-gpio is used, and card is removed
1339 * one: cd-gpio is used, and card is present
1340 */
1341 present = mmc_gpio_get_cd(host->mmc);
1342 if (present < 0) {
1343 /* If polling, assume that the card is always present. */
1344 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1345 present = 1;
1346 else
1347 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1348 SDHCI_CARD_PRESENT;
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +08001349 }
1350
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001351 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001352 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001353 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301354 } else {
1355 u32 present_state;
1356
1357 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1358 /*
1359 * Check if the re-tuning timer has already expired and there
1360 * is no on-going data transfer. If so, we need to execute
1361 * tuning procedure before sending command.
1362 */
1363 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1364 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
Chris Ball14efd952012-11-05 14:29:49 -05001365 if (mmc->card) {
1366 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1367 tuning_opcode =
1368 mmc->card->type == MMC_TYPE_MMC ?
1369 MMC_SEND_TUNING_BLOCK_HS200 :
1370 MMC_SEND_TUNING_BLOCK;
Chuansheng Liu63c21182013-11-05 14:52:45 +08001371
1372 /* Here we need to set the host->mrq to NULL,
1373 * in case the pending finish_tasklet
1374 * finishes it incorrectly.
1375 */
1376 host->mrq = NULL;
1377
Chris Ball14efd952012-11-05 14:29:49 -05001378 spin_unlock_irqrestore(&host->lock, flags);
1379 sdhci_execute_tuning(mmc, tuning_opcode);
1380 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301381
Chris Ball14efd952012-11-05 14:29:49 -05001382 /* Restore original mmc_request structure */
1383 host->mrq = mrq;
1384 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301385 }
1386
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001387 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001388 sdhci_send_command(host, mrq->sbc);
1389 else
1390 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301391 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001392
Pierre Ossman5f25a662006-10-04 02:15:39 -07001393 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001394 spin_unlock_irqrestore(&host->lock, flags);
1395}
1396
Russell King2317f562014-04-25 12:57:07 +01001397void sdhci_set_bus_width(struct sdhci_host *host, int width)
1398{
1399 u8 ctrl;
1400
1401 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1402 if (width == MMC_BUS_WIDTH_8) {
1403 ctrl &= ~SDHCI_CTRL_4BITBUS;
1404 if (host->version >= SDHCI_SPEC_300)
1405 ctrl |= SDHCI_CTRL_8BITBUS;
1406 } else {
1407 if (host->version >= SDHCI_SPEC_300)
1408 ctrl &= ~SDHCI_CTRL_8BITBUS;
1409 if (width == MMC_BUS_WIDTH_4)
1410 ctrl |= SDHCI_CTRL_4BITBUS;
1411 else
1412 ctrl &= ~SDHCI_CTRL_4BITBUS;
1413 }
1414 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1415}
1416EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1417
Russell King96d7b782014-04-25 12:59:26 +01001418void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1419{
1420 u16 ctrl_2;
1421
1422 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1423 /* Select Bus Speed Mode for host */
1424 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1425 if ((timing == MMC_TIMING_MMC_HS200) ||
1426 (timing == MMC_TIMING_UHS_SDR104))
1427 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1428 else if (timing == MMC_TIMING_UHS_SDR12)
1429 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1430 else if (timing == MMC_TIMING_UHS_SDR25)
1431 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1432 else if (timing == MMC_TIMING_UHS_SDR50)
1433 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1434 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1435 (timing == MMC_TIMING_MMC_DDR52))
1436 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1437 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1438}
1439EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1440
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001441static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001442{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001443 unsigned long flags;
1444 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001445 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001446
Pierre Ossmand129bce2006-03-24 03:18:17 -08001447 spin_lock_irqsave(&host->lock, flags);
1448
Adrian Hunterceb61432011-12-27 15:48:41 +02001449 if (host->flags & SDHCI_DEVICE_DEAD) {
1450 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001451 if (!IS_ERR(mmc->supply.vmmc) &&
1452 ios->power_mode == MMC_POWER_OFF)
1453 mmc_regulator_set_ocr(host->mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001454 return;
1455 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001456
Pierre Ossmand129bce2006-03-24 03:18:17 -08001457 /*
1458 * Reset the chip on each power off.
1459 * Should clear out any weird states.
1460 */
1461 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001462 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001463 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001464 }
1465
Kevin Liu52983382013-01-31 11:31:37 +08001466 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001467 (ios->power_mode == MMC_POWER_UP) &&
1468 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001469 sdhci_enable_preset_value(host, false);
1470
Russell King373073e2014-04-25 12:58:45 +01001471 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001472 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001473 host->clock = ios->clock;
1474 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001475
Russell King24fbb3c2014-04-25 13:00:06 +01001476 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001477
Philip Rakity643a81f2010-09-23 08:24:32 -07001478 if (host->ops->platform_send_init_74_clocks)
1479 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1480
Russell King2317f562014-04-25 12:57:07 +01001481 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001482
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001483 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001484
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001485 if ((ios->timing == MMC_TIMING_SD_HS ||
1486 ios->timing == MMC_TIMING_MMC_HS)
1487 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001488 ctrl |= SDHCI_CTRL_HISPD;
1489 else
1490 ctrl &= ~SDHCI_CTRL_HISPD;
1491
Arindam Nathd6d50a12011-05-05 12:18:59 +05301492 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301493 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301494
1495 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301496 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001497 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301498 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301499 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1500 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001501 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301502 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301503
Russell Kingda91a8f2014-04-25 13:00:12 +01001504 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301505 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301506 /*
1507 * We only need to set Driver Strength if the
1508 * preset value enable is not set.
1509 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001510 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301511 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1512 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1513 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1514 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1515 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1516
1517 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301518 } else {
1519 /*
1520 * According to SDHC Spec v3.00, if the Preset Value
1521 * Enable in the Host Control 2 register is set, we
1522 * need to reset SD Clock Enable before changing High
1523 * Speed Enable to avoid generating clock gliches.
1524 */
Arindam Nath758535c2011-05-05 12:19:00 +05301525
1526 /* Reset SD Clock Enable */
1527 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1528 clk &= ~SDHCI_CLOCK_CARD_EN;
1529 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1530
1531 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1532
1533 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001534 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301535 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301536
Arindam Nath49c468f2011-05-05 12:19:01 +05301537
1538 /* Reset SD Clock Enable */
1539 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1540 clk &= ~SDHCI_CLOCK_CARD_EN;
1541 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1542
Russell King96d7b782014-04-25 12:59:26 +01001543 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001544 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301545
Kevin Liu52983382013-01-31 11:31:37 +08001546 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1547 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1548 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1549 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1550 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1551 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1552 u16 preset;
1553
1554 sdhci_enable_preset_value(host, true);
1555 preset = sdhci_get_preset_value(host);
1556 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1557 >> SDHCI_PRESET_DRV_SHIFT;
1558 }
1559
Arindam Nath49c468f2011-05-05 12:19:01 +05301560 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001561 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301562 } else
1563 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301564
Leandro Dorileob8352262007-07-25 23:47:04 +02001565 /*
1566 * Some (ENE) controllers go apeshit on some ios operation,
1567 * signalling timeout and CRC errors even on CMD0. Resetting
1568 * it on each ios seems to solve the problem.
1569 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001570 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001571 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001572
Pierre Ossman5f25a662006-10-04 02:15:39 -07001573 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001574 spin_unlock_irqrestore(&host->lock, flags);
1575}
1576
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001577static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1578{
1579 struct sdhci_host *host = mmc_priv(mmc);
1580
1581 sdhci_runtime_pm_get(host);
1582 sdhci_do_set_ios(host, ios);
1583 sdhci_runtime_pm_put(host);
1584}
1585
Kevin Liu94144a42013-02-28 17:35:53 +08001586static int sdhci_do_get_cd(struct sdhci_host *host)
1587{
1588 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1589
1590 if (host->flags & SDHCI_DEVICE_DEAD)
1591 return 0;
1592
1593 /* If polling/nonremovable, assume that the card is always present. */
1594 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1595 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1596 return 1;
1597
1598 /* Try slot gpio detect */
1599 if (!IS_ERR_VALUE(gpio_cd))
1600 return !!gpio_cd;
1601
1602 /* Host native card detect */
1603 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1604}
1605
1606static int sdhci_get_cd(struct mmc_host *mmc)
1607{
1608 struct sdhci_host *host = mmc_priv(mmc);
1609 int ret;
1610
1611 sdhci_runtime_pm_get(host);
1612 ret = sdhci_do_get_cd(host);
1613 sdhci_runtime_pm_put(host);
1614 return ret;
1615}
1616
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001617static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001618{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001619 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001620 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001621
Pierre Ossmand129bce2006-03-24 03:18:17 -08001622 spin_lock_irqsave(&host->lock, flags);
1623
Pierre Ossman1e728592008-04-16 19:13:13 +02001624 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001625 is_readonly = 0;
1626 else if (host->ops->get_ro)
1627 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001628 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001629 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1630 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001631
1632 spin_unlock_irqrestore(&host->lock, flags);
1633
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001634 /* This quirk needs to be replaced by a callback-function later */
1635 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1636 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001637}
1638
Takashi Iwai82b0e232011-04-21 20:26:38 +02001639#define SAMPLE_COUNT 5
1640
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001641static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001642{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001643 int i, ro_count;
1644
Takashi Iwai82b0e232011-04-21 20:26:38 +02001645 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001646 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001647
1648 ro_count = 0;
1649 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001650 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001651 if (++ro_count > SAMPLE_COUNT / 2)
1652 return 1;
1653 }
1654 msleep(30);
1655 }
1656 return 0;
1657}
1658
Adrian Hunter20758b62011-08-29 16:42:12 +03001659static void sdhci_hw_reset(struct mmc_host *mmc)
1660{
1661 struct sdhci_host *host = mmc_priv(mmc);
1662
1663 if (host->ops && host->ops->hw_reset)
1664 host->ops->hw_reset(host);
1665}
1666
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001667static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001668{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001669 struct sdhci_host *host = mmc_priv(mmc);
1670 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001671
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001672 sdhci_runtime_pm_get(host);
1673 ret = sdhci_do_get_ro(host);
1674 sdhci_runtime_pm_put(host);
1675 return ret;
1676}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001677
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001678static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1679{
Russell Kingbe138552014-04-25 12:55:56 +01001680 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001681 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001682 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001683 else
Russell Kingb537f942014-04-25 12:56:01 +01001684 host->ier &= ~SDHCI_INT_CARD_INT;
1685
1686 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1687 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001688 mmiowb();
1689 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001690}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001691
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001692static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1693{
1694 struct sdhci_host *host = mmc_priv(mmc);
1695 unsigned long flags;
1696
Russell Kingef104332014-04-25 12:55:41 +01001697 sdhci_runtime_pm_get(host);
1698
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001699 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001700 if (enable)
1701 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1702 else
1703 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1704
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001705 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001706 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001707
1708 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001709}
1710
Philip Rakity6231f3d2012-07-23 15:56:23 -07001711static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001712 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001713{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001714 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001715 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001716 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001717
1718 /*
1719 * Signal Voltage Switching is only applicable for Host Controllers
1720 * v3.00 and above.
1721 */
1722 if (host->version < SDHCI_SPEC_300)
1723 return 0;
1724
Philip Rakity6231f3d2012-07-23 15:56:23 -07001725 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001726
Fabio Estevam21f59982013-02-14 10:35:03 -02001727 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001728 case MMC_SIGNAL_VOLTAGE_330:
1729 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1730 ctrl &= ~SDHCI_CTRL_VDD_180;
1731 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1732
Tim Kryger3a48edc2014-06-13 10:13:56 -07001733 if (!IS_ERR(mmc->supply.vqmmc)) {
1734 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1735 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001736 if (ret) {
1737 pr_warning("%s: Switching to 3.3V signalling voltage "
1738 " failed\n", mmc_hostname(host->mmc));
1739 return -EIO;
1740 }
1741 }
1742 /* Wait for 5ms */
1743 usleep_range(5000, 5500);
1744
1745 /* 3.3V regulator output should be stable within 5 ms */
1746 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1747 if (!(ctrl & SDHCI_CTRL_VDD_180))
1748 return 0;
1749
1750 pr_warning("%s: 3.3V regulator output did not became stable\n",
1751 mmc_hostname(host->mmc));
1752
1753 return -EAGAIN;
1754 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001755 if (!IS_ERR(mmc->supply.vqmmc)) {
1756 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001757 1700000, 1950000);
1758 if (ret) {
1759 pr_warning("%s: Switching to 1.8V signalling voltage "
1760 " failed\n", mmc_hostname(host->mmc));
1761 return -EIO;
1762 }
1763 }
1764
1765 /*
1766 * Enable 1.8V Signal Enable in the Host Control2
1767 * register
1768 */
1769 ctrl |= SDHCI_CTRL_VDD_180;
1770 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1771
1772 /* Wait for 5ms */
1773 usleep_range(5000, 5500);
1774
1775 /* 1.8V regulator output should be stable within 5 ms */
1776 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1777 if (ctrl & SDHCI_CTRL_VDD_180)
1778 return 0;
1779
1780 pr_warning("%s: 1.8V regulator output did not became stable\n",
1781 mmc_hostname(host->mmc));
1782
1783 return -EAGAIN;
1784 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001785 if (!IS_ERR(mmc->supply.vqmmc)) {
1786 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1787 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001788 if (ret) {
1789 pr_warning("%s: Switching to 1.2V signalling voltage "
1790 " failed\n", mmc_hostname(host->mmc));
1791 return -EIO;
1792 }
1793 }
1794 return 0;
1795 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301796 /* No signal voltage switch required */
1797 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001798 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301799}
1800
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001801static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001802 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001803{
1804 struct sdhci_host *host = mmc_priv(mmc);
1805 int err;
1806
1807 if (host->version < SDHCI_SPEC_300)
1808 return 0;
1809 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001810 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001811 sdhci_runtime_pm_put(host);
1812 return err;
1813}
1814
Kevin Liu20b92a32012-12-17 19:29:26 +08001815static int sdhci_card_busy(struct mmc_host *mmc)
1816{
1817 struct sdhci_host *host = mmc_priv(mmc);
1818 u32 present_state;
1819
1820 sdhci_runtime_pm_get(host);
1821 /* Check whether DAT[3:0] is 0000 */
1822 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1823 sdhci_runtime_pm_put(host);
1824
1825 return !(present_state & SDHCI_DATA_LVL_MASK);
1826}
1827
Girish K S069c9f12012-01-06 09:56:39 +05301828static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301829{
Russell King4b6f37d2014-04-25 12:59:36 +01001830 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301831 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301832 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301833 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001834 unsigned long flags;
Arindam Nathb513ea22011-05-05 12:19:04 +05301835
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001836 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001837 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301838
Arindam Nathb513ea22011-05-05 12:19:04 +05301839 /*
Girish K S069c9f12012-01-06 09:56:39 +05301840 * The Host Controller needs tuning only in case of SDR104 mode
1841 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301842 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301843 * If the Host Controller supports the HS200 mode then the
1844 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301845 */
Russell King4b6f37d2014-04-25 12:59:36 +01001846 switch (host->timing) {
1847 case MMC_TIMING_MMC_HS200:
1848 case MMC_TIMING_UHS_SDR104:
1849 break;
Girish K S069c9f12012-01-06 09:56:39 +05301850
Russell King4b6f37d2014-04-25 12:59:36 +01001851 case MMC_TIMING_UHS_SDR50:
1852 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1853 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1854 break;
1855 /* FALLTHROUGH */
1856
1857 default:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001858 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001859 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301860 return 0;
1861 }
1862
Dong Aisheng45251812013-09-13 19:11:30 +08001863 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001864 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001865 err = host->ops->platform_execute_tuning(host, opcode);
1866 sdhci_runtime_pm_put(host);
1867 return err;
1868 }
1869
Russell King4b6f37d2014-04-25 12:59:36 +01001870 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1871 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Arindam Nathb513ea22011-05-05 12:19:04 +05301872 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1873
1874 /*
1875 * As per the Host Controller spec v3.00, tuning command
1876 * generates Buffer Read Ready interrupt, so enable that.
1877 *
1878 * Note: The spec clearly says that when tuning sequence
1879 * is being performed, the controller does not generate
1880 * interrupts other than Buffer Read Ready interrupt. But
1881 * to make sure we don't hit a controller bug, we _only_
1882 * enable Buffer Read Ready interrupt here.
1883 */
Russell Kingb537f942014-04-25 12:56:01 +01001884 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1885 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301886
1887 /*
1888 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1889 * of loops reaches 40 times or a timeout of 150ms occurs.
1890 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301891 do {
1892 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001893 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301894
Girish K S069c9f12012-01-06 09:56:39 +05301895 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301896 cmd.arg = 0;
1897 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1898 cmd.retries = 0;
1899 cmd.data = NULL;
1900 cmd.error = 0;
1901
Al Cooper7ce45e92014-05-09 11:34:07 -04001902 if (tuning_loop_counter-- == 0)
1903 break;
1904
Arindam Nathb513ea22011-05-05 12:19:04 +05301905 mrq.cmd = &cmd;
1906 host->mrq = &mrq;
1907
1908 /*
1909 * In response to CMD19, the card sends 64 bytes of tuning
1910 * block to the Host Controller. So we set the block size
1911 * to 64 here.
1912 */
Girish K S069c9f12012-01-06 09:56:39 +05301913 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1914 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1915 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1916 SDHCI_BLOCK_SIZE);
1917 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1918 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1919 SDHCI_BLOCK_SIZE);
1920 } else {
1921 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1922 SDHCI_BLOCK_SIZE);
1923 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301924
1925 /*
1926 * The tuning block is sent by the card to the host controller.
1927 * So we set the TRNS_READ bit in the Transfer Mode register.
1928 * This also takes care of setting DMA Enable and Multi Block
1929 * Select in the same register to 0.
1930 */
1931 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1932
1933 sdhci_send_command(host, &cmd);
1934
1935 host->cmd = NULL;
1936 host->mrq = NULL;
1937
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001938 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301939 /* Wait for Buffer Read Ready interrupt */
1940 wait_event_interruptible_timeout(host->buf_ready_int,
1941 (host->tuning_done == 1),
1942 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001943 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301944
1945 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301946 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301947 "Buffer Read Ready interrupt during tuning "
1948 "procedure, falling back to fixed sampling "
1949 "clock\n");
1950 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1951 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1952 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1953 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1954
1955 err = -EIO;
1956 goto out;
1957 }
1958
1959 host->tuning_done = 0;
1960
1961 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07001962
1963 /* eMMC spec does not require a delay between tuning cycles */
1964 if (opcode == MMC_SEND_TUNING_BLOCK)
1965 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05301966 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1967
1968 /*
1969 * The Host Driver has exhausted the maximum number of loops allowed,
1970 * so use fixed sampling frequency.
1971 */
Al Cooper7ce45e92014-05-09 11:34:07 -04001972 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05301973 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1974 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04001975 }
1976 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
1977 pr_info(DRIVER_NAME ": Tuning procedure"
1978 " failed, falling back to fixed sampling"
1979 " clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08001980 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05301981 }
1982
1983out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301984 /*
1985 * If this is the very first time we are here, we start the retuning
1986 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1987 * flag won't be set, we check this condition before actually starting
1988 * the timer.
1989 */
1990 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1991 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
Aaron Lu973905f2012-07-04 13:29:09 +08001992 host->flags |= SDHCI_USING_RETUNING_TIMER;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301993 mod_timer(&host->tuning_timer, jiffies +
1994 host->tuning_count * HZ);
1995 /* Tuning mode 1 limits the maximum data length to 4MB */
1996 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
Arend van Spriel2bc02482014-01-04 13:51:26 +01001997 } else if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301998 host->flags &= ~SDHCI_NEEDS_RETUNING;
1999 /* Reload the new initial value for timer */
Arend van Spriel2bc02482014-01-04 13:51:26 +01002000 mod_timer(&host->tuning_timer, jiffies +
2001 host->tuning_count * HZ);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302002 }
2003
2004 /*
2005 * In case tuning fails, host controllers which support re-tuning can
2006 * try tuning again at a later time, when the re-tuning timer expires.
2007 * So for these controllers, we return 0. Since there might be other
2008 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08002009 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2010 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302011 */
Aaron Lu973905f2012-07-04 13:29:09 +08002012 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302013 err = 0;
2014
Russell Kingb537f942014-04-25 12:56:01 +01002015 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2016 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002017 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002018 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302019
2020 return err;
2021}
2022
Kevin Liu52983382013-01-31 11:31:37 +08002023
2024static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302025{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302026 /* Host Controller v3.00 defines preset value registers */
2027 if (host->version < SDHCI_SPEC_300)
2028 return;
2029
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302030 /*
2031 * We only enable or disable Preset Value if they are not already
2032 * enabled or disabled respectively. Otherwise, we bail out.
2033 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002034 if (host->preset_enabled != enable) {
2035 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2036
2037 if (enable)
2038 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2039 else
2040 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2041
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302042 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002043
2044 if (enable)
2045 host->flags |= SDHCI_PV_ENABLED;
2046 else
2047 host->flags &= ~SDHCI_PV_ENABLED;
2048
2049 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302050 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002051}
2052
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002053static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002054{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002055 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002056 unsigned long flags;
2057
Christian Daudt722e1282013-06-20 14:26:36 -07002058 /* First check if client has provided their own card event */
2059 if (host->ops->card_event)
2060 host->ops->card_event(host);
2061
Pierre Ossmand129bce2006-03-24 03:18:17 -08002062 spin_lock_irqsave(&host->lock, flags);
2063
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002064 /* Check host->mrq first in case we are runtime suspended */
Shawn Guo9668d762013-06-09 19:49:24 +08002065 if (host->mrq && !sdhci_do_get_cd(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302066 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002067 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302068 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002069 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002070
Russell King03231f92014-04-25 12:57:12 +01002071 sdhci_do_reset(host, SDHCI_RESET_CMD);
2072 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002073
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002074 host->mrq->cmd->error = -ENOMEDIUM;
2075 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002076 }
2077
2078 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002079}
2080
2081static const struct mmc_host_ops sdhci_ops = {
2082 .request = sdhci_request,
2083 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002084 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002085 .get_ro = sdhci_get_ro,
2086 .hw_reset = sdhci_hw_reset,
2087 .enable_sdio_irq = sdhci_enable_sdio_irq,
2088 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2089 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002090 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002091 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002092};
2093
2094/*****************************************************************************\
2095 * *
2096 * Tasklets *
2097 * *
2098\*****************************************************************************/
2099
Pierre Ossmand129bce2006-03-24 03:18:17 -08002100static void sdhci_tasklet_finish(unsigned long param)
2101{
2102 struct sdhci_host *host;
2103 unsigned long flags;
2104 struct mmc_request *mrq;
2105
2106 host = (struct sdhci_host*)param;
2107
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002108 spin_lock_irqsave(&host->lock, flags);
2109
Chris Ball0c9c99a2011-04-27 17:35:31 -04002110 /*
2111 * If this tasklet gets rescheduled while running, it will
2112 * be run again afterwards but without any active request.
2113 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002114 if (!host->mrq) {
2115 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002116 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002117 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002118
2119 del_timer(&host->timer);
2120
2121 mrq = host->mrq;
2122
Pierre Ossmand129bce2006-03-24 03:18:17 -08002123 /*
2124 * The controller needs a reset of internal state machines
2125 * upon error conditions.
2126 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002127 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002128 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002129 (mrq->data && (mrq->data->error ||
2130 (mrq->data->stop && mrq->data->stop->error))) ||
2131 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002132
2133 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002134 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002135 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002136 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002137
2138 /* Spec says we should do both at the same time, but Ricoh
2139 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002140 sdhci_do_reset(host, SDHCI_RESET_CMD);
2141 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002142 }
2143
2144 host->mrq = NULL;
2145 host->cmd = NULL;
2146 host->data = NULL;
2147
Pierre Ossmanf9134312008-12-21 17:01:48 +01002148#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002149 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002150#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002151
Pierre Ossman5f25a662006-10-04 02:15:39 -07002152 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002153 spin_unlock_irqrestore(&host->lock, flags);
2154
2155 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002156 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002157}
2158
2159static void sdhci_timeout_timer(unsigned long data)
2160{
2161 struct sdhci_host *host;
2162 unsigned long flags;
2163
2164 host = (struct sdhci_host*)data;
2165
2166 spin_lock_irqsave(&host->lock, flags);
2167
2168 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302169 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002170 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002171 sdhci_dumpregs(host);
2172
2173 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002174 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002175 sdhci_finish_data(host);
2176 } else {
2177 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002178 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002179 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002180 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002181
2182 tasklet_schedule(&host->finish_tasklet);
2183 }
2184 }
2185
Pierre Ossman5f25a662006-10-04 02:15:39 -07002186 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002187 spin_unlock_irqrestore(&host->lock, flags);
2188}
2189
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302190static void sdhci_tuning_timer(unsigned long data)
2191{
2192 struct sdhci_host *host;
2193 unsigned long flags;
2194
2195 host = (struct sdhci_host *)data;
2196
2197 spin_lock_irqsave(&host->lock, flags);
2198
2199 host->flags |= SDHCI_NEEDS_RETUNING;
2200
2201 spin_unlock_irqrestore(&host->lock, flags);
2202}
2203
Pierre Ossmand129bce2006-03-24 03:18:17 -08002204/*****************************************************************************\
2205 * *
2206 * Interrupt handling *
2207 * *
2208\*****************************************************************************/
2209
2210static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2211{
2212 BUG_ON(intmask == 0);
2213
2214 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302215 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002216 "though no command operation was in progress.\n",
2217 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002218 sdhci_dumpregs(host);
2219 return;
2220 }
2221
Pierre Ossman43b58b32007-07-25 23:15:27 +02002222 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002223 host->cmd->error = -ETIMEDOUT;
2224 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2225 SDHCI_INT_INDEX))
2226 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002227
Pierre Ossmane8095172008-07-25 01:09:08 +02002228 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002229 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002230 return;
2231 }
2232
2233 /*
2234 * The host can send and interrupt when the busy state has
2235 * ended, allowing us to wait without wasting CPU cycles.
2236 * Unfortunately this is overloaded on the "data complete"
2237 * interrupt, so we need to take some care when handling
2238 * it.
2239 *
2240 * Note: The 1.0 specification is a bit ambiguous about this
2241 * feature so there might be some problems with older
2242 * controllers.
2243 */
2244 if (host->cmd->flags & MMC_RSP_BUSY) {
2245 if (host->cmd->data)
2246 DBG("Cannot wait for busy signal when also "
2247 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002248 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002249 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002250
2251 /* The controller does not support the end-of-busy IRQ,
2252 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002253 }
2254
2255 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002256 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002257}
2258
George G. Davis0957c332010-02-18 12:32:12 -05002259#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002260static void sdhci_show_adma_error(struct sdhci_host *host)
2261{
2262 const char *name = mmc_hostname(host->mmc);
2263 u8 *desc = host->adma_desc;
2264 __le32 *dma;
2265 __le16 *len;
2266 u8 attr;
2267
2268 sdhci_dumpregs(host);
2269
2270 while (true) {
2271 dma = (__le32 *)(desc + 4);
2272 len = (__le16 *)(desc + 2);
2273 attr = *desc;
2274
2275 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2276 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2277
2278 desc += 8;
2279
2280 if (attr & 2)
2281 break;
2282 }
2283}
2284#else
2285static void sdhci_show_adma_error(struct sdhci_host *host) { }
2286#endif
2287
Pierre Ossmand129bce2006-03-24 03:18:17 -08002288static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2289{
Girish K S069c9f12012-01-06 09:56:39 +05302290 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002291 BUG_ON(intmask == 0);
2292
Arindam Nathb513ea22011-05-05 12:19:04 +05302293 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2294 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302295 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2296 if (command == MMC_SEND_TUNING_BLOCK ||
2297 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302298 host->tuning_done = 1;
2299 wake_up(&host->buf_ready_int);
2300 return;
2301 }
2302 }
2303
Pierre Ossmand129bce2006-03-24 03:18:17 -08002304 if (!host->data) {
2305 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002306 * The "data complete" interrupt is also used to
2307 * indicate that a busy state has ended. See comment
2308 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002309 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002310 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2311 if (intmask & SDHCI_INT_DATA_END) {
2312 sdhci_finish_command(host);
2313 return;
2314 }
2315 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002316
Girish K Sa3c76eb2011-10-11 11:44:09 +05302317 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002318 "though no data operation was in progress.\n",
2319 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002320 sdhci_dumpregs(host);
2321
2322 return;
2323 }
2324
2325 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002326 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002327 else if (intmask & SDHCI_INT_DATA_END_BIT)
2328 host->data->error = -EILSEQ;
2329 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2330 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2331 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002332 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002333 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302334 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002335 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002336 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002337 if (host->ops->adma_workaround)
2338 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002339 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002340
Pierre Ossman17b04292007-07-22 22:18:46 +02002341 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002342 sdhci_finish_data(host);
2343 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002344 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002345 sdhci_transfer_pio(host);
2346
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002347 /*
2348 * We currently don't do anything fancy with DMA
2349 * boundaries, but as we can't disable the feature
2350 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002351 *
2352 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2353 * should return a valid address to continue from, but as
2354 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002355 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002356 if (intmask & SDHCI_INT_DMA_END) {
2357 u32 dmastart, dmanow;
2358 dmastart = sg_dma_address(host->data->sg);
2359 dmanow = dmastart + host->data->bytes_xfered;
2360 /*
2361 * Force update to the next DMA block boundary.
2362 */
2363 dmanow = (dmanow &
2364 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2365 SDHCI_DEFAULT_BOUNDARY_SIZE;
2366 host->data->bytes_xfered = dmanow - dmastart;
2367 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2368 " next 0x%08x\n",
2369 mmc_hostname(host->mmc), dmastart,
2370 host->data->bytes_xfered, dmanow);
2371 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2372 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002373
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002374 if (intmask & SDHCI_INT_DATA_END) {
2375 if (host->cmd) {
2376 /*
2377 * Data managed to finish before the
2378 * command completed. Make sure we do
2379 * things in the proper order.
2380 */
2381 host->data_early = 1;
2382 } else {
2383 sdhci_finish_data(host);
2384 }
2385 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002386 }
2387}
2388
David Howells7d12e782006-10-05 14:55:46 +01002389static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002390{
Russell King781e9892014-04-25 12:55:46 +01002391 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002392 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002393 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002394 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002395
2396 spin_lock(&host->lock);
2397
Russell Kingbe138552014-04-25 12:55:56 +01002398 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002399 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002400 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002401 }
2402
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002403 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002404 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002405 result = IRQ_NONE;
2406 goto out;
2407 }
2408
Russell King41005002014-04-25 12:55:36 +01002409 do {
2410 /* Clear selected interrupts. */
2411 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2412 SDHCI_INT_BUS_POWER);
2413 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002414
Russell King41005002014-04-25 12:55:36 +01002415 DBG("*** %s got interrupt: 0x%08x\n",
2416 mmc_hostname(host->mmc), intmask);
2417
2418 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2419 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2420 SDHCI_CARD_PRESENT;
2421
2422 /*
2423 * There is a observation on i.mx esdhc. INSERT
2424 * bit will be immediately set again when it gets
2425 * cleared, if a card is inserted. We have to mask
2426 * the irq to prevent interrupt storm which will
2427 * freeze the system. And the REMOVE gets the
2428 * same situation.
2429 *
2430 * More testing are needed here to ensure it works
2431 * for other platforms though.
2432 */
Russell Kingb537f942014-04-25 12:56:01 +01002433 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2434 SDHCI_INT_CARD_REMOVE);
2435 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2436 SDHCI_INT_CARD_INSERT;
2437 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2438 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002439
2440 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2441 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002442
2443 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2444 SDHCI_INT_CARD_REMOVE);
2445 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002446 }
2447
2448 if (intmask & SDHCI_INT_CMD_MASK)
2449 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
2450
2451 if (intmask & SDHCI_INT_DATA_MASK)
2452 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2453
2454 if (intmask & SDHCI_INT_BUS_POWER)
2455 pr_err("%s: Card is consuming too much power!\n",
2456 mmc_hostname(host->mmc));
2457
Russell King781e9892014-04-25 12:55:46 +01002458 if (intmask & SDHCI_INT_CARD_INT) {
2459 sdhci_enable_sdio_irq_nolock(host, false);
2460 host->thread_isr |= SDHCI_INT_CARD_INT;
2461 result = IRQ_WAKE_THREAD;
2462 }
Russell King41005002014-04-25 12:55:36 +01002463
2464 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2465 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2466 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2467 SDHCI_INT_CARD_INT);
2468
2469 if (intmask) {
2470 unexpected |= intmask;
2471 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2472 }
2473
Russell King781e9892014-04-25 12:55:46 +01002474 if (result == IRQ_NONE)
2475 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002476
2477 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002478 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002479out:
2480 spin_unlock(&host->lock);
2481
Alexander Stein6379b232012-03-14 09:52:10 +01002482 if (unexpected) {
2483 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2484 mmc_hostname(host->mmc), unexpected);
2485 sdhci_dumpregs(host);
2486 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002487
Pierre Ossmand129bce2006-03-24 03:18:17 -08002488 return result;
2489}
2490
Russell King781e9892014-04-25 12:55:46 +01002491static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2492{
2493 struct sdhci_host *host = dev_id;
2494 unsigned long flags;
2495 u32 isr;
2496
2497 spin_lock_irqsave(&host->lock, flags);
2498 isr = host->thread_isr;
2499 host->thread_isr = 0;
2500 spin_unlock_irqrestore(&host->lock, flags);
2501
Russell King3560db82014-04-25 12:55:51 +01002502 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2503 sdhci_card_event(host->mmc);
2504 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2505 }
2506
Russell King781e9892014-04-25 12:55:46 +01002507 if (isr & SDHCI_INT_CARD_INT) {
2508 sdio_run_irqs(host->mmc);
2509
2510 spin_lock_irqsave(&host->lock, flags);
2511 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2512 sdhci_enable_sdio_irq_nolock(host, true);
2513 spin_unlock_irqrestore(&host->lock, flags);
2514 }
2515
2516 return isr ? IRQ_HANDLED : IRQ_NONE;
2517}
2518
Pierre Ossmand129bce2006-03-24 03:18:17 -08002519/*****************************************************************************\
2520 * *
2521 * Suspend/resume *
2522 * *
2523\*****************************************************************************/
2524
2525#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002526void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2527{
2528 u8 val;
2529 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2530 | SDHCI_WAKE_ON_INT;
2531
2532 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2533 val |= mask ;
2534 /* Avoid fake wake up */
2535 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2536 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2537 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2538}
2539EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2540
2541void sdhci_disable_irq_wakeups(struct sdhci_host *host)
2542{
2543 u8 val;
2544 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2545 | SDHCI_WAKE_ON_INT;
2546
2547 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2548 val &= ~mask;
2549 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2550}
2551EXPORT_SYMBOL_GPL(sdhci_disable_irq_wakeups);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002552
Manuel Lauss29495aa2011-11-03 11:09:45 +01002553int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002554{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002555 sdhci_disable_card_detection(host);
2556
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302557 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002558 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002559 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302560 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302561 }
2562
Kevin Liuad080d72013-01-05 17:21:33 +08002563 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002564 host->ier = 0;
2565 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2566 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002567 free_irq(host->irq, host);
2568 } else {
2569 sdhci_enable_irq_wakeups(host);
2570 enable_irq_wake(host->irq);
2571 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002572 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002573}
2574
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002575EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002576
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002577int sdhci_resume_host(struct sdhci_host *host)
2578{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002579 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002580
Richard Röjforsa13abc72009-09-22 16:45:30 -07002581 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002582 if (host->ops->enable_dma)
2583 host->ops->enable_dma(host);
2584 }
2585
Kevin Liuad080d72013-01-05 17:21:33 +08002586 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell King781e9892014-04-25 12:55:46 +01002587 ret = request_threaded_irq(host->irq, sdhci_irq,
2588 sdhci_thread_irq, IRQF_SHARED,
2589 mmc_hostname(host->mmc), host);
Kevin Liuad080d72013-01-05 17:21:33 +08002590 if (ret)
2591 return ret;
2592 } else {
2593 sdhci_disable_irq_wakeups(host);
2594 disable_irq_wake(host->irq);
2595 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002596
Adrian Hunter6308d292012-02-07 14:48:54 +02002597 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2598 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2599 /* Card keeps power but host controller does not */
2600 sdhci_init(host, 0);
2601 host->pwr = 0;
2602 host->clock = 0;
2603 sdhci_do_set_ios(host, &host->mmc->ios);
2604 } else {
2605 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2606 mmiowb();
2607 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002608
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002609 sdhci_enable_card_detection(host);
2610
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302611 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002612 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302613 host->flags |= SDHCI_NEEDS_RETUNING;
2614
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002615 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002616}
2617
2618EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002619#endif /* CONFIG_PM */
2620
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002621#ifdef CONFIG_PM_RUNTIME
2622
2623static int sdhci_runtime_pm_get(struct sdhci_host *host)
2624{
2625 return pm_runtime_get_sync(host->mmc->parent);
2626}
2627
2628static int sdhci_runtime_pm_put(struct sdhci_host *host)
2629{
2630 pm_runtime_mark_last_busy(host->mmc->parent);
2631 return pm_runtime_put_autosuspend(host->mmc->parent);
2632}
2633
Adrian Hunterf0710a52013-05-06 12:17:32 +03002634static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2635{
2636 if (host->runtime_suspended || host->bus_on)
2637 return;
2638 host->bus_on = true;
2639 pm_runtime_get_noresume(host->mmc->parent);
2640}
2641
2642static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2643{
2644 if (host->runtime_suspended || !host->bus_on)
2645 return;
2646 host->bus_on = false;
2647 pm_runtime_put_noidle(host->mmc->parent);
2648}
2649
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002650int sdhci_runtime_suspend_host(struct sdhci_host *host)
2651{
2652 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002653
2654 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002655 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002656 del_timer_sync(&host->tuning_timer);
2657 host->flags &= ~SDHCI_NEEDS_RETUNING;
2658 }
2659
2660 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002661 host->ier &= SDHCI_INT_CARD_INT;
2662 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2663 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002664 spin_unlock_irqrestore(&host->lock, flags);
2665
Russell King781e9892014-04-25 12:55:46 +01002666 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002667
2668 spin_lock_irqsave(&host->lock, flags);
2669 host->runtime_suspended = true;
2670 spin_unlock_irqrestore(&host->lock, flags);
2671
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002672 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002673}
2674EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2675
2676int sdhci_runtime_resume_host(struct sdhci_host *host)
2677{
2678 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002679 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002680
2681 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2682 if (host->ops->enable_dma)
2683 host->ops->enable_dma(host);
2684 }
2685
2686 sdhci_init(host, 0);
2687
2688 /* Force clock and power re-program */
2689 host->pwr = 0;
2690 host->clock = 0;
2691 sdhci_do_set_ios(host, &host->mmc->ios);
2692
2693 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002694 if ((host_flags & SDHCI_PV_ENABLED) &&
2695 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2696 spin_lock_irqsave(&host->lock, flags);
2697 sdhci_enable_preset_value(host, true);
2698 spin_unlock_irqrestore(&host->lock, flags);
2699 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002700
2701 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002702 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002703 host->flags |= SDHCI_NEEDS_RETUNING;
2704
2705 spin_lock_irqsave(&host->lock, flags);
2706
2707 host->runtime_suspended = false;
2708
2709 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002710 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002711 sdhci_enable_sdio_irq_nolock(host, true);
2712
2713 /* Enable Card Detection */
2714 sdhci_enable_card_detection(host);
2715
2716 spin_unlock_irqrestore(&host->lock, flags);
2717
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002718 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002719}
2720EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2721
2722#endif
2723
Pierre Ossmand129bce2006-03-24 03:18:17 -08002724/*****************************************************************************\
2725 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002726 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002727 * *
2728\*****************************************************************************/
2729
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002730struct sdhci_host *sdhci_alloc_host(struct device *dev,
2731 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002732{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002733 struct mmc_host *mmc;
2734 struct sdhci_host *host;
2735
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002736 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002737
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002738 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002739 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002740 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002741
2742 host = mmc_priv(mmc);
2743 host->mmc = mmc;
2744
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002745 return host;
2746}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002747
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002748EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002749
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002750int sdhci_add_host(struct sdhci_host *host)
2751{
2752 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002753 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302754 u32 max_current_caps;
2755 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002756 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002757
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002758 WARN_ON(host == NULL);
2759 if (host == NULL)
2760 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002761
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002762 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002763
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002764 if (debug_quirks)
2765 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002766 if (debug_quirks2)
2767 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002768
Russell King03231f92014-04-25 12:57:12 +01002769 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002770
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002771 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002772 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2773 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002774 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302775 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002776 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002777 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002778 }
2779
Arindam Nathf2119df2011-05-05 12:18:57 +05302780 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002781 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002782
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002783 if (host->version >= SDHCI_SPEC_300)
2784 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2785 host->caps1 :
2786 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302787
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002788 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002789 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302790 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002791 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002792 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002793 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002794
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002795 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002796 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002797 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002798 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002799 }
2800
Arindam Nathf2119df2011-05-05 12:18:57 +05302801 if ((host->version >= SDHCI_SPEC_200) &&
2802 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002803 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002804
2805 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2806 (host->flags & SDHCI_USE_ADMA)) {
2807 DBG("Disabling ADMA as it is marked broken\n");
2808 host->flags &= ~SDHCI_USE_ADMA;
2809 }
2810
Richard Röjforsa13abc72009-09-22 16:45:30 -07002811 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002812 if (host->ops->enable_dma) {
2813 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302814 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002815 "available. Falling back to PIO.\n",
2816 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002817 host->flags &=
2818 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002819 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002820 }
2821 }
2822
Pierre Ossman2134a922008-06-28 18:28:51 +02002823 if (host->flags & SDHCI_USE_ADMA) {
2824 /*
2825 * We need to allocate descriptors for all sg entries
2826 * (128) and potentially one alignment transfer for
2827 * each of those entries.
2828 */
Russell Kingd1e49f72014-04-25 12:58:34 +01002829 host->adma_desc = dma_alloc_coherent(mmc_dev(host->mmc),
2830 ADMA_SIZE, &host->adma_addr,
2831 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02002832 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2833 if (!host->adma_desc || !host->align_buffer) {
Russell Kingd1e49f72014-04-25 12:58:34 +01002834 dma_free_coherent(mmc_dev(host->mmc), ADMA_SIZE,
2835 host->adma_desc, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02002836 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302837 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002838 "buffers. Falling back to standard DMA.\n",
2839 mmc_hostname(mmc));
2840 host->flags &= ~SDHCI_USE_ADMA;
Russell Kingd1e49f72014-04-25 12:58:34 +01002841 host->adma_desc = NULL;
2842 host->align_buffer = NULL;
2843 } else if (host->adma_addr & 3) {
2844 pr_warning("%s: unable to allocate aligned ADMA descriptor\n",
2845 mmc_hostname(mmc));
2846 host->flags &= ~SDHCI_USE_ADMA;
2847 dma_free_coherent(mmc_dev(host->mmc), ADMA_SIZE,
2848 host->adma_desc, host->adma_addr);
2849 kfree(host->align_buffer);
2850 host->adma_desc = NULL;
2851 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02002852 }
2853 }
2854
Pierre Ossman76591502008-07-21 00:32:11 +02002855 /*
2856 * If we use DMA, then it's up to the caller to set the DMA
2857 * mask, but PIO does not need the hw shim so we set a new
2858 * mask here in that case.
2859 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002860 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002861 host->dma_mask = DMA_BIT_MASK(64);
2862 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2863 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002864
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002865 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302866 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002867 >> SDHCI_CLOCK_BASE_SHIFT;
2868 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302869 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002870 >> SDHCI_CLOCK_BASE_SHIFT;
2871
Pierre Ossmand129bce2006-03-24 03:18:17 -08002872 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002873 if (host->max_clk == 0 || host->quirks &
2874 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002875 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302876 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002877 "frequency.\n", mmc_hostname(mmc));
2878 return -ENODEV;
2879 }
2880 host->max_clk = host->ops->get_max_clock(host);
2881 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002882
2883 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302884 * In case of Host Controller v3.00, find out whether clock
2885 * multiplier is supported.
2886 */
2887 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2888 SDHCI_CLOCK_MUL_SHIFT;
2889
2890 /*
2891 * In case the value in Clock Multiplier is 0, then programmable
2892 * clock mode is not supported, otherwise the actual clock
2893 * multiplier is one more than the value of Clock Multiplier
2894 * in the Capabilities Register.
2895 */
2896 if (host->clk_mul)
2897 host->clk_mul += 1;
2898
2899 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002900 * Set host parameters.
2901 */
2902 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302903 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002904 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002905 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302906 else if (host->version >= SDHCI_SPEC_300) {
2907 if (host->clk_mul) {
2908 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2909 mmc->f_max = host->max_clk * host->clk_mul;
2910 } else
2911 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2912 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002913 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002914
Andy Shevchenko272308c2011-08-03 18:36:00 +03002915 host->timeout_clk =
2916 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2917 if (host->timeout_clk == 0) {
2918 if (host->ops->get_timeout_clock) {
2919 host->timeout_clk = host->ops->get_timeout_clock(host);
2920 } else if (!(host->quirks &
2921 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302922 pr_err("%s: Hardware doesn't specify timeout clock "
Andy Shevchenko272308c2011-08-03 18:36:00 +03002923 "frequency.\n", mmc_hostname(mmc));
2924 return -ENODEV;
2925 }
2926 }
2927 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2928 host->timeout_clk *= 1000;
2929
2930 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002931 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002932
Ulf Hansson68eb80e2013-12-18 09:57:38 +01002933 mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002934
Andrei Warkentine89d4562011-05-23 15:06:37 -05002935 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01002936 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05002937
2938 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2939 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002940
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002941 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002942 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002943 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002944 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002945 host->flags |= SDHCI_AUTO_CMD23;
2946 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2947 } else {
2948 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2949 }
2950
Philip Rakity15ec4462010-11-19 16:48:39 -05002951 /*
2952 * A controller may support 8-bit width, but the board itself
2953 * might not have the pins brought out. Boards that support
2954 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2955 * their platform code before calling sdhci_add_host(), and we
2956 * won't assume 8-bit width for hosts without that CAP.
2957 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002958 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002959 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002960
Jerry Huang63ef5d82012-10-25 13:47:19 +08002961 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
2962 mmc->caps &= ~MMC_CAP_CMD23;
2963
Arindam Nathf2119df2011-05-05 12:18:57 +05302964 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002965 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002966
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002967 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Daniel Drakeeb6d5ae2012-07-05 22:06:13 +01002968 !(host->mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002969 mmc->caps |= MMC_CAP_NEEDS_POLL;
2970
Tim Kryger3a48edc2014-06-13 10:13:56 -07002971 /* If there are external regulators, get them */
2972 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
2973 return -EPROBE_DEFER;
2974
Philip Rakity6231f3d2012-07-23 15:56:23 -07002975 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07002976 if (!IS_ERR(mmc->supply.vqmmc)) {
2977 ret = regulator_enable(mmc->supply.vqmmc);
2978 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
2979 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05002980 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
2981 SDHCI_SUPPORT_SDR50 |
2982 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04002983 if (ret) {
2984 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
2985 mmc_hostname(mmc), ret);
Tim Kryger3a48edc2014-06-13 10:13:56 -07002986 mmc->supply.vqmmc = NULL;
Chris Balla3361ab2013-03-11 17:51:53 -04002987 }
Kevin Liu8363c372012-11-17 17:55:51 -05002988 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07002989
Daniel Drake6a661802012-11-25 13:01:19 -05002990 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
2991 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2992 SDHCI_SUPPORT_DDR50);
2993
Al Cooper4188bba2012-03-16 15:54:17 -04002994 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
2995 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2996 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05302997 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2998
2999 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003000 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303001 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003002 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3003 * field can be promoted to support HS200.
3004 */
David Cohen13868bf2013-10-29 10:58:26 -07003005 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
3006 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003007 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303008 mmc->caps |= MMC_CAP_UHS_SDR50;
3009
Micky Ching9107ebb2014-02-21 18:40:35 +08003010 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3011 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303012 mmc->caps |= MMC_CAP_UHS_DDR50;
3013
Girish K S069c9f12012-01-06 09:56:39 +05303014 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303015 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3016 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3017
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003018 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303019 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003020 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303021
Arindam Nathd6d50a12011-05-05 12:18:59 +05303022 /* Driver Type(s) (A, C, D) supported by the host */
3023 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3024 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3025 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3026 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3027 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3028 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3029
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303030 /* Initial value for re-tuning timer count */
3031 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3032 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3033
3034 /*
3035 * In case Re-tuning Timer is not disabled, the actual value of
3036 * re-tuning timer will be 2 ^ (n - 1).
3037 */
3038 if (host->tuning_count)
3039 host->tuning_count = 1 << (host->tuning_count - 1);
3040
3041 /* Re-tuning mode supported by the Host Controller */
3042 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3043 SDHCI_RETUNING_MODE_SHIFT;
3044
Takashi Iwai8f230f42010-12-08 10:04:30 +01003045 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003046
Arindam Nathf2119df2011-05-05 12:18:57 +05303047 /*
3048 * According to SD Host Controller spec v3.00, if the Host System
3049 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3050 * the value is meaningful only if Voltage Support in the Capabilities
3051 * register is set. The actual current value is 4 times the register
3052 * value.
3053 */
3054 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003055 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
3056 u32 curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003057 if (curr > 0) {
3058
3059 /* convert to SDHCI_MAX_CURRENT format */
3060 curr = curr/1000; /* convert to mA */
3061 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3062
3063 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3064 max_current_caps =
3065 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3066 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3067 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3068 }
3069 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303070
3071 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003072 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303073
Aaron Lu55c46652012-07-04 13:31:48 +08003074 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303075 SDHCI_MAX_CURRENT_330_MASK) >>
3076 SDHCI_MAX_CURRENT_330_SHIFT) *
3077 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303078 }
3079 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003080 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303081
Aaron Lu55c46652012-07-04 13:31:48 +08003082 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303083 SDHCI_MAX_CURRENT_300_MASK) >>
3084 SDHCI_MAX_CURRENT_300_SHIFT) *
3085 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303086 }
3087 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003088 ocr_avail |= MMC_VDD_165_195;
3089
Aaron Lu55c46652012-07-04 13:31:48 +08003090 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303091 SDHCI_MAX_CURRENT_180_MASK) >>
3092 SDHCI_MAX_CURRENT_180_SHIFT) *
3093 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303094 }
3095
Tim Kryger52221612014-06-25 00:25:34 -07003096 /* If OCR set by external regulators, use it instead */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003097 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003098 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003099
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003100 if (host->ocr_mask)
Tim Kryger3a48edc2014-06-13 10:13:56 -07003101 ocr_avail &= host->ocr_mask;
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003102
Takashi Iwai8f230f42010-12-08 10:04:30 +01003103 mmc->ocr_avail = ocr_avail;
3104 mmc->ocr_avail_sdio = ocr_avail;
3105 if (host->ocr_avail_sdio)
3106 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3107 mmc->ocr_avail_sd = ocr_avail;
3108 if (host->ocr_avail_sd)
3109 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3110 else /* normal SD controllers don't support 1.8V */
3111 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3112 mmc->ocr_avail_mmc = ocr_avail;
3113 if (host->ocr_avail_mmc)
3114 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003115
3116 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303117 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003118 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003119 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003120 }
3121
Pierre Ossmand129bce2006-03-24 03:18:17 -08003122 spin_lock_init(&host->lock);
3123
3124 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003125 * Maximum number of segments. Depends on if the hardware
3126 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003127 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003128 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003129 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003130 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003131 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003132 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04003133 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003134
3135 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003136 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01003137 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08003138 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003139 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003140
3141 /*
3142 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003143 * of bytes. When doing hardware scatter/gather, each entry cannot
3144 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003145 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003146 if (host->flags & SDHCI_USE_ADMA) {
3147 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3148 mmc->max_seg_size = 65535;
3149 else
3150 mmc->max_seg_size = 65536;
3151 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003152 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003153 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003154
3155 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003156 * Maximum block size. This varies from controller to controller and
3157 * is specified in the capabilities register.
3158 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003159 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3160 mmc->max_blk_size = 2;
3161 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303162 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003163 SDHCI_MAX_BLOCK_SHIFT;
3164 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303165 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003166 "assuming 512 bytes\n", mmc_hostname(mmc));
3167 mmc->max_blk_size = 0;
3168 }
3169 }
3170
3171 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003172
3173 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003174 * Maximum block count.
3175 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003176 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003177
3178 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003179 * Init tasklets.
3180 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003181 tasklet_init(&host->finish_tasklet,
3182 sdhci_tasklet_finish, (unsigned long)host);
3183
Al Viroe4cad1b2006-10-10 22:47:07 +01003184 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003185
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303186 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303187 init_waitqueue_head(&host->buf_ready_int);
3188
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303189 /* Initialize re-tuning timer */
3190 init_timer(&host->tuning_timer);
3191 host->tuning_timer.data = (unsigned long)host;
3192 host->tuning_timer.function = sdhci_tuning_timer;
3193 }
3194
Shawn Guo2af502c2013-07-05 14:38:55 +08003195 sdhci_init(host, 0);
3196
Russell King781e9892014-04-25 12:55:46 +01003197 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3198 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003199 if (ret) {
3200 pr_err("%s: Failed to request IRQ %d: %d\n",
3201 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003202 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003203 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003204
Pierre Ossmand129bce2006-03-24 03:18:17 -08003205#ifdef CONFIG_MMC_DEBUG
3206 sdhci_dumpregs(host);
3207#endif
3208
Pierre Ossmanf9134312008-12-21 17:01:48 +01003209#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003210 snprintf(host->led_name, sizeof(host->led_name),
3211 "%s::", mmc_hostname(mmc));
3212 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003213 host->led.brightness = LED_OFF;
3214 host->led.default_trigger = mmc_hostname(mmc);
3215 host->led.brightness_set = sdhci_led_control;
3216
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003217 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003218 if (ret) {
3219 pr_err("%s: Failed to register LED device: %d\n",
3220 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003221 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003222 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003223#endif
3224
Pierre Ossman5f25a662006-10-04 02:15:39 -07003225 mmiowb();
3226
Pierre Ossmand129bce2006-03-24 03:18:17 -08003227 mmc_add_host(mmc);
3228
Girish K Sa3c76eb2011-10-11 11:44:09 +05303229 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003230 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003231 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3232 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003233
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003234 sdhci_enable_card_detection(host);
3235
Pierre Ossmand129bce2006-03-24 03:18:17 -08003236 return 0;
3237
Pierre Ossmanf9134312008-12-21 17:01:48 +01003238#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003239reset:
Russell King03231f92014-04-25 12:57:12 +01003240 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003241 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3242 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003243 free_irq(host->irq, host);
3244#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003245untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003246 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003247
3248 return ret;
3249}
3250
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003251EXPORT_SYMBOL_GPL(sdhci_add_host);
3252
Pierre Ossman1e728592008-04-16 19:13:13 +02003253void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003254{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003255 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003256 unsigned long flags;
3257
3258 if (dead) {
3259 spin_lock_irqsave(&host->lock, flags);
3260
3261 host->flags |= SDHCI_DEVICE_DEAD;
3262
3263 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303264 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003265 " transfer!\n", mmc_hostname(host->mmc));
3266
3267 host->mrq->cmd->error = -ENOMEDIUM;
3268 tasklet_schedule(&host->finish_tasklet);
3269 }
3270
3271 spin_unlock_irqrestore(&host->lock, flags);
3272 }
3273
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003274 sdhci_disable_card_detection(host);
3275
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003276 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003277
Pierre Ossmanf9134312008-12-21 17:01:48 +01003278#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003279 led_classdev_unregister(&host->led);
3280#endif
3281
Pierre Ossman1e728592008-04-16 19:13:13 +02003282 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003283 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003284
Russell Kingb537f942014-04-25 12:56:01 +01003285 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3286 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003287 free_irq(host->irq, host);
3288
3289 del_timer_sync(&host->timer);
3290
Pierre Ossmand129bce2006-03-24 03:18:17 -08003291 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003292
Tim Kryger3a48edc2014-06-13 10:13:56 -07003293 if (!IS_ERR(mmc->supply.vmmc))
3294 regulator_disable(mmc->supply.vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003295
Tim Kryger3a48edc2014-06-13 10:13:56 -07003296 if (!IS_ERR(mmc->supply.vqmmc))
3297 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003298
Russell Kingd1e49f72014-04-25 12:58:34 +01003299 if (host->adma_desc)
3300 dma_free_coherent(mmc_dev(host->mmc), ADMA_SIZE,
3301 host->adma_desc, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003302 kfree(host->align_buffer);
3303
3304 host->adma_desc = NULL;
3305 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003306}
3307
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003308EXPORT_SYMBOL_GPL(sdhci_remove_host);
3309
3310void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003311{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003312 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003313}
3314
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003315EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003316
3317/*****************************************************************************\
3318 * *
3319 * Driver init/exit *
3320 * *
3321\*****************************************************************************/
3322
3323static int __init sdhci_drv_init(void)
3324{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303325 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003326 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303327 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003328
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003329 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003330}
3331
3332static void __exit sdhci_drv_exit(void)
3333{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003334}
3335
3336module_init(sdhci_drv_init);
3337module_exit(sdhci_drv_exit);
3338
Pierre Ossmandf673b22006-06-30 02:22:31 -07003339module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003340module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003341
Pierre Ossman32710e82009-04-08 20:14:54 +02003342MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003343MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003344MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003345
Pierre Ossmandf673b22006-06-30 02:22:31 -07003346MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003347MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");