blob: 3e4fe967d7e53e1dafffbd0d9dcc44b7f97b8e3a [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Aaron Lu473b0952012-07-03 17:27:49 +080030#include <linux/mmc/card.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080031#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080032
Pierre Ossmand129bce2006-03-24 03:18:17 -080033#include "sdhci.h"
34
35#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080036
Pierre Ossmand129bce2006-03-24 03:18:17 -080037#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010038 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080039
Pierre Ossmanf9134312008-12-21 17:01:48 +010040#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
41 defined(CONFIG_MMC_SDHCI_MODULE))
42#define SDHCI_USE_LEDS_CLASS
43#endif
44
Arindam Nathb513ea22011-05-05 12:19:04 +053045#define MAX_TUNING_LOOP 40
46
Pierre Ossmandf673b22006-06-30 02:22:31 -070047static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030048static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070049
Pierre Ossmand129bce2006-03-24 03:18:17 -080050static void sdhci_finish_data(struct sdhci_host *);
51
Pierre Ossmand129bce2006-03-24 03:18:17 -080052static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053053static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053054static void sdhci_tuning_timer(unsigned long data);
Kevin Liu52983382013-01-31 11:31:37 +080055static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Haibo Chen348487c2014-12-09 17:04:05 +080056static int sdhci_pre_dma_transfer(struct sdhci_host *host,
57 struct mmc_data *data,
58 struct sdhci_host_next *next);
Pierre Ossmand129bce2006-03-24 03:18:17 -080059
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +010060#ifdef CONFIG_PM
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030061static int sdhci_runtime_pm_get(struct sdhci_host *host);
62static int sdhci_runtime_pm_put(struct sdhci_host *host);
Adrian Hunterf0710a52013-05-06 12:17:32 +030063static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
64static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030065#else
66static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
67{
68 return 0;
69}
70static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
71{
72 return 0;
73}
Adrian Hunterf0710a52013-05-06 12:17:32 +030074static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
75{
76}
77static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
78{
79}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030080#endif
81
Pierre Ossmand129bce2006-03-24 03:18:17 -080082static void sdhci_dumpregs(struct sdhci_host *host)
83{
Girish K Sa3c76eb2011-10-11 11:44:09 +053084 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070085 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080086
Girish K Sa3c76eb2011-10-11 11:44:09 +053087 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030088 sdhci_readl(host, SDHCI_DMA_ADDRESS),
89 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053090 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030091 sdhci_readw(host, SDHCI_BLOCK_SIZE),
92 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053093 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030094 sdhci_readl(host, SDHCI_ARGUMENT),
95 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053096 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030097 sdhci_readl(host, SDHCI_PRESENT_STATE),
98 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053099 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300100 sdhci_readb(host, SDHCI_POWER_CONTROL),
101 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530102 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300103 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
104 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530105 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
107 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530108 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300109 sdhci_readl(host, SDHCI_INT_ENABLE),
110 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530111 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300112 sdhci_readw(host, SDHCI_ACMD12_ERR),
113 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530114 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300115 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500116 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530117 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500118 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300119 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530120 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530121 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800122
Adrian Huntere57a5f62014-11-04 12:42:46 +0200123 if (host->flags & SDHCI_USE_ADMA) {
124 if (host->flags & SDHCI_USE_64_BIT_DMA)
125 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
126 readl(host->ioaddr + SDHCI_ADMA_ERROR),
127 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
128 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
129 else
130 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
131 readl(host->ioaddr + SDHCI_ADMA_ERROR),
132 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
133 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100134
Girish K Sa3c76eb2011-10-11 11:44:09 +0530135 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800136}
137
138/*****************************************************************************\
139 * *
140 * Low level functions *
141 * *
142\*****************************************************************************/
143
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300144static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
145{
Russell King5b4f1f62014-04-25 12:57:02 +0100146 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300147
Adrian Hunterc79396c2011-12-27 15:48:42 +0200148 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100149 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300150 return;
151
Russell King5b4f1f62014-04-25 12:57:02 +0100152 if (enable) {
153 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
154 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800155
Russell King5b4f1f62014-04-25 12:57:02 +0100156 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
157 SDHCI_INT_CARD_INSERT;
158 } else {
159 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
160 }
Russell Kingb537f942014-04-25 12:56:01 +0100161
162 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
163 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300164}
165
166static void sdhci_enable_card_detection(struct sdhci_host *host)
167{
168 sdhci_set_card_detection(host, true);
169}
170
171static void sdhci_disable_card_detection(struct sdhci_host *host)
172{
173 sdhci_set_card_detection(host, false);
174}
175
Russell King03231f92014-04-25 12:57:12 +0100176void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800177{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700178 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800179
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300180 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800181
Adrian Hunterf0710a52013-05-06 12:17:32 +0300182 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800183 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300184 /* Reset-all turns off SD Bus Power */
185 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
186 sdhci_runtime_pm_bus_off(host);
187 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800188
Pierre Ossmane16514d82006-06-30 02:22:24 -0700189 /* Wait max 100 ms */
190 timeout = 100;
191
192 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300193 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700194 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530195 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700196 mmc_hostname(host->mmc), (int)mask);
197 sdhci_dumpregs(host);
198 return;
199 }
200 timeout--;
201 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800202 }
Russell King03231f92014-04-25 12:57:12 +0100203}
204EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300205
Russell King03231f92014-04-25 12:57:12 +0100206static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
207{
208 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
209 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
210 SDHCI_CARD_PRESENT))
211 return;
212 }
213
214 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800215
Russell Kingda91a8f2014-04-25 13:00:12 +0100216 if (mask & SDHCI_RESET_ALL) {
217 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
218 if (host->ops->enable_dma)
219 host->ops->enable_dma(host);
220 }
221
222 /* Resetting the controller clears many */
223 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800224 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800225}
226
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800227static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
228
229static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800230{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800231 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100232 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800233 else
Russell King03231f92014-04-25 12:57:12 +0100234 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800235
Russell Kingb537f942014-04-25 12:56:01 +0100236 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
237 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
238 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
239 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
240 SDHCI_INT_RESPONSE;
241
242 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
243 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800244
245 if (soft) {
246 /* force clock reconfiguration */
247 host->clock = 0;
248 sdhci_set_ios(host->mmc, &host->mmc->ios);
249 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300250}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800251
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300252static void sdhci_reinit(struct sdhci_host *host)
253{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800254 sdhci_init(host, 0);
Aaron Lub67c6b42012-06-29 16:17:31 +0800255 /*
256 * Retuning stuffs are affected by different cards inserted and only
257 * applicable to UHS-I cards. So reset these fields to their initial
258 * value when card is removed.
259 */
Aaron Lu973905f2012-07-04 13:29:09 +0800260 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
261 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
262
Aaron Lub67c6b42012-06-29 16:17:31 +0800263 del_timer_sync(&host->tuning_timer);
264 host->flags &= ~SDHCI_NEEDS_RETUNING;
Aaron Lub67c6b42012-06-29 16:17:31 +0800265 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300266 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800267}
268
269static void sdhci_activate_led(struct sdhci_host *host)
270{
271 u8 ctrl;
272
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300273 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800274 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300275 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800276}
277
278static void sdhci_deactivate_led(struct sdhci_host *host)
279{
280 u8 ctrl;
281
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300282 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800283 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300284 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800285}
286
Pierre Ossmanf9134312008-12-21 17:01:48 +0100287#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100288static void sdhci_led_control(struct led_classdev *led,
289 enum led_brightness brightness)
290{
291 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
292 unsigned long flags;
293
294 spin_lock_irqsave(&host->lock, flags);
295
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300296 if (host->runtime_suspended)
297 goto out;
298
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100299 if (brightness == LED_OFF)
300 sdhci_deactivate_led(host);
301 else
302 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300303out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100304 spin_unlock_irqrestore(&host->lock, flags);
305}
306#endif
307
Pierre Ossmand129bce2006-03-24 03:18:17 -0800308/*****************************************************************************\
309 * *
310 * Core functions *
311 * *
312\*****************************************************************************/
313
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100314static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800315{
Pierre Ossman76591502008-07-21 00:32:11 +0200316 unsigned long flags;
317 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700318 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200319 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800320
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100321 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800322
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100323 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200324 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800325
Pierre Ossman76591502008-07-21 00:32:11 +0200326 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800327
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100328 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200329 if (!sg_miter_next(&host->sg_miter))
330 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800331
Pierre Ossman76591502008-07-21 00:32:11 +0200332 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800333
Pierre Ossman76591502008-07-21 00:32:11 +0200334 blksize -= len;
335 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200336
Pierre Ossman76591502008-07-21 00:32:11 +0200337 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800338
Pierre Ossman76591502008-07-21 00:32:11 +0200339 while (len) {
340 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300341 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200342 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800343 }
Pierre Ossman76591502008-07-21 00:32:11 +0200344
345 *buf = scratch & 0xFF;
346
347 buf++;
348 scratch >>= 8;
349 chunk--;
350 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800351 }
352 }
Pierre Ossman76591502008-07-21 00:32:11 +0200353
354 sg_miter_stop(&host->sg_miter);
355
356 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100357}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800358
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100359static void sdhci_write_block_pio(struct sdhci_host *host)
360{
Pierre Ossman76591502008-07-21 00:32:11 +0200361 unsigned long flags;
362 size_t blksize, len, chunk;
363 u32 scratch;
364 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100365
366 DBG("PIO writing\n");
367
368 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200369 chunk = 0;
370 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100371
Pierre Ossman76591502008-07-21 00:32:11 +0200372 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100373
374 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200375 if (!sg_miter_next(&host->sg_miter))
376 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100377
Pierre Ossman76591502008-07-21 00:32:11 +0200378 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200379
Pierre Ossman76591502008-07-21 00:32:11 +0200380 blksize -= len;
381 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100382
Pierre Ossman76591502008-07-21 00:32:11 +0200383 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100384
Pierre Ossman76591502008-07-21 00:32:11 +0200385 while (len) {
386 scratch |= (u32)*buf << (chunk * 8);
387
388 buf++;
389 chunk++;
390 len--;
391
392 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300393 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200394 chunk = 0;
395 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100396 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100397 }
398 }
Pierre Ossman76591502008-07-21 00:32:11 +0200399
400 sg_miter_stop(&host->sg_miter);
401
402 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100403}
404
405static void sdhci_transfer_pio(struct sdhci_host *host)
406{
407 u32 mask;
408
409 BUG_ON(!host->data);
410
Pierre Ossman76591502008-07-21 00:32:11 +0200411 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100412 return;
413
414 if (host->data->flags & MMC_DATA_READ)
415 mask = SDHCI_DATA_AVAILABLE;
416 else
417 mask = SDHCI_SPACE_AVAILABLE;
418
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200419 /*
420 * Some controllers (JMicron JMB38x) mess up the buffer bits
421 * for transfers < 4 bytes. As long as it is just one block,
422 * we can ignore the bits.
423 */
424 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
425 (host->data->blocks == 1))
426 mask = ~0;
427
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300428 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300429 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
430 udelay(100);
431
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100432 if (host->data->flags & MMC_DATA_READ)
433 sdhci_read_block_pio(host);
434 else
435 sdhci_write_block_pio(host);
436
Pierre Ossman76591502008-07-21 00:32:11 +0200437 host->blocks--;
438 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100439 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100440 }
441
442 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800443}
444
Pierre Ossman2134a922008-06-28 18:28:51 +0200445static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
446{
447 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800448 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200449}
450
451static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
452{
Cong Wang482fce92011-11-27 13:27:00 +0800453 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200454 local_irq_restore(*flags);
455}
456
Adrian Huntere57a5f62014-11-04 12:42:46 +0200457static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
458 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800459{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200460 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800461
Adrian Huntere57a5f62014-11-04 12:42:46 +0200462 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200463 dma_desc->cmd = cpu_to_le16(cmd);
464 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200465 dma_desc->addr_lo = cpu_to_le32((u32)addr);
466
467 if (host->flags & SDHCI_USE_64_BIT_DMA)
468 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800469}
470
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200471static void sdhci_adma_mark_end(void *desc)
472{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200473 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200474
Adrian Huntere57a5f62014-11-04 12:42:46 +0200475 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200476 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200477}
478
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200479static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200480 struct mmc_data *data)
481{
482 int direction;
483
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200484 void *desc;
485 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200486 dma_addr_t addr;
487 dma_addr_t align_addr;
488 int len, offset;
489
490 struct scatterlist *sg;
491 int i;
492 char *buffer;
493 unsigned long flags;
494
495 /*
496 * The spec does not specify endianness of descriptor table.
497 * We currently guess that it is LE.
498 */
499
500 if (data->flags & MMC_DATA_READ)
501 direction = DMA_FROM_DEVICE;
502 else
503 direction = DMA_TO_DEVICE;
504
Pierre Ossman2134a922008-06-28 18:28:51 +0200505 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200506 host->align_buffer, host->align_buffer_sz, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700507 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200508 goto fail;
Adrian Hunter76fe3792014-11-04 12:42:42 +0200509 BUG_ON(host->align_addr & host->align_mask);
Pierre Ossman2134a922008-06-28 18:28:51 +0200510
Haibo Chen348487c2014-12-09 17:04:05 +0800511 host->sg_count = sdhci_pre_dma_transfer(host, data, NULL);
512 if (host->sg_count < 0)
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200513 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200514
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200515 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200516 align = host->align_buffer;
517
518 align_addr = host->align_addr;
519
520 for_each_sg(data->sg, sg, host->sg_count, i) {
521 addr = sg_dma_address(sg);
522 len = sg_dma_len(sg);
523
524 /*
525 * The SDHCI specification states that ADMA
526 * addresses must be 32-bit aligned. If they
527 * aren't, then we use a bounce buffer for
528 * the (up to three) bytes that screw up the
529 * alignment.
530 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200531 offset = (host->align_sz - (addr & host->align_mask)) &
532 host->align_mask;
Pierre Ossman2134a922008-06-28 18:28:51 +0200533 if (offset) {
534 if (data->flags & MMC_DATA_WRITE) {
535 buffer = sdhci_kmap_atomic(sg, &flags);
536 memcpy(align, buffer, offset);
537 sdhci_kunmap_atomic(buffer, &flags);
538 }
539
Ben Dooks118cd172010-03-05 13:43:26 -0800540 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200541 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200542 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200543
544 BUG_ON(offset > 65536);
545
Adrian Hunter76fe3792014-11-04 12:42:42 +0200546 align += host->align_sz;
547 align_addr += host->align_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200548
Adrian Hunter76fe3792014-11-04 12:42:42 +0200549 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200550
551 addr += offset;
552 len -= offset;
553 }
554
Pierre Ossman2134a922008-06-28 18:28:51 +0200555 BUG_ON(len > 65536);
556
Ben Dooks118cd172010-03-05 13:43:26 -0800557 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200558 sdhci_adma_write_desc(host, desc, addr, len, ADMA2_TRAN_VALID);
Adrian Hunter76fe3792014-11-04 12:42:42 +0200559 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200560
561 /*
562 * If this triggers then we have a calculation bug
563 * somewhere. :/
564 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200565 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200566 }
567
Thomas Abraham70764a92010-05-26 14:42:04 -0700568 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
569 /*
570 * Mark the last descriptor as the terminating descriptor
571 */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200572 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200573 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200574 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700575 }
576 } else {
577 /*
578 * Add a terminating entry.
579 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200580
Thomas Abraham70764a92010-05-26 14:42:04 -0700581 /* nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200582 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700583 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200584
585 /*
586 * Resync align buffer as we might have changed it.
587 */
588 if (data->flags & MMC_DATA_WRITE) {
589 dma_sync_single_for_device(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200590 host->align_addr, host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200591 }
592
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200593 return 0;
594
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200595unmap_align:
596 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200597 host->align_buffer_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200598fail:
599 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200600}
601
602static void sdhci_adma_table_post(struct sdhci_host *host,
603 struct mmc_data *data)
604{
605 int direction;
606
607 struct scatterlist *sg;
608 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200609 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200610 char *buffer;
611 unsigned long flags;
Russell Kingde0b65a2014-04-25 12:58:29 +0100612 bool has_unaligned;
Pierre Ossman2134a922008-06-28 18:28:51 +0200613
614 if (data->flags & MMC_DATA_READ)
615 direction = DMA_FROM_DEVICE;
616 else
617 direction = DMA_TO_DEVICE;
618
Pierre Ossman2134a922008-06-28 18:28:51 +0200619 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200620 host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200621
Russell Kingde0b65a2014-04-25 12:58:29 +0100622 /* Do a quick scan of the SG list for any unaligned mappings */
623 has_unaligned = false;
624 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter76fe3792014-11-04 12:42:42 +0200625 if (sg_dma_address(sg) & host->align_mask) {
Russell Kingde0b65a2014-04-25 12:58:29 +0100626 has_unaligned = true;
627 break;
628 }
629
630 if (has_unaligned && data->flags & MMC_DATA_READ) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200631 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
632 data->sg_len, direction);
633
634 align = host->align_buffer;
635
636 for_each_sg(data->sg, sg, host->sg_count, i) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200637 if (sg_dma_address(sg) & host->align_mask) {
638 size = host->align_sz -
639 (sg_dma_address(sg) & host->align_mask);
Pierre Ossman2134a922008-06-28 18:28:51 +0200640
641 buffer = sdhci_kmap_atomic(sg, &flags);
642 memcpy(buffer, align, size);
643 sdhci_kunmap_atomic(buffer, &flags);
644
Adrian Hunter76fe3792014-11-04 12:42:42 +0200645 align += host->align_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200646 }
647 }
648 }
649
Haibo Chen348487c2014-12-09 17:04:05 +0800650 if (!data->host_cookie)
651 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
652 data->sg_len, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200653}
654
Andrei Warkentina3c77782011-04-11 16:13:42 -0500655static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800656{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700657 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500658 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700659 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800660
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200661 /*
662 * If the host controller provides us with an incorrect timeout
663 * value, just skip the check and use 0xE. The hardware may take
664 * longer to time out, but that's much better than having a too-short
665 * timeout value.
666 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200667 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200668 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200669
Andrei Warkentina3c77782011-04-11 16:13:42 -0500670 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100671 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500672 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800673
Andrei Warkentina3c77782011-04-11 16:13:42 -0500674 /* timeout in us */
675 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100676 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300677 else {
678 target_timeout = data->timeout_ns / 1000;
679 if (host->clock)
680 target_timeout += data->timeout_clks / host->clock;
681 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700682
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700683 /*
684 * Figure out needed cycles.
685 * We do this in steps in order to fit inside a 32 bit int.
686 * The first step is the minimum timeout, which will have a
687 * minimum resolution of 6 bits:
688 * (1) 2^13*1000 > 2^22,
689 * (2) host->timeout_clk < 2^16
690 * =>
691 * (1) / (2) > 2^6
692 */
693 count = 0;
694 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
695 while (current_timeout < target_timeout) {
696 count++;
697 current_timeout <<= 1;
698 if (count >= 0xF)
699 break;
700 }
701
702 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400703 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
704 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700705 count = 0xE;
706 }
707
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200708 return count;
709}
710
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300711static void sdhci_set_transfer_irqs(struct sdhci_host *host)
712{
713 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
714 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
715
716 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100717 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300718 else
Russell Kingb537f942014-04-25 12:56:01 +0100719 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
720
721 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
722 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300723}
724
Aisheng Dongb45e6682014-08-27 15:26:29 +0800725static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200726{
727 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800728
729 if (host->ops->set_timeout) {
730 host->ops->set_timeout(host, cmd);
731 } else {
732 count = sdhci_calc_timeout(host, cmd);
733 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
734 }
735}
736
737static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
738{
Pierre Ossman2134a922008-06-28 18:28:51 +0200739 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500740 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200741 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200742
743 WARN_ON(host->data);
744
Aisheng Dongb45e6682014-08-27 15:26:29 +0800745 if (data || (cmd->flags & MMC_RSP_BUSY))
746 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500747
748 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200749 return;
750
751 /* Sanity checks */
752 BUG_ON(data->blksz * data->blocks > 524288);
753 BUG_ON(data->blksz > host->mmc->max_blk_size);
754 BUG_ON(data->blocks > 65535);
755
756 host->data = data;
757 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400758 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200759
Richard Röjforsa13abc72009-09-22 16:45:30 -0700760 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100761 host->flags |= SDHCI_REQ_USE_DMA;
762
Pierre Ossman2134a922008-06-28 18:28:51 +0200763 /*
764 * FIXME: This doesn't account for merging when mapping the
765 * scatterlist.
766 */
767 if (host->flags & SDHCI_REQ_USE_DMA) {
768 int broken, i;
769 struct scatterlist *sg;
770
771 broken = 0;
772 if (host->flags & SDHCI_USE_ADMA) {
773 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
774 broken = 1;
775 } else {
776 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
777 broken = 1;
778 }
779
780 if (unlikely(broken)) {
781 for_each_sg(data->sg, sg, data->sg_len, i) {
782 if (sg->length & 0x3) {
783 DBG("Reverting to PIO because of "
784 "transfer size (%d)\n",
785 sg->length);
786 host->flags &= ~SDHCI_REQ_USE_DMA;
787 break;
788 }
789 }
790 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100791 }
792
793 /*
794 * The assumption here being that alignment is the same after
795 * translation to device address space.
796 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200797 if (host->flags & SDHCI_REQ_USE_DMA) {
798 int broken, i;
799 struct scatterlist *sg;
800
801 broken = 0;
802 if (host->flags & SDHCI_USE_ADMA) {
803 /*
804 * As we use 3 byte chunks to work around
805 * alignment problems, we need to check this
806 * quirk.
807 */
808 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
809 broken = 1;
810 } else {
811 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
812 broken = 1;
813 }
814
815 if (unlikely(broken)) {
816 for_each_sg(data->sg, sg, data->sg_len, i) {
817 if (sg->offset & 0x3) {
818 DBG("Reverting to PIO because of "
819 "bad alignment\n");
820 host->flags &= ~SDHCI_REQ_USE_DMA;
821 break;
822 }
823 }
824 }
825 }
826
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200827 if (host->flags & SDHCI_REQ_USE_DMA) {
828 if (host->flags & SDHCI_USE_ADMA) {
829 ret = sdhci_adma_table_pre(host, data);
830 if (ret) {
831 /*
832 * This only happens when someone fed
833 * us an invalid request.
834 */
835 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200836 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200837 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300838 sdhci_writel(host, host->adma_addr,
839 SDHCI_ADMA_ADDRESS);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200840 if (host->flags & SDHCI_USE_64_BIT_DMA)
841 sdhci_writel(host,
842 (u64)host->adma_addr >> 32,
843 SDHCI_ADMA_ADDRESS_HI);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200844 }
845 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300846 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200847
Haibo Chen348487c2014-12-09 17:04:05 +0800848 sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300849 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200850 /*
851 * This only happens when someone fed
852 * us an invalid request.
853 */
854 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200855 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200856 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200857 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300858 sdhci_writel(host, sg_dma_address(data->sg),
859 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200860 }
861 }
862 }
863
Pierre Ossman2134a922008-06-28 18:28:51 +0200864 /*
865 * Always adjust the DMA selection as some controllers
866 * (e.g. JMicron) can't do PIO properly when the selection
867 * is ADMA.
868 */
869 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300870 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200871 ctrl &= ~SDHCI_CTRL_DMA_MASK;
872 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200873 (host->flags & SDHCI_USE_ADMA)) {
874 if (host->flags & SDHCI_USE_64_BIT_DMA)
875 ctrl |= SDHCI_CTRL_ADMA64;
876 else
877 ctrl |= SDHCI_CTRL_ADMA32;
878 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200879 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200880 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300881 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100882 }
883
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200884 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200885 int flags;
886
887 flags = SG_MITER_ATOMIC;
888 if (host->data->flags & MMC_DATA_READ)
889 flags |= SG_MITER_TO_SG;
890 else
891 flags |= SG_MITER_FROM_SG;
892 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200893 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800894 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700895
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300896 sdhci_set_transfer_irqs(host);
897
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400898 /* Set the DMA boundary value and block size */
899 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
900 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300901 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700902}
903
904static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500905 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700906{
907 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500908 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700909
Dong Aisheng2b558c12013-10-30 22:09:48 +0800910 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800911 if (host->quirks2 &
912 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
913 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
914 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800915 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800916 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
917 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800918 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800919 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700920 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800921 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700922
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200923 WARN_ON(!host->data);
924
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700925 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500926 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
927 mode |= SDHCI_TRNS_MULTI;
928 /*
929 * If we are sending CMD23, CMD12 never gets sent
930 * on successful completion (so no Auto-CMD12).
931 */
932 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
933 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500934 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
935 mode |= SDHCI_TRNS_AUTO_CMD23;
936 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
937 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700938 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500939
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700940 if (data->flags & MMC_DATA_READ)
941 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100942 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700943 mode |= SDHCI_TRNS_DMA;
944
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300945 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800946}
947
948static void sdhci_finish_data(struct sdhci_host *host)
949{
950 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800951
952 BUG_ON(!host->data);
953
954 data = host->data;
955 host->data = NULL;
956
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100957 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200958 if (host->flags & SDHCI_USE_ADMA)
959 sdhci_adma_table_post(host, data);
960 else {
Haibo Chen348487c2014-12-09 17:04:05 +0800961 if (!data->host_cookie)
962 dma_unmap_sg(mmc_dev(host->mmc),
963 data->sg, data->sg_len,
964 (data->flags & MMC_DATA_READ) ?
Pierre Ossman2134a922008-06-28 18:28:51 +0200965 DMA_FROM_DEVICE : DMA_TO_DEVICE);
966 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800967 }
968
969 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200970 * The specification states that the block count register must
971 * be updated, but it does not specify at what point in the
972 * data flow. That makes the register entirely useless to read
973 * back so we have to assume that nothing made it to the card
974 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800975 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200976 if (data->error)
977 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800978 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200979 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800980
Andrei Warkentine89d4562011-05-23 15:06:37 -0500981 /*
982 * Need to send CMD12 if -
983 * a) open-ended multiblock transfer (no CMD23)
984 * b) error in multiblock transfer
985 */
986 if (data->stop &&
987 (data->error ||
988 !host->mrq->sbc)) {
989
Pierre Ossmand129bce2006-03-24 03:18:17 -0800990 /*
991 * The controller needs a reset of internal state machines
992 * upon error conditions.
993 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200994 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100995 sdhci_do_reset(host, SDHCI_RESET_CMD);
996 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997 }
998
999 sdhci_send_command(host, data->stop);
1000 } else
1001 tasklet_schedule(&host->finish_tasklet);
1002}
1003
Dong Aishengc0e551292013-09-13 19:11:31 +08001004void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001005{
1006 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001007 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001008 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009
1010 WARN_ON(host->cmd);
1011
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001013 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001014
1015 mask = SDHCI_CMD_INHIBIT;
1016 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1017 mask |= SDHCI_DATA_INHIBIT;
1018
1019 /* We shouldn't wait for data inihibit for stop commands, even
1020 though they might use busy signaling */
1021 if (host->mrq->data && (cmd == host->mrq->data->stop))
1022 mask &= ~SDHCI_DATA_INHIBIT;
1023
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001024 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001025 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301026 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001027 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001028 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001029 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030 tasklet_schedule(&host->finish_tasklet);
1031 return;
1032 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001033 timeout--;
1034 mdelay(1);
1035 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001036
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001037 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001038 if (!cmd->data && cmd->busy_timeout > 9000)
1039 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001040 else
1041 timeout += 10 * HZ;
1042 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001043
1044 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001045 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001046
Andrei Warkentina3c77782011-04-11 16:13:42 -05001047 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001048
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001049 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001050
Andrei Warkentine89d4562011-05-23 15:06:37 -05001051 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001052
Pierre Ossmand129bce2006-03-24 03:18:17 -08001053 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301054 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001055 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001056 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057 tasklet_schedule(&host->finish_tasklet);
1058 return;
1059 }
1060
1061 if (!(cmd->flags & MMC_RSP_PRESENT))
1062 flags = SDHCI_CMD_RESP_NONE;
1063 else if (cmd->flags & MMC_RSP_136)
1064 flags = SDHCI_CMD_RESP_LONG;
1065 else if (cmd->flags & MMC_RSP_BUSY)
1066 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1067 else
1068 flags = SDHCI_CMD_RESP_SHORT;
1069
1070 if (cmd->flags & MMC_RSP_CRC)
1071 flags |= SDHCI_CMD_CRC;
1072 if (cmd->flags & MMC_RSP_OPCODE)
1073 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301074
1075 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301076 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1077 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001078 flags |= SDHCI_CMD_DATA;
1079
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001080 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001081}
Dong Aishengc0e551292013-09-13 19:11:31 +08001082EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001083
1084static void sdhci_finish_command(struct sdhci_host *host)
1085{
1086 int i;
1087
1088 BUG_ON(host->cmd == NULL);
1089
1090 if (host->cmd->flags & MMC_RSP_PRESENT) {
1091 if (host->cmd->flags & MMC_RSP_136) {
1092 /* CRC is stripped so we need to do some shifting. */
1093 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001094 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001095 SDHCI_RESPONSE + (3-i)*4) << 8;
1096 if (i != 3)
1097 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001098 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001099 SDHCI_RESPONSE + (3-i)*4-1);
1100 }
1101 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001102 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001103 }
1104 }
1105
Pierre Ossman17b04292007-07-22 22:18:46 +02001106 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001107
Andrei Warkentine89d4562011-05-23 15:06:37 -05001108 /* Finished CMD23, now send actual command. */
1109 if (host->cmd == host->mrq->sbc) {
1110 host->cmd = NULL;
1111 sdhci_send_command(host, host->mrq->cmd);
1112 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001113
Andrei Warkentine89d4562011-05-23 15:06:37 -05001114 /* Processed actual command. */
1115 if (host->data && host->data_early)
1116 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001117
Andrei Warkentine89d4562011-05-23 15:06:37 -05001118 if (!host->cmd->data)
1119 tasklet_schedule(&host->finish_tasklet);
1120
1121 host->cmd = NULL;
1122 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001123}
1124
Kevin Liu52983382013-01-31 11:31:37 +08001125static u16 sdhci_get_preset_value(struct sdhci_host *host)
1126{
Russell Kingd975f122014-04-25 12:59:31 +01001127 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001128
Russell Kingd975f122014-04-25 12:59:31 +01001129 switch (host->timing) {
1130 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001131 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1132 break;
Russell Kingd975f122014-04-25 12:59:31 +01001133 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001134 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1135 break;
Russell Kingd975f122014-04-25 12:59:31 +01001136 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001137 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1138 break;
Russell Kingd975f122014-04-25 12:59:31 +01001139 case MMC_TIMING_UHS_SDR104:
1140 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001141 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1142 break;
Russell Kingd975f122014-04-25 12:59:31 +01001143 case MMC_TIMING_UHS_DDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001144 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1145 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001146 case MMC_TIMING_MMC_HS400:
1147 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1148 break;
Kevin Liu52983382013-01-31 11:31:37 +08001149 default:
1150 pr_warn("%s: Invalid UHS-I mode selected\n",
1151 mmc_hostname(host->mmc));
1152 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1153 break;
1154 }
1155 return preset;
1156}
1157
Russell King17710592014-04-25 12:58:55 +01001158void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001159{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301160 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001161 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301162 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001163 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001164
Russell King1650d0c2014-04-25 12:58:50 +01001165 host->mmc->actual_clock = 0;
1166
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001167 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001168
1169 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001170 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001171
Zhangfei Gao85105c52010-08-06 07:10:01 +08001172 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001173 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001174 u16 pre_val;
1175
1176 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1177 pre_val = sdhci_get_preset_value(host);
1178 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1179 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1180 if (host->clk_mul &&
1181 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1182 clk = SDHCI_PROG_CLOCK_MODE;
1183 real_div = div + 1;
1184 clk_mul = host->clk_mul;
1185 } else {
1186 real_div = max_t(int, 1, div << 1);
1187 }
1188 goto clock_set;
1189 }
1190
Arindam Nathc3ed3872011-05-05 12:19:06 +05301191 /*
1192 * Check if the Host Controller supports Programmable Clock
1193 * Mode.
1194 */
1195 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001196 for (div = 1; div <= 1024; div++) {
1197 if ((host->max_clk * host->clk_mul / div)
1198 <= clock)
1199 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001200 }
Kevin Liu52983382013-01-31 11:31:37 +08001201 /*
1202 * Set Programmable Clock Mode in the Clock
1203 * Control register.
1204 */
1205 clk = SDHCI_PROG_CLOCK_MODE;
1206 real_div = div;
1207 clk_mul = host->clk_mul;
1208 div--;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301209 } else {
1210 /* Version 3.00 divisors must be a multiple of 2. */
1211 if (host->max_clk <= clock)
1212 div = 1;
1213 else {
1214 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1215 div += 2) {
1216 if ((host->max_clk / div) <= clock)
1217 break;
1218 }
1219 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001220 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301221 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001222 }
1223 } else {
1224 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001225 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001226 if ((host->max_clk / div) <= clock)
1227 break;
1228 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001229 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301230 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001231 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001232
Kevin Liu52983382013-01-31 11:31:37 +08001233clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001234 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001235 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301236 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001237 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1238 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001239 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001240 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001241
Chris Ball27f6cb12009-09-22 16:45:31 -07001242 /* Wait max 20 ms */
1243 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001244 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001245 & SDHCI_CLOCK_INT_STABLE)) {
1246 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301247 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001248 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001249 sdhci_dumpregs(host);
1250 return;
1251 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001252 timeout--;
1253 mdelay(1);
1254 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001255
1256 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001257 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001258}
Russell King17710592014-04-25 12:58:55 +01001259EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001260
Russell King24fbb3c2014-04-25 13:00:06 +01001261static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1262 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001263{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001264 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001265 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001266
Tim Kryger52221612014-06-25 00:25:34 -07001267 if (!IS_ERR(mmc->supply.vmmc)) {
1268 spin_unlock_irq(&host->lock);
Markus Mayer4e743f12014-07-03 13:27:42 -07001269 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Tim Kryger52221612014-06-25 00:25:34 -07001270 spin_lock_irq(&host->lock);
Tim Kryger3cbc6122015-01-14 07:24:12 +01001271
1272 if (mode != MMC_POWER_OFF)
1273 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1274 else
1275 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1276
Tim Kryger52221612014-06-25 00:25:34 -07001277 return;
1278 }
1279
Russell King24fbb3c2014-04-25 13:00:06 +01001280 if (mode != MMC_POWER_OFF) {
1281 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001282 case MMC_VDD_165_195:
1283 pwr = SDHCI_POWER_180;
1284 break;
1285 case MMC_VDD_29_30:
1286 case MMC_VDD_30_31:
1287 pwr = SDHCI_POWER_300;
1288 break;
1289 case MMC_VDD_32_33:
1290 case MMC_VDD_33_34:
1291 pwr = SDHCI_POWER_330;
1292 break;
1293 default:
1294 BUG();
1295 }
1296 }
1297
1298 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001299 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001300
Pierre Ossmanae628902009-05-03 20:45:03 +02001301 host->pwr = pwr;
1302
1303 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001304 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001305 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1306 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001307 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001308 } else {
1309 /*
1310 * Spec says that we should clear the power reg before setting
1311 * a new value. Some controllers don't seem to like this though.
1312 */
1313 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1314 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001315
Russell Kinge921a8b2014-04-25 13:00:01 +01001316 /*
1317 * At least the Marvell CaFe chip gets confused if we set the
1318 * voltage and set turn on power at the same time, so set the
1319 * voltage first.
1320 */
1321 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1322 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001323
Russell Kinge921a8b2014-04-25 13:00:01 +01001324 pwr |= SDHCI_POWER_ON;
1325
Pierre Ossmanae628902009-05-03 20:45:03 +02001326 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1327
Russell Kinge921a8b2014-04-25 13:00:01 +01001328 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1329 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001330
Russell Kinge921a8b2014-04-25 13:00:01 +01001331 /*
1332 * Some controllers need an extra 10ms delay of 10ms before
1333 * they can apply clock after applying power
1334 */
1335 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1336 mdelay(10);
1337 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001338}
1339
Pierre Ossmand129bce2006-03-24 03:18:17 -08001340/*****************************************************************************\
1341 * *
1342 * MMC callbacks *
1343 * *
1344\*****************************************************************************/
1345
1346static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1347{
1348 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001349 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001350 unsigned long flags;
Aaron Lu473b0952012-07-03 17:27:49 +08001351 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001352
1353 host = mmc_priv(mmc);
1354
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001355 sdhci_runtime_pm_get(host);
1356
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001357 present = mmc_gpio_get_cd(host->mmc);
1358
Pierre Ossmand129bce2006-03-24 03:18:17 -08001359 spin_lock_irqsave(&host->lock, flags);
1360
1361 WARN_ON(host->mrq != NULL);
1362
Pierre Ossmanf9134312008-12-21 17:01:48 +01001363#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001364 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001365#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001366
1367 /*
1368 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1369 * requests if Auto-CMD12 is enabled.
1370 */
1371 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001372 if (mrq->stop) {
1373 mrq->data->stop = NULL;
1374 mrq->stop = NULL;
1375 }
1376 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001377
1378 host->mrq = mrq;
1379
Shawn Guo505a8682012-12-11 15:23:42 +08001380 /*
1381 * Firstly check card presence from cd-gpio. The return could
1382 * be one of the following possibilities:
1383 * negative: cd-gpio is not available
1384 * zero: cd-gpio is used, and card is removed
1385 * one: cd-gpio is used, and card is present
1386 */
Shawn Guo505a8682012-12-11 15:23:42 +08001387 if (present < 0) {
1388 /* If polling, assume that the card is always present. */
1389 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1390 present = 1;
1391 else
1392 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1393 SDHCI_CARD_PRESENT;
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +08001394 }
1395
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001396 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001397 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001398 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301399 } else {
1400 u32 present_state;
1401
1402 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1403 /*
1404 * Check if the re-tuning timer has already expired and there
Yi Sun7756a96d2014-09-09 02:13:59 +00001405 * is no on-going data transfer and DAT0 is not busy. If so,
1406 * we need to execute tuning procedure before sending command.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301407 */
1408 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
Yi Sun7756a96d2014-09-09 02:13:59 +00001409 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ)) &&
1410 (present_state & SDHCI_DATA_0_LVL_MASK)) {
Chris Ball14efd952012-11-05 14:29:49 -05001411 if (mmc->card) {
1412 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1413 tuning_opcode =
1414 mmc->card->type == MMC_TYPE_MMC ?
1415 MMC_SEND_TUNING_BLOCK_HS200 :
1416 MMC_SEND_TUNING_BLOCK;
Chuansheng Liu63c21182013-11-05 14:52:45 +08001417
1418 /* Here we need to set the host->mrq to NULL,
1419 * in case the pending finish_tasklet
1420 * finishes it incorrectly.
1421 */
1422 host->mrq = NULL;
1423
Chris Ball14efd952012-11-05 14:29:49 -05001424 spin_unlock_irqrestore(&host->lock, flags);
1425 sdhci_execute_tuning(mmc, tuning_opcode);
1426 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301427
Chris Ball14efd952012-11-05 14:29:49 -05001428 /* Restore original mmc_request structure */
1429 host->mrq = mrq;
1430 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301431 }
1432
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001433 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001434 sdhci_send_command(host, mrq->sbc);
1435 else
1436 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301437 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001438
Pierre Ossman5f25a662006-10-04 02:15:39 -07001439 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001440 spin_unlock_irqrestore(&host->lock, flags);
1441}
1442
Russell King2317f562014-04-25 12:57:07 +01001443void sdhci_set_bus_width(struct sdhci_host *host, int width)
1444{
1445 u8 ctrl;
1446
1447 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1448 if (width == MMC_BUS_WIDTH_8) {
1449 ctrl &= ~SDHCI_CTRL_4BITBUS;
1450 if (host->version >= SDHCI_SPEC_300)
1451 ctrl |= SDHCI_CTRL_8BITBUS;
1452 } else {
1453 if (host->version >= SDHCI_SPEC_300)
1454 ctrl &= ~SDHCI_CTRL_8BITBUS;
1455 if (width == MMC_BUS_WIDTH_4)
1456 ctrl |= SDHCI_CTRL_4BITBUS;
1457 else
1458 ctrl &= ~SDHCI_CTRL_4BITBUS;
1459 }
1460 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1461}
1462EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1463
Russell King96d7b782014-04-25 12:59:26 +01001464void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1465{
1466 u16 ctrl_2;
1467
1468 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1469 /* Select Bus Speed Mode for host */
1470 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1471 if ((timing == MMC_TIMING_MMC_HS200) ||
1472 (timing == MMC_TIMING_UHS_SDR104))
1473 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1474 else if (timing == MMC_TIMING_UHS_SDR12)
1475 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1476 else if (timing == MMC_TIMING_UHS_SDR25)
1477 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1478 else if (timing == MMC_TIMING_UHS_SDR50)
1479 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1480 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1481 (timing == MMC_TIMING_MMC_DDR52))
1482 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001483 else if (timing == MMC_TIMING_MMC_HS400)
1484 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001485 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1486}
1487EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1488
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001489static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001490{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001491 unsigned long flags;
1492 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001493 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001494
Pierre Ossmand129bce2006-03-24 03:18:17 -08001495 spin_lock_irqsave(&host->lock, flags);
1496
Adrian Hunterceb61432011-12-27 15:48:41 +02001497 if (host->flags & SDHCI_DEVICE_DEAD) {
1498 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001499 if (!IS_ERR(mmc->supply.vmmc) &&
1500 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001501 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001502 return;
1503 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001504
Pierre Ossmand129bce2006-03-24 03:18:17 -08001505 /*
1506 * Reset the chip on each power off.
1507 * Should clear out any weird states.
1508 */
1509 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001510 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001511 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001512 }
1513
Kevin Liu52983382013-01-31 11:31:37 +08001514 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001515 (ios->power_mode == MMC_POWER_UP) &&
1516 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001517 sdhci_enable_preset_value(host, false);
1518
Russell King373073e2014-04-25 12:58:45 +01001519 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001520 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001521 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001522
1523 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1524 host->clock) {
1525 host->timeout_clk = host->mmc->actual_clock ?
1526 host->mmc->actual_clock / 1000 :
1527 host->clock / 1000;
1528 host->mmc->max_busy_timeout =
1529 host->ops->get_max_timeout_count ?
1530 host->ops->get_max_timeout_count(host) :
1531 1 << 27;
1532 host->mmc->max_busy_timeout /= host->timeout_clk;
1533 }
Russell King373073e2014-04-25 12:58:45 +01001534 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001535
Russell King24fbb3c2014-04-25 13:00:06 +01001536 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001537
Philip Rakity643a81f2010-09-23 08:24:32 -07001538 if (host->ops->platform_send_init_74_clocks)
1539 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1540
Russell King2317f562014-04-25 12:57:07 +01001541 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001542
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001543 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001544
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001545 if ((ios->timing == MMC_TIMING_SD_HS ||
1546 ios->timing == MMC_TIMING_MMC_HS)
1547 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001548 ctrl |= SDHCI_CTRL_HISPD;
1549 else
1550 ctrl &= ~SDHCI_CTRL_HISPD;
1551
Arindam Nathd6d50a12011-05-05 12:18:59 +05301552 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301553 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301554
1555 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001556 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1557 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001558 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301559 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301560 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1561 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001562 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301563 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301564
Russell Kingda91a8f2014-04-25 13:00:12 +01001565 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301566 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301567 /*
1568 * We only need to set Driver Strength if the
1569 * preset value enable is not set.
1570 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001571 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301572 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1573 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1574 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1575 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1576 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1577
1578 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301579 } else {
1580 /*
1581 * According to SDHC Spec v3.00, if the Preset Value
1582 * Enable in the Host Control 2 register is set, we
1583 * need to reset SD Clock Enable before changing High
1584 * Speed Enable to avoid generating clock gliches.
1585 */
Arindam Nath758535c2011-05-05 12:19:00 +05301586
1587 /* Reset SD Clock Enable */
1588 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1589 clk &= ~SDHCI_CLOCK_CARD_EN;
1590 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1591
1592 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1593
1594 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001595 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301596 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301597
Arindam Nath49c468f2011-05-05 12:19:01 +05301598 /* Reset SD Clock Enable */
1599 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1600 clk &= ~SDHCI_CLOCK_CARD_EN;
1601 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1602
Russell King96d7b782014-04-25 12:59:26 +01001603 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001604 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301605
Kevin Liu52983382013-01-31 11:31:37 +08001606 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1607 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1608 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1609 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1610 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1611 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1612 u16 preset;
1613
1614 sdhci_enable_preset_value(host, true);
1615 preset = sdhci_get_preset_value(host);
1616 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1617 >> SDHCI_PRESET_DRV_SHIFT;
1618 }
1619
Arindam Nath49c468f2011-05-05 12:19:01 +05301620 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001621 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301622 } else
1623 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301624
Leandro Dorileob8352262007-07-25 23:47:04 +02001625 /*
1626 * Some (ENE) controllers go apeshit on some ios operation,
1627 * signalling timeout and CRC errors even on CMD0. Resetting
1628 * it on each ios seems to solve the problem.
1629 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301630 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001631 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001632
Pierre Ossman5f25a662006-10-04 02:15:39 -07001633 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001634 spin_unlock_irqrestore(&host->lock, flags);
1635}
1636
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001637static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1638{
1639 struct sdhci_host *host = mmc_priv(mmc);
1640
1641 sdhci_runtime_pm_get(host);
1642 sdhci_do_set_ios(host, ios);
1643 sdhci_runtime_pm_put(host);
1644}
1645
Kevin Liu94144a42013-02-28 17:35:53 +08001646static int sdhci_do_get_cd(struct sdhci_host *host)
1647{
1648 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1649
1650 if (host->flags & SDHCI_DEVICE_DEAD)
1651 return 0;
1652
1653 /* If polling/nonremovable, assume that the card is always present. */
1654 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1655 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1656 return 1;
1657
1658 /* Try slot gpio detect */
1659 if (!IS_ERR_VALUE(gpio_cd))
1660 return !!gpio_cd;
1661
1662 /* Host native card detect */
1663 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1664}
1665
1666static int sdhci_get_cd(struct mmc_host *mmc)
1667{
1668 struct sdhci_host *host = mmc_priv(mmc);
1669 int ret;
1670
1671 sdhci_runtime_pm_get(host);
1672 ret = sdhci_do_get_cd(host);
1673 sdhci_runtime_pm_put(host);
1674 return ret;
1675}
1676
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001677static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001678{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001679 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001680 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001681
Pierre Ossmand129bce2006-03-24 03:18:17 -08001682 spin_lock_irqsave(&host->lock, flags);
1683
Pierre Ossman1e728592008-04-16 19:13:13 +02001684 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001685 is_readonly = 0;
1686 else if (host->ops->get_ro)
1687 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001688 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001689 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1690 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001691
1692 spin_unlock_irqrestore(&host->lock, flags);
1693
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001694 /* This quirk needs to be replaced by a callback-function later */
1695 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1696 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001697}
1698
Takashi Iwai82b0e232011-04-21 20:26:38 +02001699#define SAMPLE_COUNT 5
1700
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001701static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001702{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001703 int i, ro_count;
1704
Takashi Iwai82b0e232011-04-21 20:26:38 +02001705 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001706 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001707
1708 ro_count = 0;
1709 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001710 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001711 if (++ro_count > SAMPLE_COUNT / 2)
1712 return 1;
1713 }
1714 msleep(30);
1715 }
1716 return 0;
1717}
1718
Adrian Hunter20758b62011-08-29 16:42:12 +03001719static void sdhci_hw_reset(struct mmc_host *mmc)
1720{
1721 struct sdhci_host *host = mmc_priv(mmc);
1722
1723 if (host->ops && host->ops->hw_reset)
1724 host->ops->hw_reset(host);
1725}
1726
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001727static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001728{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001729 struct sdhci_host *host = mmc_priv(mmc);
1730 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001731
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001732 sdhci_runtime_pm_get(host);
1733 ret = sdhci_do_get_ro(host);
1734 sdhci_runtime_pm_put(host);
1735 return ret;
1736}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001737
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001738static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1739{
Russell Kingbe138552014-04-25 12:55:56 +01001740 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001741 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001742 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001743 else
Russell Kingb537f942014-04-25 12:56:01 +01001744 host->ier &= ~SDHCI_INT_CARD_INT;
1745
1746 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1747 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001748 mmiowb();
1749 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001750}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001751
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001752static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1753{
1754 struct sdhci_host *host = mmc_priv(mmc);
1755 unsigned long flags;
1756
Russell Kingef104332014-04-25 12:55:41 +01001757 sdhci_runtime_pm_get(host);
1758
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001759 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001760 if (enable)
1761 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1762 else
1763 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1764
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001765 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001766 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001767
1768 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001769}
1770
Philip Rakity6231f3d2012-07-23 15:56:23 -07001771static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001772 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001773{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001774 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001775 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001776 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001777
1778 /*
1779 * Signal Voltage Switching is only applicable for Host Controllers
1780 * v3.00 and above.
1781 */
1782 if (host->version < SDHCI_SPEC_300)
1783 return 0;
1784
Philip Rakity6231f3d2012-07-23 15:56:23 -07001785 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001786
Fabio Estevam21f59982013-02-14 10:35:03 -02001787 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001788 case MMC_SIGNAL_VOLTAGE_330:
1789 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1790 ctrl &= ~SDHCI_CTRL_VDD_180;
1791 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1792
Tim Kryger3a48edc2014-06-13 10:13:56 -07001793 if (!IS_ERR(mmc->supply.vqmmc)) {
1794 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1795 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001796 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001797 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1798 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001799 return -EIO;
1800 }
1801 }
1802 /* Wait for 5ms */
1803 usleep_range(5000, 5500);
1804
1805 /* 3.3V regulator output should be stable within 5 ms */
1806 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1807 if (!(ctrl & SDHCI_CTRL_VDD_180))
1808 return 0;
1809
Joe Perches66061102014-09-12 14:56:56 -07001810 pr_warn("%s: 3.3V regulator output did not became stable\n",
1811 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001812
1813 return -EAGAIN;
1814 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001815 if (!IS_ERR(mmc->supply.vqmmc)) {
1816 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001817 1700000, 1950000);
1818 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001819 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1820 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001821 return -EIO;
1822 }
1823 }
1824
1825 /*
1826 * Enable 1.8V Signal Enable in the Host Control2
1827 * register
1828 */
1829 ctrl |= SDHCI_CTRL_VDD_180;
1830 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1831
Vincent Yang9d967a62015-01-20 16:05:15 +08001832 /* Some controller need to do more when switching */
1833 if (host->ops->voltage_switch)
1834 host->ops->voltage_switch(host);
1835
Kevin Liu20b92a32012-12-17 19:29:26 +08001836 /* 1.8V regulator output should be stable within 5 ms */
1837 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1838 if (ctrl & SDHCI_CTRL_VDD_180)
1839 return 0;
1840
Joe Perches66061102014-09-12 14:56:56 -07001841 pr_warn("%s: 1.8V regulator output did not became stable\n",
1842 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001843
1844 return -EAGAIN;
1845 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001846 if (!IS_ERR(mmc->supply.vqmmc)) {
1847 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1848 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001849 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001850 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1851 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001852 return -EIO;
1853 }
1854 }
1855 return 0;
1856 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301857 /* No signal voltage switch required */
1858 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001859 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301860}
1861
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001862static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001863 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001864{
1865 struct sdhci_host *host = mmc_priv(mmc);
1866 int err;
1867
1868 if (host->version < SDHCI_SPEC_300)
1869 return 0;
1870 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001871 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001872 sdhci_runtime_pm_put(host);
1873 return err;
1874}
1875
Kevin Liu20b92a32012-12-17 19:29:26 +08001876static int sdhci_card_busy(struct mmc_host *mmc)
1877{
1878 struct sdhci_host *host = mmc_priv(mmc);
1879 u32 present_state;
1880
1881 sdhci_runtime_pm_get(host);
1882 /* Check whether DAT[3:0] is 0000 */
1883 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1884 sdhci_runtime_pm_put(host);
1885
1886 return !(present_state & SDHCI_DATA_LVL_MASK);
1887}
1888
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001889static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1890{
1891 struct sdhci_host *host = mmc_priv(mmc);
1892 unsigned long flags;
1893
1894 spin_lock_irqsave(&host->lock, flags);
1895 host->flags |= SDHCI_HS400_TUNING;
1896 spin_unlock_irqrestore(&host->lock, flags);
1897
1898 return 0;
1899}
1900
Girish K S069c9f12012-01-06 09:56:39 +05301901static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301902{
Russell King4b6f37d2014-04-25 12:59:36 +01001903 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301904 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301905 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301906 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001907 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001908 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001909 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301910
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001911 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001912 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301913
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001914 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1915 host->flags &= ~SDHCI_HS400_TUNING;
1916
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001917 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1918 tuning_count = host->tuning_count;
1919
Arindam Nathb513ea22011-05-05 12:19:04 +05301920 /*
Girish K S069c9f12012-01-06 09:56:39 +05301921 * The Host Controller needs tuning only in case of SDR104 mode
1922 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301923 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301924 * If the Host Controller supports the HS200 mode then the
1925 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301926 */
Russell King4b6f37d2014-04-25 12:59:36 +01001927 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001928 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001929 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001930 err = -EINVAL;
1931 goto out_unlock;
1932
Russell King4b6f37d2014-04-25 12:59:36 +01001933 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001934 /*
1935 * Periodic re-tuning for HS400 is not expected to be needed, so
1936 * disable it here.
1937 */
1938 if (hs400_tuning)
1939 tuning_count = 0;
1940 break;
1941
Russell King4b6f37d2014-04-25 12:59:36 +01001942 case MMC_TIMING_UHS_SDR104:
1943 break;
Girish K S069c9f12012-01-06 09:56:39 +05301944
Russell King4b6f37d2014-04-25 12:59:36 +01001945 case MMC_TIMING_UHS_SDR50:
1946 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1947 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1948 break;
1949 /* FALLTHROUGH */
1950
1951 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001952 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301953 }
1954
Dong Aisheng45251812013-09-13 19:11:30 +08001955 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001956 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001957 err = host->ops->platform_execute_tuning(host, opcode);
1958 sdhci_runtime_pm_put(host);
1959 return err;
1960 }
1961
Russell King4b6f37d2014-04-25 12:59:36 +01001962 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1963 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Arindam Nathb513ea22011-05-05 12:19:04 +05301964 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1965
1966 /*
1967 * As per the Host Controller spec v3.00, tuning command
1968 * generates Buffer Read Ready interrupt, so enable that.
1969 *
1970 * Note: The spec clearly says that when tuning sequence
1971 * is being performed, the controller does not generate
1972 * interrupts other than Buffer Read Ready interrupt. But
1973 * to make sure we don't hit a controller bug, we _only_
1974 * enable Buffer Read Ready interrupt here.
1975 */
Russell Kingb537f942014-04-25 12:56:01 +01001976 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1977 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301978
1979 /*
1980 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1981 * of loops reaches 40 times or a timeout of 150ms occurs.
1982 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301983 do {
1984 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001985 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301986
Girish K S069c9f12012-01-06 09:56:39 +05301987 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301988 cmd.arg = 0;
1989 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1990 cmd.retries = 0;
1991 cmd.data = NULL;
1992 cmd.error = 0;
1993
Al Cooper7ce45e92014-05-09 11:34:07 -04001994 if (tuning_loop_counter-- == 0)
1995 break;
1996
Arindam Nathb513ea22011-05-05 12:19:04 +05301997 mrq.cmd = &cmd;
1998 host->mrq = &mrq;
1999
2000 /*
2001 * In response to CMD19, the card sends 64 bytes of tuning
2002 * block to the Host Controller. So we set the block size
2003 * to 64 here.
2004 */
Girish K S069c9f12012-01-06 09:56:39 +05302005 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
2006 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2007 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
2008 SDHCI_BLOCK_SIZE);
2009 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
2010 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2011 SDHCI_BLOCK_SIZE);
2012 } else {
2013 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2014 SDHCI_BLOCK_SIZE);
2015 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302016
2017 /*
2018 * The tuning block is sent by the card to the host controller.
2019 * So we set the TRNS_READ bit in the Transfer Mode register.
2020 * This also takes care of setting DMA Enable and Multi Block
2021 * Select in the same register to 0.
2022 */
2023 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2024
2025 sdhci_send_command(host, &cmd);
2026
2027 host->cmd = NULL;
2028 host->mrq = NULL;
2029
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002030 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302031 /* Wait for Buffer Read Ready interrupt */
2032 wait_event_interruptible_timeout(host->buf_ready_int,
2033 (host->tuning_done == 1),
2034 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002035 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302036
2037 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302038 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05302039 "Buffer Read Ready interrupt during tuning "
2040 "procedure, falling back to fixed sampling "
2041 "clock\n");
2042 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2043 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2044 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2045 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2046
2047 err = -EIO;
2048 goto out;
2049 }
2050
2051 host->tuning_done = 0;
2052
2053 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07002054
2055 /* eMMC spec does not require a delay between tuning cycles */
2056 if (opcode == MMC_SEND_TUNING_BLOCK)
2057 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302058 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2059
2060 /*
2061 * The Host Driver has exhausted the maximum number of loops allowed,
2062 * so use fixed sampling frequency.
2063 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002064 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302065 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2066 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002067 }
2068 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
2069 pr_info(DRIVER_NAME ": Tuning procedure"
2070 " failed, falling back to fixed sampling"
2071 " clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002072 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302073 }
2074
2075out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002076 host->flags &= ~SDHCI_NEEDS_RETUNING;
2077
2078 if (tuning_count) {
Aaron Lu973905f2012-07-04 13:29:09 +08002079 host->flags |= SDHCI_USING_RETUNING_TIMER;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002080 mod_timer(&host->tuning_timer, jiffies + tuning_count * HZ);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302081 }
2082
2083 /*
2084 * In case tuning fails, host controllers which support re-tuning can
2085 * try tuning again at a later time, when the re-tuning timer expires.
2086 * So for these controllers, we return 0. Since there might be other
2087 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08002088 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2089 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302090 */
Aaron Lu973905f2012-07-04 13:29:09 +08002091 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302092 err = 0;
2093
Russell Kingb537f942014-04-25 12:56:01 +01002094 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2095 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002096out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002097 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002098 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302099
2100 return err;
2101}
2102
Kevin Liu52983382013-01-31 11:31:37 +08002103
2104static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302105{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302106 /* Host Controller v3.00 defines preset value registers */
2107 if (host->version < SDHCI_SPEC_300)
2108 return;
2109
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302110 /*
2111 * We only enable or disable Preset Value if they are not already
2112 * enabled or disabled respectively. Otherwise, we bail out.
2113 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002114 if (host->preset_enabled != enable) {
2115 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2116
2117 if (enable)
2118 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2119 else
2120 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2121
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302122 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002123
2124 if (enable)
2125 host->flags |= SDHCI_PV_ENABLED;
2126 else
2127 host->flags &= ~SDHCI_PV_ENABLED;
2128
2129 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302130 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002131}
2132
Haibo Chen348487c2014-12-09 17:04:05 +08002133static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2134 int err)
2135{
2136 struct sdhci_host *host = mmc_priv(mmc);
2137 struct mmc_data *data = mrq->data;
2138
2139 if (host->flags & SDHCI_REQ_USE_DMA) {
2140 if (data->host_cookie)
2141 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2142 data->flags & MMC_DATA_WRITE ?
2143 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2144 mrq->data->host_cookie = 0;
2145 }
2146}
2147
2148static int sdhci_pre_dma_transfer(struct sdhci_host *host,
2149 struct mmc_data *data,
2150 struct sdhci_host_next *next)
2151{
2152 int sg_count;
2153
2154 if (!next && data->host_cookie &&
2155 data->host_cookie != host->next_data.cookie) {
2156 pr_debug(DRIVER_NAME "[%s] invalid cookie: %d, next-cookie %d\n",
2157 __func__, data->host_cookie, host->next_data.cookie);
2158 data->host_cookie = 0;
2159 }
2160
2161 /* Check if next job is already prepared */
2162 if (next ||
2163 (!next && data->host_cookie != host->next_data.cookie)) {
2164 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg,
2165 data->sg_len,
2166 data->flags & MMC_DATA_WRITE ?
2167 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2168
2169 } else {
2170 sg_count = host->next_data.sg_count;
2171 host->next_data.sg_count = 0;
2172 }
2173
2174
2175 if (sg_count == 0)
2176 return -EINVAL;
2177
2178 if (next) {
2179 next->sg_count = sg_count;
2180 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
2181 } else
2182 host->sg_count = sg_count;
2183
2184 return sg_count;
2185}
2186
2187static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2188 bool is_first_req)
2189{
2190 struct sdhci_host *host = mmc_priv(mmc);
2191
2192 if (mrq->data->host_cookie) {
2193 mrq->data->host_cookie = 0;
2194 return;
2195 }
2196
2197 if (host->flags & SDHCI_REQ_USE_DMA)
2198 if (sdhci_pre_dma_transfer(host,
2199 mrq->data,
2200 &host->next_data) < 0)
2201 mrq->data->host_cookie = 0;
2202}
2203
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002204static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002205{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002206 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002207 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002208 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002209
Christian Daudt722e1282013-06-20 14:26:36 -07002210 /* First check if client has provided their own card event */
2211 if (host->ops->card_event)
2212 host->ops->card_event(host);
2213
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002214 present = sdhci_do_get_cd(host);
2215
Pierre Ossmand129bce2006-03-24 03:18:17 -08002216 spin_lock_irqsave(&host->lock, flags);
2217
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002218 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002219 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302220 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002221 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302222 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002223 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002224
Russell King03231f92014-04-25 12:57:12 +01002225 sdhci_do_reset(host, SDHCI_RESET_CMD);
2226 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002227
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002228 host->mrq->cmd->error = -ENOMEDIUM;
2229 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002230 }
2231
2232 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002233}
2234
2235static const struct mmc_host_ops sdhci_ops = {
2236 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002237 .post_req = sdhci_post_req,
2238 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002239 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002240 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002241 .get_ro = sdhci_get_ro,
2242 .hw_reset = sdhci_hw_reset,
2243 .enable_sdio_irq = sdhci_enable_sdio_irq,
2244 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002245 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002246 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002247 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002248 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002249};
2250
2251/*****************************************************************************\
2252 * *
2253 * Tasklets *
2254 * *
2255\*****************************************************************************/
2256
Pierre Ossmand129bce2006-03-24 03:18:17 -08002257static void sdhci_tasklet_finish(unsigned long param)
2258{
2259 struct sdhci_host *host;
2260 unsigned long flags;
2261 struct mmc_request *mrq;
2262
2263 host = (struct sdhci_host*)param;
2264
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002265 spin_lock_irqsave(&host->lock, flags);
2266
Chris Ball0c9c99a2011-04-27 17:35:31 -04002267 /*
2268 * If this tasklet gets rescheduled while running, it will
2269 * be run again afterwards but without any active request.
2270 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002271 if (!host->mrq) {
2272 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002273 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002274 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002275
2276 del_timer(&host->timer);
2277
2278 mrq = host->mrq;
2279
Pierre Ossmand129bce2006-03-24 03:18:17 -08002280 /*
2281 * The controller needs a reset of internal state machines
2282 * upon error conditions.
2283 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002284 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002285 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002286 (mrq->sbc && mrq->sbc->error) ||
2287 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2288 (mrq->data->stop && mrq->data->stop->error))) ||
2289 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002290
2291 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002292 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002293 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002294 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002295
2296 /* Spec says we should do both at the same time, but Ricoh
2297 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002298 sdhci_do_reset(host, SDHCI_RESET_CMD);
2299 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002300 }
2301
2302 host->mrq = NULL;
2303 host->cmd = NULL;
2304 host->data = NULL;
2305
Pierre Ossmanf9134312008-12-21 17:01:48 +01002306#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002307 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002308#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002309
Pierre Ossman5f25a662006-10-04 02:15:39 -07002310 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002311 spin_unlock_irqrestore(&host->lock, flags);
2312
2313 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002314 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002315}
2316
2317static void sdhci_timeout_timer(unsigned long data)
2318{
2319 struct sdhci_host *host;
2320 unsigned long flags;
2321
2322 host = (struct sdhci_host*)data;
2323
2324 spin_lock_irqsave(&host->lock, flags);
2325
2326 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302327 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002328 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002329 sdhci_dumpregs(host);
2330
2331 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002332 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002333 sdhci_finish_data(host);
2334 } else {
2335 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002336 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002337 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002338 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002339
2340 tasklet_schedule(&host->finish_tasklet);
2341 }
2342 }
2343
Pierre Ossman5f25a662006-10-04 02:15:39 -07002344 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002345 spin_unlock_irqrestore(&host->lock, flags);
2346}
2347
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302348static void sdhci_tuning_timer(unsigned long data)
2349{
2350 struct sdhci_host *host;
2351 unsigned long flags;
2352
2353 host = (struct sdhci_host *)data;
2354
2355 spin_lock_irqsave(&host->lock, flags);
2356
2357 host->flags |= SDHCI_NEEDS_RETUNING;
2358
2359 spin_unlock_irqrestore(&host->lock, flags);
2360}
2361
Pierre Ossmand129bce2006-03-24 03:18:17 -08002362/*****************************************************************************\
2363 * *
2364 * Interrupt handling *
2365 * *
2366\*****************************************************************************/
2367
Adrian Hunter61541392014-09-24 10:27:27 +03002368static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002369{
2370 BUG_ON(intmask == 0);
2371
2372 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302373 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002374 "though no command operation was in progress.\n",
2375 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002376 sdhci_dumpregs(host);
2377 return;
2378 }
2379
Pierre Ossman43b58b32007-07-25 23:15:27 +02002380 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002381 host->cmd->error = -ETIMEDOUT;
2382 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2383 SDHCI_INT_INDEX))
2384 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002385
Pierre Ossmane8095172008-07-25 01:09:08 +02002386 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002387 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002388 return;
2389 }
2390
2391 /*
2392 * The host can send and interrupt when the busy state has
2393 * ended, allowing us to wait without wasting CPU cycles.
2394 * Unfortunately this is overloaded on the "data complete"
2395 * interrupt, so we need to take some care when handling
2396 * it.
2397 *
2398 * Note: The 1.0 specification is a bit ambiguous about this
2399 * feature so there might be some problems with older
2400 * controllers.
2401 */
2402 if (host->cmd->flags & MMC_RSP_BUSY) {
2403 if (host->cmd->data)
2404 DBG("Cannot wait for busy signal when also "
2405 "doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002406 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2407 && !host->busy_handle) {
2408 /* Mark that command complete before busy is ended */
2409 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002410 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002411 }
Ben Dooksf9454052009-02-20 20:33:08 +03002412
2413 /* The controller does not support the end-of-busy IRQ,
2414 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002415 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2416 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2417 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002418 }
2419
2420 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002421 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002422}
2423
George G. Davis0957c332010-02-18 12:32:12 -05002424#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002425static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002426{
2427 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002428 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002429
2430 sdhci_dumpregs(host);
2431
2432 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002433 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002434
Adrian Huntere57a5f62014-11-04 12:42:46 +02002435 if (host->flags & SDHCI_USE_64_BIT_DMA)
2436 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2437 name, desc, le32_to_cpu(dma_desc->addr_hi),
2438 le32_to_cpu(dma_desc->addr_lo),
2439 le16_to_cpu(dma_desc->len),
2440 le16_to_cpu(dma_desc->cmd));
2441 else
2442 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2443 name, desc, le32_to_cpu(dma_desc->addr_lo),
2444 le16_to_cpu(dma_desc->len),
2445 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002446
Adrian Hunter76fe3792014-11-04 12:42:42 +02002447 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002448
Adrian Hunter05452302014-11-04 12:42:45 +02002449 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002450 break;
2451 }
2452}
2453#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002454static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002455#endif
2456
Pierre Ossmand129bce2006-03-24 03:18:17 -08002457static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2458{
Girish K S069c9f12012-01-06 09:56:39 +05302459 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002460 BUG_ON(intmask == 0);
2461
Arindam Nathb513ea22011-05-05 12:19:04 +05302462 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2463 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302464 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2465 if (command == MMC_SEND_TUNING_BLOCK ||
2466 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302467 host->tuning_done = 1;
2468 wake_up(&host->buf_ready_int);
2469 return;
2470 }
2471 }
2472
Pierre Ossmand129bce2006-03-24 03:18:17 -08002473 if (!host->data) {
2474 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002475 * The "data complete" interrupt is also used to
2476 * indicate that a busy state has ended. See comment
2477 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002478 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002479 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002480 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2481 host->cmd->error = -ETIMEDOUT;
2482 tasklet_schedule(&host->finish_tasklet);
2483 return;
2484 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002485 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002486 /*
2487 * Some cards handle busy-end interrupt
2488 * before the command completed, so make
2489 * sure we do things in the proper order.
2490 */
2491 if (host->busy_handle)
2492 sdhci_finish_command(host);
2493 else
2494 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002495 return;
2496 }
2497 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002498
Girish K Sa3c76eb2011-10-11 11:44:09 +05302499 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002500 "though no data operation was in progress.\n",
2501 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002502 sdhci_dumpregs(host);
2503
2504 return;
2505 }
2506
2507 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002508 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002509 else if (intmask & SDHCI_INT_DATA_END_BIT)
2510 host->data->error = -EILSEQ;
2511 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2512 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2513 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002514 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002515 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302516 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002517 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002518 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002519 if (host->ops->adma_workaround)
2520 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002521 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002522
Pierre Ossman17b04292007-07-22 22:18:46 +02002523 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002524 sdhci_finish_data(host);
2525 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002526 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002527 sdhci_transfer_pio(host);
2528
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002529 /*
2530 * We currently don't do anything fancy with DMA
2531 * boundaries, but as we can't disable the feature
2532 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002533 *
2534 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2535 * should return a valid address to continue from, but as
2536 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002537 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002538 if (intmask & SDHCI_INT_DMA_END) {
2539 u32 dmastart, dmanow;
2540 dmastart = sg_dma_address(host->data->sg);
2541 dmanow = dmastart + host->data->bytes_xfered;
2542 /*
2543 * Force update to the next DMA block boundary.
2544 */
2545 dmanow = (dmanow &
2546 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2547 SDHCI_DEFAULT_BOUNDARY_SIZE;
2548 host->data->bytes_xfered = dmanow - dmastart;
2549 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2550 " next 0x%08x\n",
2551 mmc_hostname(host->mmc), dmastart,
2552 host->data->bytes_xfered, dmanow);
2553 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2554 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002555
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002556 if (intmask & SDHCI_INT_DATA_END) {
2557 if (host->cmd) {
2558 /*
2559 * Data managed to finish before the
2560 * command completed. Make sure we do
2561 * things in the proper order.
2562 */
2563 host->data_early = 1;
2564 } else {
2565 sdhci_finish_data(host);
2566 }
2567 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002568 }
2569}
2570
David Howells7d12e782006-10-05 14:55:46 +01002571static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002572{
Russell King781e9892014-04-25 12:55:46 +01002573 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002574 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002575 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002576 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002577
2578 spin_lock(&host->lock);
2579
Russell Kingbe138552014-04-25 12:55:56 +01002580 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002581 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002582 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002583 }
2584
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002585 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002586 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002587 result = IRQ_NONE;
2588 goto out;
2589 }
2590
Russell King41005002014-04-25 12:55:36 +01002591 do {
2592 /* Clear selected interrupts. */
2593 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2594 SDHCI_INT_BUS_POWER);
2595 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002596
Russell King41005002014-04-25 12:55:36 +01002597 DBG("*** %s got interrupt: 0x%08x\n",
2598 mmc_hostname(host->mmc), intmask);
2599
2600 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2601 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2602 SDHCI_CARD_PRESENT;
2603
2604 /*
2605 * There is a observation on i.mx esdhc. INSERT
2606 * bit will be immediately set again when it gets
2607 * cleared, if a card is inserted. We have to mask
2608 * the irq to prevent interrupt storm which will
2609 * freeze the system. And the REMOVE gets the
2610 * same situation.
2611 *
2612 * More testing are needed here to ensure it works
2613 * for other platforms though.
2614 */
Russell Kingb537f942014-04-25 12:56:01 +01002615 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2616 SDHCI_INT_CARD_REMOVE);
2617 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2618 SDHCI_INT_CARD_INSERT;
2619 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2620 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002621
2622 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2623 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002624
2625 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2626 SDHCI_INT_CARD_REMOVE);
2627 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002628 }
2629
2630 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002631 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2632 &intmask);
Russell King41005002014-04-25 12:55:36 +01002633
2634 if (intmask & SDHCI_INT_DATA_MASK)
2635 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2636
2637 if (intmask & SDHCI_INT_BUS_POWER)
2638 pr_err("%s: Card is consuming too much power!\n",
2639 mmc_hostname(host->mmc));
2640
Russell King781e9892014-04-25 12:55:46 +01002641 if (intmask & SDHCI_INT_CARD_INT) {
2642 sdhci_enable_sdio_irq_nolock(host, false);
2643 host->thread_isr |= SDHCI_INT_CARD_INT;
2644 result = IRQ_WAKE_THREAD;
2645 }
Russell King41005002014-04-25 12:55:36 +01002646
2647 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2648 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2649 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2650 SDHCI_INT_CARD_INT);
2651
2652 if (intmask) {
2653 unexpected |= intmask;
2654 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2655 }
2656
Russell King781e9892014-04-25 12:55:46 +01002657 if (result == IRQ_NONE)
2658 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002659
2660 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002661 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002662out:
2663 spin_unlock(&host->lock);
2664
Alexander Stein6379b232012-03-14 09:52:10 +01002665 if (unexpected) {
2666 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2667 mmc_hostname(host->mmc), unexpected);
2668 sdhci_dumpregs(host);
2669 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002670
Pierre Ossmand129bce2006-03-24 03:18:17 -08002671 return result;
2672}
2673
Russell King781e9892014-04-25 12:55:46 +01002674static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2675{
2676 struct sdhci_host *host = dev_id;
2677 unsigned long flags;
2678 u32 isr;
2679
2680 spin_lock_irqsave(&host->lock, flags);
2681 isr = host->thread_isr;
2682 host->thread_isr = 0;
2683 spin_unlock_irqrestore(&host->lock, flags);
2684
Russell King3560db82014-04-25 12:55:51 +01002685 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2686 sdhci_card_event(host->mmc);
2687 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2688 }
2689
Russell King781e9892014-04-25 12:55:46 +01002690 if (isr & SDHCI_INT_CARD_INT) {
2691 sdio_run_irqs(host->mmc);
2692
2693 spin_lock_irqsave(&host->lock, flags);
2694 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2695 sdhci_enable_sdio_irq_nolock(host, true);
2696 spin_unlock_irqrestore(&host->lock, flags);
2697 }
2698
2699 return isr ? IRQ_HANDLED : IRQ_NONE;
2700}
2701
Pierre Ossmand129bce2006-03-24 03:18:17 -08002702/*****************************************************************************\
2703 * *
2704 * Suspend/resume *
2705 * *
2706\*****************************************************************************/
2707
2708#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002709void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2710{
2711 u8 val;
2712 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2713 | SDHCI_WAKE_ON_INT;
2714
2715 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2716 val |= mask ;
2717 /* Avoid fake wake up */
2718 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2719 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2720 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2721}
2722EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2723
Fabio Estevam0b10f472014-08-30 14:53:13 -03002724static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002725{
2726 u8 val;
2727 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2728 | SDHCI_WAKE_ON_INT;
2729
2730 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2731 val &= ~mask;
2732 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2733}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002734
Manuel Lauss29495aa2011-11-03 11:09:45 +01002735int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002736{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002737 sdhci_disable_card_detection(host);
2738
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302739 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002740 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002741 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302742 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302743 }
2744
Kevin Liuad080d72013-01-05 17:21:33 +08002745 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002746 host->ier = 0;
2747 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2748 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002749 free_irq(host->irq, host);
2750 } else {
2751 sdhci_enable_irq_wakeups(host);
2752 enable_irq_wake(host->irq);
2753 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002754 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002755}
2756
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002757EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002758
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002759int sdhci_resume_host(struct sdhci_host *host)
2760{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002761 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002762
Richard Röjforsa13abc72009-09-22 16:45:30 -07002763 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002764 if (host->ops->enable_dma)
2765 host->ops->enable_dma(host);
2766 }
2767
Kevin Liuad080d72013-01-05 17:21:33 +08002768 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell King781e9892014-04-25 12:55:46 +01002769 ret = request_threaded_irq(host->irq, sdhci_irq,
2770 sdhci_thread_irq, IRQF_SHARED,
2771 mmc_hostname(host->mmc), host);
Kevin Liuad080d72013-01-05 17:21:33 +08002772 if (ret)
2773 return ret;
2774 } else {
2775 sdhci_disable_irq_wakeups(host);
2776 disable_irq_wake(host->irq);
2777 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002778
Adrian Hunter6308d292012-02-07 14:48:54 +02002779 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2780 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2781 /* Card keeps power but host controller does not */
2782 sdhci_init(host, 0);
2783 host->pwr = 0;
2784 host->clock = 0;
2785 sdhci_do_set_ios(host, &host->mmc->ios);
2786 } else {
2787 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2788 mmiowb();
2789 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002790
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002791 sdhci_enable_card_detection(host);
2792
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302793 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002794 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302795 host->flags |= SDHCI_NEEDS_RETUNING;
2796
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002797 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002798}
2799
2800EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002801
2802static int sdhci_runtime_pm_get(struct sdhci_host *host)
2803{
2804 return pm_runtime_get_sync(host->mmc->parent);
2805}
2806
2807static int sdhci_runtime_pm_put(struct sdhci_host *host)
2808{
2809 pm_runtime_mark_last_busy(host->mmc->parent);
2810 return pm_runtime_put_autosuspend(host->mmc->parent);
2811}
2812
Adrian Hunterf0710a52013-05-06 12:17:32 +03002813static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2814{
2815 if (host->runtime_suspended || host->bus_on)
2816 return;
2817 host->bus_on = true;
2818 pm_runtime_get_noresume(host->mmc->parent);
2819}
2820
2821static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2822{
2823 if (host->runtime_suspended || !host->bus_on)
2824 return;
2825 host->bus_on = false;
2826 pm_runtime_put_noidle(host->mmc->parent);
2827}
2828
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002829int sdhci_runtime_suspend_host(struct sdhci_host *host)
2830{
2831 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002832
2833 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002834 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002835 del_timer_sync(&host->tuning_timer);
2836 host->flags &= ~SDHCI_NEEDS_RETUNING;
2837 }
2838
2839 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002840 host->ier &= SDHCI_INT_CARD_INT;
2841 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2842 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002843 spin_unlock_irqrestore(&host->lock, flags);
2844
Russell King781e9892014-04-25 12:55:46 +01002845 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002846
2847 spin_lock_irqsave(&host->lock, flags);
2848 host->runtime_suspended = true;
2849 spin_unlock_irqrestore(&host->lock, flags);
2850
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002851 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002852}
2853EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2854
2855int sdhci_runtime_resume_host(struct sdhci_host *host)
2856{
2857 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002858 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002859
2860 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2861 if (host->ops->enable_dma)
2862 host->ops->enable_dma(host);
2863 }
2864
2865 sdhci_init(host, 0);
2866
2867 /* Force clock and power re-program */
2868 host->pwr = 0;
2869 host->clock = 0;
2870 sdhci_do_set_ios(host, &host->mmc->ios);
2871
2872 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002873 if ((host_flags & SDHCI_PV_ENABLED) &&
2874 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2875 spin_lock_irqsave(&host->lock, flags);
2876 sdhci_enable_preset_value(host, true);
2877 spin_unlock_irqrestore(&host->lock, flags);
2878 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002879
2880 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002881 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002882 host->flags |= SDHCI_NEEDS_RETUNING;
2883
2884 spin_lock_irqsave(&host->lock, flags);
2885
2886 host->runtime_suspended = false;
2887
2888 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002889 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002890 sdhci_enable_sdio_irq_nolock(host, true);
2891
2892 /* Enable Card Detection */
2893 sdhci_enable_card_detection(host);
2894
2895 spin_unlock_irqrestore(&host->lock, flags);
2896
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002897 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002898}
2899EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2900
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002901#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002902
Pierre Ossmand129bce2006-03-24 03:18:17 -08002903/*****************************************************************************\
2904 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002905 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002906 * *
2907\*****************************************************************************/
2908
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002909struct sdhci_host *sdhci_alloc_host(struct device *dev,
2910 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002911{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002912 struct mmc_host *mmc;
2913 struct sdhci_host *host;
2914
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002915 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002916
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002917 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002918 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002919 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002920
2921 host = mmc_priv(mmc);
2922 host->mmc = mmc;
2923
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002924 return host;
2925}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002926
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002927EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002928
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002929int sdhci_add_host(struct sdhci_host *host)
2930{
2931 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002932 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302933 u32 max_current_caps;
2934 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002935 unsigned int override_timeout_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002936 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002937
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002938 WARN_ON(host == NULL);
2939 if (host == NULL)
2940 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002941
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002942 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002943
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002944 if (debug_quirks)
2945 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002946 if (debug_quirks2)
2947 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002948
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002949 override_timeout_clk = host->timeout_clk;
2950
Russell King03231f92014-04-25 12:57:12 +01002951 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002952
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002953 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002954 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2955 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002956 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302957 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002958 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002959 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002960 }
2961
Arindam Nathf2119df2011-05-05 12:18:57 +05302962 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002963 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002964
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002965 if (host->version >= SDHCI_SPEC_300)
2966 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2967 host->caps1 :
2968 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302969
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002970 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002971 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302972 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002973 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002974 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002975 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002976
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002977 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002978 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002979 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002980 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002981 }
2982
Arindam Nathf2119df2011-05-05 12:18:57 +05302983 if ((host->version >= SDHCI_SPEC_200) &&
2984 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002985 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002986
2987 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2988 (host->flags & SDHCI_USE_ADMA)) {
2989 DBG("Disabling ADMA as it is marked broken\n");
2990 host->flags &= ~SDHCI_USE_ADMA;
2991 }
2992
Adrian Huntere57a5f62014-11-04 12:42:46 +02002993 /*
2994 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2995 * and *must* do 64-bit DMA. A driver has the opportunity to change
2996 * that during the first call to ->enable_dma(). Similarly
2997 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2998 * implement.
2999 */
3000 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT)
3001 host->flags |= SDHCI_USE_64_BIT_DMA;
3002
Richard Röjforsa13abc72009-09-22 16:45:30 -07003003 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003004 if (host->ops->enable_dma) {
3005 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07003006 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003007 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07003008 host->flags &=
3009 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003010 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003011 }
3012 }
3013
Adrian Huntere57a5f62014-11-04 12:42:46 +02003014 /* SDMA does not support 64-bit DMA */
3015 if (host->flags & SDHCI_USE_64_BIT_DMA)
3016 host->flags &= ~SDHCI_USE_SDMA;
3017
Pierre Ossman2134a922008-06-28 18:28:51 +02003018 if (host->flags & SDHCI_USE_ADMA) {
3019 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02003020 * The DMA descriptor table size is calculated as the maximum
3021 * number of segments times 2, to allow for an alignment
3022 * descriptor for each segment, plus 1 for a nop end descriptor,
3023 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02003024 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02003025 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3026 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3027 SDHCI_ADMA2_64_DESC_SZ;
3028 host->align_buffer_sz = SDHCI_MAX_SEGS *
3029 SDHCI_ADMA2_64_ALIGN;
3030 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
3031 host->align_sz = SDHCI_ADMA2_64_ALIGN;
3032 host->align_mask = SDHCI_ADMA2_64_ALIGN - 1;
3033 } else {
3034 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3035 SDHCI_ADMA2_32_DESC_SZ;
3036 host->align_buffer_sz = SDHCI_MAX_SEGS *
3037 SDHCI_ADMA2_32_ALIGN;
3038 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
3039 host->align_sz = SDHCI_ADMA2_32_ALIGN;
3040 host->align_mask = SDHCI_ADMA2_32_ALIGN - 1;
3041 }
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003042 host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +02003043 host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003044 &host->adma_addr,
3045 GFP_KERNEL);
Adrian Hunter76fe3792014-11-04 12:42:42 +02003046 host->align_buffer = kmalloc(host->align_buffer_sz, GFP_KERNEL);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003047 if (!host->adma_table || !host->align_buffer) {
Adrian Hunter76fe3792014-11-04 12:42:42 +02003048 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003049 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003050 kfree(host->align_buffer);
Joe Perches66061102014-09-12 14:56:56 -07003051 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003052 mmc_hostname(mmc));
3053 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003054 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01003055 host->align_buffer = NULL;
Adrian Hunter76fe3792014-11-04 12:42:42 +02003056 } else if (host->adma_addr & host->align_mask) {
Joe Perches66061102014-09-12 14:56:56 -07003057 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3058 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003059 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter76fe3792014-11-04 12:42:42 +02003060 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003061 host->adma_table, host->adma_addr);
Russell Kingd1e49f72014-04-25 12:58:34 +01003062 kfree(host->align_buffer);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003063 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01003064 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003065 }
3066 }
3067
Pierre Ossman76591502008-07-21 00:32:11 +02003068 /*
3069 * If we use DMA, then it's up to the caller to set the DMA
3070 * mask, but PIO does not need the hw shim so we set a new
3071 * mask here in that case.
3072 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003073 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003074 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003075 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003076 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003077
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003078 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05303079 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003080 >> SDHCI_CLOCK_BASE_SHIFT;
3081 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303082 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003083 >> SDHCI_CLOCK_BASE_SHIFT;
3084
Pierre Ossmand129bce2006-03-24 03:18:17 -08003085 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003086 if (host->max_clk == 0 || host->quirks &
3087 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003088 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303089 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03003090 "frequency.\n", mmc_hostname(mmc));
3091 return -ENODEV;
3092 }
3093 host->max_clk = host->ops->get_max_clock(host);
3094 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003095
Haibo Chen348487c2014-12-09 17:04:05 +08003096 host->next_data.cookie = 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003097 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303098 * In case of Host Controller v3.00, find out whether clock
3099 * multiplier is supported.
3100 */
3101 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3102 SDHCI_CLOCK_MUL_SHIFT;
3103
3104 /*
3105 * In case the value in Clock Multiplier is 0, then programmable
3106 * clock mode is not supported, otherwise the actual clock
3107 * multiplier is one more than the value of Clock Multiplier
3108 * in the Capabilities Register.
3109 */
3110 if (host->clk_mul)
3111 host->clk_mul += 1;
3112
3113 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003114 * Set host parameters.
3115 */
3116 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303117 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003118 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003119 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303120 else if (host->version >= SDHCI_SPEC_300) {
3121 if (host->clk_mul) {
3122 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3123 mmc->f_max = host->max_clk * host->clk_mul;
3124 } else
3125 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3126 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003127 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003128
Aisheng Dong28aab052014-08-27 15:26:31 +08003129 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3130 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3131 SDHCI_TIMEOUT_CLK_SHIFT;
3132 if (host->timeout_clk == 0) {
3133 if (host->ops->get_timeout_clock) {
3134 host->timeout_clk =
3135 host->ops->get_timeout_clock(host);
3136 } else {
3137 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3138 mmc_hostname(mmc));
3139 return -ENODEV;
3140 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003141 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003142
Aisheng Dong28aab052014-08-27 15:26:31 +08003143 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3144 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003145
Aisheng Dong28aab052014-08-27 15:26:31 +08003146 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003147 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003148 mmc->max_busy_timeout /= host->timeout_clk;
3149 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003150
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003151 if (override_timeout_clk)
3152 host->timeout_clk = override_timeout_clk;
3153
Andrei Warkentine89d4562011-05-23 15:06:37 -05003154 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003155 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003156
3157 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3158 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003159
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003160 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003161 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003162 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003163 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003164 host->flags |= SDHCI_AUTO_CMD23;
3165 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3166 } else {
3167 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3168 }
3169
Philip Rakity15ec4462010-11-19 16:48:39 -05003170 /*
3171 * A controller may support 8-bit width, but the board itself
3172 * might not have the pins brought out. Boards that support
3173 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3174 * their platform code before calling sdhci_add_host(), and we
3175 * won't assume 8-bit width for hosts without that CAP.
3176 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003177 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003178 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003179
Jerry Huang63ef5d82012-10-25 13:47:19 +08003180 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3181 mmc->caps &= ~MMC_CAP_CMD23;
3182
Arindam Nathf2119df2011-05-05 12:18:57 +05303183 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003184 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003185
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003186 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Markus Mayer4e743f12014-07-03 13:27:42 -07003187 !(mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003188 mmc->caps |= MMC_CAP_NEEDS_POLL;
3189
Tim Kryger3a48edc2014-06-13 10:13:56 -07003190 /* If there are external regulators, get them */
3191 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3192 return -EPROBE_DEFER;
3193
Philip Rakity6231f3d2012-07-23 15:56:23 -07003194 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003195 if (!IS_ERR(mmc->supply.vqmmc)) {
3196 ret = regulator_enable(mmc->supply.vqmmc);
3197 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3198 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003199 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3200 SDHCI_SUPPORT_SDR50 |
3201 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003202 if (ret) {
3203 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3204 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003205 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003206 }
Kevin Liu8363c372012-11-17 17:55:51 -05003207 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003208
Daniel Drake6a661802012-11-25 13:01:19 -05003209 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3210 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3211 SDHCI_SUPPORT_DDR50);
3212
Al Cooper4188bba2012-03-16 15:54:17 -04003213 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3214 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3215 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303216 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3217
3218 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003219 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303220 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003221 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3222 * field can be promoted to support HS200.
3223 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003224 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003225 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003226 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303227 mmc->caps |= MMC_CAP_UHS_SDR50;
3228
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003229 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3230 (caps[1] & SDHCI_SUPPORT_HS400))
3231 mmc->caps2 |= MMC_CAP2_HS400;
3232
Adrian Hunter549c0b12014-11-06 15:19:05 +02003233 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3234 (IS_ERR(mmc->supply.vqmmc) ||
3235 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3236 1300000)))
3237 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3238
Micky Ching9107ebb2014-02-21 18:40:35 +08003239 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3240 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303241 mmc->caps |= MMC_CAP_UHS_DDR50;
3242
Girish K S069c9f12012-01-06 09:56:39 +05303243 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303244 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3245 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3246
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003247 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303248 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003249 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303250
Arindam Nathd6d50a12011-05-05 12:18:59 +05303251 /* Driver Type(s) (A, C, D) supported by the host */
3252 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3253 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3254 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3255 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3256 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3257 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3258
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303259 /* Initial value for re-tuning timer count */
3260 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3261 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3262
3263 /*
3264 * In case Re-tuning Timer is not disabled, the actual value of
3265 * re-tuning timer will be 2 ^ (n - 1).
3266 */
3267 if (host->tuning_count)
3268 host->tuning_count = 1 << (host->tuning_count - 1);
3269
3270 /* Re-tuning mode supported by the Host Controller */
3271 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3272 SDHCI_RETUNING_MODE_SHIFT;
3273
Takashi Iwai8f230f42010-12-08 10:04:30 +01003274 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003275
Arindam Nathf2119df2011-05-05 12:18:57 +05303276 /*
3277 * According to SD Host Controller spec v3.00, if the Host System
3278 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3279 * the value is meaningful only if Voltage Support in the Capabilities
3280 * register is set. The actual current value is 4 times the register
3281 * value.
3282 */
3283 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003284 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003285 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003286 if (curr > 0) {
3287
3288 /* convert to SDHCI_MAX_CURRENT format */
3289 curr = curr/1000; /* convert to mA */
3290 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3291
3292 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3293 max_current_caps =
3294 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3295 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3296 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3297 }
3298 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303299
3300 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003301 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303302
Aaron Lu55c46652012-07-04 13:31:48 +08003303 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303304 SDHCI_MAX_CURRENT_330_MASK) >>
3305 SDHCI_MAX_CURRENT_330_SHIFT) *
3306 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303307 }
3308 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003309 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303310
Aaron Lu55c46652012-07-04 13:31:48 +08003311 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303312 SDHCI_MAX_CURRENT_300_MASK) >>
3313 SDHCI_MAX_CURRENT_300_SHIFT) *
3314 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303315 }
3316 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003317 ocr_avail |= MMC_VDD_165_195;
3318
Aaron Lu55c46652012-07-04 13:31:48 +08003319 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303320 SDHCI_MAX_CURRENT_180_MASK) >>
3321 SDHCI_MAX_CURRENT_180_SHIFT) *
3322 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303323 }
3324
Tim Kryger52221612014-06-25 00:25:34 -07003325 /* If OCR set by external regulators, use it instead */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003326 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003327 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003328
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003329 if (host->ocr_mask)
Tim Kryger3a48edc2014-06-13 10:13:56 -07003330 ocr_avail &= host->ocr_mask;
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003331
Takashi Iwai8f230f42010-12-08 10:04:30 +01003332 mmc->ocr_avail = ocr_avail;
3333 mmc->ocr_avail_sdio = ocr_avail;
3334 if (host->ocr_avail_sdio)
3335 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3336 mmc->ocr_avail_sd = ocr_avail;
3337 if (host->ocr_avail_sd)
3338 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3339 else /* normal SD controllers don't support 1.8V */
3340 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3341 mmc->ocr_avail_mmc = ocr_avail;
3342 if (host->ocr_avail_mmc)
3343 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003344
3345 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303346 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003347 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003348 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003349 }
3350
Pierre Ossmand129bce2006-03-24 03:18:17 -08003351 spin_lock_init(&host->lock);
3352
3353 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003354 * Maximum number of segments. Depends on if the hardware
3355 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003356 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003357 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003358 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003359 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003360 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003361 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003362 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003363
3364 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003365 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3366 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3367 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003368 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003369 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003370
3371 /*
3372 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003373 * of bytes. When doing hardware scatter/gather, each entry cannot
3374 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003375 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003376 if (host->flags & SDHCI_USE_ADMA) {
3377 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3378 mmc->max_seg_size = 65535;
3379 else
3380 mmc->max_seg_size = 65536;
3381 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003382 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003383 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003384
3385 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003386 * Maximum block size. This varies from controller to controller and
3387 * is specified in the capabilities register.
3388 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003389 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3390 mmc->max_blk_size = 2;
3391 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303392 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003393 SDHCI_MAX_BLOCK_SHIFT;
3394 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003395 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3396 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003397 mmc->max_blk_size = 0;
3398 }
3399 }
3400
3401 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003402
3403 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003404 * Maximum block count.
3405 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003406 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003407
3408 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003409 * Init tasklets.
3410 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003411 tasklet_init(&host->finish_tasklet,
3412 sdhci_tasklet_finish, (unsigned long)host);
3413
Al Viroe4cad1b2006-10-10 22:47:07 +01003414 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003415
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003416 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303417
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003418 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303419 /* Initialize re-tuning timer */
3420 init_timer(&host->tuning_timer);
3421 host->tuning_timer.data = (unsigned long)host;
3422 host->tuning_timer.function = sdhci_tuning_timer;
3423 }
3424
Shawn Guo2af502c2013-07-05 14:38:55 +08003425 sdhci_init(host, 0);
3426
Russell King781e9892014-04-25 12:55:46 +01003427 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3428 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003429 if (ret) {
3430 pr_err("%s: Failed to request IRQ %d: %d\n",
3431 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003432 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003433 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003434
Pierre Ossmand129bce2006-03-24 03:18:17 -08003435#ifdef CONFIG_MMC_DEBUG
3436 sdhci_dumpregs(host);
3437#endif
3438
Pierre Ossmanf9134312008-12-21 17:01:48 +01003439#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003440 snprintf(host->led_name, sizeof(host->led_name),
3441 "%s::", mmc_hostname(mmc));
3442 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003443 host->led.brightness = LED_OFF;
3444 host->led.default_trigger = mmc_hostname(mmc);
3445 host->led.brightness_set = sdhci_led_control;
3446
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003447 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003448 if (ret) {
3449 pr_err("%s: Failed to register LED device: %d\n",
3450 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003451 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003452 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003453#endif
3454
Pierre Ossman5f25a662006-10-04 02:15:39 -07003455 mmiowb();
3456
Pierre Ossmand129bce2006-03-24 03:18:17 -08003457 mmc_add_host(mmc);
3458
Girish K Sa3c76eb2011-10-11 11:44:09 +05303459 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003460 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003461 (host->flags & SDHCI_USE_ADMA) ?
3462 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003463 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003464
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003465 sdhci_enable_card_detection(host);
3466
Pierre Ossmand129bce2006-03-24 03:18:17 -08003467 return 0;
3468
Pierre Ossmanf9134312008-12-21 17:01:48 +01003469#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003470reset:
Russell King03231f92014-04-25 12:57:12 +01003471 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003472 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3473 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003474 free_irq(host->irq, host);
3475#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003476untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003477 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003478
3479 return ret;
3480}
3481
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003482EXPORT_SYMBOL_GPL(sdhci_add_host);
3483
Pierre Ossman1e728592008-04-16 19:13:13 +02003484void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003485{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003486 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003487 unsigned long flags;
3488
3489 if (dead) {
3490 spin_lock_irqsave(&host->lock, flags);
3491
3492 host->flags |= SDHCI_DEVICE_DEAD;
3493
3494 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303495 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003496 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003497
3498 host->mrq->cmd->error = -ENOMEDIUM;
3499 tasklet_schedule(&host->finish_tasklet);
3500 }
3501
3502 spin_unlock_irqrestore(&host->lock, flags);
3503 }
3504
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003505 sdhci_disable_card_detection(host);
3506
Markus Mayer4e743f12014-07-03 13:27:42 -07003507 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003508
Pierre Ossmanf9134312008-12-21 17:01:48 +01003509#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003510 led_classdev_unregister(&host->led);
3511#endif
3512
Pierre Ossman1e728592008-04-16 19:13:13 +02003513 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003514 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003515
Russell Kingb537f942014-04-25 12:56:01 +01003516 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3517 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003518 free_irq(host->irq, host);
3519
3520 del_timer_sync(&host->timer);
3521
Pierre Ossmand129bce2006-03-24 03:18:17 -08003522 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003523
Tim Kryger3a48edc2014-06-13 10:13:56 -07003524 if (!IS_ERR(mmc->supply.vqmmc))
3525 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003526
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003527 if (host->adma_table)
Adrian Hunter76fe3792014-11-04 12:42:42 +02003528 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003529 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003530 kfree(host->align_buffer);
3531
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003532 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003533 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003534}
3535
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003536EXPORT_SYMBOL_GPL(sdhci_remove_host);
3537
3538void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003539{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003540 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003541}
3542
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003543EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003544
3545/*****************************************************************************\
3546 * *
3547 * Driver init/exit *
3548 * *
3549\*****************************************************************************/
3550
3551static int __init sdhci_drv_init(void)
3552{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303553 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003554 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303555 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003556
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003557 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003558}
3559
3560static void __exit sdhci_drv_exit(void)
3561{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003562}
3563
3564module_init(sdhci_drv_init);
3565module_exit(sdhci_drv_exit);
3566
Pierre Ossmandf673b22006-06-30 02:22:31 -07003567module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003568module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003569
Pierre Ossman32710e82009-04-08 20:14:54 +02003570MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003571MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003572MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003573
Pierre Ossmandf673b22006-06-30 02:22:31 -07003574MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003575MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");