blob: 3d7ebbb82318ae07a8395bd3fb04e8ddeb64d0b3 [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>
Corneliu Doban85cc1c32015-02-09 16:06:29 -080031#include <linux/mmc/sdio.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080032#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080033
Pierre Ossmand129bce2006-03-24 03:18:17 -080034#include "sdhci.h"
35
36#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmand129bce2006-03-24 03:18:17 -080038#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010039 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080040
Pierre Ossmanf9134312008-12-21 17:01:48 +010041#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
42 defined(CONFIG_MMC_SDHCI_MODULE))
43#define SDHCI_USE_LEDS_CLASS
44#endif
45
Arindam Nathb513ea22011-05-05 12:19:04 +053046#define MAX_TUNING_LOOP 40
47
Pierre Ossmandf673b22006-06-30 02:22:31 -070048static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030049static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070050
Pierre Ossmand129bce2006-03-24 03:18:17 -080051static void sdhci_finish_data(struct sdhci_host *);
52
Pierre Ossmand129bce2006-03-24 03:18:17 -080053static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053054static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053055static void sdhci_tuning_timer(unsigned long data);
Kevin Liu52983382013-01-31 11:31:37 +080056static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Haibo Chen348487c2014-12-09 17:04:05 +080057static int sdhci_pre_dma_transfer(struct sdhci_host *host,
58 struct mmc_data *data,
59 struct sdhci_host_next *next);
Pierre Ossmand129bce2006-03-24 03:18:17 -080060
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +010061#ifdef CONFIG_PM
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030062static int sdhci_runtime_pm_get(struct sdhci_host *host);
63static int sdhci_runtime_pm_put(struct sdhci_host *host);
Adrian Hunterf0710a52013-05-06 12:17:32 +030064static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
65static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030066#else
67static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
68{
69 return 0;
70}
71static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
72{
73 return 0;
74}
Adrian Hunterf0710a52013-05-06 12:17:32 +030075static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
76{
77}
78static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
79{
80}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030081#endif
82
Pierre Ossmand129bce2006-03-24 03:18:17 -080083static void sdhci_dumpregs(struct sdhci_host *host)
84{
Girish K Sa3c76eb2011-10-11 11:44:09 +053085 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070086 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080087
Girish K Sa3c76eb2011-10-11 11:44:09 +053088 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030089 sdhci_readl(host, SDHCI_DMA_ADDRESS),
90 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053091 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030092 sdhci_readw(host, SDHCI_BLOCK_SIZE),
93 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053094 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030095 sdhci_readl(host, SDHCI_ARGUMENT),
96 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053097 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030098 sdhci_readl(host, SDHCI_PRESENT_STATE),
99 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530100 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300101 sdhci_readb(host, SDHCI_POWER_CONTROL),
102 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530103 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300104 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
105 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530106 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300107 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
108 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530109 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300110 sdhci_readl(host, SDHCI_INT_ENABLE),
111 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530112 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300113 sdhci_readw(host, SDHCI_ACMD12_ERR),
114 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530115 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300116 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500117 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530118 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500119 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300120 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530121 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530122 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800123
Adrian Huntere57a5f62014-11-04 12:42:46 +0200124 if (host->flags & SDHCI_USE_ADMA) {
125 if (host->flags & SDHCI_USE_64_BIT_DMA)
126 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
127 readl(host->ioaddr + SDHCI_ADMA_ERROR),
128 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
129 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
130 else
131 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
132 readl(host->ioaddr + SDHCI_ADMA_ERROR),
133 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
134 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100135
Girish K Sa3c76eb2011-10-11 11:44:09 +0530136 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800137}
138
139/*****************************************************************************\
140 * *
141 * Low level functions *
142 * *
143\*****************************************************************************/
144
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300145static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
146{
Russell King5b4f1f62014-04-25 12:57:02 +0100147 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300148
Adrian Hunterc79396c2011-12-27 15:48:42 +0200149 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100150 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300151 return;
152
Russell King5b4f1f62014-04-25 12:57:02 +0100153 if (enable) {
154 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
155 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800156
Russell King5b4f1f62014-04-25 12:57:02 +0100157 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
158 SDHCI_INT_CARD_INSERT;
159 } else {
160 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
161 }
Russell Kingb537f942014-04-25 12:56:01 +0100162
163 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
164 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300165}
166
167static void sdhci_enable_card_detection(struct sdhci_host *host)
168{
169 sdhci_set_card_detection(host, true);
170}
171
172static void sdhci_disable_card_detection(struct sdhci_host *host)
173{
174 sdhci_set_card_detection(host, false);
175}
176
Russell King03231f92014-04-25 12:57:12 +0100177void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800178{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700179 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800180
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300181 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800182
Adrian Hunterf0710a52013-05-06 12:17:32 +0300183 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800184 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300185 /* Reset-all turns off SD Bus Power */
186 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
187 sdhci_runtime_pm_bus_off(host);
188 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800189
Pierre Ossmane16514d82006-06-30 02:22:24 -0700190 /* Wait max 100 ms */
191 timeout = 100;
192
193 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300194 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700195 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530196 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700197 mmc_hostname(host->mmc), (int)mask);
198 sdhci_dumpregs(host);
199 return;
200 }
201 timeout--;
202 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800203 }
Russell King03231f92014-04-25 12:57:12 +0100204}
205EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300206
Russell King03231f92014-04-25 12:57:12 +0100207static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
208{
209 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
210 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
211 SDHCI_CARD_PRESENT))
212 return;
213 }
214
215 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800216
Russell Kingda91a8f2014-04-25 13:00:12 +0100217 if (mask & SDHCI_RESET_ALL) {
218 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
219 if (host->ops->enable_dma)
220 host->ops->enable_dma(host);
221 }
222
223 /* Resetting the controller clears many */
224 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800225 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226}
227
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800228static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
229
230static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800231{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800232 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100233 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800234 else
Russell King03231f92014-04-25 12:57:12 +0100235 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800236
Russell Kingb537f942014-04-25 12:56:01 +0100237 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
238 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
239 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
240 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
241 SDHCI_INT_RESPONSE;
242
243 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
244 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800245
246 if (soft) {
247 /* force clock reconfiguration */
248 host->clock = 0;
249 sdhci_set_ios(host->mmc, &host->mmc->ios);
250 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300251}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800252
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300253static void sdhci_reinit(struct sdhci_host *host)
254{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800255 sdhci_init(host, 0);
Aaron Lub67c6b42012-06-29 16:17:31 +0800256 /*
257 * Retuning stuffs are affected by different cards inserted and only
258 * applicable to UHS-I cards. So reset these fields to their initial
259 * value when card is removed.
260 */
Aaron Lu973905f2012-07-04 13:29:09 +0800261 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
262 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
263
Aaron Lub67c6b42012-06-29 16:17:31 +0800264 del_timer_sync(&host->tuning_timer);
265 host->flags &= ~SDHCI_NEEDS_RETUNING;
Aaron Lub67c6b42012-06-29 16:17:31 +0800266 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300267 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800268}
269
270static void sdhci_activate_led(struct sdhci_host *host)
271{
272 u8 ctrl;
273
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300274 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800275 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300276 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800277}
278
279static void sdhci_deactivate_led(struct sdhci_host *host)
280{
281 u8 ctrl;
282
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300283 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800284 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300285 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800286}
287
Pierre Ossmanf9134312008-12-21 17:01:48 +0100288#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100289static void sdhci_led_control(struct led_classdev *led,
290 enum led_brightness brightness)
291{
292 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
293 unsigned long flags;
294
295 spin_lock_irqsave(&host->lock, flags);
296
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300297 if (host->runtime_suspended)
298 goto out;
299
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100300 if (brightness == LED_OFF)
301 sdhci_deactivate_led(host);
302 else
303 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300304out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100305 spin_unlock_irqrestore(&host->lock, flags);
306}
307#endif
308
Pierre Ossmand129bce2006-03-24 03:18:17 -0800309/*****************************************************************************\
310 * *
311 * Core functions *
312 * *
313\*****************************************************************************/
314
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100315static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800316{
Pierre Ossman76591502008-07-21 00:32:11 +0200317 unsigned long flags;
318 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700319 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200320 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800321
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100322 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800323
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100324 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200325 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800326
Pierre Ossman76591502008-07-21 00:32:11 +0200327 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800328
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100329 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200330 if (!sg_miter_next(&host->sg_miter))
331 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800332
Pierre Ossman76591502008-07-21 00:32:11 +0200333 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800334
Pierre Ossman76591502008-07-21 00:32:11 +0200335 blksize -= len;
336 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200337
Pierre Ossman76591502008-07-21 00:32:11 +0200338 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800339
Pierre Ossman76591502008-07-21 00:32:11 +0200340 while (len) {
341 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300342 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200343 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800344 }
Pierre Ossman76591502008-07-21 00:32:11 +0200345
346 *buf = scratch & 0xFF;
347
348 buf++;
349 scratch >>= 8;
350 chunk--;
351 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800352 }
353 }
Pierre Ossman76591502008-07-21 00:32:11 +0200354
355 sg_miter_stop(&host->sg_miter);
356
357 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800359
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100360static void sdhci_write_block_pio(struct sdhci_host *host)
361{
Pierre Ossman76591502008-07-21 00:32:11 +0200362 unsigned long flags;
363 size_t blksize, len, chunk;
364 u32 scratch;
365 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100366
367 DBG("PIO writing\n");
368
369 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200370 chunk = 0;
371 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100372
Pierre Ossman76591502008-07-21 00:32:11 +0200373 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100374
375 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200376 if (!sg_miter_next(&host->sg_miter))
377 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100378
Pierre Ossman76591502008-07-21 00:32:11 +0200379 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200380
Pierre Ossman76591502008-07-21 00:32:11 +0200381 blksize -= len;
382 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100383
Pierre Ossman76591502008-07-21 00:32:11 +0200384 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100385
Pierre Ossman76591502008-07-21 00:32:11 +0200386 while (len) {
387 scratch |= (u32)*buf << (chunk * 8);
388
389 buf++;
390 chunk++;
391 len--;
392
393 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300394 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200395 chunk = 0;
396 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100397 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100398 }
399 }
Pierre Ossman76591502008-07-21 00:32:11 +0200400
401 sg_miter_stop(&host->sg_miter);
402
403 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100404}
405
406static void sdhci_transfer_pio(struct sdhci_host *host)
407{
408 u32 mask;
409
410 BUG_ON(!host->data);
411
Pierre Ossman76591502008-07-21 00:32:11 +0200412 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100413 return;
414
415 if (host->data->flags & MMC_DATA_READ)
416 mask = SDHCI_DATA_AVAILABLE;
417 else
418 mask = SDHCI_SPACE_AVAILABLE;
419
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200420 /*
421 * Some controllers (JMicron JMB38x) mess up the buffer bits
422 * for transfers < 4 bytes. As long as it is just one block,
423 * we can ignore the bits.
424 */
425 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
426 (host->data->blocks == 1))
427 mask = ~0;
428
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300429 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300430 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
431 udelay(100);
432
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100433 if (host->data->flags & MMC_DATA_READ)
434 sdhci_read_block_pio(host);
435 else
436 sdhci_write_block_pio(host);
437
Pierre Ossman76591502008-07-21 00:32:11 +0200438 host->blocks--;
439 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100440 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100441 }
442
443 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800444}
445
Pierre Ossman2134a922008-06-28 18:28:51 +0200446static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
447{
448 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800449 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200450}
451
452static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
453{
Cong Wang482fce92011-11-27 13:27:00 +0800454 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200455 local_irq_restore(*flags);
456}
457
Adrian Huntere57a5f62014-11-04 12:42:46 +0200458static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
459 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800460{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200461 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800462
Adrian Huntere57a5f62014-11-04 12:42:46 +0200463 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200464 dma_desc->cmd = cpu_to_le16(cmd);
465 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200466 dma_desc->addr_lo = cpu_to_le32((u32)addr);
467
468 if (host->flags & SDHCI_USE_64_BIT_DMA)
469 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800470}
471
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200472static void sdhci_adma_mark_end(void *desc)
473{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200474 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200475
Adrian Huntere57a5f62014-11-04 12:42:46 +0200476 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200477 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200478}
479
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200480static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200481 struct mmc_data *data)
482{
483 int direction;
484
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200485 void *desc;
486 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200487 dma_addr_t addr;
488 dma_addr_t align_addr;
489 int len, offset;
490
491 struct scatterlist *sg;
492 int i;
493 char *buffer;
494 unsigned long flags;
495
496 /*
497 * The spec does not specify endianness of descriptor table.
498 * We currently guess that it is LE.
499 */
500
501 if (data->flags & MMC_DATA_READ)
502 direction = DMA_FROM_DEVICE;
503 else
504 direction = DMA_TO_DEVICE;
505
Pierre Ossman2134a922008-06-28 18:28:51 +0200506 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200507 host->align_buffer, host->align_buffer_sz, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700508 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200509 goto fail;
Adrian Hunter76fe3792014-11-04 12:42:42 +0200510 BUG_ON(host->align_addr & host->align_mask);
Pierre Ossman2134a922008-06-28 18:28:51 +0200511
Haibo Chen348487c2014-12-09 17:04:05 +0800512 host->sg_count = sdhci_pre_dma_transfer(host, data, NULL);
513 if (host->sg_count < 0)
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200514 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200515
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200516 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200517 align = host->align_buffer;
518
519 align_addr = host->align_addr;
520
521 for_each_sg(data->sg, sg, host->sg_count, i) {
522 addr = sg_dma_address(sg);
523 len = sg_dma_len(sg);
524
525 /*
526 * The SDHCI specification states that ADMA
527 * addresses must be 32-bit aligned. If they
528 * aren't, then we use a bounce buffer for
529 * the (up to three) bytes that screw up the
530 * alignment.
531 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200532 offset = (host->align_sz - (addr & host->align_mask)) &
533 host->align_mask;
Pierre Ossman2134a922008-06-28 18:28:51 +0200534 if (offset) {
535 if (data->flags & MMC_DATA_WRITE) {
536 buffer = sdhci_kmap_atomic(sg, &flags);
537 memcpy(align, buffer, offset);
538 sdhci_kunmap_atomic(buffer, &flags);
539 }
540
Ben Dooks118cd172010-03-05 13:43:26 -0800541 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200542 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200543 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200544
545 BUG_ON(offset > 65536);
546
Adrian Hunter76fe3792014-11-04 12:42:42 +0200547 align += host->align_sz;
548 align_addr += host->align_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200549
Adrian Hunter76fe3792014-11-04 12:42:42 +0200550 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200551
552 addr += offset;
553 len -= offset;
554 }
555
Pierre Ossman2134a922008-06-28 18:28:51 +0200556 BUG_ON(len > 65536);
557
Ben Dooks118cd172010-03-05 13:43:26 -0800558 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200559 sdhci_adma_write_desc(host, desc, addr, len, ADMA2_TRAN_VALID);
Adrian Hunter76fe3792014-11-04 12:42:42 +0200560 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200561
562 /*
563 * If this triggers then we have a calculation bug
564 * somewhere. :/
565 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200566 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200567 }
568
Thomas Abraham70764a92010-05-26 14:42:04 -0700569 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
570 /*
571 * Mark the last descriptor as the terminating descriptor
572 */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200573 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200574 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200575 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700576 }
577 } else {
578 /*
579 * Add a terminating entry.
580 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200581
Thomas Abraham70764a92010-05-26 14:42:04 -0700582 /* nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200583 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700584 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200585
586 /*
587 * Resync align buffer as we might have changed it.
588 */
589 if (data->flags & MMC_DATA_WRITE) {
590 dma_sync_single_for_device(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200591 host->align_addr, host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200592 }
593
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200594 return 0;
595
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200596unmap_align:
597 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200598 host->align_buffer_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200599fail:
600 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200601}
602
603static void sdhci_adma_table_post(struct sdhci_host *host,
604 struct mmc_data *data)
605{
606 int direction;
607
608 struct scatterlist *sg;
609 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200610 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200611 char *buffer;
612 unsigned long flags;
Russell Kingde0b65a2014-04-25 12:58:29 +0100613 bool has_unaligned;
Pierre Ossman2134a922008-06-28 18:28:51 +0200614
615 if (data->flags & MMC_DATA_READ)
616 direction = DMA_FROM_DEVICE;
617 else
618 direction = DMA_TO_DEVICE;
619
Pierre Ossman2134a922008-06-28 18:28:51 +0200620 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200621 host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200622
Russell Kingde0b65a2014-04-25 12:58:29 +0100623 /* Do a quick scan of the SG list for any unaligned mappings */
624 has_unaligned = false;
625 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter76fe3792014-11-04 12:42:42 +0200626 if (sg_dma_address(sg) & host->align_mask) {
Russell Kingde0b65a2014-04-25 12:58:29 +0100627 has_unaligned = true;
628 break;
629 }
630
631 if (has_unaligned && data->flags & MMC_DATA_READ) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200632 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
633 data->sg_len, direction);
634
635 align = host->align_buffer;
636
637 for_each_sg(data->sg, sg, host->sg_count, i) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200638 if (sg_dma_address(sg) & host->align_mask) {
639 size = host->align_sz -
640 (sg_dma_address(sg) & host->align_mask);
Pierre Ossman2134a922008-06-28 18:28:51 +0200641
642 buffer = sdhci_kmap_atomic(sg, &flags);
643 memcpy(buffer, align, size);
644 sdhci_kunmap_atomic(buffer, &flags);
645
Adrian Hunter76fe3792014-11-04 12:42:42 +0200646 align += host->align_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200647 }
648 }
649 }
650
Haibo Chen348487c2014-12-09 17:04:05 +0800651 if (!data->host_cookie)
652 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
653 data->sg_len, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200654}
655
Andrei Warkentina3c77782011-04-11 16:13:42 -0500656static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800657{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700658 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500659 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700660 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800661
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200662 /*
663 * If the host controller provides us with an incorrect timeout
664 * value, just skip the check and use 0xE. The hardware may take
665 * longer to time out, but that's much better than having a too-short
666 * timeout value.
667 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200668 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200669 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200670
Andrei Warkentina3c77782011-04-11 16:13:42 -0500671 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100672 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500673 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800674
Andrei Warkentina3c77782011-04-11 16:13:42 -0500675 /* timeout in us */
676 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100677 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300678 else {
679 target_timeout = data->timeout_ns / 1000;
680 if (host->clock)
681 target_timeout += data->timeout_clks / host->clock;
682 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700683
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700684 /*
685 * Figure out needed cycles.
686 * We do this in steps in order to fit inside a 32 bit int.
687 * The first step is the minimum timeout, which will have a
688 * minimum resolution of 6 bits:
689 * (1) 2^13*1000 > 2^22,
690 * (2) host->timeout_clk < 2^16
691 * =>
692 * (1) / (2) > 2^6
693 */
694 count = 0;
695 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
696 while (current_timeout < target_timeout) {
697 count++;
698 current_timeout <<= 1;
699 if (count >= 0xF)
700 break;
701 }
702
703 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400704 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
705 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700706 count = 0xE;
707 }
708
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200709 return count;
710}
711
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300712static void sdhci_set_transfer_irqs(struct sdhci_host *host)
713{
714 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
715 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
716
717 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100718 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300719 else
Russell Kingb537f942014-04-25 12:56:01 +0100720 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
721
722 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
723 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300724}
725
Aisheng Dongb45e6682014-08-27 15:26:29 +0800726static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200727{
728 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800729
730 if (host->ops->set_timeout) {
731 host->ops->set_timeout(host, cmd);
732 } else {
733 count = sdhci_calc_timeout(host, cmd);
734 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
735 }
736}
737
738static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
739{
Pierre Ossman2134a922008-06-28 18:28:51 +0200740 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500741 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200742 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200743
744 WARN_ON(host->data);
745
Aisheng Dongb45e6682014-08-27 15:26:29 +0800746 if (data || (cmd->flags & MMC_RSP_BUSY))
747 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500748
749 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200750 return;
751
752 /* Sanity checks */
753 BUG_ON(data->blksz * data->blocks > 524288);
754 BUG_ON(data->blksz > host->mmc->max_blk_size);
755 BUG_ON(data->blocks > 65535);
756
757 host->data = data;
758 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400759 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200760
Richard Röjforsa13abc72009-09-22 16:45:30 -0700761 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100762 host->flags |= SDHCI_REQ_USE_DMA;
763
Pierre Ossman2134a922008-06-28 18:28:51 +0200764 /*
765 * FIXME: This doesn't account for merging when mapping the
766 * scatterlist.
767 */
768 if (host->flags & SDHCI_REQ_USE_DMA) {
769 int broken, i;
770 struct scatterlist *sg;
771
772 broken = 0;
773 if (host->flags & SDHCI_USE_ADMA) {
774 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
775 broken = 1;
776 } else {
777 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
778 broken = 1;
779 }
780
781 if (unlikely(broken)) {
782 for_each_sg(data->sg, sg, data->sg_len, i) {
783 if (sg->length & 0x3) {
784 DBG("Reverting to PIO because of "
785 "transfer size (%d)\n",
786 sg->length);
787 host->flags &= ~SDHCI_REQ_USE_DMA;
788 break;
789 }
790 }
791 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100792 }
793
794 /*
795 * The assumption here being that alignment is the same after
796 * translation to device address space.
797 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200798 if (host->flags & SDHCI_REQ_USE_DMA) {
799 int broken, i;
800 struct scatterlist *sg;
801
802 broken = 0;
803 if (host->flags & SDHCI_USE_ADMA) {
804 /*
805 * As we use 3 byte chunks to work around
806 * alignment problems, we need to check this
807 * quirk.
808 */
809 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
810 broken = 1;
811 } else {
812 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
813 broken = 1;
814 }
815
816 if (unlikely(broken)) {
817 for_each_sg(data->sg, sg, data->sg_len, i) {
818 if (sg->offset & 0x3) {
819 DBG("Reverting to PIO because of "
820 "bad alignment\n");
821 host->flags &= ~SDHCI_REQ_USE_DMA;
822 break;
823 }
824 }
825 }
826 }
827
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200828 if (host->flags & SDHCI_REQ_USE_DMA) {
829 if (host->flags & SDHCI_USE_ADMA) {
830 ret = sdhci_adma_table_pre(host, data);
831 if (ret) {
832 /*
833 * This only happens when someone fed
834 * us an invalid request.
835 */
836 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200837 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200838 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300839 sdhci_writel(host, host->adma_addr,
840 SDHCI_ADMA_ADDRESS);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200841 if (host->flags & SDHCI_USE_64_BIT_DMA)
842 sdhci_writel(host,
843 (u64)host->adma_addr >> 32,
844 SDHCI_ADMA_ADDRESS_HI);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200845 }
846 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300847 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200848
Haibo Chen348487c2014-12-09 17:04:05 +0800849 sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300850 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200851 /*
852 * This only happens when someone fed
853 * us an invalid request.
854 */
855 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200856 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200857 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200858 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300859 sdhci_writel(host, sg_dma_address(data->sg),
860 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200861 }
862 }
863 }
864
Pierre Ossman2134a922008-06-28 18:28:51 +0200865 /*
866 * Always adjust the DMA selection as some controllers
867 * (e.g. JMicron) can't do PIO properly when the selection
868 * is ADMA.
869 */
870 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300871 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200872 ctrl &= ~SDHCI_CTRL_DMA_MASK;
873 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200874 (host->flags & SDHCI_USE_ADMA)) {
875 if (host->flags & SDHCI_USE_64_BIT_DMA)
876 ctrl |= SDHCI_CTRL_ADMA64;
877 else
878 ctrl |= SDHCI_CTRL_ADMA32;
879 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200880 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200881 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300882 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100883 }
884
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200885 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200886 int flags;
887
888 flags = SG_MITER_ATOMIC;
889 if (host->data->flags & MMC_DATA_READ)
890 flags |= SG_MITER_TO_SG;
891 else
892 flags |= SG_MITER_FROM_SG;
893 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200894 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800895 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700896
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300897 sdhci_set_transfer_irqs(host);
898
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400899 /* Set the DMA boundary value and block size */
900 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
901 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300902 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700903}
904
905static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500906 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700907{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800908 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500909 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700910
Dong Aisheng2b558c12013-10-30 22:09:48 +0800911 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800912 if (host->quirks2 &
913 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
914 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
915 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800916 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800917 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
918 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800919 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800920 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700921 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800922 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700923
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200924 WARN_ON(!host->data);
925
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800926 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
927 mode = SDHCI_TRNS_BLK_CNT_EN;
928
Andrei Warkentine89d4562011-05-23 15:06:37 -0500929 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800930 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500931 /*
932 * If we are sending CMD23, CMD12 never gets sent
933 * on successful completion (so no Auto-CMD12).
934 */
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800935 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
936 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500937 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500938 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
939 mode |= SDHCI_TRNS_AUTO_CMD23;
940 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
941 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700942 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500943
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700944 if (data->flags & MMC_DATA_READ)
945 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100946 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700947 mode |= SDHCI_TRNS_DMA;
948
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300949 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800950}
951
952static void sdhci_finish_data(struct sdhci_host *host)
953{
954 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800955
956 BUG_ON(!host->data);
957
958 data = host->data;
959 host->data = NULL;
960
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100961 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200962 if (host->flags & SDHCI_USE_ADMA)
963 sdhci_adma_table_post(host, data);
964 else {
Haibo Chen348487c2014-12-09 17:04:05 +0800965 if (!data->host_cookie)
966 dma_unmap_sg(mmc_dev(host->mmc),
967 data->sg, data->sg_len,
968 (data->flags & MMC_DATA_READ) ?
Pierre Ossman2134a922008-06-28 18:28:51 +0200969 DMA_FROM_DEVICE : DMA_TO_DEVICE);
970 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800971 }
972
973 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200974 * The specification states that the block count register must
975 * be updated, but it does not specify at what point in the
976 * data flow. That makes the register entirely useless to read
977 * back so we have to assume that nothing made it to the card
978 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800979 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200980 if (data->error)
981 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800982 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200983 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800984
Andrei Warkentine89d4562011-05-23 15:06:37 -0500985 /*
986 * Need to send CMD12 if -
987 * a) open-ended multiblock transfer (no CMD23)
988 * b) error in multiblock transfer
989 */
990 if (data->stop &&
991 (data->error ||
992 !host->mrq->sbc)) {
993
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994 /*
995 * The controller needs a reset of internal state machines
996 * upon error conditions.
997 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200998 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100999 sdhci_do_reset(host, SDHCI_RESET_CMD);
1000 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001001 }
1002
1003 sdhci_send_command(host, data->stop);
1004 } else
1005 tasklet_schedule(&host->finish_tasklet);
1006}
1007
Dong Aishengc0e551292013-09-13 19:11:31 +08001008void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009{
1010 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001011 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001012 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001013
1014 WARN_ON(host->cmd);
1015
Pierre Ossmand129bce2006-03-24 03:18:17 -08001016 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001017 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001018
1019 mask = SDHCI_CMD_INHIBIT;
1020 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1021 mask |= SDHCI_DATA_INHIBIT;
1022
1023 /* We shouldn't wait for data inihibit for stop commands, even
1024 though they might use busy signaling */
1025 if (host->mrq->data && (cmd == host->mrq->data->stop))
1026 mask &= ~SDHCI_DATA_INHIBIT;
1027
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001028 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001029 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301030 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001031 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001032 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001033 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001034 tasklet_schedule(&host->finish_tasklet);
1035 return;
1036 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001037 timeout--;
1038 mdelay(1);
1039 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001040
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001041 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001042 if (!cmd->data && cmd->busy_timeout > 9000)
1043 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001044 else
1045 timeout += 10 * HZ;
1046 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047
1048 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001049 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001050
Andrei Warkentina3c77782011-04-11 16:13:42 -05001051 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001052
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001053 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001054
Andrei Warkentine89d4562011-05-23 15:06:37 -05001055 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001056
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301058 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001059 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001060 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001061 tasklet_schedule(&host->finish_tasklet);
1062 return;
1063 }
1064
1065 if (!(cmd->flags & MMC_RSP_PRESENT))
1066 flags = SDHCI_CMD_RESP_NONE;
1067 else if (cmd->flags & MMC_RSP_136)
1068 flags = SDHCI_CMD_RESP_LONG;
1069 else if (cmd->flags & MMC_RSP_BUSY)
1070 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1071 else
1072 flags = SDHCI_CMD_RESP_SHORT;
1073
1074 if (cmd->flags & MMC_RSP_CRC)
1075 flags |= SDHCI_CMD_CRC;
1076 if (cmd->flags & MMC_RSP_OPCODE)
1077 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301078
1079 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301080 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1081 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001082 flags |= SDHCI_CMD_DATA;
1083
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001084 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001085}
Dong Aishengc0e551292013-09-13 19:11:31 +08001086EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001087
1088static void sdhci_finish_command(struct sdhci_host *host)
1089{
1090 int i;
1091
1092 BUG_ON(host->cmd == NULL);
1093
1094 if (host->cmd->flags & MMC_RSP_PRESENT) {
1095 if (host->cmd->flags & MMC_RSP_136) {
1096 /* CRC is stripped so we need to do some shifting. */
1097 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001098 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001099 SDHCI_RESPONSE + (3-i)*4) << 8;
1100 if (i != 3)
1101 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001102 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001103 SDHCI_RESPONSE + (3-i)*4-1);
1104 }
1105 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001106 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001107 }
1108 }
1109
Pierre Ossman17b04292007-07-22 22:18:46 +02001110 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001111
Andrei Warkentine89d4562011-05-23 15:06:37 -05001112 /* Finished CMD23, now send actual command. */
1113 if (host->cmd == host->mrq->sbc) {
1114 host->cmd = NULL;
1115 sdhci_send_command(host, host->mrq->cmd);
1116 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001117
Andrei Warkentine89d4562011-05-23 15:06:37 -05001118 /* Processed actual command. */
1119 if (host->data && host->data_early)
1120 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001121
Andrei Warkentine89d4562011-05-23 15:06:37 -05001122 if (!host->cmd->data)
1123 tasklet_schedule(&host->finish_tasklet);
1124
1125 host->cmd = NULL;
1126 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001127}
1128
Kevin Liu52983382013-01-31 11:31:37 +08001129static u16 sdhci_get_preset_value(struct sdhci_host *host)
1130{
Russell Kingd975f122014-04-25 12:59:31 +01001131 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001132
Russell Kingd975f122014-04-25 12:59:31 +01001133 switch (host->timing) {
1134 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001135 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1136 break;
Russell Kingd975f122014-04-25 12:59:31 +01001137 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001138 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1139 break;
Russell Kingd975f122014-04-25 12:59:31 +01001140 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001141 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1142 break;
Russell Kingd975f122014-04-25 12:59:31 +01001143 case MMC_TIMING_UHS_SDR104:
1144 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001145 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1146 break;
Russell Kingd975f122014-04-25 12:59:31 +01001147 case MMC_TIMING_UHS_DDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001148 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1149 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001150 case MMC_TIMING_MMC_HS400:
1151 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1152 break;
Kevin Liu52983382013-01-31 11:31:37 +08001153 default:
1154 pr_warn("%s: Invalid UHS-I mode selected\n",
1155 mmc_hostname(host->mmc));
1156 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1157 break;
1158 }
1159 return preset;
1160}
1161
Russell King17710592014-04-25 12:58:55 +01001162void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001163{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301164 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001165 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301166 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001167 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001168
Russell King1650d0c2014-04-25 12:58:50 +01001169 host->mmc->actual_clock = 0;
1170
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001171 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001172
1173 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001174 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001175
Zhangfei Gao85105c52010-08-06 07:10:01 +08001176 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001177 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001178 u16 pre_val;
1179
1180 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1181 pre_val = sdhci_get_preset_value(host);
1182 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1183 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1184 if (host->clk_mul &&
1185 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1186 clk = SDHCI_PROG_CLOCK_MODE;
1187 real_div = div + 1;
1188 clk_mul = host->clk_mul;
1189 } else {
1190 real_div = max_t(int, 1, div << 1);
1191 }
1192 goto clock_set;
1193 }
1194
Arindam Nathc3ed3872011-05-05 12:19:06 +05301195 /*
1196 * Check if the Host Controller supports Programmable Clock
1197 * Mode.
1198 */
1199 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001200 for (div = 1; div <= 1024; div++) {
1201 if ((host->max_clk * host->clk_mul / div)
1202 <= clock)
1203 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001204 }
Kevin Liu52983382013-01-31 11:31:37 +08001205 /*
1206 * Set Programmable Clock Mode in the Clock
1207 * Control register.
1208 */
1209 clk = SDHCI_PROG_CLOCK_MODE;
1210 real_div = div;
1211 clk_mul = host->clk_mul;
1212 div--;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301213 } else {
1214 /* Version 3.00 divisors must be a multiple of 2. */
1215 if (host->max_clk <= clock)
1216 div = 1;
1217 else {
1218 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1219 div += 2) {
1220 if ((host->max_clk / div) <= clock)
1221 break;
1222 }
1223 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001224 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301225 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001226 }
1227 } else {
1228 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001229 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001230 if ((host->max_clk / div) <= clock)
1231 break;
1232 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001233 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301234 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001235 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001236
Kevin Liu52983382013-01-31 11:31:37 +08001237clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001238 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001239 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301240 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001241 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1242 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001243 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001244 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001245
Chris Ball27f6cb12009-09-22 16:45:31 -07001246 /* Wait max 20 ms */
1247 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001248 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001249 & SDHCI_CLOCK_INT_STABLE)) {
1250 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301251 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001252 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001253 sdhci_dumpregs(host);
1254 return;
1255 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001256 timeout--;
1257 mdelay(1);
1258 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001259
1260 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001261 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001262}
Russell King17710592014-04-25 12:58:55 +01001263EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001264
Russell King24fbb3c2014-04-25 13:00:06 +01001265static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1266 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001267{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001268 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001269 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001270
Tim Kryger52221612014-06-25 00:25:34 -07001271 if (!IS_ERR(mmc->supply.vmmc)) {
1272 spin_unlock_irq(&host->lock);
Markus Mayer4e743f12014-07-03 13:27:42 -07001273 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Tim Kryger52221612014-06-25 00:25:34 -07001274 spin_lock_irq(&host->lock);
Tim Kryger3cbc6122015-01-14 07:24:12 +01001275
1276 if (mode != MMC_POWER_OFF)
1277 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1278 else
1279 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1280
Tim Kryger52221612014-06-25 00:25:34 -07001281 return;
1282 }
1283
Russell King24fbb3c2014-04-25 13:00:06 +01001284 if (mode != MMC_POWER_OFF) {
1285 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001286 case MMC_VDD_165_195:
1287 pwr = SDHCI_POWER_180;
1288 break;
1289 case MMC_VDD_29_30:
1290 case MMC_VDD_30_31:
1291 pwr = SDHCI_POWER_300;
1292 break;
1293 case MMC_VDD_32_33:
1294 case MMC_VDD_33_34:
1295 pwr = SDHCI_POWER_330;
1296 break;
1297 default:
1298 BUG();
1299 }
1300 }
1301
1302 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001303 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001304
Pierre Ossmanae628902009-05-03 20:45:03 +02001305 host->pwr = pwr;
1306
1307 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001308 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001309 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1310 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001311 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001312 } else {
1313 /*
1314 * Spec says that we should clear the power reg before setting
1315 * a new value. Some controllers don't seem to like this though.
1316 */
1317 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1318 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001319
Russell Kinge921a8b2014-04-25 13:00:01 +01001320 /*
1321 * At least the Marvell CaFe chip gets confused if we set the
1322 * voltage and set turn on power at the same time, so set the
1323 * voltage first.
1324 */
1325 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1326 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001327
Russell Kinge921a8b2014-04-25 13:00:01 +01001328 pwr |= SDHCI_POWER_ON;
1329
Pierre Ossmanae628902009-05-03 20:45:03 +02001330 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1331
Russell Kinge921a8b2014-04-25 13:00:01 +01001332 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1333 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001334
Russell Kinge921a8b2014-04-25 13:00:01 +01001335 /*
1336 * Some controllers need an extra 10ms delay of 10ms before
1337 * they can apply clock after applying power
1338 */
1339 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1340 mdelay(10);
1341 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001342}
1343
Pierre Ossmand129bce2006-03-24 03:18:17 -08001344/*****************************************************************************\
1345 * *
1346 * MMC callbacks *
1347 * *
1348\*****************************************************************************/
1349
1350static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1351{
1352 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001353 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001354 unsigned long flags;
Aaron Lu473b0952012-07-03 17:27:49 +08001355 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001356
1357 host = mmc_priv(mmc);
1358
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001359 sdhci_runtime_pm_get(host);
1360
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001361 present = mmc_gpio_get_cd(host->mmc);
1362
Pierre Ossmand129bce2006-03-24 03:18:17 -08001363 spin_lock_irqsave(&host->lock, flags);
1364
1365 WARN_ON(host->mrq != NULL);
1366
Pierre Ossmanf9134312008-12-21 17:01:48 +01001367#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001368 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001369#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001370
1371 /*
1372 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1373 * requests if Auto-CMD12 is enabled.
1374 */
1375 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001376 if (mrq->stop) {
1377 mrq->data->stop = NULL;
1378 mrq->stop = NULL;
1379 }
1380 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001381
1382 host->mrq = mrq;
1383
Shawn Guo505a8682012-12-11 15:23:42 +08001384 /*
1385 * Firstly check card presence from cd-gpio. The return could
1386 * be one of the following possibilities:
1387 * negative: cd-gpio is not available
1388 * zero: cd-gpio is used, and card is removed
1389 * one: cd-gpio is used, and card is present
1390 */
Shawn Guo505a8682012-12-11 15:23:42 +08001391 if (present < 0) {
1392 /* If polling, assume that the card is always present. */
1393 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1394 present = 1;
1395 else
1396 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1397 SDHCI_CARD_PRESENT;
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +08001398 }
1399
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001400 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001401 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001402 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301403 } else {
1404 u32 present_state;
1405
1406 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1407 /*
1408 * Check if the re-tuning timer has already expired and there
Yi Sun7756a96d2014-09-09 02:13:59 +00001409 * is no on-going data transfer and DAT0 is not busy. If so,
1410 * we need to execute tuning procedure before sending command.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301411 */
1412 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
Yi Sun7756a96d2014-09-09 02:13:59 +00001413 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ)) &&
1414 (present_state & SDHCI_DATA_0_LVL_MASK)) {
Chris Ball14efd952012-11-05 14:29:49 -05001415 if (mmc->card) {
1416 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1417 tuning_opcode =
1418 mmc->card->type == MMC_TYPE_MMC ?
1419 MMC_SEND_TUNING_BLOCK_HS200 :
1420 MMC_SEND_TUNING_BLOCK;
Chuansheng Liu63c21182013-11-05 14:52:45 +08001421
1422 /* Here we need to set the host->mrq to NULL,
1423 * in case the pending finish_tasklet
1424 * finishes it incorrectly.
1425 */
1426 host->mrq = NULL;
1427
Chris Ball14efd952012-11-05 14:29:49 -05001428 spin_unlock_irqrestore(&host->lock, flags);
1429 sdhci_execute_tuning(mmc, tuning_opcode);
1430 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301431
Chris Ball14efd952012-11-05 14:29:49 -05001432 /* Restore original mmc_request structure */
1433 host->mrq = mrq;
1434 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301435 }
1436
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001437 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001438 sdhci_send_command(host, mrq->sbc);
1439 else
1440 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301441 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001442
Pierre Ossman5f25a662006-10-04 02:15:39 -07001443 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001444 spin_unlock_irqrestore(&host->lock, flags);
1445}
1446
Russell King2317f562014-04-25 12:57:07 +01001447void sdhci_set_bus_width(struct sdhci_host *host, int width)
1448{
1449 u8 ctrl;
1450
1451 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1452 if (width == MMC_BUS_WIDTH_8) {
1453 ctrl &= ~SDHCI_CTRL_4BITBUS;
1454 if (host->version >= SDHCI_SPEC_300)
1455 ctrl |= SDHCI_CTRL_8BITBUS;
1456 } else {
1457 if (host->version >= SDHCI_SPEC_300)
1458 ctrl &= ~SDHCI_CTRL_8BITBUS;
1459 if (width == MMC_BUS_WIDTH_4)
1460 ctrl |= SDHCI_CTRL_4BITBUS;
1461 else
1462 ctrl &= ~SDHCI_CTRL_4BITBUS;
1463 }
1464 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1465}
1466EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1467
Russell King96d7b782014-04-25 12:59:26 +01001468void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1469{
1470 u16 ctrl_2;
1471
1472 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1473 /* Select Bus Speed Mode for host */
1474 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1475 if ((timing == MMC_TIMING_MMC_HS200) ||
1476 (timing == MMC_TIMING_UHS_SDR104))
1477 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1478 else if (timing == MMC_TIMING_UHS_SDR12)
1479 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1480 else if (timing == MMC_TIMING_UHS_SDR25)
1481 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1482 else if (timing == MMC_TIMING_UHS_SDR50)
1483 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1484 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1485 (timing == MMC_TIMING_MMC_DDR52))
1486 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001487 else if (timing == MMC_TIMING_MMC_HS400)
1488 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001489 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1490}
1491EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1492
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001493static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001494{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001495 unsigned long flags;
1496 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001497 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001498
Pierre Ossmand129bce2006-03-24 03:18:17 -08001499 spin_lock_irqsave(&host->lock, flags);
1500
Adrian Hunterceb61432011-12-27 15:48:41 +02001501 if (host->flags & SDHCI_DEVICE_DEAD) {
1502 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001503 if (!IS_ERR(mmc->supply.vmmc) &&
1504 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001505 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001506 return;
1507 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001508
Pierre Ossmand129bce2006-03-24 03:18:17 -08001509 /*
1510 * Reset the chip on each power off.
1511 * Should clear out any weird states.
1512 */
1513 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001514 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001515 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001516 }
1517
Kevin Liu52983382013-01-31 11:31:37 +08001518 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001519 (ios->power_mode == MMC_POWER_UP) &&
1520 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001521 sdhci_enable_preset_value(host, false);
1522
Russell King373073e2014-04-25 12:58:45 +01001523 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001524 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001525 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001526
1527 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1528 host->clock) {
1529 host->timeout_clk = host->mmc->actual_clock ?
1530 host->mmc->actual_clock / 1000 :
1531 host->clock / 1000;
1532 host->mmc->max_busy_timeout =
1533 host->ops->get_max_timeout_count ?
1534 host->ops->get_max_timeout_count(host) :
1535 1 << 27;
1536 host->mmc->max_busy_timeout /= host->timeout_clk;
1537 }
Russell King373073e2014-04-25 12:58:45 +01001538 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001539
Russell King24fbb3c2014-04-25 13:00:06 +01001540 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001541
Philip Rakity643a81f2010-09-23 08:24:32 -07001542 if (host->ops->platform_send_init_74_clocks)
1543 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1544
Russell King2317f562014-04-25 12:57:07 +01001545 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001546
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001547 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001548
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001549 if ((ios->timing == MMC_TIMING_SD_HS ||
1550 ios->timing == MMC_TIMING_MMC_HS)
1551 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001552 ctrl |= SDHCI_CTRL_HISPD;
1553 else
1554 ctrl &= ~SDHCI_CTRL_HISPD;
1555
Arindam Nathd6d50a12011-05-05 12:18:59 +05301556 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301557 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301558
1559 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001560 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1561 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001562 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301563 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301564 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1565 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001566 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301567 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301568
Russell Kingda91a8f2014-04-25 13:00:12 +01001569 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301570 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301571 /*
1572 * We only need to set Driver Strength if the
1573 * preset value enable is not set.
1574 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001575 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301576 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1577 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1578 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1579 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1580 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1581
1582 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301583 } else {
1584 /*
1585 * According to SDHC Spec v3.00, if the Preset Value
1586 * Enable in the Host Control 2 register is set, we
1587 * need to reset SD Clock Enable before changing High
1588 * Speed Enable to avoid generating clock gliches.
1589 */
Arindam Nath758535c2011-05-05 12:19:00 +05301590
1591 /* Reset SD Clock Enable */
1592 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1593 clk &= ~SDHCI_CLOCK_CARD_EN;
1594 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1595
1596 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1597
1598 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001599 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301600 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301601
Arindam Nath49c468f2011-05-05 12:19:01 +05301602 /* Reset SD Clock Enable */
1603 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1604 clk &= ~SDHCI_CLOCK_CARD_EN;
1605 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1606
Russell King96d7b782014-04-25 12:59:26 +01001607 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001608 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301609
Kevin Liu52983382013-01-31 11:31:37 +08001610 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1611 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1612 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1613 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1614 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1615 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1616 u16 preset;
1617
1618 sdhci_enable_preset_value(host, true);
1619 preset = sdhci_get_preset_value(host);
1620 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1621 >> SDHCI_PRESET_DRV_SHIFT;
1622 }
1623
Arindam Nath49c468f2011-05-05 12:19:01 +05301624 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001625 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301626 } else
1627 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301628
Leandro Dorileob8352262007-07-25 23:47:04 +02001629 /*
1630 * Some (ENE) controllers go apeshit on some ios operation,
1631 * signalling timeout and CRC errors even on CMD0. Resetting
1632 * it on each ios seems to solve the problem.
1633 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301634 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001635 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001636
Pierre Ossman5f25a662006-10-04 02:15:39 -07001637 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001638 spin_unlock_irqrestore(&host->lock, flags);
1639}
1640
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001641static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1642{
1643 struct sdhci_host *host = mmc_priv(mmc);
1644
1645 sdhci_runtime_pm_get(host);
1646 sdhci_do_set_ios(host, ios);
1647 sdhci_runtime_pm_put(host);
1648}
1649
Kevin Liu94144a42013-02-28 17:35:53 +08001650static int sdhci_do_get_cd(struct sdhci_host *host)
1651{
1652 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1653
1654 if (host->flags & SDHCI_DEVICE_DEAD)
1655 return 0;
1656
1657 /* If polling/nonremovable, assume that the card is always present. */
1658 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1659 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1660 return 1;
1661
1662 /* Try slot gpio detect */
1663 if (!IS_ERR_VALUE(gpio_cd))
1664 return !!gpio_cd;
1665
1666 /* Host native card detect */
1667 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1668}
1669
1670static int sdhci_get_cd(struct mmc_host *mmc)
1671{
1672 struct sdhci_host *host = mmc_priv(mmc);
1673 int ret;
1674
1675 sdhci_runtime_pm_get(host);
1676 ret = sdhci_do_get_cd(host);
1677 sdhci_runtime_pm_put(host);
1678 return ret;
1679}
1680
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001681static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001682{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001683 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001684 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001685
Pierre Ossmand129bce2006-03-24 03:18:17 -08001686 spin_lock_irqsave(&host->lock, flags);
1687
Pierre Ossman1e728592008-04-16 19:13:13 +02001688 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001689 is_readonly = 0;
1690 else if (host->ops->get_ro)
1691 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001692 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001693 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1694 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001695
1696 spin_unlock_irqrestore(&host->lock, flags);
1697
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001698 /* This quirk needs to be replaced by a callback-function later */
1699 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1700 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001701}
1702
Takashi Iwai82b0e232011-04-21 20:26:38 +02001703#define SAMPLE_COUNT 5
1704
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001705static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001706{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001707 int i, ro_count;
1708
Takashi Iwai82b0e232011-04-21 20:26:38 +02001709 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001710 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001711
1712 ro_count = 0;
1713 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001714 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001715 if (++ro_count > SAMPLE_COUNT / 2)
1716 return 1;
1717 }
1718 msleep(30);
1719 }
1720 return 0;
1721}
1722
Adrian Hunter20758b62011-08-29 16:42:12 +03001723static void sdhci_hw_reset(struct mmc_host *mmc)
1724{
1725 struct sdhci_host *host = mmc_priv(mmc);
1726
1727 if (host->ops && host->ops->hw_reset)
1728 host->ops->hw_reset(host);
1729}
1730
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001731static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001732{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001733 struct sdhci_host *host = mmc_priv(mmc);
1734 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001735
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001736 sdhci_runtime_pm_get(host);
1737 ret = sdhci_do_get_ro(host);
1738 sdhci_runtime_pm_put(host);
1739 return ret;
1740}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001741
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001742static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1743{
Russell Kingbe138552014-04-25 12:55:56 +01001744 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001745 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001746 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001747 else
Russell Kingb537f942014-04-25 12:56:01 +01001748 host->ier &= ~SDHCI_INT_CARD_INT;
1749
1750 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1751 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001752 mmiowb();
1753 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001754}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001755
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001756static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1757{
1758 struct sdhci_host *host = mmc_priv(mmc);
1759 unsigned long flags;
1760
Russell Kingef104332014-04-25 12:55:41 +01001761 sdhci_runtime_pm_get(host);
1762
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001763 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001764 if (enable)
1765 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1766 else
1767 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1768
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001769 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001770 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001771
1772 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001773}
1774
Philip Rakity6231f3d2012-07-23 15:56:23 -07001775static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001776 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001777{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001778 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001779 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001780 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001781
1782 /*
1783 * Signal Voltage Switching is only applicable for Host Controllers
1784 * v3.00 and above.
1785 */
1786 if (host->version < SDHCI_SPEC_300)
1787 return 0;
1788
Philip Rakity6231f3d2012-07-23 15:56:23 -07001789 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001790
Fabio Estevam21f59982013-02-14 10:35:03 -02001791 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001792 case MMC_SIGNAL_VOLTAGE_330:
1793 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1794 ctrl &= ~SDHCI_CTRL_VDD_180;
1795 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1796
Tim Kryger3a48edc2014-06-13 10:13:56 -07001797 if (!IS_ERR(mmc->supply.vqmmc)) {
1798 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1799 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001800 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001801 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1802 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001803 return -EIO;
1804 }
1805 }
1806 /* Wait for 5ms */
1807 usleep_range(5000, 5500);
1808
1809 /* 3.3V regulator output should be stable within 5 ms */
1810 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1811 if (!(ctrl & SDHCI_CTRL_VDD_180))
1812 return 0;
1813
Joe Perches66061102014-09-12 14:56:56 -07001814 pr_warn("%s: 3.3V regulator output did not became stable\n",
1815 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001816
1817 return -EAGAIN;
1818 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001819 if (!IS_ERR(mmc->supply.vqmmc)) {
1820 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001821 1700000, 1950000);
1822 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001823 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1824 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001825 return -EIO;
1826 }
1827 }
1828
1829 /*
1830 * Enable 1.8V Signal Enable in the Host Control2
1831 * register
1832 */
1833 ctrl |= SDHCI_CTRL_VDD_180;
1834 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1835
Vincent Yang9d967a62015-01-20 16:05:15 +08001836 /* Some controller need to do more when switching */
1837 if (host->ops->voltage_switch)
1838 host->ops->voltage_switch(host);
1839
Kevin Liu20b92a32012-12-17 19:29:26 +08001840 /* 1.8V regulator output should be stable within 5 ms */
1841 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1842 if (ctrl & SDHCI_CTRL_VDD_180)
1843 return 0;
1844
Joe Perches66061102014-09-12 14:56:56 -07001845 pr_warn("%s: 1.8V regulator output did not became stable\n",
1846 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001847
1848 return -EAGAIN;
1849 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001850 if (!IS_ERR(mmc->supply.vqmmc)) {
1851 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1852 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001853 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001854 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1855 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001856 return -EIO;
1857 }
1858 }
1859 return 0;
1860 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301861 /* No signal voltage switch required */
1862 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001863 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301864}
1865
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001866static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001867 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001868{
1869 struct sdhci_host *host = mmc_priv(mmc);
1870 int err;
1871
1872 if (host->version < SDHCI_SPEC_300)
1873 return 0;
1874 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001875 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001876 sdhci_runtime_pm_put(host);
1877 return err;
1878}
1879
Kevin Liu20b92a32012-12-17 19:29:26 +08001880static int sdhci_card_busy(struct mmc_host *mmc)
1881{
1882 struct sdhci_host *host = mmc_priv(mmc);
1883 u32 present_state;
1884
1885 sdhci_runtime_pm_get(host);
1886 /* Check whether DAT[3:0] is 0000 */
1887 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1888 sdhci_runtime_pm_put(host);
1889
1890 return !(present_state & SDHCI_DATA_LVL_MASK);
1891}
1892
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001893static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1894{
1895 struct sdhci_host *host = mmc_priv(mmc);
1896 unsigned long flags;
1897
1898 spin_lock_irqsave(&host->lock, flags);
1899 host->flags |= SDHCI_HS400_TUNING;
1900 spin_unlock_irqrestore(&host->lock, flags);
1901
1902 return 0;
1903}
1904
Girish K S069c9f12012-01-06 09:56:39 +05301905static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301906{
Russell King4b6f37d2014-04-25 12:59:36 +01001907 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301908 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301909 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301910 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001911 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001912 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001913 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301914
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001915 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001916 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301917
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001918 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1919 host->flags &= ~SDHCI_HS400_TUNING;
1920
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001921 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1922 tuning_count = host->tuning_count;
1923
Arindam Nathb513ea22011-05-05 12:19:04 +05301924 /*
Girish K S069c9f12012-01-06 09:56:39 +05301925 * The Host Controller needs tuning only in case of SDR104 mode
1926 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301927 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301928 * If the Host Controller supports the HS200 mode then the
1929 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301930 */
Russell King4b6f37d2014-04-25 12:59:36 +01001931 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001932 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001933 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001934 err = -EINVAL;
1935 goto out_unlock;
1936
Russell King4b6f37d2014-04-25 12:59:36 +01001937 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001938 /*
1939 * Periodic re-tuning for HS400 is not expected to be needed, so
1940 * disable it here.
1941 */
1942 if (hs400_tuning)
1943 tuning_count = 0;
1944 break;
1945
Russell King4b6f37d2014-04-25 12:59:36 +01001946 case MMC_TIMING_UHS_SDR104:
1947 break;
Girish K S069c9f12012-01-06 09:56:39 +05301948
Russell King4b6f37d2014-04-25 12:59:36 +01001949 case MMC_TIMING_UHS_SDR50:
1950 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1951 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1952 break;
1953 /* FALLTHROUGH */
1954
1955 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001956 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301957 }
1958
Dong Aisheng45251812013-09-13 19:11:30 +08001959 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001960 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001961 err = host->ops->platform_execute_tuning(host, opcode);
1962 sdhci_runtime_pm_put(host);
1963 return err;
1964 }
1965
Russell King4b6f37d2014-04-25 12:59:36 +01001966 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1967 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001968 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1969 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301970 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1971
1972 /*
1973 * As per the Host Controller spec v3.00, tuning command
1974 * generates Buffer Read Ready interrupt, so enable that.
1975 *
1976 * Note: The spec clearly says that when tuning sequence
1977 * is being performed, the controller does not generate
1978 * interrupts other than Buffer Read Ready interrupt. But
1979 * to make sure we don't hit a controller bug, we _only_
1980 * enable Buffer Read Ready interrupt here.
1981 */
Russell Kingb537f942014-04-25 12:56:01 +01001982 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1983 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301984
1985 /*
1986 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1987 * of loops reaches 40 times or a timeout of 150ms occurs.
1988 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301989 do {
1990 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001991 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301992
Girish K S069c9f12012-01-06 09:56:39 +05301993 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301994 cmd.arg = 0;
1995 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1996 cmd.retries = 0;
1997 cmd.data = NULL;
1998 cmd.error = 0;
1999
Al Cooper7ce45e92014-05-09 11:34:07 -04002000 if (tuning_loop_counter-- == 0)
2001 break;
2002
Arindam Nathb513ea22011-05-05 12:19:04 +05302003 mrq.cmd = &cmd;
2004 host->mrq = &mrq;
2005
2006 /*
2007 * In response to CMD19, the card sends 64 bytes of tuning
2008 * block to the Host Controller. So we set the block size
2009 * to 64 here.
2010 */
Girish K S069c9f12012-01-06 09:56:39 +05302011 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
2012 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2013 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
2014 SDHCI_BLOCK_SIZE);
2015 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
2016 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2017 SDHCI_BLOCK_SIZE);
2018 } else {
2019 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2020 SDHCI_BLOCK_SIZE);
2021 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302022
2023 /*
2024 * The tuning block is sent by the card to the host controller.
2025 * So we set the TRNS_READ bit in the Transfer Mode register.
2026 * This also takes care of setting DMA Enable and Multi Block
2027 * Select in the same register to 0.
2028 */
2029 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2030
2031 sdhci_send_command(host, &cmd);
2032
2033 host->cmd = NULL;
2034 host->mrq = NULL;
2035
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002036 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302037 /* Wait for Buffer Read Ready interrupt */
2038 wait_event_interruptible_timeout(host->buf_ready_int,
2039 (host->tuning_done == 1),
2040 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002041 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302042
2043 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302044 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05302045 "Buffer Read Ready interrupt during tuning "
2046 "procedure, falling back to fixed sampling "
2047 "clock\n");
2048 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2049 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2050 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2051 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2052
2053 err = -EIO;
2054 goto out;
2055 }
2056
2057 host->tuning_done = 0;
2058
2059 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07002060
2061 /* eMMC spec does not require a delay between tuning cycles */
2062 if (opcode == MMC_SEND_TUNING_BLOCK)
2063 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302064 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2065
2066 /*
2067 * The Host Driver has exhausted the maximum number of loops allowed,
2068 * so use fixed sampling frequency.
2069 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002070 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302071 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2072 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002073 }
2074 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
2075 pr_info(DRIVER_NAME ": Tuning procedure"
2076 " failed, falling back to fixed sampling"
2077 " clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002078 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302079 }
2080
2081out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002082 host->flags &= ~SDHCI_NEEDS_RETUNING;
2083
2084 if (tuning_count) {
Aaron Lu973905f2012-07-04 13:29:09 +08002085 host->flags |= SDHCI_USING_RETUNING_TIMER;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002086 mod_timer(&host->tuning_timer, jiffies + tuning_count * HZ);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302087 }
2088
2089 /*
2090 * In case tuning fails, host controllers which support re-tuning can
2091 * try tuning again at a later time, when the re-tuning timer expires.
2092 * So for these controllers, we return 0. Since there might be other
2093 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08002094 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2095 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302096 */
Aaron Lu973905f2012-07-04 13:29:09 +08002097 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302098 err = 0;
2099
Russell Kingb537f942014-04-25 12:56:01 +01002100 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2101 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002102out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002103 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002104 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302105
2106 return err;
2107}
2108
Kevin Liu52983382013-01-31 11:31:37 +08002109
2110static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302111{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302112 /* Host Controller v3.00 defines preset value registers */
2113 if (host->version < SDHCI_SPEC_300)
2114 return;
2115
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302116 /*
2117 * We only enable or disable Preset Value if they are not already
2118 * enabled or disabled respectively. Otherwise, we bail out.
2119 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002120 if (host->preset_enabled != enable) {
2121 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2122
2123 if (enable)
2124 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2125 else
2126 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2127
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302128 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002129
2130 if (enable)
2131 host->flags |= SDHCI_PV_ENABLED;
2132 else
2133 host->flags &= ~SDHCI_PV_ENABLED;
2134
2135 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302136 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002137}
2138
Haibo Chen348487c2014-12-09 17:04:05 +08002139static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2140 int err)
2141{
2142 struct sdhci_host *host = mmc_priv(mmc);
2143 struct mmc_data *data = mrq->data;
2144
2145 if (host->flags & SDHCI_REQ_USE_DMA) {
2146 if (data->host_cookie)
2147 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2148 data->flags & MMC_DATA_WRITE ?
2149 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2150 mrq->data->host_cookie = 0;
2151 }
2152}
2153
2154static int sdhci_pre_dma_transfer(struct sdhci_host *host,
2155 struct mmc_data *data,
2156 struct sdhci_host_next *next)
2157{
2158 int sg_count;
2159
2160 if (!next && data->host_cookie &&
2161 data->host_cookie != host->next_data.cookie) {
2162 pr_debug(DRIVER_NAME "[%s] invalid cookie: %d, next-cookie %d\n",
2163 __func__, data->host_cookie, host->next_data.cookie);
2164 data->host_cookie = 0;
2165 }
2166
2167 /* Check if next job is already prepared */
2168 if (next ||
2169 (!next && data->host_cookie != host->next_data.cookie)) {
2170 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg,
2171 data->sg_len,
2172 data->flags & MMC_DATA_WRITE ?
2173 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2174
2175 } else {
2176 sg_count = host->next_data.sg_count;
2177 host->next_data.sg_count = 0;
2178 }
2179
2180
2181 if (sg_count == 0)
2182 return -EINVAL;
2183
2184 if (next) {
2185 next->sg_count = sg_count;
2186 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
2187 } else
2188 host->sg_count = sg_count;
2189
2190 return sg_count;
2191}
2192
2193static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2194 bool is_first_req)
2195{
2196 struct sdhci_host *host = mmc_priv(mmc);
2197
2198 if (mrq->data->host_cookie) {
2199 mrq->data->host_cookie = 0;
2200 return;
2201 }
2202
2203 if (host->flags & SDHCI_REQ_USE_DMA)
2204 if (sdhci_pre_dma_transfer(host,
2205 mrq->data,
2206 &host->next_data) < 0)
2207 mrq->data->host_cookie = 0;
2208}
2209
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002210static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002211{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002212 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002213 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002214 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002215
Christian Daudt722e1282013-06-20 14:26:36 -07002216 /* First check if client has provided their own card event */
2217 if (host->ops->card_event)
2218 host->ops->card_event(host);
2219
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002220 present = sdhci_do_get_cd(host);
2221
Pierre Ossmand129bce2006-03-24 03:18:17 -08002222 spin_lock_irqsave(&host->lock, flags);
2223
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002224 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002225 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302226 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002227 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302228 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002229 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002230
Russell King03231f92014-04-25 12:57:12 +01002231 sdhci_do_reset(host, SDHCI_RESET_CMD);
2232 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002233
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002234 host->mrq->cmd->error = -ENOMEDIUM;
2235 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002236 }
2237
2238 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002239}
2240
2241static const struct mmc_host_ops sdhci_ops = {
2242 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002243 .post_req = sdhci_post_req,
2244 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002245 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002246 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002247 .get_ro = sdhci_get_ro,
2248 .hw_reset = sdhci_hw_reset,
2249 .enable_sdio_irq = sdhci_enable_sdio_irq,
2250 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002251 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002252 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002253 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002254 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002255};
2256
2257/*****************************************************************************\
2258 * *
2259 * Tasklets *
2260 * *
2261\*****************************************************************************/
2262
Pierre Ossmand129bce2006-03-24 03:18:17 -08002263static void sdhci_tasklet_finish(unsigned long param)
2264{
2265 struct sdhci_host *host;
2266 unsigned long flags;
2267 struct mmc_request *mrq;
2268
2269 host = (struct sdhci_host*)param;
2270
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002271 spin_lock_irqsave(&host->lock, flags);
2272
Chris Ball0c9c99a2011-04-27 17:35:31 -04002273 /*
2274 * If this tasklet gets rescheduled while running, it will
2275 * be run again afterwards but without any active request.
2276 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002277 if (!host->mrq) {
2278 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002279 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002280 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002281
2282 del_timer(&host->timer);
2283
2284 mrq = host->mrq;
2285
Pierre Ossmand129bce2006-03-24 03:18:17 -08002286 /*
2287 * The controller needs a reset of internal state machines
2288 * upon error conditions.
2289 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002290 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002291 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002292 (mrq->sbc && mrq->sbc->error) ||
2293 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2294 (mrq->data->stop && mrq->data->stop->error))) ||
2295 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002296
2297 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002298 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002299 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002300 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002301
2302 /* Spec says we should do both at the same time, but Ricoh
2303 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002304 sdhci_do_reset(host, SDHCI_RESET_CMD);
2305 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002306 }
2307
2308 host->mrq = NULL;
2309 host->cmd = NULL;
2310 host->data = NULL;
2311
Pierre Ossmanf9134312008-12-21 17:01:48 +01002312#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002313 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002314#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002315
Pierre Ossman5f25a662006-10-04 02:15:39 -07002316 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002317 spin_unlock_irqrestore(&host->lock, flags);
2318
2319 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002320 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002321}
2322
2323static void sdhci_timeout_timer(unsigned long data)
2324{
2325 struct sdhci_host *host;
2326 unsigned long flags;
2327
2328 host = (struct sdhci_host*)data;
2329
2330 spin_lock_irqsave(&host->lock, flags);
2331
2332 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302333 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002334 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002335 sdhci_dumpregs(host);
2336
2337 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002338 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002339 sdhci_finish_data(host);
2340 } else {
2341 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002342 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002343 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002344 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002345
2346 tasklet_schedule(&host->finish_tasklet);
2347 }
2348 }
2349
Pierre Ossman5f25a662006-10-04 02:15:39 -07002350 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002351 spin_unlock_irqrestore(&host->lock, flags);
2352}
2353
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302354static void sdhci_tuning_timer(unsigned long data)
2355{
2356 struct sdhci_host *host;
2357 unsigned long flags;
2358
2359 host = (struct sdhci_host *)data;
2360
2361 spin_lock_irqsave(&host->lock, flags);
2362
2363 host->flags |= SDHCI_NEEDS_RETUNING;
2364
2365 spin_unlock_irqrestore(&host->lock, flags);
2366}
2367
Pierre Ossmand129bce2006-03-24 03:18:17 -08002368/*****************************************************************************\
2369 * *
2370 * Interrupt handling *
2371 * *
2372\*****************************************************************************/
2373
Adrian Hunter61541392014-09-24 10:27:27 +03002374static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002375{
2376 BUG_ON(intmask == 0);
2377
2378 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302379 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002380 "though no command operation was in progress.\n",
2381 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002382 sdhci_dumpregs(host);
2383 return;
2384 }
2385
Pierre Ossman43b58b32007-07-25 23:15:27 +02002386 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002387 host->cmd->error = -ETIMEDOUT;
2388 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2389 SDHCI_INT_INDEX))
2390 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002391
Pierre Ossmane8095172008-07-25 01:09:08 +02002392 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002393 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002394 return;
2395 }
2396
2397 /*
2398 * The host can send and interrupt when the busy state has
2399 * ended, allowing us to wait without wasting CPU cycles.
2400 * Unfortunately this is overloaded on the "data complete"
2401 * interrupt, so we need to take some care when handling
2402 * it.
2403 *
2404 * Note: The 1.0 specification is a bit ambiguous about this
2405 * feature so there might be some problems with older
2406 * controllers.
2407 */
2408 if (host->cmd->flags & MMC_RSP_BUSY) {
2409 if (host->cmd->data)
2410 DBG("Cannot wait for busy signal when also "
2411 "doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002412 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2413 && !host->busy_handle) {
2414 /* Mark that command complete before busy is ended */
2415 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002416 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002417 }
Ben Dooksf9454052009-02-20 20:33:08 +03002418
2419 /* The controller does not support the end-of-busy IRQ,
2420 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002421 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2422 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2423 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002424 }
2425
2426 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002427 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002428}
2429
George G. Davis0957c332010-02-18 12:32:12 -05002430#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002431static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002432{
2433 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002434 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002435
2436 sdhci_dumpregs(host);
2437
2438 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002439 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002440
Adrian Huntere57a5f62014-11-04 12:42:46 +02002441 if (host->flags & SDHCI_USE_64_BIT_DMA)
2442 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2443 name, desc, le32_to_cpu(dma_desc->addr_hi),
2444 le32_to_cpu(dma_desc->addr_lo),
2445 le16_to_cpu(dma_desc->len),
2446 le16_to_cpu(dma_desc->cmd));
2447 else
2448 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2449 name, desc, le32_to_cpu(dma_desc->addr_lo),
2450 le16_to_cpu(dma_desc->len),
2451 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002452
Adrian Hunter76fe3792014-11-04 12:42:42 +02002453 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002454
Adrian Hunter05452302014-11-04 12:42:45 +02002455 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002456 break;
2457 }
2458}
2459#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002460static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002461#endif
2462
Pierre Ossmand129bce2006-03-24 03:18:17 -08002463static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2464{
Girish K S069c9f12012-01-06 09:56:39 +05302465 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002466 BUG_ON(intmask == 0);
2467
Arindam Nathb513ea22011-05-05 12:19:04 +05302468 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2469 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302470 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2471 if (command == MMC_SEND_TUNING_BLOCK ||
2472 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302473 host->tuning_done = 1;
2474 wake_up(&host->buf_ready_int);
2475 return;
2476 }
2477 }
2478
Pierre Ossmand129bce2006-03-24 03:18:17 -08002479 if (!host->data) {
2480 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002481 * The "data complete" interrupt is also used to
2482 * indicate that a busy state has ended. See comment
2483 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002484 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002485 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002486 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2487 host->cmd->error = -ETIMEDOUT;
2488 tasklet_schedule(&host->finish_tasklet);
2489 return;
2490 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002491 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002492 /*
2493 * Some cards handle busy-end interrupt
2494 * before the command completed, so make
2495 * sure we do things in the proper order.
2496 */
2497 if (host->busy_handle)
2498 sdhci_finish_command(host);
2499 else
2500 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002501 return;
2502 }
2503 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002504
Girish K Sa3c76eb2011-10-11 11:44:09 +05302505 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002506 "though no data operation was in progress.\n",
2507 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002508 sdhci_dumpregs(host);
2509
2510 return;
2511 }
2512
2513 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002514 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002515 else if (intmask & SDHCI_INT_DATA_END_BIT)
2516 host->data->error = -EILSEQ;
2517 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2518 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2519 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002520 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002521 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302522 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002523 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002524 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002525 if (host->ops->adma_workaround)
2526 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002527 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002528
Pierre Ossman17b04292007-07-22 22:18:46 +02002529 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002530 sdhci_finish_data(host);
2531 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002532 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002533 sdhci_transfer_pio(host);
2534
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002535 /*
2536 * We currently don't do anything fancy with DMA
2537 * boundaries, but as we can't disable the feature
2538 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002539 *
2540 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2541 * should return a valid address to continue from, but as
2542 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002543 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002544 if (intmask & SDHCI_INT_DMA_END) {
2545 u32 dmastart, dmanow;
2546 dmastart = sg_dma_address(host->data->sg);
2547 dmanow = dmastart + host->data->bytes_xfered;
2548 /*
2549 * Force update to the next DMA block boundary.
2550 */
2551 dmanow = (dmanow &
2552 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2553 SDHCI_DEFAULT_BOUNDARY_SIZE;
2554 host->data->bytes_xfered = dmanow - dmastart;
2555 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2556 " next 0x%08x\n",
2557 mmc_hostname(host->mmc), dmastart,
2558 host->data->bytes_xfered, dmanow);
2559 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2560 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002561
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002562 if (intmask & SDHCI_INT_DATA_END) {
2563 if (host->cmd) {
2564 /*
2565 * Data managed to finish before the
2566 * command completed. Make sure we do
2567 * things in the proper order.
2568 */
2569 host->data_early = 1;
2570 } else {
2571 sdhci_finish_data(host);
2572 }
2573 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002574 }
2575}
2576
David Howells7d12e782006-10-05 14:55:46 +01002577static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002578{
Russell King781e9892014-04-25 12:55:46 +01002579 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002580 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002581 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002582 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002583
2584 spin_lock(&host->lock);
2585
Russell Kingbe138552014-04-25 12:55:56 +01002586 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002587 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002588 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002589 }
2590
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002591 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002592 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002593 result = IRQ_NONE;
2594 goto out;
2595 }
2596
Russell King41005002014-04-25 12:55:36 +01002597 do {
2598 /* Clear selected interrupts. */
2599 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2600 SDHCI_INT_BUS_POWER);
2601 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002602
Russell King41005002014-04-25 12:55:36 +01002603 DBG("*** %s got interrupt: 0x%08x\n",
2604 mmc_hostname(host->mmc), intmask);
2605
2606 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2607 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2608 SDHCI_CARD_PRESENT;
2609
2610 /*
2611 * There is a observation on i.mx esdhc. INSERT
2612 * bit will be immediately set again when it gets
2613 * cleared, if a card is inserted. We have to mask
2614 * the irq to prevent interrupt storm which will
2615 * freeze the system. And the REMOVE gets the
2616 * same situation.
2617 *
2618 * More testing are needed here to ensure it works
2619 * for other platforms though.
2620 */
Russell Kingb537f942014-04-25 12:56:01 +01002621 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2622 SDHCI_INT_CARD_REMOVE);
2623 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2624 SDHCI_INT_CARD_INSERT;
2625 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2626 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002627
2628 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2629 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002630
2631 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2632 SDHCI_INT_CARD_REMOVE);
2633 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002634 }
2635
2636 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002637 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2638 &intmask);
Russell King41005002014-04-25 12:55:36 +01002639
2640 if (intmask & SDHCI_INT_DATA_MASK)
2641 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2642
2643 if (intmask & SDHCI_INT_BUS_POWER)
2644 pr_err("%s: Card is consuming too much power!\n",
2645 mmc_hostname(host->mmc));
2646
Russell King781e9892014-04-25 12:55:46 +01002647 if (intmask & SDHCI_INT_CARD_INT) {
2648 sdhci_enable_sdio_irq_nolock(host, false);
2649 host->thread_isr |= SDHCI_INT_CARD_INT;
2650 result = IRQ_WAKE_THREAD;
2651 }
Russell King41005002014-04-25 12:55:36 +01002652
2653 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2654 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2655 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2656 SDHCI_INT_CARD_INT);
2657
2658 if (intmask) {
2659 unexpected |= intmask;
2660 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2661 }
2662
Russell King781e9892014-04-25 12:55:46 +01002663 if (result == IRQ_NONE)
2664 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002665
2666 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002667 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002668out:
2669 spin_unlock(&host->lock);
2670
Alexander Stein6379b232012-03-14 09:52:10 +01002671 if (unexpected) {
2672 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2673 mmc_hostname(host->mmc), unexpected);
2674 sdhci_dumpregs(host);
2675 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002676
Pierre Ossmand129bce2006-03-24 03:18:17 -08002677 return result;
2678}
2679
Russell King781e9892014-04-25 12:55:46 +01002680static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2681{
2682 struct sdhci_host *host = dev_id;
2683 unsigned long flags;
2684 u32 isr;
2685
2686 spin_lock_irqsave(&host->lock, flags);
2687 isr = host->thread_isr;
2688 host->thread_isr = 0;
2689 spin_unlock_irqrestore(&host->lock, flags);
2690
Russell King3560db82014-04-25 12:55:51 +01002691 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2692 sdhci_card_event(host->mmc);
2693 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2694 }
2695
Russell King781e9892014-04-25 12:55:46 +01002696 if (isr & SDHCI_INT_CARD_INT) {
2697 sdio_run_irqs(host->mmc);
2698
2699 spin_lock_irqsave(&host->lock, flags);
2700 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2701 sdhci_enable_sdio_irq_nolock(host, true);
2702 spin_unlock_irqrestore(&host->lock, flags);
2703 }
2704
2705 return isr ? IRQ_HANDLED : IRQ_NONE;
2706}
2707
Pierre Ossmand129bce2006-03-24 03:18:17 -08002708/*****************************************************************************\
2709 * *
2710 * Suspend/resume *
2711 * *
2712\*****************************************************************************/
2713
2714#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002715void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2716{
2717 u8 val;
2718 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2719 | SDHCI_WAKE_ON_INT;
2720
2721 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2722 val |= mask ;
2723 /* Avoid fake wake up */
2724 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2725 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2726 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2727}
2728EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2729
Fabio Estevam0b10f472014-08-30 14:53:13 -03002730static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002731{
2732 u8 val;
2733 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2734 | SDHCI_WAKE_ON_INT;
2735
2736 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2737 val &= ~mask;
2738 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2739}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002740
Manuel Lauss29495aa2011-11-03 11:09:45 +01002741int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002742{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002743 sdhci_disable_card_detection(host);
2744
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302745 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002746 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002747 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302748 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302749 }
2750
Kevin Liuad080d72013-01-05 17:21:33 +08002751 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002752 host->ier = 0;
2753 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2754 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002755 free_irq(host->irq, host);
2756 } else {
2757 sdhci_enable_irq_wakeups(host);
2758 enable_irq_wake(host->irq);
2759 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002760 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002761}
2762
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002763EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002764
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002765int sdhci_resume_host(struct sdhci_host *host)
2766{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002767 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002768
Richard Röjforsa13abc72009-09-22 16:45:30 -07002769 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002770 if (host->ops->enable_dma)
2771 host->ops->enable_dma(host);
2772 }
2773
Kevin Liuad080d72013-01-05 17:21:33 +08002774 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell King781e9892014-04-25 12:55:46 +01002775 ret = request_threaded_irq(host->irq, sdhci_irq,
2776 sdhci_thread_irq, IRQF_SHARED,
2777 mmc_hostname(host->mmc), host);
Kevin Liuad080d72013-01-05 17:21:33 +08002778 if (ret)
2779 return ret;
2780 } else {
2781 sdhci_disable_irq_wakeups(host);
2782 disable_irq_wake(host->irq);
2783 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002784
Adrian Hunter6308d292012-02-07 14:48:54 +02002785 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2786 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2787 /* Card keeps power but host controller does not */
2788 sdhci_init(host, 0);
2789 host->pwr = 0;
2790 host->clock = 0;
2791 sdhci_do_set_ios(host, &host->mmc->ios);
2792 } else {
2793 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2794 mmiowb();
2795 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002796
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002797 sdhci_enable_card_detection(host);
2798
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302799 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002800 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302801 host->flags |= SDHCI_NEEDS_RETUNING;
2802
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002803 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002804}
2805
2806EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002807
2808static int sdhci_runtime_pm_get(struct sdhci_host *host)
2809{
2810 return pm_runtime_get_sync(host->mmc->parent);
2811}
2812
2813static int sdhci_runtime_pm_put(struct sdhci_host *host)
2814{
2815 pm_runtime_mark_last_busy(host->mmc->parent);
2816 return pm_runtime_put_autosuspend(host->mmc->parent);
2817}
2818
Adrian Hunterf0710a52013-05-06 12:17:32 +03002819static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2820{
2821 if (host->runtime_suspended || host->bus_on)
2822 return;
2823 host->bus_on = true;
2824 pm_runtime_get_noresume(host->mmc->parent);
2825}
2826
2827static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2828{
2829 if (host->runtime_suspended || !host->bus_on)
2830 return;
2831 host->bus_on = false;
2832 pm_runtime_put_noidle(host->mmc->parent);
2833}
2834
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002835int sdhci_runtime_suspend_host(struct sdhci_host *host)
2836{
2837 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002838
2839 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002840 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002841 del_timer_sync(&host->tuning_timer);
2842 host->flags &= ~SDHCI_NEEDS_RETUNING;
2843 }
2844
2845 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002846 host->ier &= SDHCI_INT_CARD_INT;
2847 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2848 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002849 spin_unlock_irqrestore(&host->lock, flags);
2850
Russell King781e9892014-04-25 12:55:46 +01002851 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002852
2853 spin_lock_irqsave(&host->lock, flags);
2854 host->runtime_suspended = true;
2855 spin_unlock_irqrestore(&host->lock, flags);
2856
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002857 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002858}
2859EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2860
2861int sdhci_runtime_resume_host(struct sdhci_host *host)
2862{
2863 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002864 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002865
2866 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2867 if (host->ops->enable_dma)
2868 host->ops->enable_dma(host);
2869 }
2870
2871 sdhci_init(host, 0);
2872
2873 /* Force clock and power re-program */
2874 host->pwr = 0;
2875 host->clock = 0;
Jisheng Zhang3396e732015-01-29 17:42:12 +08002876 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002877 sdhci_do_set_ios(host, &host->mmc->ios);
2878
Kevin Liu52983382013-01-31 11:31:37 +08002879 if ((host_flags & SDHCI_PV_ENABLED) &&
2880 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2881 spin_lock_irqsave(&host->lock, flags);
2882 sdhci_enable_preset_value(host, true);
2883 spin_unlock_irqrestore(&host->lock, flags);
2884 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002885
2886 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002887 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002888 host->flags |= SDHCI_NEEDS_RETUNING;
2889
2890 spin_lock_irqsave(&host->lock, flags);
2891
2892 host->runtime_suspended = false;
2893
2894 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002895 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002896 sdhci_enable_sdio_irq_nolock(host, true);
2897
2898 /* Enable Card Detection */
2899 sdhci_enable_card_detection(host);
2900
2901 spin_unlock_irqrestore(&host->lock, flags);
2902
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002903 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002904}
2905EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2906
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002907#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002908
Pierre Ossmand129bce2006-03-24 03:18:17 -08002909/*****************************************************************************\
2910 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002911 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002912 * *
2913\*****************************************************************************/
2914
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002915struct sdhci_host *sdhci_alloc_host(struct device *dev,
2916 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002917{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002918 struct mmc_host *mmc;
2919 struct sdhci_host *host;
2920
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002921 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002922
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002923 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002924 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002925 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002926
2927 host = mmc_priv(mmc);
2928 host->mmc = mmc;
2929
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002930 return host;
2931}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002932
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002933EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002934
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002935int sdhci_add_host(struct sdhci_host *host)
2936{
2937 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002938 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302939 u32 max_current_caps;
2940 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002941 unsigned int override_timeout_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002942 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002943
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002944 WARN_ON(host == NULL);
2945 if (host == NULL)
2946 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002947
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002948 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002949
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002950 if (debug_quirks)
2951 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002952 if (debug_quirks2)
2953 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002954
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002955 override_timeout_clk = host->timeout_clk;
2956
Russell King03231f92014-04-25 12:57:12 +01002957 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002958
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002959 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002960 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2961 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002962 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302963 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002964 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002965 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002966 }
2967
Arindam Nathf2119df2011-05-05 12:18:57 +05302968 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002969 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002970
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002971 if (host->version >= SDHCI_SPEC_300)
2972 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2973 host->caps1 :
2974 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302975
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002976 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002977 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302978 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002979 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002980 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002981 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002982
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002983 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002984 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002985 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002986 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002987 }
2988
Arindam Nathf2119df2011-05-05 12:18:57 +05302989 if ((host->version >= SDHCI_SPEC_200) &&
2990 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002991 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002992
2993 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2994 (host->flags & SDHCI_USE_ADMA)) {
2995 DBG("Disabling ADMA as it is marked broken\n");
2996 host->flags &= ~SDHCI_USE_ADMA;
2997 }
2998
Adrian Huntere57a5f62014-11-04 12:42:46 +02002999 /*
3000 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
3001 * and *must* do 64-bit DMA. A driver has the opportunity to change
3002 * that during the first call to ->enable_dma(). Similarly
3003 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
3004 * implement.
3005 */
3006 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT)
3007 host->flags |= SDHCI_USE_64_BIT_DMA;
3008
Richard Röjforsa13abc72009-09-22 16:45:30 -07003009 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003010 if (host->ops->enable_dma) {
3011 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07003012 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003013 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07003014 host->flags &=
3015 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003016 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003017 }
3018 }
3019
Adrian Huntere57a5f62014-11-04 12:42:46 +02003020 /* SDMA does not support 64-bit DMA */
3021 if (host->flags & SDHCI_USE_64_BIT_DMA)
3022 host->flags &= ~SDHCI_USE_SDMA;
3023
Pierre Ossman2134a922008-06-28 18:28:51 +02003024 if (host->flags & SDHCI_USE_ADMA) {
3025 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02003026 * The DMA descriptor table size is calculated as the maximum
3027 * number of segments times 2, to allow for an alignment
3028 * descriptor for each segment, plus 1 for a nop end descriptor,
3029 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02003030 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02003031 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3032 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3033 SDHCI_ADMA2_64_DESC_SZ;
3034 host->align_buffer_sz = SDHCI_MAX_SEGS *
3035 SDHCI_ADMA2_64_ALIGN;
3036 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
3037 host->align_sz = SDHCI_ADMA2_64_ALIGN;
3038 host->align_mask = SDHCI_ADMA2_64_ALIGN - 1;
3039 } else {
3040 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3041 SDHCI_ADMA2_32_DESC_SZ;
3042 host->align_buffer_sz = SDHCI_MAX_SEGS *
3043 SDHCI_ADMA2_32_ALIGN;
3044 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
3045 host->align_sz = SDHCI_ADMA2_32_ALIGN;
3046 host->align_mask = SDHCI_ADMA2_32_ALIGN - 1;
3047 }
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003048 host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +02003049 host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003050 &host->adma_addr,
3051 GFP_KERNEL);
Adrian Hunter76fe3792014-11-04 12:42:42 +02003052 host->align_buffer = kmalloc(host->align_buffer_sz, GFP_KERNEL);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003053 if (!host->adma_table || !host->align_buffer) {
Adrian Hunter76fe3792014-11-04 12:42:42 +02003054 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003055 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003056 kfree(host->align_buffer);
Joe Perches66061102014-09-12 14:56:56 -07003057 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003058 mmc_hostname(mmc));
3059 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003060 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01003061 host->align_buffer = NULL;
Adrian Hunter76fe3792014-11-04 12:42:42 +02003062 } else if (host->adma_addr & host->align_mask) {
Joe Perches66061102014-09-12 14:56:56 -07003063 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3064 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003065 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter76fe3792014-11-04 12:42:42 +02003066 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003067 host->adma_table, host->adma_addr);
Russell Kingd1e49f72014-04-25 12:58:34 +01003068 kfree(host->align_buffer);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003069 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01003070 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003071 }
3072 }
3073
Pierre Ossman76591502008-07-21 00:32:11 +02003074 /*
3075 * If we use DMA, then it's up to the caller to set the DMA
3076 * mask, but PIO does not need the hw shim so we set a new
3077 * mask here in that case.
3078 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003079 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003080 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003081 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003082 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003083
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003084 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05303085 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003086 >> SDHCI_CLOCK_BASE_SHIFT;
3087 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303088 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003089 >> SDHCI_CLOCK_BASE_SHIFT;
3090
Pierre Ossmand129bce2006-03-24 03:18:17 -08003091 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003092 if (host->max_clk == 0 || host->quirks &
3093 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003094 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303095 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03003096 "frequency.\n", mmc_hostname(mmc));
3097 return -ENODEV;
3098 }
3099 host->max_clk = host->ops->get_max_clock(host);
3100 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003101
Haibo Chen348487c2014-12-09 17:04:05 +08003102 host->next_data.cookie = 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003103 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303104 * In case of Host Controller v3.00, find out whether clock
3105 * multiplier is supported.
3106 */
3107 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3108 SDHCI_CLOCK_MUL_SHIFT;
3109
3110 /*
3111 * In case the value in Clock Multiplier is 0, then programmable
3112 * clock mode is not supported, otherwise the actual clock
3113 * multiplier is one more than the value of Clock Multiplier
3114 * in the Capabilities Register.
3115 */
3116 if (host->clk_mul)
3117 host->clk_mul += 1;
3118
3119 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003120 * Set host parameters.
3121 */
3122 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303123 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003124 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003125 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303126 else if (host->version >= SDHCI_SPEC_300) {
3127 if (host->clk_mul) {
3128 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3129 mmc->f_max = host->max_clk * host->clk_mul;
3130 } else
3131 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3132 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003133 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003134
Aisheng Dong28aab052014-08-27 15:26:31 +08003135 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3136 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3137 SDHCI_TIMEOUT_CLK_SHIFT;
3138 if (host->timeout_clk == 0) {
3139 if (host->ops->get_timeout_clock) {
3140 host->timeout_clk =
3141 host->ops->get_timeout_clock(host);
3142 } else {
3143 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3144 mmc_hostname(mmc));
3145 return -ENODEV;
3146 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003147 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003148
Aisheng Dong28aab052014-08-27 15:26:31 +08003149 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3150 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003151
Aisheng Dong28aab052014-08-27 15:26:31 +08003152 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003153 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003154 mmc->max_busy_timeout /= host->timeout_clk;
3155 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003156
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003157 if (override_timeout_clk)
3158 host->timeout_clk = override_timeout_clk;
3159
Andrei Warkentine89d4562011-05-23 15:06:37 -05003160 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003161 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003162
3163 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3164 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003165
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003166 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003167 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003168 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003169 !(host->flags & SDHCI_USE_SDMA)) &&
3170 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003171 host->flags |= SDHCI_AUTO_CMD23;
3172 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3173 } else {
3174 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3175 }
3176
Philip Rakity15ec4462010-11-19 16:48:39 -05003177 /*
3178 * A controller may support 8-bit width, but the board itself
3179 * might not have the pins brought out. Boards that support
3180 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3181 * their platform code before calling sdhci_add_host(), and we
3182 * won't assume 8-bit width for hosts without that CAP.
3183 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003184 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003185 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003186
Jerry Huang63ef5d82012-10-25 13:47:19 +08003187 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3188 mmc->caps &= ~MMC_CAP_CMD23;
3189
Arindam Nathf2119df2011-05-05 12:18:57 +05303190 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003191 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003192
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003193 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Markus Mayer4e743f12014-07-03 13:27:42 -07003194 !(mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003195 mmc->caps |= MMC_CAP_NEEDS_POLL;
3196
Tim Kryger3a48edc2014-06-13 10:13:56 -07003197 /* If there are external regulators, get them */
3198 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3199 return -EPROBE_DEFER;
3200
Philip Rakity6231f3d2012-07-23 15:56:23 -07003201 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003202 if (!IS_ERR(mmc->supply.vqmmc)) {
3203 ret = regulator_enable(mmc->supply.vqmmc);
3204 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3205 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003206 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3207 SDHCI_SUPPORT_SDR50 |
3208 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003209 if (ret) {
3210 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3211 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003212 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003213 }
Kevin Liu8363c372012-11-17 17:55:51 -05003214 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003215
Daniel Drake6a661802012-11-25 13:01:19 -05003216 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3217 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3218 SDHCI_SUPPORT_DDR50);
3219
Al Cooper4188bba2012-03-16 15:54:17 -04003220 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3221 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3222 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303223 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3224
3225 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003226 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303227 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003228 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3229 * field can be promoted to support HS200.
3230 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003231 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003232 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003233 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303234 mmc->caps |= MMC_CAP_UHS_SDR50;
3235
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003236 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3237 (caps[1] & SDHCI_SUPPORT_HS400))
3238 mmc->caps2 |= MMC_CAP2_HS400;
3239
Adrian Hunter549c0b12014-11-06 15:19:05 +02003240 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3241 (IS_ERR(mmc->supply.vqmmc) ||
3242 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3243 1300000)))
3244 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3245
Micky Ching9107ebb2014-02-21 18:40:35 +08003246 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3247 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303248 mmc->caps |= MMC_CAP_UHS_DDR50;
3249
Girish K S069c9f12012-01-06 09:56:39 +05303250 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303251 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3252 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3253
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003254 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303255 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003256 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303257
Arindam Nathd6d50a12011-05-05 12:18:59 +05303258 /* Driver Type(s) (A, C, D) supported by the host */
3259 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3260 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3261 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3262 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3263 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3264 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3265
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303266 /* Initial value for re-tuning timer count */
3267 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3268 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3269
3270 /*
3271 * In case Re-tuning Timer is not disabled, the actual value of
3272 * re-tuning timer will be 2 ^ (n - 1).
3273 */
3274 if (host->tuning_count)
3275 host->tuning_count = 1 << (host->tuning_count - 1);
3276
3277 /* Re-tuning mode supported by the Host Controller */
3278 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3279 SDHCI_RETUNING_MODE_SHIFT;
3280
Takashi Iwai8f230f42010-12-08 10:04:30 +01003281 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003282
Arindam Nathf2119df2011-05-05 12:18:57 +05303283 /*
3284 * According to SD Host Controller spec v3.00, if the Host System
3285 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3286 * the value is meaningful only if Voltage Support in the Capabilities
3287 * register is set. The actual current value is 4 times the register
3288 * value.
3289 */
3290 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003291 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003292 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003293 if (curr > 0) {
3294
3295 /* convert to SDHCI_MAX_CURRENT format */
3296 curr = curr/1000; /* convert to mA */
3297 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3298
3299 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3300 max_current_caps =
3301 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3302 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3303 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3304 }
3305 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303306
3307 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003308 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303309
Aaron Lu55c46652012-07-04 13:31:48 +08003310 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303311 SDHCI_MAX_CURRENT_330_MASK) >>
3312 SDHCI_MAX_CURRENT_330_SHIFT) *
3313 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303314 }
3315 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003316 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303317
Aaron Lu55c46652012-07-04 13:31:48 +08003318 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303319 SDHCI_MAX_CURRENT_300_MASK) >>
3320 SDHCI_MAX_CURRENT_300_SHIFT) *
3321 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303322 }
3323 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003324 ocr_avail |= MMC_VDD_165_195;
3325
Aaron Lu55c46652012-07-04 13:31:48 +08003326 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303327 SDHCI_MAX_CURRENT_180_MASK) >>
3328 SDHCI_MAX_CURRENT_180_SHIFT) *
3329 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303330 }
3331
Tim Kryger52221612014-06-25 00:25:34 -07003332 /* If OCR set by external regulators, use it instead */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003333 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003334 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003335
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003336 if (host->ocr_mask)
Tim Kryger3a48edc2014-06-13 10:13:56 -07003337 ocr_avail &= host->ocr_mask;
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003338
Takashi Iwai8f230f42010-12-08 10:04:30 +01003339 mmc->ocr_avail = ocr_avail;
3340 mmc->ocr_avail_sdio = ocr_avail;
3341 if (host->ocr_avail_sdio)
3342 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3343 mmc->ocr_avail_sd = ocr_avail;
3344 if (host->ocr_avail_sd)
3345 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3346 else /* normal SD controllers don't support 1.8V */
3347 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3348 mmc->ocr_avail_mmc = ocr_avail;
3349 if (host->ocr_avail_mmc)
3350 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003351
3352 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303353 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003354 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003355 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003356 }
3357
Pierre Ossmand129bce2006-03-24 03:18:17 -08003358 spin_lock_init(&host->lock);
3359
3360 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003361 * Maximum number of segments. Depends on if the hardware
3362 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003363 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003364 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003365 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003366 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003367 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003368 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003369 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003370
3371 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003372 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3373 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3374 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003375 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003376 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003377
3378 /*
3379 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003380 * of bytes. When doing hardware scatter/gather, each entry cannot
3381 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003382 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003383 if (host->flags & SDHCI_USE_ADMA) {
3384 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3385 mmc->max_seg_size = 65535;
3386 else
3387 mmc->max_seg_size = 65536;
3388 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003389 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003390 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003391
3392 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003393 * Maximum block size. This varies from controller to controller and
3394 * is specified in the capabilities register.
3395 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003396 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3397 mmc->max_blk_size = 2;
3398 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303399 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003400 SDHCI_MAX_BLOCK_SHIFT;
3401 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003402 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3403 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003404 mmc->max_blk_size = 0;
3405 }
3406 }
3407
3408 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003409
3410 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003411 * Maximum block count.
3412 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003413 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003414
3415 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003416 * Init tasklets.
3417 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003418 tasklet_init(&host->finish_tasklet,
3419 sdhci_tasklet_finish, (unsigned long)host);
3420
Al Viroe4cad1b2006-10-10 22:47:07 +01003421 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003422
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003423 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303424
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003425 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303426 /* Initialize re-tuning timer */
3427 init_timer(&host->tuning_timer);
3428 host->tuning_timer.data = (unsigned long)host;
3429 host->tuning_timer.function = sdhci_tuning_timer;
3430 }
3431
Shawn Guo2af502c2013-07-05 14:38:55 +08003432 sdhci_init(host, 0);
3433
Russell King781e9892014-04-25 12:55:46 +01003434 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3435 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003436 if (ret) {
3437 pr_err("%s: Failed to request IRQ %d: %d\n",
3438 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003439 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003440 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003441
Pierre Ossmand129bce2006-03-24 03:18:17 -08003442#ifdef CONFIG_MMC_DEBUG
3443 sdhci_dumpregs(host);
3444#endif
3445
Pierre Ossmanf9134312008-12-21 17:01:48 +01003446#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003447 snprintf(host->led_name, sizeof(host->led_name),
3448 "%s::", mmc_hostname(mmc));
3449 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003450 host->led.brightness = LED_OFF;
3451 host->led.default_trigger = mmc_hostname(mmc);
3452 host->led.brightness_set = sdhci_led_control;
3453
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003454 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003455 if (ret) {
3456 pr_err("%s: Failed to register LED device: %d\n",
3457 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003458 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003459 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003460#endif
3461
Pierre Ossman5f25a662006-10-04 02:15:39 -07003462 mmiowb();
3463
Pierre Ossmand129bce2006-03-24 03:18:17 -08003464 mmc_add_host(mmc);
3465
Girish K Sa3c76eb2011-10-11 11:44:09 +05303466 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003467 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003468 (host->flags & SDHCI_USE_ADMA) ?
3469 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003470 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003471
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003472 sdhci_enable_card_detection(host);
3473
Pierre Ossmand129bce2006-03-24 03:18:17 -08003474 return 0;
3475
Pierre Ossmanf9134312008-12-21 17:01:48 +01003476#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003477reset:
Russell King03231f92014-04-25 12:57:12 +01003478 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003479 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3480 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003481 free_irq(host->irq, host);
3482#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003483untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003484 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003485
3486 return ret;
3487}
3488
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003489EXPORT_SYMBOL_GPL(sdhci_add_host);
3490
Pierre Ossman1e728592008-04-16 19:13:13 +02003491void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003492{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003493 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003494 unsigned long flags;
3495
3496 if (dead) {
3497 spin_lock_irqsave(&host->lock, flags);
3498
3499 host->flags |= SDHCI_DEVICE_DEAD;
3500
3501 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303502 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003503 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003504
3505 host->mrq->cmd->error = -ENOMEDIUM;
3506 tasklet_schedule(&host->finish_tasklet);
3507 }
3508
3509 spin_unlock_irqrestore(&host->lock, flags);
3510 }
3511
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003512 sdhci_disable_card_detection(host);
3513
Markus Mayer4e743f12014-07-03 13:27:42 -07003514 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003515
Pierre Ossmanf9134312008-12-21 17:01:48 +01003516#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003517 led_classdev_unregister(&host->led);
3518#endif
3519
Pierre Ossman1e728592008-04-16 19:13:13 +02003520 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003521 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003522
Russell Kingb537f942014-04-25 12:56:01 +01003523 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3524 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003525 free_irq(host->irq, host);
3526
3527 del_timer_sync(&host->timer);
3528
Pierre Ossmand129bce2006-03-24 03:18:17 -08003529 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003530
Tim Kryger3a48edc2014-06-13 10:13:56 -07003531 if (!IS_ERR(mmc->supply.vqmmc))
3532 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003533
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003534 if (host->adma_table)
Adrian Hunter76fe3792014-11-04 12:42:42 +02003535 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003536 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003537 kfree(host->align_buffer);
3538
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003539 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003540 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003541}
3542
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003543EXPORT_SYMBOL_GPL(sdhci_remove_host);
3544
3545void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003546{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003547 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003548}
3549
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003550EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003551
3552/*****************************************************************************\
3553 * *
3554 * Driver init/exit *
3555 * *
3556\*****************************************************************************/
3557
3558static int __init sdhci_drv_init(void)
3559{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303560 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003561 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303562 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003563
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003564 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003565}
3566
3567static void __exit sdhci_drv_exit(void)
3568{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003569}
3570
3571module_init(sdhci_drv_init);
3572module_exit(sdhci_drv_exit);
3573
Pierre Ossmandf673b22006-06-30 02:22:31 -07003574module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003575module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003576
Pierre Ossman32710e82009-04-08 20:14:54 +02003577MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003578MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003579MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003580
Pierre Ossmandf673b22006-06-30 02:22:31 -07003581MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003582MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");