blob: 082c2f9fbfe679ae98419c5610e2a5a065031c50 [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Aaron Lu473b0952012-07-03 17:27:49 +080030#include <linux/mmc/card.h>
Corneliu Doban85cc1c32015-02-09 16:06:29 -080031#include <linux/mmc/sdio.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080032#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080033
Pierre Ossmand129bce2006-03-24 03:18:17 -080034#include "sdhci.h"
35
36#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmand129bce2006-03-24 03:18:17 -080038#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010039 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080040
Pierre Ossmanf9134312008-12-21 17:01:48 +010041#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
42 defined(CONFIG_MMC_SDHCI_MODULE))
43#define SDHCI_USE_LEDS_CLASS
44#endif
45
Arindam Nathb513ea22011-05-05 12:19:04 +053046#define MAX_TUNING_LOOP 40
47
Pierre Ossmandf673b22006-06-30 02:22:31 -070048static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030049static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070050
Pierre Ossmand129bce2006-03-24 03:18:17 -080051static void sdhci_finish_data(struct sdhci_host *);
52
Pierre Ossmand129bce2006-03-24 03:18:17 -080053static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053054static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Kevin Liu52983382013-01-31 11:31:37 +080055static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Scott Branden04e079cf2015-03-10 11:35:10 -070056static int sdhci_do_get_cd(struct sdhci_host *host);
Pierre Ossmand129bce2006-03-24 03:18:17 -080057
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +010058#ifdef CONFIG_PM
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030059static int sdhci_runtime_pm_get(struct sdhci_host *host);
60static int sdhci_runtime_pm_put(struct sdhci_host *host);
Adrian Hunterf0710a52013-05-06 12:17:32 +030061static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
62static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030063#else
64static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
65{
66 return 0;
67}
68static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
69{
70 return 0;
71}
Adrian Hunterf0710a52013-05-06 12:17:32 +030072static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
73{
74}
75static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
76{
77}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030078#endif
79
Pierre Ossmand129bce2006-03-24 03:18:17 -080080static void sdhci_dumpregs(struct sdhci_host *host)
81{
Girish K Sa3c76eb2011-10-11 11:44:09 +053082 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070083 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080084
Girish K Sa3c76eb2011-10-11 11:44:09 +053085 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030086 sdhci_readl(host, SDHCI_DMA_ADDRESS),
87 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053088 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030089 sdhci_readw(host, SDHCI_BLOCK_SIZE),
90 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053091 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030092 sdhci_readl(host, SDHCI_ARGUMENT),
93 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053094 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030095 sdhci_readl(host, SDHCI_PRESENT_STATE),
96 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053097 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030098 sdhci_readb(host, SDHCI_POWER_CONTROL),
99 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530100 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300101 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
102 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530103 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300104 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
105 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530106 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300107 sdhci_readl(host, SDHCI_INT_ENABLE),
108 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530109 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300110 sdhci_readw(host, SDHCI_ACMD12_ERR),
111 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530112 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300113 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500114 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530115 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500116 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300117 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530118 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530119 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800120
Adrian Huntere57a5f62014-11-04 12:42:46 +0200121 if (host->flags & SDHCI_USE_ADMA) {
122 if (host->flags & SDHCI_USE_64_BIT_DMA)
123 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
124 readl(host->ioaddr + SDHCI_ADMA_ERROR),
125 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
126 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
127 else
128 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
129 readl(host->ioaddr + SDHCI_ADMA_ERROR),
130 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
131 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100132
Girish K Sa3c76eb2011-10-11 11:44:09 +0530133 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800134}
135
136/*****************************************************************************\
137 * *
138 * Low level functions *
139 * *
140\*****************************************************************************/
141
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300142static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
143{
Russell King5b4f1f62014-04-25 12:57:02 +0100144 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300145
Adrian Hunterc79396c2011-12-27 15:48:42 +0200146 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100147 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300148 return;
149
Russell King5b4f1f62014-04-25 12:57:02 +0100150 if (enable) {
151 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
152 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800153
Russell King5b4f1f62014-04-25 12:57:02 +0100154 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
155 SDHCI_INT_CARD_INSERT;
156 } else {
157 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
158 }
Russell Kingb537f942014-04-25 12:56:01 +0100159
160 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
161 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300162}
163
164static void sdhci_enable_card_detection(struct sdhci_host *host)
165{
166 sdhci_set_card_detection(host, true);
167}
168
169static void sdhci_disable_card_detection(struct sdhci_host *host)
170{
171 sdhci_set_card_detection(host, false);
172}
173
Russell King03231f92014-04-25 12:57:12 +0100174void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800175{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700176 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800177
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300178 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800179
Adrian Hunterf0710a52013-05-06 12:17:32 +0300180 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800181 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300182 /* Reset-all turns off SD Bus Power */
183 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
184 sdhci_runtime_pm_bus_off(host);
185 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800186
Pierre Ossmane16514d82006-06-30 02:22:24 -0700187 /* Wait max 100 ms */
188 timeout = 100;
189
190 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300191 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700192 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530193 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700194 mmc_hostname(host->mmc), (int)mask);
195 sdhci_dumpregs(host);
196 return;
197 }
198 timeout--;
199 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800200 }
Russell King03231f92014-04-25 12:57:12 +0100201}
202EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300203
Russell King03231f92014-04-25 12:57:12 +0100204static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
205{
206 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Ivan T. Ivanov135b0a22015-07-06 15:16:21 +0300207 if (!sdhci_do_get_cd(host))
Russell King03231f92014-04-25 12:57:12 +0100208 return;
209 }
210
211 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800212
Russell Kingda91a8f2014-04-25 13:00:12 +0100213 if (mask & SDHCI_RESET_ALL) {
214 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
215 if (host->ops->enable_dma)
216 host->ops->enable_dma(host);
217 }
218
219 /* Resetting the controller clears many */
220 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800221 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222}
223
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800224static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
225
226static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800227{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800228 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100229 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800230 else
Russell King03231f92014-04-25 12:57:12 +0100231 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800232
Russell Kingb537f942014-04-25 12:56:01 +0100233 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
234 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
235 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
236 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
237 SDHCI_INT_RESPONSE;
238
239 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
240 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800241
242 if (soft) {
243 /* force clock reconfiguration */
244 host->clock = 0;
245 sdhci_set_ios(host->mmc, &host->mmc->ios);
246 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300247}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800248
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300249static void sdhci_reinit(struct sdhci_host *host)
250{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800251 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300252 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800253}
254
255static void sdhci_activate_led(struct sdhci_host *host)
256{
257 u8 ctrl;
258
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300259 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800260 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300261 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800262}
263
264static void sdhci_deactivate_led(struct sdhci_host *host)
265{
266 u8 ctrl;
267
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300268 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800269 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300270 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800271}
272
Pierre Ossmanf9134312008-12-21 17:01:48 +0100273#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100274static void sdhci_led_control(struct led_classdev *led,
275 enum led_brightness brightness)
276{
277 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
278 unsigned long flags;
279
280 spin_lock_irqsave(&host->lock, flags);
281
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300282 if (host->runtime_suspended)
283 goto out;
284
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100285 if (brightness == LED_OFF)
286 sdhci_deactivate_led(host);
287 else
288 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300289out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100290 spin_unlock_irqrestore(&host->lock, flags);
291}
292#endif
293
Pierre Ossmand129bce2006-03-24 03:18:17 -0800294/*****************************************************************************\
295 * *
296 * Core functions *
297 * *
298\*****************************************************************************/
299
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100300static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800301{
Pierre Ossman76591502008-07-21 00:32:11 +0200302 unsigned long flags;
303 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700304 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200305 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800306
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100307 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800308
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100309 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200310 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800311
Pierre Ossman76591502008-07-21 00:32:11 +0200312 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800313
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100314 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300315 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800316
Pierre Ossman76591502008-07-21 00:32:11 +0200317 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800318
Pierre Ossman76591502008-07-21 00:32:11 +0200319 blksize -= len;
320 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200321
Pierre Ossman76591502008-07-21 00:32:11 +0200322 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800323
Pierre Ossman76591502008-07-21 00:32:11 +0200324 while (len) {
325 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300326 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200327 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800328 }
Pierre Ossman76591502008-07-21 00:32:11 +0200329
330 *buf = scratch & 0xFF;
331
332 buf++;
333 scratch >>= 8;
334 chunk--;
335 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800336 }
337 }
Pierre Ossman76591502008-07-21 00:32:11 +0200338
339 sg_miter_stop(&host->sg_miter);
340
341 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100342}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800343
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100344static void sdhci_write_block_pio(struct sdhci_host *host)
345{
Pierre Ossman76591502008-07-21 00:32:11 +0200346 unsigned long flags;
347 size_t blksize, len, chunk;
348 u32 scratch;
349 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100350
351 DBG("PIO writing\n");
352
353 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200354 chunk = 0;
355 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100356
Pierre Ossman76591502008-07-21 00:32:11 +0200357 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358
359 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300360 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100361
Pierre Ossman76591502008-07-21 00:32:11 +0200362 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200363
Pierre Ossman76591502008-07-21 00:32:11 +0200364 blksize -= len;
365 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100366
Pierre Ossman76591502008-07-21 00:32:11 +0200367 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100368
Pierre Ossman76591502008-07-21 00:32:11 +0200369 while (len) {
370 scratch |= (u32)*buf << (chunk * 8);
371
372 buf++;
373 chunk++;
374 len--;
375
376 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300377 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200378 chunk = 0;
379 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100380 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381 }
382 }
Pierre Ossman76591502008-07-21 00:32:11 +0200383
384 sg_miter_stop(&host->sg_miter);
385
386 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100387}
388
389static void sdhci_transfer_pio(struct sdhci_host *host)
390{
391 u32 mask;
392
393 BUG_ON(!host->data);
394
Pierre Ossman76591502008-07-21 00:32:11 +0200395 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100396 return;
397
398 if (host->data->flags & MMC_DATA_READ)
399 mask = SDHCI_DATA_AVAILABLE;
400 else
401 mask = SDHCI_SPACE_AVAILABLE;
402
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200403 /*
404 * Some controllers (JMicron JMB38x) mess up the buffer bits
405 * for transfers < 4 bytes. As long as it is just one block,
406 * we can ignore the bits.
407 */
408 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
409 (host->data->blocks == 1))
410 mask = ~0;
411
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300412 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300413 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
414 udelay(100);
415
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100416 if (host->data->flags & MMC_DATA_READ)
417 sdhci_read_block_pio(host);
418 else
419 sdhci_write_block_pio(host);
420
Pierre Ossman76591502008-07-21 00:32:11 +0200421 host->blocks--;
422 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100423 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100424 }
425
426 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800427}
428
Russell King48857d92016-01-26 13:40:16 +0000429static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000430 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000431{
432 int sg_count;
433
Russell King94538e52016-01-26 13:40:37 +0000434 /*
435 * If the data buffers are already mapped, return the previous
436 * dma_map_sg() result.
437 */
438 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000439 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000440
441 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
442 data->flags & MMC_DATA_WRITE ?
443 DMA_TO_DEVICE : DMA_FROM_DEVICE);
444
445 if (sg_count == 0)
446 return -ENOSPC;
447
448 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000449 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000450
451 return sg_count;
452}
453
Pierre Ossman2134a922008-06-28 18:28:51 +0200454static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
455{
456 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800457 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200458}
459
460static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
461{
Cong Wang482fce92011-11-27 13:27:00 +0800462 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200463 local_irq_restore(*flags);
464}
465
Adrian Huntere57a5f62014-11-04 12:42:46 +0200466static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
467 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800468{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200469 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800470
Adrian Huntere57a5f62014-11-04 12:42:46 +0200471 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200472 dma_desc->cmd = cpu_to_le16(cmd);
473 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200474 dma_desc->addr_lo = cpu_to_le32((u32)addr);
475
476 if (host->flags & SDHCI_USE_64_BIT_DMA)
477 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800478}
479
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200480static void sdhci_adma_mark_end(void *desc)
481{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200482 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200483
Adrian Huntere57a5f62014-11-04 12:42:46 +0200484 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200485 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200486}
487
Russell King60c64762016-01-26 13:40:22 +0000488static void sdhci_adma_table_pre(struct sdhci_host *host,
489 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200490{
Pierre Ossman2134a922008-06-28 18:28:51 +0200491 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200492 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000493 dma_addr_t addr, align_addr;
494 void *desc, *align;
495 char *buffer;
496 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200497
498 /*
499 * The spec does not specify endianness of descriptor table.
500 * We currently guess that it is LE.
501 */
502
Russell King60c64762016-01-26 13:40:22 +0000503 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200504
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200505 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200506 align = host->align_buffer;
507
508 align_addr = host->align_addr;
509
510 for_each_sg(data->sg, sg, host->sg_count, i) {
511 addr = sg_dma_address(sg);
512 len = sg_dma_len(sg);
513
514 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000515 * The SDHCI specification states that ADMA addresses must
516 * be 32-bit aligned. If they aren't, then we use a bounce
517 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200518 * alignment.
519 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200520 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
521 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200522 if (offset) {
523 if (data->flags & MMC_DATA_WRITE) {
524 buffer = sdhci_kmap_atomic(sg, &flags);
525 memcpy(align, buffer, offset);
526 sdhci_kunmap_atomic(buffer, &flags);
527 }
528
Ben Dooks118cd172010-03-05 13:43:26 -0800529 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200530 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200531 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200532
533 BUG_ON(offset > 65536);
534
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200535 align += SDHCI_ADMA2_ALIGN;
536 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200537
Adrian Hunter76fe3792014-11-04 12:42:42 +0200538 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200539
540 addr += offset;
541 len -= offset;
542 }
543
Pierre Ossman2134a922008-06-28 18:28:51 +0200544 BUG_ON(len > 65536);
545
Adrian Hunter347ea322015-11-26 14:00:48 +0200546 if (len) {
547 /* tran, valid */
548 sdhci_adma_write_desc(host, desc, addr, len,
549 ADMA2_TRAN_VALID);
550 desc += host->desc_sz;
551 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200552
553 /*
554 * If this triggers then we have a calculation bug
555 * somewhere. :/
556 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200557 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200558 }
559
Thomas Abraham70764a92010-05-26 14:42:04 -0700560 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000561 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200562 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200563 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200564 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700565 }
566 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000567 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200568 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700569 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200570}
571
572static void sdhci_adma_table_post(struct sdhci_host *host,
573 struct mmc_data *data)
574{
Pierre Ossman2134a922008-06-28 18:28:51 +0200575 struct scatterlist *sg;
576 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200577 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200578 char *buffer;
579 unsigned long flags;
580
Russell King47fa9612016-01-26 13:40:06 +0000581 if (data->flags & MMC_DATA_READ) {
582 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100583
Russell King47fa9612016-01-26 13:40:06 +0000584 /* Do a quick scan of the SG list for any unaligned mappings */
585 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200586 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000587 has_unaligned = true;
588 break;
589 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200590
Russell King47fa9612016-01-26 13:40:06 +0000591 if (has_unaligned) {
592 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000593 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200594
Russell King47fa9612016-01-26 13:40:06 +0000595 align = host->align_buffer;
596
597 for_each_sg(data->sg, sg, host->sg_count, i) {
598 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
599 size = SDHCI_ADMA2_ALIGN -
600 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
601
602 buffer = sdhci_kmap_atomic(sg, &flags);
603 memcpy(buffer, align, size);
604 sdhci_kunmap_atomic(buffer, &flags);
605
606 align += SDHCI_ADMA2_ALIGN;
607 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200608 }
609 }
610 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200611}
612
Andrei Warkentina3c77782011-04-11 16:13:42 -0500613static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800614{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700615 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500616 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700617 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800618
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200619 /*
620 * If the host controller provides us with an incorrect timeout
621 * value, just skip the check and use 0xE. The hardware may take
622 * longer to time out, but that's much better than having a too-short
623 * timeout value.
624 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200625 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200626 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200627
Andrei Warkentina3c77782011-04-11 16:13:42 -0500628 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100629 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500630 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800631
Andrei Warkentina3c77782011-04-11 16:13:42 -0500632 /* timeout in us */
633 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100634 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300635 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000636 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000637 if (host->clock && data->timeout_clks) {
638 unsigned long long val;
639
640 /*
641 * data->timeout_clks is in units of clock cycles.
642 * host->clock is in Hz. target_timeout is in us.
643 * Hence, us = 1000000 * cycles / Hz. Round up.
644 */
645 val = 1000000 * data->timeout_clks;
646 if (do_div(val, host->clock))
647 target_timeout++;
648 target_timeout += val;
649 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300650 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700651
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700652 /*
653 * Figure out needed cycles.
654 * We do this in steps in order to fit inside a 32 bit int.
655 * The first step is the minimum timeout, which will have a
656 * minimum resolution of 6 bits:
657 * (1) 2^13*1000 > 2^22,
658 * (2) host->timeout_clk < 2^16
659 * =>
660 * (1) / (2) > 2^6
661 */
662 count = 0;
663 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
664 while (current_timeout < target_timeout) {
665 count++;
666 current_timeout <<= 1;
667 if (count >= 0xF)
668 break;
669 }
670
671 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400672 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
673 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700674 count = 0xE;
675 }
676
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200677 return count;
678}
679
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300680static void sdhci_set_transfer_irqs(struct sdhci_host *host)
681{
682 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
683 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
684
685 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100686 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300687 else
Russell Kingb537f942014-04-25 12:56:01 +0100688 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
689
690 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
691 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300692}
693
Aisheng Dongb45e6682014-08-27 15:26:29 +0800694static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200695{
696 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800697
698 if (host->ops->set_timeout) {
699 host->ops->set_timeout(host, cmd);
700 } else {
701 count = sdhci_calc_timeout(host, cmd);
702 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
703 }
704}
705
706static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
707{
Pierre Ossman2134a922008-06-28 18:28:51 +0200708 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500709 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200710
711 WARN_ON(host->data);
712
Aisheng Dongb45e6682014-08-27 15:26:29 +0800713 if (data || (cmd->flags & MMC_RSP_BUSY))
714 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500715
716 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200717 return;
718
719 /* Sanity checks */
720 BUG_ON(data->blksz * data->blocks > 524288);
721 BUG_ON(data->blksz > host->mmc->max_blk_size);
722 BUG_ON(data->blocks > 65535);
723
724 host->data = data;
725 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400726 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200727
Richard Röjforsa13abc72009-09-22 16:45:30 -0700728 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100729 host->flags |= SDHCI_REQ_USE_DMA;
730
Pierre Ossman2134a922008-06-28 18:28:51 +0200731 /*
732 * FIXME: This doesn't account for merging when mapping the
733 * scatterlist.
734 */
735 if (host->flags & SDHCI_REQ_USE_DMA) {
736 int broken, i;
737 struct scatterlist *sg;
738
739 broken = 0;
740 if (host->flags & SDHCI_USE_ADMA) {
741 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
742 broken = 1;
743 } else {
744 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
745 broken = 1;
746 }
747
748 if (unlikely(broken)) {
749 for_each_sg(data->sg, sg, data->sg_len, i) {
750 if (sg->length & 0x3) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100751 DBG("Reverting to PIO because of transfer size (%d)\n",
Pierre Ossman2134a922008-06-28 18:28:51 +0200752 sg->length);
753 host->flags &= ~SDHCI_REQ_USE_DMA;
754 break;
755 }
756 }
757 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100758 }
759
760 /*
761 * The assumption here being that alignment is the same after
762 * translation to device address space.
763 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200764 if (host->flags & SDHCI_REQ_USE_DMA) {
765 int broken, i;
766 struct scatterlist *sg;
767
768 broken = 0;
769 if (host->flags & SDHCI_USE_ADMA) {
770 /*
771 * As we use 3 byte chunks to work around
772 * alignment problems, we need to check this
773 * quirk.
774 */
775 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
776 broken = 1;
777 } else {
778 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
779 broken = 1;
780 }
781
782 if (unlikely(broken)) {
783 for_each_sg(data->sg, sg, data->sg_len, i) {
784 if (sg->offset & 0x3) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100785 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200786 host->flags &= ~SDHCI_REQ_USE_DMA;
787 break;
788 }
789 }
790 }
791 }
792
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200793 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000794 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200795
Russell King60c64762016-01-26 13:40:22 +0000796 if (sg_cnt <= 0) {
797 /*
798 * This only happens when someone fed
799 * us an invalid request.
800 */
801 WARN_ON(1);
802 host->flags &= ~SDHCI_REQ_USE_DMA;
803 } else if (host->flags & SDHCI_USE_ADMA) {
804 sdhci_adma_table_pre(host, data, sg_cnt);
805
806 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
807 if (host->flags & SDHCI_USE_64_BIT_DMA)
808 sdhci_writel(host,
809 (u64)host->adma_addr >> 32,
810 SDHCI_ADMA_ADDRESS_HI);
811 } else {
812 WARN_ON(sg_cnt != 1);
813 sdhci_writel(host, sg_dma_address(data->sg),
814 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200815 }
816 }
817
Pierre Ossman2134a922008-06-28 18:28:51 +0200818 /*
819 * Always adjust the DMA selection as some controllers
820 * (e.g. JMicron) can't do PIO properly when the selection
821 * is ADMA.
822 */
823 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300824 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200825 ctrl &= ~SDHCI_CTRL_DMA_MASK;
826 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200827 (host->flags & SDHCI_USE_ADMA)) {
828 if (host->flags & SDHCI_USE_64_BIT_DMA)
829 ctrl |= SDHCI_CTRL_ADMA64;
830 else
831 ctrl |= SDHCI_CTRL_ADMA32;
832 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200833 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200834 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300835 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100836 }
837
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200838 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200839 int flags;
840
841 flags = SG_MITER_ATOMIC;
842 if (host->data->flags & MMC_DATA_READ)
843 flags |= SG_MITER_TO_SG;
844 else
845 flags |= SG_MITER_FROM_SG;
846 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200847 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800848 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700849
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300850 sdhci_set_transfer_irqs(host);
851
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400852 /* Set the DMA boundary value and block size */
853 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
854 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300855 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700856}
857
858static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500859 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700860{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800861 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500862 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700863
Dong Aisheng2b558c12013-10-30 22:09:48 +0800864 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800865 if (host->quirks2 &
866 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
867 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
868 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800869 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800870 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
871 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800872 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800873 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700874 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800875 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700876
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200877 WARN_ON(!host->data);
878
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800879 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
880 mode = SDHCI_TRNS_BLK_CNT_EN;
881
Andrei Warkentine89d4562011-05-23 15:06:37 -0500882 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800883 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500884 /*
885 * If we are sending CMD23, CMD12 never gets sent
886 * on successful completion (so no Auto-CMD12).
887 */
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800888 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
889 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500890 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500891 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
892 mode |= SDHCI_TRNS_AUTO_CMD23;
893 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
894 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700895 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500896
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700897 if (data->flags & MMC_DATA_READ)
898 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100899 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700900 mode |= SDHCI_TRNS_DMA;
901
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300902 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800903}
904
905static void sdhci_finish_data(struct sdhci_host *host)
906{
907 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800908
909 BUG_ON(!host->data);
910
911 data = host->data;
912 host->data = NULL;
913
Russell Kingadd89132016-01-26 13:40:42 +0000914 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
915 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
916 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800917
918 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200919 * The specification states that the block count register must
920 * be updated, but it does not specify at what point in the
921 * data flow. That makes the register entirely useless to read
922 * back so we have to assume that nothing made it to the card
923 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800924 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200925 if (data->error)
926 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800927 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200928 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800929
Andrei Warkentine89d4562011-05-23 15:06:37 -0500930 /*
931 * Need to send CMD12 if -
932 * a) open-ended multiblock transfer (no CMD23)
933 * b) error in multiblock transfer
934 */
935 if (data->stop &&
936 (data->error ||
937 !host->mrq->sbc)) {
938
Pierre Ossmand129bce2006-03-24 03:18:17 -0800939 /*
940 * The controller needs a reset of internal state machines
941 * upon error conditions.
942 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200943 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100944 sdhci_do_reset(host, SDHCI_RESET_CMD);
945 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800946 }
947
948 sdhci_send_command(host, data->stop);
949 } else
950 tasklet_schedule(&host->finish_tasklet);
951}
952
Dong Aishengc0e551292013-09-13 19:11:31 +0800953void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800954{
955 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700956 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700957 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800958
959 WARN_ON(host->cmd);
960
Russell King96776202016-01-26 13:39:34 +0000961 /* Initially, a command has no error */
962 cmd->error = 0;
963
Pierre Ossmand129bce2006-03-24 03:18:17 -0800964 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700965 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700966
967 mask = SDHCI_CMD_INHIBIT;
968 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
969 mask |= SDHCI_DATA_INHIBIT;
970
971 /* We shouldn't wait for data inihibit for stop commands, even
972 though they might use busy signaling */
973 if (host->mrq->data && (cmd == host->mrq->data->stop))
974 mask &= ~SDHCI_DATA_INHIBIT;
975
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300976 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700977 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100978 pr_err("%s: Controller never released inhibit bit(s).\n",
979 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800980 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200981 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800982 tasklet_schedule(&host->finish_tasklet);
983 return;
984 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700985 timeout--;
986 mdelay(1);
987 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800988
Adrian Hunter3e1a6892013-11-14 10:16:20 +0200989 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100990 if (!cmd->data && cmd->busy_timeout > 9000)
991 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +0200992 else
993 timeout += 10 * HZ;
994 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800995
996 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +0900997 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998
Andrei Warkentina3c77782011-04-11 16:13:42 -0500999 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001000
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001001 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001002
Andrei Warkentine89d4562011-05-23 15:06:37 -05001003 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001004
Pierre Ossmand129bce2006-03-24 03:18:17 -08001005 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301006 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001007 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001008 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009 tasklet_schedule(&host->finish_tasklet);
1010 return;
1011 }
1012
1013 if (!(cmd->flags & MMC_RSP_PRESENT))
1014 flags = SDHCI_CMD_RESP_NONE;
1015 else if (cmd->flags & MMC_RSP_136)
1016 flags = SDHCI_CMD_RESP_LONG;
1017 else if (cmd->flags & MMC_RSP_BUSY)
1018 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1019 else
1020 flags = SDHCI_CMD_RESP_SHORT;
1021
1022 if (cmd->flags & MMC_RSP_CRC)
1023 flags |= SDHCI_CMD_CRC;
1024 if (cmd->flags & MMC_RSP_OPCODE)
1025 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301026
1027 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301028 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1029 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030 flags |= SDHCI_CMD_DATA;
1031
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001032 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033}
Dong Aishengc0e551292013-09-13 19:11:31 +08001034EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001035
1036static void sdhci_finish_command(struct sdhci_host *host)
1037{
1038 int i;
1039
1040 BUG_ON(host->cmd == NULL);
1041
1042 if (host->cmd->flags & MMC_RSP_PRESENT) {
1043 if (host->cmd->flags & MMC_RSP_136) {
1044 /* CRC is stripped so we need to do some shifting. */
1045 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001046 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047 SDHCI_RESPONSE + (3-i)*4) << 8;
1048 if (i != 3)
1049 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001050 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001051 SDHCI_RESPONSE + (3-i)*4-1);
1052 }
1053 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001054 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001055 }
1056 }
1057
Andrei Warkentine89d4562011-05-23 15:06:37 -05001058 /* Finished CMD23, now send actual command. */
1059 if (host->cmd == host->mrq->sbc) {
1060 host->cmd = NULL;
1061 sdhci_send_command(host, host->mrq->cmd);
1062 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001063
Andrei Warkentine89d4562011-05-23 15:06:37 -05001064 /* Processed actual command. */
1065 if (host->data && host->data_early)
1066 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001067
Andrei Warkentine89d4562011-05-23 15:06:37 -05001068 if (!host->cmd->data)
1069 tasklet_schedule(&host->finish_tasklet);
1070
1071 host->cmd = NULL;
1072 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001073}
1074
Kevin Liu52983382013-01-31 11:31:37 +08001075static u16 sdhci_get_preset_value(struct sdhci_host *host)
1076{
Russell Kingd975f122014-04-25 12:59:31 +01001077 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001078
Russell Kingd975f122014-04-25 12:59:31 +01001079 switch (host->timing) {
1080 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001081 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1082 break;
Russell Kingd975f122014-04-25 12:59:31 +01001083 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001084 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1085 break;
Russell Kingd975f122014-04-25 12:59:31 +01001086 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001087 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1088 break;
Russell Kingd975f122014-04-25 12:59:31 +01001089 case MMC_TIMING_UHS_SDR104:
1090 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001091 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1092 break;
Russell Kingd975f122014-04-25 12:59:31 +01001093 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001094 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001095 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1096 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001097 case MMC_TIMING_MMC_HS400:
1098 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1099 break;
Kevin Liu52983382013-01-31 11:31:37 +08001100 default:
1101 pr_warn("%s: Invalid UHS-I mode selected\n",
1102 mmc_hostname(host->mmc));
1103 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1104 break;
1105 }
1106 return preset;
1107}
1108
Russell King17710592014-04-25 12:58:55 +01001109void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001110{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301111 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001112 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301113 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001114 unsigned long timeout;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001115 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001116
Russell King1650d0c2014-04-25 12:58:50 +01001117 host->mmc->actual_clock = 0;
1118
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001119 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
ludovic.desroches@atmel.comaf951762015-09-17 10:16:19 +02001120 if (host->quirks2 & SDHCI_QUIRK2_NEED_DELAY_AFTER_INT_CLK_RST)
1121 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001122
1123 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001124 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001125
Zhangfei Gao85105c52010-08-06 07:10:01 +08001126 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001127 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001128 u16 pre_val;
1129
1130 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1131 pre_val = sdhci_get_preset_value(host);
1132 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1133 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1134 if (host->clk_mul &&
1135 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1136 clk = SDHCI_PROG_CLOCK_MODE;
1137 real_div = div + 1;
1138 clk_mul = host->clk_mul;
1139 } else {
1140 real_div = max_t(int, 1, div << 1);
1141 }
1142 goto clock_set;
1143 }
1144
Arindam Nathc3ed3872011-05-05 12:19:06 +05301145 /*
1146 * Check if the Host Controller supports Programmable Clock
1147 * Mode.
1148 */
1149 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001150 for (div = 1; div <= 1024; div++) {
1151 if ((host->max_clk * host->clk_mul / div)
1152 <= clock)
1153 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001154 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001155 if ((host->max_clk * host->clk_mul / div) <= clock) {
1156 /*
1157 * Set Programmable Clock Mode in the Clock
1158 * Control register.
1159 */
1160 clk = SDHCI_PROG_CLOCK_MODE;
1161 real_div = div;
1162 clk_mul = host->clk_mul;
1163 div--;
1164 } else {
1165 /*
1166 * Divisor can be too small to reach clock
1167 * speed requirement. Then use the base clock.
1168 */
1169 switch_base_clk = true;
1170 }
1171 }
1172
1173 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301174 /* Version 3.00 divisors must be a multiple of 2. */
1175 if (host->max_clk <= clock)
1176 div = 1;
1177 else {
1178 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1179 div += 2) {
1180 if ((host->max_clk / div) <= clock)
1181 break;
1182 }
1183 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001184 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301185 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301186 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1187 && !div && host->max_clk <= 25000000)
1188 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001189 }
1190 } else {
1191 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001192 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001193 if ((host->max_clk / div) <= clock)
1194 break;
1195 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001196 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301197 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001198 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001199
Kevin Liu52983382013-01-31 11:31:37 +08001200clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001201 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001202 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301203 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001204 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1205 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001206 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001207 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001208
Chris Ball27f6cb12009-09-22 16:45:31 -07001209 /* Wait max 20 ms */
1210 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001211 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001212 & SDHCI_CLOCK_INT_STABLE)) {
1213 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001214 pr_err("%s: Internal clock never stabilised.\n",
1215 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001216 sdhci_dumpregs(host);
1217 return;
1218 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001219 timeout--;
1220 mdelay(1);
1221 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001222
1223 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001224 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001225}
Russell King17710592014-04-25 12:58:55 +01001226EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001227
Russell King24fbb3c2014-04-25 13:00:06 +01001228static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1229 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001230{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001231 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001232 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001233
Russell King24fbb3c2014-04-25 13:00:06 +01001234 if (mode != MMC_POWER_OFF) {
1235 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001236 case MMC_VDD_165_195:
1237 pwr = SDHCI_POWER_180;
1238 break;
1239 case MMC_VDD_29_30:
1240 case MMC_VDD_30_31:
1241 pwr = SDHCI_POWER_300;
1242 break;
1243 case MMC_VDD_32_33:
1244 case MMC_VDD_33_34:
1245 pwr = SDHCI_POWER_330;
1246 break;
1247 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001248 WARN(1, "%s: Invalid vdd %#x\n",
1249 mmc_hostname(host->mmc), vdd);
1250 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001251 }
1252 }
1253
1254 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001255 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001256
Pierre Ossmanae628902009-05-03 20:45:03 +02001257 host->pwr = pwr;
1258
1259 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001260 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001261 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1262 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001263 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001264 } else {
1265 /*
1266 * Spec says that we should clear the power reg before setting
1267 * a new value. Some controllers don't seem to like this though.
1268 */
1269 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1270 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001271
Russell Kinge921a8b2014-04-25 13:00:01 +01001272 /*
1273 * At least the Marvell CaFe chip gets confused if we set the
1274 * voltage and set turn on power at the same time, so set the
1275 * voltage first.
1276 */
1277 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1278 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001279
Russell Kinge921a8b2014-04-25 13:00:01 +01001280 pwr |= SDHCI_POWER_ON;
1281
Pierre Ossmanae628902009-05-03 20:45:03 +02001282 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1283
Russell Kinge921a8b2014-04-25 13:00:01 +01001284 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1285 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001286
Russell Kinge921a8b2014-04-25 13:00:01 +01001287 /*
1288 * Some controllers need an extra 10ms delay of 10ms before
1289 * they can apply clock after applying power
1290 */
1291 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1292 mdelay(10);
1293 }
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001294
1295 if (!IS_ERR(mmc->supply.vmmc)) {
1296 spin_unlock_irq(&host->lock);
1297 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1298 spin_lock_irq(&host->lock);
1299 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001300}
1301
Pierre Ossmand129bce2006-03-24 03:18:17 -08001302/*****************************************************************************\
1303 * *
1304 * MMC callbacks *
1305 * *
1306\*****************************************************************************/
1307
1308static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1309{
1310 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001311 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001312 unsigned long flags;
1313
1314 host = mmc_priv(mmc);
1315
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001316 sdhci_runtime_pm_get(host);
1317
Scott Branden04e079cf2015-03-10 11:35:10 -07001318 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001319 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001320
Pierre Ossmand129bce2006-03-24 03:18:17 -08001321 spin_lock_irqsave(&host->lock, flags);
1322
1323 WARN_ON(host->mrq != NULL);
1324
Pierre Ossmanf9134312008-12-21 17:01:48 +01001325#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001326 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001327#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001328
1329 /*
1330 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1331 * requests if Auto-CMD12 is enabled.
1332 */
1333 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001334 if (mrq->stop) {
1335 mrq->data->stop = NULL;
1336 mrq->stop = NULL;
1337 }
1338 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001339
1340 host->mrq = mrq;
1341
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001342 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001343 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001344 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301345 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001346 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001347 sdhci_send_command(host, mrq->sbc);
1348 else
1349 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301350 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001351
Pierre Ossman5f25a662006-10-04 02:15:39 -07001352 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001353 spin_unlock_irqrestore(&host->lock, flags);
1354}
1355
Russell King2317f562014-04-25 12:57:07 +01001356void sdhci_set_bus_width(struct sdhci_host *host, int width)
1357{
1358 u8 ctrl;
1359
1360 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1361 if (width == MMC_BUS_WIDTH_8) {
1362 ctrl &= ~SDHCI_CTRL_4BITBUS;
1363 if (host->version >= SDHCI_SPEC_300)
1364 ctrl |= SDHCI_CTRL_8BITBUS;
1365 } else {
1366 if (host->version >= SDHCI_SPEC_300)
1367 ctrl &= ~SDHCI_CTRL_8BITBUS;
1368 if (width == MMC_BUS_WIDTH_4)
1369 ctrl |= SDHCI_CTRL_4BITBUS;
1370 else
1371 ctrl &= ~SDHCI_CTRL_4BITBUS;
1372 }
1373 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1374}
1375EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1376
Russell King96d7b782014-04-25 12:59:26 +01001377void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1378{
1379 u16 ctrl_2;
1380
1381 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1382 /* Select Bus Speed Mode for host */
1383 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1384 if ((timing == MMC_TIMING_MMC_HS200) ||
1385 (timing == MMC_TIMING_UHS_SDR104))
1386 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1387 else if (timing == MMC_TIMING_UHS_SDR12)
1388 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1389 else if (timing == MMC_TIMING_UHS_SDR25)
1390 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1391 else if (timing == MMC_TIMING_UHS_SDR50)
1392 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1393 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1394 (timing == MMC_TIMING_MMC_DDR52))
1395 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001396 else if (timing == MMC_TIMING_MMC_HS400)
1397 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001398 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1399}
1400EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1401
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001402static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001403{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001404 unsigned long flags;
1405 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001406 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001407
Pierre Ossmand129bce2006-03-24 03:18:17 -08001408 spin_lock_irqsave(&host->lock, flags);
1409
Adrian Hunterceb61432011-12-27 15:48:41 +02001410 if (host->flags & SDHCI_DEVICE_DEAD) {
1411 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001412 if (!IS_ERR(mmc->supply.vmmc) &&
1413 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001414 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001415 return;
1416 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001417
Pierre Ossmand129bce2006-03-24 03:18:17 -08001418 /*
1419 * Reset the chip on each power off.
1420 * Should clear out any weird states.
1421 */
1422 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001423 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001424 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001425 }
1426
Kevin Liu52983382013-01-31 11:31:37 +08001427 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001428 (ios->power_mode == MMC_POWER_UP) &&
1429 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001430 sdhci_enable_preset_value(host, false);
1431
Russell King373073e2014-04-25 12:58:45 +01001432 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001433 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001434 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001435
1436 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1437 host->clock) {
1438 host->timeout_clk = host->mmc->actual_clock ?
1439 host->mmc->actual_clock / 1000 :
1440 host->clock / 1000;
1441 host->mmc->max_busy_timeout =
1442 host->ops->get_max_timeout_count ?
1443 host->ops->get_max_timeout_count(host) :
1444 1 << 27;
1445 host->mmc->max_busy_timeout /= host->timeout_clk;
1446 }
Russell King373073e2014-04-25 12:58:45 +01001447 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001448
Russell King24fbb3c2014-04-25 13:00:06 +01001449 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001450
Philip Rakity643a81f2010-09-23 08:24:32 -07001451 if (host->ops->platform_send_init_74_clocks)
1452 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1453
Russell King2317f562014-04-25 12:57:07 +01001454 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001455
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001456 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001457
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001458 if ((ios->timing == MMC_TIMING_SD_HS ||
1459 ios->timing == MMC_TIMING_MMC_HS)
1460 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001461 ctrl |= SDHCI_CTRL_HISPD;
1462 else
1463 ctrl &= ~SDHCI_CTRL_HISPD;
1464
Arindam Nathd6d50a12011-05-05 12:18:59 +05301465 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301466 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301467
1468 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001469 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1470 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001471 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301472 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301473 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1474 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001475 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301476 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301477
Russell Kingda91a8f2014-04-25 13:00:12 +01001478 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301479 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301480 /*
1481 * We only need to set Driver Strength if the
1482 * preset value enable is not set.
1483 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001484 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301485 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1486 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1487 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001488 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1489 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301490 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1491 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001492 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1493 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1494 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001495 pr_warn("%s: invalid driver type, default to driver type B\n",
1496 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001497 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1498 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301499
1500 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301501 } else {
1502 /*
1503 * According to SDHC Spec v3.00, if the Preset Value
1504 * Enable in the Host Control 2 register is set, we
1505 * need to reset SD Clock Enable before changing High
1506 * Speed Enable to avoid generating clock gliches.
1507 */
Arindam Nath758535c2011-05-05 12:19:00 +05301508
1509 /* Reset SD Clock Enable */
1510 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1511 clk &= ~SDHCI_CLOCK_CARD_EN;
1512 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1513
1514 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1515
1516 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001517 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301518 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301519
Arindam Nath49c468f2011-05-05 12:19:01 +05301520 /* Reset SD Clock Enable */
1521 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1522 clk &= ~SDHCI_CLOCK_CARD_EN;
1523 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1524
Russell King96d7b782014-04-25 12:59:26 +01001525 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001526 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301527
Kevin Liu52983382013-01-31 11:31:37 +08001528 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1529 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1530 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1531 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1532 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001533 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1534 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001535 u16 preset;
1536
1537 sdhci_enable_preset_value(host, true);
1538 preset = sdhci_get_preset_value(host);
1539 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1540 >> SDHCI_PRESET_DRV_SHIFT;
1541 }
1542
Arindam Nath49c468f2011-05-05 12:19:01 +05301543 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001544 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301545 } else
1546 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301547
Leandro Dorileob8352262007-07-25 23:47:04 +02001548 /*
1549 * Some (ENE) controllers go apeshit on some ios operation,
1550 * signalling timeout and CRC errors even on CMD0. Resetting
1551 * it on each ios seems to solve the problem.
1552 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301553 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001554 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001555
Pierre Ossman5f25a662006-10-04 02:15:39 -07001556 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001557 spin_unlock_irqrestore(&host->lock, flags);
1558}
1559
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001560static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1561{
1562 struct sdhci_host *host = mmc_priv(mmc);
1563
1564 sdhci_runtime_pm_get(host);
1565 sdhci_do_set_ios(host, ios);
1566 sdhci_runtime_pm_put(host);
1567}
1568
Kevin Liu94144a42013-02-28 17:35:53 +08001569static int sdhci_do_get_cd(struct sdhci_host *host)
1570{
1571 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1572
1573 if (host->flags & SDHCI_DEVICE_DEAD)
1574 return 0;
1575
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001576 /* If nonremovable, assume that the card is always present. */
1577 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
Kevin Liu94144a42013-02-28 17:35:53 +08001578 return 1;
1579
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001580 /*
1581 * Try slot gpio detect, if defined it take precedence
1582 * over build in controller functionality
1583 */
Kevin Liu94144a42013-02-28 17:35:53 +08001584 if (!IS_ERR_VALUE(gpio_cd))
1585 return !!gpio_cd;
1586
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001587 /* If polling, assume that the card is always present. */
1588 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1589 return 1;
1590
Kevin Liu94144a42013-02-28 17:35:53 +08001591 /* Host native card detect */
1592 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1593}
1594
1595static int sdhci_get_cd(struct mmc_host *mmc)
1596{
1597 struct sdhci_host *host = mmc_priv(mmc);
1598 int ret;
1599
1600 sdhci_runtime_pm_get(host);
1601 ret = sdhci_do_get_cd(host);
1602 sdhci_runtime_pm_put(host);
1603 return ret;
1604}
1605
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001606static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001607{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001608 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001609 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001610
Pierre Ossmand129bce2006-03-24 03:18:17 -08001611 spin_lock_irqsave(&host->lock, flags);
1612
Pierre Ossman1e728592008-04-16 19:13:13 +02001613 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001614 is_readonly = 0;
1615 else if (host->ops->get_ro)
1616 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001617 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001618 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1619 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001620
1621 spin_unlock_irqrestore(&host->lock, flags);
1622
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001623 /* This quirk needs to be replaced by a callback-function later */
1624 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1625 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001626}
1627
Takashi Iwai82b0e232011-04-21 20:26:38 +02001628#define SAMPLE_COUNT 5
1629
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001630static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001631{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001632 int i, ro_count;
1633
Takashi Iwai82b0e232011-04-21 20:26:38 +02001634 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001635 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001636
1637 ro_count = 0;
1638 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001639 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001640 if (++ro_count > SAMPLE_COUNT / 2)
1641 return 1;
1642 }
1643 msleep(30);
1644 }
1645 return 0;
1646}
1647
Adrian Hunter20758b62011-08-29 16:42:12 +03001648static void sdhci_hw_reset(struct mmc_host *mmc)
1649{
1650 struct sdhci_host *host = mmc_priv(mmc);
1651
1652 if (host->ops && host->ops->hw_reset)
1653 host->ops->hw_reset(host);
1654}
1655
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001656static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001657{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001658 struct sdhci_host *host = mmc_priv(mmc);
1659 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001660
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001661 sdhci_runtime_pm_get(host);
1662 ret = sdhci_do_get_ro(host);
1663 sdhci_runtime_pm_put(host);
1664 return ret;
1665}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001666
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001667static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1668{
Russell Kingbe138552014-04-25 12:55:56 +01001669 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001670 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001671 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001672 else
Russell Kingb537f942014-04-25 12:56:01 +01001673 host->ier &= ~SDHCI_INT_CARD_INT;
1674
1675 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1676 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001677 mmiowb();
1678 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001679}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001680
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001681static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1682{
1683 struct sdhci_host *host = mmc_priv(mmc);
1684 unsigned long flags;
1685
Russell Kingef104332014-04-25 12:55:41 +01001686 sdhci_runtime_pm_get(host);
1687
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001688 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001689 if (enable)
1690 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1691 else
1692 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1693
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001694 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001695 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001696
1697 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001698}
1699
Philip Rakity6231f3d2012-07-23 15:56:23 -07001700static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001701 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001702{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001703 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001704 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001705 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001706
1707 /*
1708 * Signal Voltage Switching is only applicable for Host Controllers
1709 * v3.00 and above.
1710 */
1711 if (host->version < SDHCI_SPEC_300)
1712 return 0;
1713
Philip Rakity6231f3d2012-07-23 15:56:23 -07001714 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001715
Fabio Estevam21f59982013-02-14 10:35:03 -02001716 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001717 case MMC_SIGNAL_VOLTAGE_330:
1718 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1719 ctrl &= ~SDHCI_CTRL_VDD_180;
1720 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1721
Tim Kryger3a48edc2014-06-13 10:13:56 -07001722 if (!IS_ERR(mmc->supply.vqmmc)) {
1723 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1724 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001725 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001726 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1727 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001728 return -EIO;
1729 }
1730 }
1731 /* Wait for 5ms */
1732 usleep_range(5000, 5500);
1733
1734 /* 3.3V regulator output should be stable within 5 ms */
1735 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1736 if (!(ctrl & SDHCI_CTRL_VDD_180))
1737 return 0;
1738
Joe Perches66061102014-09-12 14:56:56 -07001739 pr_warn("%s: 3.3V regulator output did not became stable\n",
1740 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001741
1742 return -EAGAIN;
1743 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001744 if (!IS_ERR(mmc->supply.vqmmc)) {
1745 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001746 1700000, 1950000);
1747 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001748 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1749 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001750 return -EIO;
1751 }
1752 }
1753
1754 /*
1755 * Enable 1.8V Signal Enable in the Host Control2
1756 * register
1757 */
1758 ctrl |= SDHCI_CTRL_VDD_180;
1759 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1760
Vincent Yang9d967a62015-01-20 16:05:15 +08001761 /* Some controller need to do more when switching */
1762 if (host->ops->voltage_switch)
1763 host->ops->voltage_switch(host);
1764
Kevin Liu20b92a32012-12-17 19:29:26 +08001765 /* 1.8V regulator output should be stable within 5 ms */
1766 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1767 if (ctrl & SDHCI_CTRL_VDD_180)
1768 return 0;
1769
Joe Perches66061102014-09-12 14:56:56 -07001770 pr_warn("%s: 1.8V regulator output did not became stable\n",
1771 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001772
1773 return -EAGAIN;
1774 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001775 if (!IS_ERR(mmc->supply.vqmmc)) {
1776 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1777 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001778 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001779 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1780 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001781 return -EIO;
1782 }
1783 }
1784 return 0;
1785 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301786 /* No signal voltage switch required */
1787 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001788 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301789}
1790
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001791static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001792 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001793{
1794 struct sdhci_host *host = mmc_priv(mmc);
1795 int err;
1796
1797 if (host->version < SDHCI_SPEC_300)
1798 return 0;
1799 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001800 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001801 sdhci_runtime_pm_put(host);
1802 return err;
1803}
1804
Kevin Liu20b92a32012-12-17 19:29:26 +08001805static int sdhci_card_busy(struct mmc_host *mmc)
1806{
1807 struct sdhci_host *host = mmc_priv(mmc);
1808 u32 present_state;
1809
1810 sdhci_runtime_pm_get(host);
1811 /* Check whether DAT[3:0] is 0000 */
1812 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1813 sdhci_runtime_pm_put(host);
1814
1815 return !(present_state & SDHCI_DATA_LVL_MASK);
1816}
1817
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001818static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1819{
1820 struct sdhci_host *host = mmc_priv(mmc);
1821 unsigned long flags;
1822
1823 spin_lock_irqsave(&host->lock, flags);
1824 host->flags |= SDHCI_HS400_TUNING;
1825 spin_unlock_irqrestore(&host->lock, flags);
1826
1827 return 0;
1828}
1829
Girish K S069c9f12012-01-06 09:56:39 +05301830static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301831{
Russell King4b6f37d2014-04-25 12:59:36 +01001832 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301833 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301834 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301835 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001836 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001837 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001838 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301839
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001840 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001841 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301842
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001843 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1844 host->flags &= ~SDHCI_HS400_TUNING;
1845
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001846 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1847 tuning_count = host->tuning_count;
1848
Arindam Nathb513ea22011-05-05 12:19:04 +05301849 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001850 * The Host Controller needs tuning in case of SDR104 and DDR50
1851 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1852 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301853 * If the Host Controller supports the HS200 mode then the
1854 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301855 */
Russell King4b6f37d2014-04-25 12:59:36 +01001856 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001857 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001858 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001859 err = -EINVAL;
1860 goto out_unlock;
1861
Russell King4b6f37d2014-04-25 12:59:36 +01001862 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001863 /*
1864 * Periodic re-tuning for HS400 is not expected to be needed, so
1865 * disable it here.
1866 */
1867 if (hs400_tuning)
1868 tuning_count = 0;
1869 break;
1870
Russell King4b6f37d2014-04-25 12:59:36 +01001871 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001872 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001873 break;
Girish K S069c9f12012-01-06 09:56:39 +05301874
Russell King4b6f37d2014-04-25 12:59:36 +01001875 case MMC_TIMING_UHS_SDR50:
1876 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1877 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1878 break;
1879 /* FALLTHROUGH */
1880
1881 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001882 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301883 }
1884
Dong Aisheng45251812013-09-13 19:11:30 +08001885 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001886 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001887 err = host->ops->platform_execute_tuning(host, opcode);
1888 sdhci_runtime_pm_put(host);
1889 return err;
1890 }
1891
Russell King4b6f37d2014-04-25 12:59:36 +01001892 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1893 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001894 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1895 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301896 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1897
1898 /*
1899 * As per the Host Controller spec v3.00, tuning command
1900 * generates Buffer Read Ready interrupt, so enable that.
1901 *
1902 * Note: The spec clearly says that when tuning sequence
1903 * is being performed, the controller does not generate
1904 * interrupts other than Buffer Read Ready interrupt. But
1905 * to make sure we don't hit a controller bug, we _only_
1906 * enable Buffer Read Ready interrupt here.
1907 */
Russell Kingb537f942014-04-25 12:56:01 +01001908 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1909 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301910
1911 /*
1912 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1913 * of loops reaches 40 times or a timeout of 150ms occurs.
1914 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301915 do {
1916 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001917 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301918
Girish K S069c9f12012-01-06 09:56:39 +05301919 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301920 cmd.arg = 0;
1921 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1922 cmd.retries = 0;
1923 cmd.data = NULL;
1924 cmd.error = 0;
1925
Al Cooper7ce45e92014-05-09 11:34:07 -04001926 if (tuning_loop_counter-- == 0)
1927 break;
1928
Arindam Nathb513ea22011-05-05 12:19:04 +05301929 mrq.cmd = &cmd;
1930 host->mrq = &mrq;
1931
1932 /*
1933 * In response to CMD19, the card sends 64 bytes of tuning
1934 * block to the Host Controller. So we set the block size
1935 * to 64 here.
1936 */
Girish K S069c9f12012-01-06 09:56:39 +05301937 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1938 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1939 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1940 SDHCI_BLOCK_SIZE);
1941 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1942 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1943 SDHCI_BLOCK_SIZE);
1944 } else {
1945 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1946 SDHCI_BLOCK_SIZE);
1947 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301948
1949 /*
1950 * The tuning block is sent by the card to the host controller.
1951 * So we set the TRNS_READ bit in the Transfer Mode register.
1952 * This also takes care of setting DMA Enable and Multi Block
1953 * Select in the same register to 0.
1954 */
1955 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1956
1957 sdhci_send_command(host, &cmd);
1958
1959 host->cmd = NULL;
1960 host->mrq = NULL;
1961
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001962 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301963 /* Wait for Buffer Read Ready interrupt */
1964 wait_event_interruptible_timeout(host->buf_ready_int,
1965 (host->tuning_done == 1),
1966 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001967 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301968
1969 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001970 pr_info(DRIVER_NAME ": Timeout waiting for Buffer Read Ready interrupt during tuning procedure, falling back to fixed sampling clock\n");
Arindam Nathb513ea22011-05-05 12:19:04 +05301971 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1972 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1973 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1974 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1975
1976 err = -EIO;
1977 goto out;
1978 }
1979
1980 host->tuning_done = 0;
1981
1982 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07001983
1984 /* eMMC spec does not require a delay between tuning cycles */
1985 if (opcode == MMC_SEND_TUNING_BLOCK)
1986 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05301987 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1988
1989 /*
1990 * The Host Driver has exhausted the maximum number of loops allowed,
1991 * so use fixed sampling frequency.
1992 */
Al Cooper7ce45e92014-05-09 11:34:07 -04001993 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05301994 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1995 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04001996 }
1997 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001998 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08001999 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302000 }
2001
2002out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002003 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002004 /*
2005 * In case tuning fails, host controllers which support
2006 * re-tuning can try tuning again at a later time, when the
2007 * re-tuning timer expires. So for these controllers, we
2008 * return 0. Since there might be other controllers who do not
2009 * have this capability, we return error for them.
2010 */
2011 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302012 }
2013
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002014 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302015
Russell Kingb537f942014-04-25 12:56:01 +01002016 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2017 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002018out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002019 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002020 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302021
2022 return err;
2023}
2024
Adrian Huntercb849642015-02-06 14:12:59 +02002025static int sdhci_select_drive_strength(struct mmc_card *card,
2026 unsigned int max_dtr, int host_drv,
2027 int card_drv, int *drv_type)
2028{
2029 struct sdhci_host *host = mmc_priv(card->host);
2030
2031 if (!host->ops->select_drive_strength)
2032 return 0;
2033
2034 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2035 card_drv, drv_type);
2036}
Kevin Liu52983382013-01-31 11:31:37 +08002037
2038static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302039{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302040 /* Host Controller v3.00 defines preset value registers */
2041 if (host->version < SDHCI_SPEC_300)
2042 return;
2043
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302044 /*
2045 * We only enable or disable Preset Value if they are not already
2046 * enabled or disabled respectively. Otherwise, we bail out.
2047 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002048 if (host->preset_enabled != enable) {
2049 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2050
2051 if (enable)
2052 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2053 else
2054 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2055
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302056 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002057
2058 if (enable)
2059 host->flags |= SDHCI_PV_ENABLED;
2060 else
2061 host->flags &= ~SDHCI_PV_ENABLED;
2062
2063 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302064 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002065}
2066
Haibo Chen348487c2014-12-09 17:04:05 +08002067static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2068 int err)
2069{
2070 struct sdhci_host *host = mmc_priv(mmc);
2071 struct mmc_data *data = mrq->data;
2072
Russell Kingf48f0392016-01-26 13:40:32 +00002073 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002074 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2075 data->flags & MMC_DATA_WRITE ?
2076 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2077
2078 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002079}
2080
Haibo Chen348487c2014-12-09 17:04:05 +08002081static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2082 bool is_first_req)
2083{
2084 struct sdhci_host *host = mmc_priv(mmc);
2085
Haibo Chend31911b2015-08-25 10:02:11 +08002086 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002087
2088 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002089 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002090}
2091
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002092static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002093{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002094 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002095 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002096 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002097
Christian Daudt722e1282013-06-20 14:26:36 -07002098 /* First check if client has provided their own card event */
2099 if (host->ops->card_event)
2100 host->ops->card_event(host);
2101
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002102 present = sdhci_do_get_cd(host);
2103
Pierre Ossmand129bce2006-03-24 03:18:17 -08002104 spin_lock_irqsave(&host->lock, flags);
2105
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002106 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002107 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302108 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002109 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302110 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002111 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002112
Russell King03231f92014-04-25 12:57:12 +01002113 sdhci_do_reset(host, SDHCI_RESET_CMD);
2114 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002115
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002116 host->mrq->cmd->error = -ENOMEDIUM;
2117 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002118 }
2119
2120 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002121}
2122
2123static const struct mmc_host_ops sdhci_ops = {
2124 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002125 .post_req = sdhci_post_req,
2126 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002127 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002128 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002129 .get_ro = sdhci_get_ro,
2130 .hw_reset = sdhci_hw_reset,
2131 .enable_sdio_irq = sdhci_enable_sdio_irq,
2132 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002133 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002134 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002135 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002136 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002137 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002138};
2139
2140/*****************************************************************************\
2141 * *
2142 * Tasklets *
2143 * *
2144\*****************************************************************************/
2145
Pierre Ossmand129bce2006-03-24 03:18:17 -08002146static void sdhci_tasklet_finish(unsigned long param)
2147{
2148 struct sdhci_host *host;
2149 unsigned long flags;
2150 struct mmc_request *mrq;
2151
2152 host = (struct sdhci_host*)param;
2153
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002154 spin_lock_irqsave(&host->lock, flags);
2155
Chris Ball0c9c99a2011-04-27 17:35:31 -04002156 /*
2157 * If this tasklet gets rescheduled while running, it will
2158 * be run again afterwards but without any active request.
2159 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002160 if (!host->mrq) {
2161 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002162 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002163 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002164
2165 del_timer(&host->timer);
2166
2167 mrq = host->mrq;
2168
Pierre Ossmand129bce2006-03-24 03:18:17 -08002169 /*
Russell King054cedf2016-01-26 13:40:42 +00002170 * Always unmap the data buffers if they were mapped by
2171 * sdhci_prepare_data() whenever we finish with a request.
2172 * This avoids leaking DMA mappings on error.
2173 */
2174 if (host->flags & SDHCI_REQ_USE_DMA) {
2175 struct mmc_data *data = mrq->data;
2176
2177 if (data && data->host_cookie == COOKIE_MAPPED) {
2178 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2179 (data->flags & MMC_DATA_READ) ?
2180 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2181 data->host_cookie = COOKIE_UNMAPPED;
2182 }
2183 }
2184
2185 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002186 * The controller needs a reset of internal state machines
2187 * upon error conditions.
2188 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002189 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002190 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002191 (mrq->sbc && mrq->sbc->error) ||
2192 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2193 (mrq->data->stop && mrq->data->stop->error))) ||
2194 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002195
2196 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002197 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002198 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002199 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002200
2201 /* Spec says we should do both at the same time, but Ricoh
2202 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002203 sdhci_do_reset(host, SDHCI_RESET_CMD);
2204 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002205 }
2206
2207 host->mrq = NULL;
2208 host->cmd = NULL;
2209 host->data = NULL;
2210
Pierre Ossmanf9134312008-12-21 17:01:48 +01002211#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002212 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002213#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002214
Pierre Ossman5f25a662006-10-04 02:15:39 -07002215 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002216 spin_unlock_irqrestore(&host->lock, flags);
2217
2218 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002219 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002220}
2221
2222static void sdhci_timeout_timer(unsigned long data)
2223{
2224 struct sdhci_host *host;
2225 unsigned long flags;
2226
2227 host = (struct sdhci_host*)data;
2228
2229 spin_lock_irqsave(&host->lock, flags);
2230
2231 if (host->mrq) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002232 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2233 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002234 sdhci_dumpregs(host);
2235
2236 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002237 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002238 sdhci_finish_data(host);
2239 } else {
2240 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002241 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002242 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002243 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002244
2245 tasklet_schedule(&host->finish_tasklet);
2246 }
2247 }
2248
Pierre Ossman5f25a662006-10-04 02:15:39 -07002249 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002250 spin_unlock_irqrestore(&host->lock, flags);
2251}
2252
2253/*****************************************************************************\
2254 * *
2255 * Interrupt handling *
2256 * *
2257\*****************************************************************************/
2258
Adrian Hunter61541392014-09-24 10:27:27 +03002259static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002260{
2261 BUG_ON(intmask == 0);
2262
2263 if (!host->cmd) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002264 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2265 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002266 sdhci_dumpregs(host);
2267 return;
2268 }
2269
Russell Kingec014cb2016-01-26 13:39:39 +00002270 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2271 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2272 if (intmask & SDHCI_INT_TIMEOUT)
2273 host->cmd->error = -ETIMEDOUT;
2274 else
2275 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002276
Russell King71fcbda2016-01-26 13:39:45 +00002277 /*
2278 * If this command initiates a data phase and a response
2279 * CRC error is signalled, the card can start transferring
2280 * data - the card may have received the command without
2281 * error. We must not terminate the mmc_request early.
2282 *
2283 * If the card did not receive the command or returned an
2284 * error which prevented it sending data, the data phase
2285 * will time out.
2286 */
2287 if (host->cmd->data &&
2288 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2289 SDHCI_INT_CRC) {
2290 host->cmd = NULL;
2291 return;
2292 }
2293
Pierre Ossmand129bce2006-03-24 03:18:17 -08002294 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002295 return;
2296 }
2297
2298 /*
2299 * The host can send and interrupt when the busy state has
2300 * ended, allowing us to wait without wasting CPU cycles.
2301 * Unfortunately this is overloaded on the "data complete"
2302 * interrupt, so we need to take some care when handling
2303 * it.
2304 *
2305 * Note: The 1.0 specification is a bit ambiguous about this
2306 * feature so there might be some problems with older
2307 * controllers.
2308 */
2309 if (host->cmd->flags & MMC_RSP_BUSY) {
2310 if (host->cmd->data)
Marek Vasut2e4456f2015-11-18 10:47:02 +01002311 DBG("Cannot wait for busy signal when also doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002312 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2313 && !host->busy_handle) {
2314 /* Mark that command complete before busy is ended */
2315 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002316 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002317 }
Ben Dooksf9454052009-02-20 20:33:08 +03002318
2319 /* The controller does not support the end-of-busy IRQ,
2320 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002321 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2322 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2323 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002324 }
2325
2326 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002327 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002328}
2329
George G. Davis0957c332010-02-18 12:32:12 -05002330#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002331static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002332{
2333 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002334 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002335
2336 sdhci_dumpregs(host);
2337
2338 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002339 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002340
Adrian Huntere57a5f62014-11-04 12:42:46 +02002341 if (host->flags & SDHCI_USE_64_BIT_DMA)
2342 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2343 name, desc, le32_to_cpu(dma_desc->addr_hi),
2344 le32_to_cpu(dma_desc->addr_lo),
2345 le16_to_cpu(dma_desc->len),
2346 le16_to_cpu(dma_desc->cmd));
2347 else
2348 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2349 name, desc, le32_to_cpu(dma_desc->addr_lo),
2350 le16_to_cpu(dma_desc->len),
2351 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002352
Adrian Hunter76fe3792014-11-04 12:42:42 +02002353 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002354
Adrian Hunter05452302014-11-04 12:42:45 +02002355 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002356 break;
2357 }
2358}
2359#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002360static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002361#endif
2362
Pierre Ossmand129bce2006-03-24 03:18:17 -08002363static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2364{
Girish K S069c9f12012-01-06 09:56:39 +05302365 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002366 BUG_ON(intmask == 0);
2367
Arindam Nathb513ea22011-05-05 12:19:04 +05302368 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2369 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302370 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2371 if (command == MMC_SEND_TUNING_BLOCK ||
2372 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302373 host->tuning_done = 1;
2374 wake_up(&host->buf_ready_int);
2375 return;
2376 }
2377 }
2378
Pierre Ossmand129bce2006-03-24 03:18:17 -08002379 if (!host->data) {
2380 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002381 * The "data complete" interrupt is also used to
2382 * indicate that a busy state has ended. See comment
2383 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002384 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002385 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002386 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2387 host->cmd->error = -ETIMEDOUT;
2388 tasklet_schedule(&host->finish_tasklet);
2389 return;
2390 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002391 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002392 /*
2393 * Some cards handle busy-end interrupt
2394 * before the command completed, so make
2395 * sure we do things in the proper order.
2396 */
2397 if (host->busy_handle)
2398 sdhci_finish_command(host);
2399 else
2400 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002401 return;
2402 }
2403 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002404
Marek Vasut2e4456f2015-11-18 10:47:02 +01002405 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2406 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002407 sdhci_dumpregs(host);
2408
2409 return;
2410 }
2411
2412 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002413 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002414 else if (intmask & SDHCI_INT_DATA_END_BIT)
2415 host->data->error = -EILSEQ;
2416 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2417 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2418 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002419 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002420 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302421 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002422 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002423 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002424 if (host->ops->adma_workaround)
2425 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002426 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002427
Pierre Ossman17b04292007-07-22 22:18:46 +02002428 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002429 sdhci_finish_data(host);
2430 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002431 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002432 sdhci_transfer_pio(host);
2433
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002434 /*
2435 * We currently don't do anything fancy with DMA
2436 * boundaries, but as we can't disable the feature
2437 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002438 *
2439 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2440 * should return a valid address to continue from, but as
2441 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002442 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002443 if (intmask & SDHCI_INT_DMA_END) {
2444 u32 dmastart, dmanow;
2445 dmastart = sg_dma_address(host->data->sg);
2446 dmanow = dmastart + host->data->bytes_xfered;
2447 /*
2448 * Force update to the next DMA block boundary.
2449 */
2450 dmanow = (dmanow &
2451 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2452 SDHCI_DEFAULT_BOUNDARY_SIZE;
2453 host->data->bytes_xfered = dmanow - dmastart;
2454 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2455 " next 0x%08x\n",
2456 mmc_hostname(host->mmc), dmastart,
2457 host->data->bytes_xfered, dmanow);
2458 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2459 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002460
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002461 if (intmask & SDHCI_INT_DATA_END) {
2462 if (host->cmd) {
2463 /*
2464 * Data managed to finish before the
2465 * command completed. Make sure we do
2466 * things in the proper order.
2467 */
2468 host->data_early = 1;
2469 } else {
2470 sdhci_finish_data(host);
2471 }
2472 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002473 }
2474}
2475
David Howells7d12e782006-10-05 14:55:46 +01002476static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002477{
Russell King781e9892014-04-25 12:55:46 +01002478 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002479 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002480 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002481 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002482
2483 spin_lock(&host->lock);
2484
Russell Kingbe138552014-04-25 12:55:56 +01002485 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002486 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002487 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002488 }
2489
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002490 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002491 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002492 result = IRQ_NONE;
2493 goto out;
2494 }
2495
Russell King41005002014-04-25 12:55:36 +01002496 do {
2497 /* Clear selected interrupts. */
2498 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2499 SDHCI_INT_BUS_POWER);
2500 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002501
Russell King41005002014-04-25 12:55:36 +01002502 DBG("*** %s got interrupt: 0x%08x\n",
2503 mmc_hostname(host->mmc), intmask);
2504
2505 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2506 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2507 SDHCI_CARD_PRESENT;
2508
2509 /*
2510 * There is a observation on i.mx esdhc. INSERT
2511 * bit will be immediately set again when it gets
2512 * cleared, if a card is inserted. We have to mask
2513 * the irq to prevent interrupt storm which will
2514 * freeze the system. And the REMOVE gets the
2515 * same situation.
2516 *
2517 * More testing are needed here to ensure it works
2518 * for other platforms though.
2519 */
Russell Kingb537f942014-04-25 12:56:01 +01002520 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2521 SDHCI_INT_CARD_REMOVE);
2522 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2523 SDHCI_INT_CARD_INSERT;
2524 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2525 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002526
2527 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2528 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002529
2530 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2531 SDHCI_INT_CARD_REMOVE);
2532 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002533 }
2534
2535 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002536 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2537 &intmask);
Russell King41005002014-04-25 12:55:36 +01002538
2539 if (intmask & SDHCI_INT_DATA_MASK)
2540 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2541
2542 if (intmask & SDHCI_INT_BUS_POWER)
2543 pr_err("%s: Card is consuming too much power!\n",
2544 mmc_hostname(host->mmc));
2545
Russell King781e9892014-04-25 12:55:46 +01002546 if (intmask & SDHCI_INT_CARD_INT) {
2547 sdhci_enable_sdio_irq_nolock(host, false);
2548 host->thread_isr |= SDHCI_INT_CARD_INT;
2549 result = IRQ_WAKE_THREAD;
2550 }
Russell King41005002014-04-25 12:55:36 +01002551
2552 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2553 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2554 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2555 SDHCI_INT_CARD_INT);
2556
2557 if (intmask) {
2558 unexpected |= intmask;
2559 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2560 }
2561
Russell King781e9892014-04-25 12:55:46 +01002562 if (result == IRQ_NONE)
2563 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002564
2565 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002566 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002567out:
2568 spin_unlock(&host->lock);
2569
Alexander Stein6379b232012-03-14 09:52:10 +01002570 if (unexpected) {
2571 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2572 mmc_hostname(host->mmc), unexpected);
2573 sdhci_dumpregs(host);
2574 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002575
Pierre Ossmand129bce2006-03-24 03:18:17 -08002576 return result;
2577}
2578
Russell King781e9892014-04-25 12:55:46 +01002579static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2580{
2581 struct sdhci_host *host = dev_id;
2582 unsigned long flags;
2583 u32 isr;
2584
2585 spin_lock_irqsave(&host->lock, flags);
2586 isr = host->thread_isr;
2587 host->thread_isr = 0;
2588 spin_unlock_irqrestore(&host->lock, flags);
2589
Russell King3560db82014-04-25 12:55:51 +01002590 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2591 sdhci_card_event(host->mmc);
2592 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2593 }
2594
Russell King781e9892014-04-25 12:55:46 +01002595 if (isr & SDHCI_INT_CARD_INT) {
2596 sdio_run_irqs(host->mmc);
2597
2598 spin_lock_irqsave(&host->lock, flags);
2599 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2600 sdhci_enable_sdio_irq_nolock(host, true);
2601 spin_unlock_irqrestore(&host->lock, flags);
2602 }
2603
2604 return isr ? IRQ_HANDLED : IRQ_NONE;
2605}
2606
Pierre Ossmand129bce2006-03-24 03:18:17 -08002607/*****************************************************************************\
2608 * *
2609 * Suspend/resume *
2610 * *
2611\*****************************************************************************/
2612
2613#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002614void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2615{
2616 u8 val;
2617 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2618 | SDHCI_WAKE_ON_INT;
2619
2620 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2621 val |= mask ;
2622 /* Avoid fake wake up */
2623 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2624 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2625 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2626}
2627EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2628
Fabio Estevam0b10f472014-08-30 14:53:13 -03002629static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002630{
2631 u8 val;
2632 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2633 | SDHCI_WAKE_ON_INT;
2634
2635 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2636 val &= ~mask;
2637 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2638}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002639
Manuel Lauss29495aa2011-11-03 11:09:45 +01002640int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002641{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002642 sdhci_disable_card_detection(host);
2643
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002644 mmc_retune_timer_stop(host->mmc);
2645 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302646
Kevin Liuad080d72013-01-05 17:21:33 +08002647 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002648 host->ier = 0;
2649 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2650 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002651 free_irq(host->irq, host);
2652 } else {
2653 sdhci_enable_irq_wakeups(host);
2654 enable_irq_wake(host->irq);
2655 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002656 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002657}
2658
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002659EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002660
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002661int sdhci_resume_host(struct sdhci_host *host)
2662{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002663 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002664
Richard Röjforsa13abc72009-09-22 16:45:30 -07002665 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002666 if (host->ops->enable_dma)
2667 host->ops->enable_dma(host);
2668 }
2669
Adrian Hunter6308d292012-02-07 14:48:54 +02002670 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2671 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2672 /* Card keeps power but host controller does not */
2673 sdhci_init(host, 0);
2674 host->pwr = 0;
2675 host->clock = 0;
2676 sdhci_do_set_ios(host, &host->mmc->ios);
2677 } else {
2678 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2679 mmiowb();
2680 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002681
Haibo Chen14a7b41642015-09-15 18:32:58 +08002682 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2683 ret = request_threaded_irq(host->irq, sdhci_irq,
2684 sdhci_thread_irq, IRQF_SHARED,
2685 mmc_hostname(host->mmc), host);
2686 if (ret)
2687 return ret;
2688 } else {
2689 sdhci_disable_irq_wakeups(host);
2690 disable_irq_wake(host->irq);
2691 }
2692
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002693 sdhci_enable_card_detection(host);
2694
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002695 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002696}
2697
2698EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002699
2700static int sdhci_runtime_pm_get(struct sdhci_host *host)
2701{
2702 return pm_runtime_get_sync(host->mmc->parent);
2703}
2704
2705static int sdhci_runtime_pm_put(struct sdhci_host *host)
2706{
2707 pm_runtime_mark_last_busy(host->mmc->parent);
2708 return pm_runtime_put_autosuspend(host->mmc->parent);
2709}
2710
Adrian Hunterf0710a52013-05-06 12:17:32 +03002711static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2712{
Adrian Hunter5c671c42015-11-26 14:00:50 +02002713 if (host->bus_on)
Adrian Hunterf0710a52013-05-06 12:17:32 +03002714 return;
2715 host->bus_on = true;
2716 pm_runtime_get_noresume(host->mmc->parent);
2717}
2718
2719static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2720{
Adrian Hunter5c671c42015-11-26 14:00:50 +02002721 if (!host->bus_on)
Adrian Hunterf0710a52013-05-06 12:17:32 +03002722 return;
2723 host->bus_on = false;
2724 pm_runtime_put_noidle(host->mmc->parent);
2725}
2726
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002727int sdhci_runtime_suspend_host(struct sdhci_host *host)
2728{
2729 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002730
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002731 mmc_retune_timer_stop(host->mmc);
2732 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002733
2734 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002735 host->ier &= SDHCI_INT_CARD_INT;
2736 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2737 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002738 spin_unlock_irqrestore(&host->lock, flags);
2739
Russell King781e9892014-04-25 12:55:46 +01002740 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002741
2742 spin_lock_irqsave(&host->lock, flags);
2743 host->runtime_suspended = true;
2744 spin_unlock_irqrestore(&host->lock, flags);
2745
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002746 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002747}
2748EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2749
2750int sdhci_runtime_resume_host(struct sdhci_host *host)
2751{
2752 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002753 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002754
2755 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2756 if (host->ops->enable_dma)
2757 host->ops->enable_dma(host);
2758 }
2759
2760 sdhci_init(host, 0);
2761
2762 /* Force clock and power re-program */
2763 host->pwr = 0;
2764 host->clock = 0;
Jisheng Zhang3396e732015-01-29 17:42:12 +08002765 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002766 sdhci_do_set_ios(host, &host->mmc->ios);
2767
Kevin Liu52983382013-01-31 11:31:37 +08002768 if ((host_flags & SDHCI_PV_ENABLED) &&
2769 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2770 spin_lock_irqsave(&host->lock, flags);
2771 sdhci_enable_preset_value(host, true);
2772 spin_unlock_irqrestore(&host->lock, flags);
2773 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002774
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002775 spin_lock_irqsave(&host->lock, flags);
2776
2777 host->runtime_suspended = false;
2778
2779 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002780 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002781 sdhci_enable_sdio_irq_nolock(host, true);
2782
2783 /* Enable Card Detection */
2784 sdhci_enable_card_detection(host);
2785
2786 spin_unlock_irqrestore(&host->lock, flags);
2787
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002788 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002789}
2790EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2791
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002792#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002793
Pierre Ossmand129bce2006-03-24 03:18:17 -08002794/*****************************************************************************\
2795 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002796 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002797 * *
2798\*****************************************************************************/
2799
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002800struct sdhci_host *sdhci_alloc_host(struct device *dev,
2801 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002802{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002803 struct mmc_host *mmc;
2804 struct sdhci_host *host;
2805
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002806 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002807
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002808 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002809 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002810 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002811
2812 host = mmc_priv(mmc);
2813 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002814 host->mmc_host_ops = sdhci_ops;
2815 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002816
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002817 return host;
2818}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002819
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002820EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002821
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002822int sdhci_add_host(struct sdhci_host *host)
2823{
2824 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002825 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302826 u32 max_current_caps;
2827 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002828 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08002829 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002830 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002831
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002832 WARN_ON(host == NULL);
2833 if (host == NULL)
2834 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002835
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002836 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002837
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002838 if (debug_quirks)
2839 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002840 if (debug_quirks2)
2841 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002842
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002843 override_timeout_clk = host->timeout_clk;
2844
Russell King03231f92014-04-25 12:57:12 +01002845 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002846
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002847 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002848 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2849 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002850 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002851 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
2852 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002853 }
2854
Arindam Nathf2119df2011-05-05 12:18:57 +05302855 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002856 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002857
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002858 if (host->version >= SDHCI_SPEC_300)
2859 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2860 host->caps1 :
2861 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302862
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002863 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002864 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302865 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002866 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002867 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002868 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002869
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002870 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002871 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002872 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002873 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002874 }
2875
Arindam Nathf2119df2011-05-05 12:18:57 +05302876 if ((host->version >= SDHCI_SPEC_200) &&
2877 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002878 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002879
2880 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2881 (host->flags & SDHCI_USE_ADMA)) {
2882 DBG("Disabling ADMA as it is marked broken\n");
2883 host->flags &= ~SDHCI_USE_ADMA;
2884 }
2885
Adrian Huntere57a5f62014-11-04 12:42:46 +02002886 /*
2887 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2888 * and *must* do 64-bit DMA. A driver has the opportunity to change
2889 * that during the first call to ->enable_dma(). Similarly
2890 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2891 * implement.
2892 */
Al Cooper5eaa7472016-02-10 15:25:39 -05002893 if (caps[0] & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02002894 host->flags |= SDHCI_USE_64_BIT_DMA;
2895
Richard Röjforsa13abc72009-09-22 16:45:30 -07002896 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002897 if (host->ops->enable_dma) {
2898 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07002899 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002900 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002901 host->flags &=
2902 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002903 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002904 }
2905 }
2906
Adrian Huntere57a5f62014-11-04 12:42:46 +02002907 /* SDMA does not support 64-bit DMA */
2908 if (host->flags & SDHCI_USE_64_BIT_DMA)
2909 host->flags &= ~SDHCI_USE_SDMA;
2910
Pierre Ossman2134a922008-06-28 18:28:51 +02002911 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00002912 dma_addr_t dma;
2913 void *buf;
2914
Pierre Ossman2134a922008-06-28 18:28:51 +02002915 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002916 * The DMA descriptor table size is calculated as the maximum
2917 * number of segments times 2, to allow for an alignment
2918 * descriptor for each segment, plus 1 for a nop end descriptor,
2919 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002920 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02002921 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2922 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2923 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002924 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002925 } else {
2926 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2927 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002928 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002929 }
Russell Kinge66e61c2016-01-26 13:39:55 +00002930
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002931 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00002932 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
2933 host->adma_table_sz, &dma, GFP_KERNEL);
2934 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07002935 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002936 mmc_hostname(mmc));
2937 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00002938 } else if ((dma + host->align_buffer_sz) &
2939 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07002940 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2941 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002942 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00002943 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
2944 host->adma_table_sz, buf, dma);
2945 } else {
2946 host->align_buffer = buf;
2947 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00002948
Russell Kinge66e61c2016-01-26 13:39:55 +00002949 host->adma_table = buf + host->align_buffer_sz;
2950 host->adma_addr = dma + host->align_buffer_sz;
2951 }
Pierre Ossman2134a922008-06-28 18:28:51 +02002952 }
2953
Pierre Ossman76591502008-07-21 00:32:11 +02002954 /*
2955 * If we use DMA, then it's up to the caller to set the DMA
2956 * mask, but PIO does not need the hw shim so we set a new
2957 * mask here in that case.
2958 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002959 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002960 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07002961 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02002962 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002963
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002964 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302965 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002966 >> SDHCI_CLOCK_BASE_SHIFT;
2967 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302968 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002969 >> SDHCI_CLOCK_BASE_SHIFT;
2970
Pierre Ossmand129bce2006-03-24 03:18:17 -08002971 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002972 if (host->max_clk == 0 || host->quirks &
2973 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002974 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002975 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
2976 mmc_hostname(mmc));
Ben Dooks4240ff02009-03-17 00:13:57 +03002977 return -ENODEV;
2978 }
2979 host->max_clk = host->ops->get_max_clock(host);
2980 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002981
2982 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302983 * In case of Host Controller v3.00, find out whether clock
2984 * multiplier is supported.
2985 */
2986 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2987 SDHCI_CLOCK_MUL_SHIFT;
2988
2989 /*
2990 * In case the value in Clock Multiplier is 0, then programmable
2991 * clock mode is not supported, otherwise the actual clock
2992 * multiplier is one more than the value of Clock Multiplier
2993 * in the Capabilities Register.
2994 */
2995 if (host->clk_mul)
2996 host->clk_mul += 1;
2997
2998 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002999 * Set host parameters.
3000 */
Dong Aisheng59241752015-07-22 20:53:07 +08003001 max_clk = host->max_clk;
3002
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003003 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003004 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303005 else if (host->version >= SDHCI_SPEC_300) {
3006 if (host->clk_mul) {
3007 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003008 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303009 } else
3010 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3011 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003012 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003013
Dong Aisheng59241752015-07-22 20:53:07 +08003014 if (!mmc->f_max || (mmc->f_max && (mmc->f_max > max_clk)))
3015 mmc->f_max = max_clk;
3016
Aisheng Dong28aab052014-08-27 15:26:31 +08003017 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3018 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3019 SDHCI_TIMEOUT_CLK_SHIFT;
3020 if (host->timeout_clk == 0) {
3021 if (host->ops->get_timeout_clock) {
3022 host->timeout_clk =
3023 host->ops->get_timeout_clock(host);
3024 } else {
3025 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3026 mmc_hostname(mmc));
3027 return -ENODEV;
3028 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003029 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003030
Aisheng Dong28aab052014-08-27 15:26:31 +08003031 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3032 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003033
Aisheng Dong28aab052014-08-27 15:26:31 +08003034 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003035 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003036 mmc->max_busy_timeout /= host->timeout_clk;
3037 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003038
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003039 if (override_timeout_clk)
3040 host->timeout_clk = override_timeout_clk;
3041
Andrei Warkentine89d4562011-05-23 15:06:37 -05003042 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003043 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003044
3045 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3046 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003047
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003048 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003049 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003050 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003051 !(host->flags & SDHCI_USE_SDMA)) &&
3052 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003053 host->flags |= SDHCI_AUTO_CMD23;
3054 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3055 } else {
3056 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3057 }
3058
Philip Rakity15ec4462010-11-19 16:48:39 -05003059 /*
3060 * A controller may support 8-bit width, but the board itself
3061 * might not have the pins brought out. Boards that support
3062 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3063 * their platform code before calling sdhci_add_host(), and we
3064 * won't assume 8-bit width for hosts without that CAP.
3065 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003066 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003067 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003068
Jerry Huang63ef5d82012-10-25 13:47:19 +08003069 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3070 mmc->caps &= ~MMC_CAP_CMD23;
3071
Arindam Nathf2119df2011-05-05 12:18:57 +05303072 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003073 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003074
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003075 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Ivan T. Ivanovc31d22e2015-07-06 15:16:20 +03003076 !(mmc->caps & MMC_CAP_NONREMOVABLE) &&
3077 IS_ERR_VALUE(mmc_gpio_get_cd(host->mmc)))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003078 mmc->caps |= MMC_CAP_NEEDS_POLL;
3079
Tim Kryger3a48edc2014-06-13 10:13:56 -07003080 /* If there are external regulators, get them */
3081 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3082 return -EPROBE_DEFER;
3083
Philip Rakity6231f3d2012-07-23 15:56:23 -07003084 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003085 if (!IS_ERR(mmc->supply.vqmmc)) {
3086 ret = regulator_enable(mmc->supply.vqmmc);
3087 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3088 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003089 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3090 SDHCI_SUPPORT_SDR50 |
3091 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003092 if (ret) {
3093 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3094 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003095 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003096 }
Kevin Liu8363c372012-11-17 17:55:51 -05003097 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003098
Daniel Drake6a661802012-11-25 13:01:19 -05003099 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3100 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3101 SDHCI_SUPPORT_DDR50);
3102
Al Cooper4188bba2012-03-16 15:54:17 -04003103 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3104 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3105 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303106 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3107
3108 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003109 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303110 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003111 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3112 * field can be promoted to support HS200.
3113 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003114 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003115 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003116 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303117 mmc->caps |= MMC_CAP_UHS_SDR50;
3118
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003119 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3120 (caps[1] & SDHCI_SUPPORT_HS400))
3121 mmc->caps2 |= MMC_CAP2_HS400;
3122
Adrian Hunter549c0b12014-11-06 15:19:05 +02003123 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3124 (IS_ERR(mmc->supply.vqmmc) ||
3125 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3126 1300000)))
3127 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3128
Micky Ching9107ebb2014-02-21 18:40:35 +08003129 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3130 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303131 mmc->caps |= MMC_CAP_UHS_DDR50;
3132
Girish K S069c9f12012-01-06 09:56:39 +05303133 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303134 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3135 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3136
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003137 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303138 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003139 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303140
Arindam Nathd6d50a12011-05-05 12:18:59 +05303141 /* Driver Type(s) (A, C, D) supported by the host */
3142 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3143 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3144 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3145 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3146 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3147 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3148
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303149 /* Initial value for re-tuning timer count */
3150 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3151 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3152
3153 /*
3154 * In case Re-tuning Timer is not disabled, the actual value of
3155 * re-tuning timer will be 2 ^ (n - 1).
3156 */
3157 if (host->tuning_count)
3158 host->tuning_count = 1 << (host->tuning_count - 1);
3159
3160 /* Re-tuning mode supported by the Host Controller */
3161 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3162 SDHCI_RETUNING_MODE_SHIFT;
3163
Takashi Iwai8f230f42010-12-08 10:04:30 +01003164 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003165
Arindam Nathf2119df2011-05-05 12:18:57 +05303166 /*
3167 * According to SD Host Controller spec v3.00, if the Host System
3168 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3169 * the value is meaningful only if Voltage Support in the Capabilities
3170 * register is set. The actual current value is 4 times the register
3171 * value.
3172 */
3173 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003174 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003175 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003176 if (curr > 0) {
3177
3178 /* convert to SDHCI_MAX_CURRENT format */
3179 curr = curr/1000; /* convert to mA */
3180 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3181
3182 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3183 max_current_caps =
3184 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3185 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3186 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3187 }
3188 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303189
3190 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003191 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303192
Aaron Lu55c46652012-07-04 13:31:48 +08003193 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303194 SDHCI_MAX_CURRENT_330_MASK) >>
3195 SDHCI_MAX_CURRENT_330_SHIFT) *
3196 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303197 }
3198 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003199 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303200
Aaron Lu55c46652012-07-04 13:31:48 +08003201 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303202 SDHCI_MAX_CURRENT_300_MASK) >>
3203 SDHCI_MAX_CURRENT_300_SHIFT) *
3204 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303205 }
3206 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003207 ocr_avail |= MMC_VDD_165_195;
3208
Aaron Lu55c46652012-07-04 13:31:48 +08003209 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303210 SDHCI_MAX_CURRENT_180_MASK) >>
3211 SDHCI_MAX_CURRENT_180_SHIFT) *
3212 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303213 }
3214
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003215 /* If OCR set by host, use it instead. */
3216 if (host->ocr_mask)
3217 ocr_avail = host->ocr_mask;
3218
3219 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003220 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003221 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003222
Takashi Iwai8f230f42010-12-08 10:04:30 +01003223 mmc->ocr_avail = ocr_avail;
3224 mmc->ocr_avail_sdio = ocr_avail;
3225 if (host->ocr_avail_sdio)
3226 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3227 mmc->ocr_avail_sd = ocr_avail;
3228 if (host->ocr_avail_sd)
3229 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3230 else /* normal SD controllers don't support 1.8V */
3231 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3232 mmc->ocr_avail_mmc = ocr_avail;
3233 if (host->ocr_avail_mmc)
3234 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003235
3236 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003237 pr_err("%s: Hardware doesn't report any support voltages.\n",
3238 mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003239 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003240 }
3241
Pierre Ossmand129bce2006-03-24 03:18:17 -08003242 spin_lock_init(&host->lock);
3243
3244 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003245 * Maximum number of segments. Depends on if the hardware
3246 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003247 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003248 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003249 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003250 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003251 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003252 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003253 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003254
3255 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003256 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3257 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3258 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003259 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003260 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003261
3262 /*
3263 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003264 * of bytes. When doing hardware scatter/gather, each entry cannot
3265 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003266 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003267 if (host->flags & SDHCI_USE_ADMA) {
3268 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3269 mmc->max_seg_size = 65535;
3270 else
3271 mmc->max_seg_size = 65536;
3272 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003273 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003274 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003275
3276 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003277 * Maximum block size. This varies from controller to controller and
3278 * is specified in the capabilities register.
3279 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003280 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3281 mmc->max_blk_size = 2;
3282 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303283 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003284 SDHCI_MAX_BLOCK_SHIFT;
3285 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003286 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3287 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003288 mmc->max_blk_size = 0;
3289 }
3290 }
3291
3292 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003293
3294 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003295 * Maximum block count.
3296 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003297 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003298
3299 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003300 * Init tasklets.
3301 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003302 tasklet_init(&host->finish_tasklet,
3303 sdhci_tasklet_finish, (unsigned long)host);
3304
Al Viroe4cad1b2006-10-10 22:47:07 +01003305 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003306
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003307 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303308
Shawn Guo2af502c2013-07-05 14:38:55 +08003309 sdhci_init(host, 0);
3310
Russell King781e9892014-04-25 12:55:46 +01003311 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3312 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003313 if (ret) {
3314 pr_err("%s: Failed to request IRQ %d: %d\n",
3315 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003316 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003317 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003318
Pierre Ossmand129bce2006-03-24 03:18:17 -08003319#ifdef CONFIG_MMC_DEBUG
3320 sdhci_dumpregs(host);
3321#endif
3322
Pierre Ossmanf9134312008-12-21 17:01:48 +01003323#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003324 snprintf(host->led_name, sizeof(host->led_name),
3325 "%s::", mmc_hostname(mmc));
3326 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003327 host->led.brightness = LED_OFF;
3328 host->led.default_trigger = mmc_hostname(mmc);
3329 host->led.brightness_set = sdhci_led_control;
3330
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003331 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003332 if (ret) {
3333 pr_err("%s: Failed to register LED device: %d\n",
3334 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003335 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003336 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003337#endif
3338
Pierre Ossman5f25a662006-10-04 02:15:39 -07003339 mmiowb();
3340
Pierre Ossmand129bce2006-03-24 03:18:17 -08003341 mmc_add_host(mmc);
3342
Girish K Sa3c76eb2011-10-11 11:44:09 +05303343 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003344 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003345 (host->flags & SDHCI_USE_ADMA) ?
3346 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003347 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003348
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003349 sdhci_enable_card_detection(host);
3350
Pierre Ossmand129bce2006-03-24 03:18:17 -08003351 return 0;
3352
Pierre Ossmanf9134312008-12-21 17:01:48 +01003353#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003354reset:
Russell King03231f92014-04-25 12:57:12 +01003355 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003356 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3357 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003358 free_irq(host->irq, host);
3359#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003360untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003361 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003362
3363 return ret;
3364}
3365
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003366EXPORT_SYMBOL_GPL(sdhci_add_host);
3367
Pierre Ossman1e728592008-04-16 19:13:13 +02003368void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003369{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003370 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003371 unsigned long flags;
3372
3373 if (dead) {
3374 spin_lock_irqsave(&host->lock, flags);
3375
3376 host->flags |= SDHCI_DEVICE_DEAD;
3377
3378 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303379 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003380 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003381
3382 host->mrq->cmd->error = -ENOMEDIUM;
3383 tasklet_schedule(&host->finish_tasklet);
3384 }
3385
3386 spin_unlock_irqrestore(&host->lock, flags);
3387 }
3388
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003389 sdhci_disable_card_detection(host);
3390
Markus Mayer4e743f12014-07-03 13:27:42 -07003391 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003392
Pierre Ossmanf9134312008-12-21 17:01:48 +01003393#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003394 led_classdev_unregister(&host->led);
3395#endif
3396
Pierre Ossman1e728592008-04-16 19:13:13 +02003397 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003398 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003399
Russell Kingb537f942014-04-25 12:56:01 +01003400 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3401 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003402 free_irq(host->irq, host);
3403
3404 del_timer_sync(&host->timer);
3405
Pierre Ossmand129bce2006-03-24 03:18:17 -08003406 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003407
Tim Kryger3a48edc2014-06-13 10:13:56 -07003408 if (!IS_ERR(mmc->supply.vqmmc))
3409 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003410
Russell Kingedd63fc2016-01-26 13:39:50 +00003411 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003412 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3413 host->adma_table_sz, host->align_buffer,
3414 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003415
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003416 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003417 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003418}
3419
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003420EXPORT_SYMBOL_GPL(sdhci_remove_host);
3421
3422void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003423{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003424 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003425}
3426
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003427EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003428
3429/*****************************************************************************\
3430 * *
3431 * Driver init/exit *
3432 * *
3433\*****************************************************************************/
3434
3435static int __init sdhci_drv_init(void)
3436{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303437 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003438 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303439 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003440
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003441 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003442}
3443
3444static void __exit sdhci_drv_exit(void)
3445{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003446}
3447
3448module_init(sdhci_drv_init);
3449module_exit(sdhci_drv_exit);
3450
Pierre Ossmandf673b22006-06-30 02:22:31 -07003451module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003452module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003453
Pierre Ossman32710e82009-04-08 20:14:54 +02003454MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003455MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003456MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003457
Pierre Ossmandf673b22006-06-30 02:22:31 -07003458MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003459MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");