blob: d622435d1bcc71e47e54cdb42cb847660b009a55 [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);
Haibo Chen348487c2014-12-09 17:04:05 +080056static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Haibo Chend31911b2015-08-25 10:02:11 +080057 struct mmc_data *data);
Scott Branden04e079c2015-03-10 11:35:10 -070058static int sdhci_do_get_cd(struct sdhci_host *host);
Pierre Ossmand129bce2006-03-24 03:18:17 -080059
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +010060#ifdef CONFIG_PM
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030061static int sdhci_runtime_pm_get(struct sdhci_host *host);
62static int sdhci_runtime_pm_put(struct sdhci_host *host);
Adrian Hunterf0710a52013-05-06 12:17:32 +030063static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
64static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030065#else
66static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
67{
68 return 0;
69}
70static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
71{
72 return 0;
73}
Adrian Hunterf0710a52013-05-06 12:17:32 +030074static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
75{
76}
77static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
78{
79}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030080#endif
81
Pierre Ossmand129bce2006-03-24 03:18:17 -080082static void sdhci_dumpregs(struct sdhci_host *host)
83{
Girish K Sa3c76eb2011-10-11 11:44:09 +053084 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070085 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080086
Girish K Sa3c76eb2011-10-11 11:44:09 +053087 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030088 sdhci_readl(host, SDHCI_DMA_ADDRESS),
89 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053090 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030091 sdhci_readw(host, SDHCI_BLOCK_SIZE),
92 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053093 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030094 sdhci_readl(host, SDHCI_ARGUMENT),
95 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053096 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030097 sdhci_readl(host, SDHCI_PRESENT_STATE),
98 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053099 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300100 sdhci_readb(host, SDHCI_POWER_CONTROL),
101 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530102 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300103 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
104 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530105 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
107 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530108 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300109 sdhci_readl(host, SDHCI_INT_ENABLE),
110 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530111 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300112 sdhci_readw(host, SDHCI_ACMD12_ERR),
113 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530114 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300115 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500116 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530117 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500118 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300119 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530120 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530121 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800122
Adrian Huntere57a5f62014-11-04 12:42:46 +0200123 if (host->flags & SDHCI_USE_ADMA) {
124 if (host->flags & SDHCI_USE_64_BIT_DMA)
125 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
126 readl(host->ioaddr + SDHCI_ADMA_ERROR),
127 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
128 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
129 else
130 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
131 readl(host->ioaddr + SDHCI_ADMA_ERROR),
132 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
133 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100134
Girish K Sa3c76eb2011-10-11 11:44:09 +0530135 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800136}
137
138/*****************************************************************************\
139 * *
140 * Low level functions *
141 * *
142\*****************************************************************************/
143
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300144static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
145{
Russell King5b4f1f62014-04-25 12:57:02 +0100146 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300147
Adrian Hunterc79396c2011-12-27 15:48:42 +0200148 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100149 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300150 return;
151
Russell King5b4f1f62014-04-25 12:57:02 +0100152 if (enable) {
153 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
154 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800155
Russell King5b4f1f62014-04-25 12:57:02 +0100156 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
157 SDHCI_INT_CARD_INSERT;
158 } else {
159 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
160 }
Russell Kingb537f942014-04-25 12:56:01 +0100161
162 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
163 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300164}
165
166static void sdhci_enable_card_detection(struct sdhci_host *host)
167{
168 sdhci_set_card_detection(host, true);
169}
170
171static void sdhci_disable_card_detection(struct sdhci_host *host)
172{
173 sdhci_set_card_detection(host, false);
174}
175
Russell King03231f92014-04-25 12:57:12 +0100176void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800177{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700178 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800179
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300180 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800181
Adrian Hunterf0710a52013-05-06 12:17:32 +0300182 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800183 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300184 /* Reset-all turns off SD Bus Power */
185 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
186 sdhci_runtime_pm_bus_off(host);
187 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800188
Pierre Ossmane16514d82006-06-30 02:22:24 -0700189 /* Wait max 100 ms */
190 timeout = 100;
191
192 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300193 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700194 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530195 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700196 mmc_hostname(host->mmc), (int)mask);
197 sdhci_dumpregs(host);
198 return;
199 }
200 timeout--;
201 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800202 }
Russell King03231f92014-04-25 12:57:12 +0100203}
204EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300205
Russell King03231f92014-04-25 12:57:12 +0100206static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
207{
208 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Ivan T. Ivanov135b0a22015-07-06 15:16:21 +0300209 if (!sdhci_do_get_cd(host))
Russell King03231f92014-04-25 12:57:12 +0100210 return;
211 }
212
213 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800214
Russell Kingda91a8f2014-04-25 13:00:12 +0100215 if (mask & SDHCI_RESET_ALL) {
216 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
217 if (host->ops->enable_dma)
218 host->ops->enable_dma(host);
219 }
220
221 /* Resetting the controller clears many */
222 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800223 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800224}
225
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800226static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
227
228static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800229{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800230 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100231 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800232 else
Russell King03231f92014-04-25 12:57:12 +0100233 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800234
Russell Kingb537f942014-04-25 12:56:01 +0100235 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
236 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
237 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
238 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
239 SDHCI_INT_RESPONSE;
240
241 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
242 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800243
244 if (soft) {
245 /* force clock reconfiguration */
246 host->clock = 0;
247 sdhci_set_ios(host->mmc, &host->mmc->ios);
248 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300249}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800250
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300251static void sdhci_reinit(struct sdhci_host *host)
252{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800253 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300254 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800255}
256
257static void sdhci_activate_led(struct sdhci_host *host)
258{
259 u8 ctrl;
260
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300261 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800262 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300263 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800264}
265
266static void sdhci_deactivate_led(struct sdhci_host *host)
267{
268 u8 ctrl;
269
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300270 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800271 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300272 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273}
274
Pierre Ossmanf9134312008-12-21 17:01:48 +0100275#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100276static void sdhci_led_control(struct led_classdev *led,
277 enum led_brightness brightness)
278{
279 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
280 unsigned long flags;
281
282 spin_lock_irqsave(&host->lock, flags);
283
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300284 if (host->runtime_suspended)
285 goto out;
286
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100287 if (brightness == LED_OFF)
288 sdhci_deactivate_led(host);
289 else
290 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300291out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100292 spin_unlock_irqrestore(&host->lock, flags);
293}
294#endif
295
Pierre Ossmand129bce2006-03-24 03:18:17 -0800296/*****************************************************************************\
297 * *
298 * Core functions *
299 * *
300\*****************************************************************************/
301
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100302static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800303{
Pierre Ossman76591502008-07-21 00:32:11 +0200304 unsigned long flags;
305 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700306 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200307 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800308
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100309 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800310
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100311 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200312 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800313
Pierre Ossman76591502008-07-21 00:32:11 +0200314 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800315
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100316 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300317 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800318
Pierre Ossman76591502008-07-21 00:32:11 +0200319 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800320
Pierre Ossman76591502008-07-21 00:32:11 +0200321 blksize -= len;
322 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200323
Pierre Ossman76591502008-07-21 00:32:11 +0200324 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800325
Pierre Ossman76591502008-07-21 00:32:11 +0200326 while (len) {
327 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300328 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200329 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800330 }
Pierre Ossman76591502008-07-21 00:32:11 +0200331
332 *buf = scratch & 0xFF;
333
334 buf++;
335 scratch >>= 8;
336 chunk--;
337 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800338 }
339 }
Pierre Ossman76591502008-07-21 00:32:11 +0200340
341 sg_miter_stop(&host->sg_miter);
342
343 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100344}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800345
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100346static void sdhci_write_block_pio(struct sdhci_host *host)
347{
Pierre Ossman76591502008-07-21 00:32:11 +0200348 unsigned long flags;
349 size_t blksize, len, chunk;
350 u32 scratch;
351 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100352
353 DBG("PIO writing\n");
354
355 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200356 chunk = 0;
357 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358
Pierre Ossman76591502008-07-21 00:32:11 +0200359 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100360
361 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300362 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100363
Pierre Ossman76591502008-07-21 00:32:11 +0200364 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200365
Pierre Ossman76591502008-07-21 00:32:11 +0200366 blksize -= len;
367 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100368
Pierre Ossman76591502008-07-21 00:32:11 +0200369 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100370
Pierre Ossman76591502008-07-21 00:32:11 +0200371 while (len) {
372 scratch |= (u32)*buf << (chunk * 8);
373
374 buf++;
375 chunk++;
376 len--;
377
378 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300379 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200380 chunk = 0;
381 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100382 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100383 }
384 }
Pierre Ossman76591502008-07-21 00:32:11 +0200385
386 sg_miter_stop(&host->sg_miter);
387
388 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100389}
390
391static void sdhci_transfer_pio(struct sdhci_host *host)
392{
393 u32 mask;
394
395 BUG_ON(!host->data);
396
Pierre Ossman76591502008-07-21 00:32:11 +0200397 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100398 return;
399
400 if (host->data->flags & MMC_DATA_READ)
401 mask = SDHCI_DATA_AVAILABLE;
402 else
403 mask = SDHCI_SPACE_AVAILABLE;
404
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200405 /*
406 * Some controllers (JMicron JMB38x) mess up the buffer bits
407 * for transfers < 4 bytes. As long as it is just one block,
408 * we can ignore the bits.
409 */
410 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
411 (host->data->blocks == 1))
412 mask = ~0;
413
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300414 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300415 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
416 udelay(100);
417
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100418 if (host->data->flags & MMC_DATA_READ)
419 sdhci_read_block_pio(host);
420 else
421 sdhci_write_block_pio(host);
422
Pierre Ossman76591502008-07-21 00:32:11 +0200423 host->blocks--;
424 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100425 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100426 }
427
428 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800429}
430
Pierre Ossman2134a922008-06-28 18:28:51 +0200431static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
432{
433 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800434 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200435}
436
437static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
438{
Cong Wang482fce92011-11-27 13:27:00 +0800439 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200440 local_irq_restore(*flags);
441}
442
Adrian Huntere57a5f62014-11-04 12:42:46 +0200443static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
444 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800445{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200446 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800447
Adrian Huntere57a5f62014-11-04 12:42:46 +0200448 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200449 dma_desc->cmd = cpu_to_le16(cmd);
450 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200451 dma_desc->addr_lo = cpu_to_le32((u32)addr);
452
453 if (host->flags & SDHCI_USE_64_BIT_DMA)
454 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800455}
456
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200457static void sdhci_adma_mark_end(void *desc)
458{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200459 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200460
Adrian Huntere57a5f62014-11-04 12:42:46 +0200461 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200462 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200463}
464
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200465static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200466 struct mmc_data *data)
467{
468 int direction;
469
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200470 void *desc;
471 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200472 dma_addr_t addr;
473 dma_addr_t align_addr;
474 int len, offset;
475
476 struct scatterlist *sg;
477 int i;
478 char *buffer;
479 unsigned long flags;
480
481 /*
482 * The spec does not specify endianness of descriptor table.
483 * We currently guess that it is LE.
484 */
485
486 if (data->flags & MMC_DATA_READ)
487 direction = DMA_FROM_DEVICE;
488 else
489 direction = DMA_TO_DEVICE;
490
Pierre Ossman2134a922008-06-28 18:28:51 +0200491 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200492 host->align_buffer, host->align_buffer_sz, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700493 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200494 goto fail;
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200495 BUG_ON(host->align_addr & SDHCI_ADMA2_MASK);
Pierre Ossman2134a922008-06-28 18:28:51 +0200496
Haibo Chend31911b2015-08-25 10:02:11 +0800497 host->sg_count = sdhci_pre_dma_transfer(host, data);
Haibo Chen348487c2014-12-09 17:04:05 +0800498 if (host->sg_count < 0)
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200499 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200500
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200501 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200502 align = host->align_buffer;
503
504 align_addr = host->align_addr;
505
506 for_each_sg(data->sg, sg, host->sg_count, i) {
507 addr = sg_dma_address(sg);
508 len = sg_dma_len(sg);
509
510 /*
511 * The SDHCI specification states that ADMA
512 * addresses must be 32-bit aligned. If they
513 * aren't, then we use a bounce buffer for
514 * the (up to three) bytes that screw up the
515 * alignment.
516 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200517 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
518 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200519 if (offset) {
520 if (data->flags & MMC_DATA_WRITE) {
521 buffer = sdhci_kmap_atomic(sg, &flags);
522 memcpy(align, buffer, offset);
523 sdhci_kunmap_atomic(buffer, &flags);
524 }
525
Ben Dooks118cd172010-03-05 13:43:26 -0800526 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200527 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200528 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200529
530 BUG_ON(offset > 65536);
531
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200532 align += SDHCI_ADMA2_ALIGN;
533 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200534
Adrian Hunter76fe3792014-11-04 12:42:42 +0200535 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200536
537 addr += offset;
538 len -= offset;
539 }
540
Pierre Ossman2134a922008-06-28 18:28:51 +0200541 BUG_ON(len > 65536);
542
Adrian Hunter347ea322015-11-26 14:00:48 +0200543 if (len) {
544 /* tran, valid */
545 sdhci_adma_write_desc(host, desc, addr, len,
546 ADMA2_TRAN_VALID);
547 desc += host->desc_sz;
548 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200549
550 /*
551 * If this triggers then we have a calculation bug
552 * somewhere. :/
553 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200554 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200555 }
556
Thomas Abraham70764a92010-05-26 14:42:04 -0700557 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
558 /*
559 * Mark the last descriptor as the terminating descriptor
560 */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200561 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200562 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200563 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700564 }
565 } else {
566 /*
567 * Add a terminating entry.
568 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200569
Thomas Abraham70764a92010-05-26 14:42:04 -0700570 /* nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200571 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700572 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200573
574 /*
575 * Resync align buffer as we might have changed it.
576 */
577 if (data->flags & MMC_DATA_WRITE) {
578 dma_sync_single_for_device(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200579 host->align_addr, host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200580 }
581
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200582 return 0;
583
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200584unmap_align:
585 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200586 host->align_buffer_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200587fail:
588 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200589}
590
591static void sdhci_adma_table_post(struct sdhci_host *host,
592 struct mmc_data *data)
593{
594 int direction;
595
596 struct scatterlist *sg;
597 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200598 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200599 char *buffer;
600 unsigned long flags;
Russell Kingde0b65a2014-04-25 12:58:29 +0100601 bool has_unaligned;
Pierre Ossman2134a922008-06-28 18:28:51 +0200602
603 if (data->flags & MMC_DATA_READ)
604 direction = DMA_FROM_DEVICE;
605 else
606 direction = DMA_TO_DEVICE;
607
Pierre Ossman2134a922008-06-28 18:28:51 +0200608 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200609 host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200610
Russell Kingde0b65a2014-04-25 12:58:29 +0100611 /* Do a quick scan of the SG list for any unaligned mappings */
612 has_unaligned = false;
613 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200614 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell Kingde0b65a2014-04-25 12:58:29 +0100615 has_unaligned = true;
616 break;
617 }
618
619 if (has_unaligned && data->flags & MMC_DATA_READ) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200620 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
621 data->sg_len, direction);
622
623 align = host->align_buffer;
624
625 for_each_sg(data->sg, sg, host->sg_count, i) {
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200626 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
627 size = SDHCI_ADMA2_ALIGN -
628 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
Pierre Ossman2134a922008-06-28 18:28:51 +0200629
630 buffer = sdhci_kmap_atomic(sg, &flags);
631 memcpy(buffer, align, size);
632 sdhci_kunmap_atomic(buffer, &flags);
633
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200634 align += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200635 }
636 }
637 }
638
Haibo Chend31911b2015-08-25 10:02:11 +0800639 if (data->host_cookie == COOKIE_MAPPED) {
Haibo Chen348487c2014-12-09 17:04:05 +0800640 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
641 data->sg_len, direction);
Haibo Chend31911b2015-08-25 10:02:11 +0800642 data->host_cookie = COOKIE_UNMAPPED;
643 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200644}
645
Andrei Warkentina3c77782011-04-11 16:13:42 -0500646static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800647{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700648 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500649 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700650 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800651
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200652 /*
653 * If the host controller provides us with an incorrect timeout
654 * value, just skip the check and use 0xE. The hardware may take
655 * longer to time out, but that's much better than having a too-short
656 * timeout value.
657 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200658 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200659 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200660
Andrei Warkentina3c77782011-04-11 16:13:42 -0500661 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100662 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500663 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800664
Andrei Warkentina3c77782011-04-11 16:13:42 -0500665 /* timeout in us */
666 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100667 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300668 else {
669 target_timeout = data->timeout_ns / 1000;
670 if (host->clock)
671 target_timeout += data->timeout_clks / host->clock;
672 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700673
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700674 /*
675 * Figure out needed cycles.
676 * We do this in steps in order to fit inside a 32 bit int.
677 * The first step is the minimum timeout, which will have a
678 * minimum resolution of 6 bits:
679 * (1) 2^13*1000 > 2^22,
680 * (2) host->timeout_clk < 2^16
681 * =>
682 * (1) / (2) > 2^6
683 */
684 count = 0;
685 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
686 while (current_timeout < target_timeout) {
687 count++;
688 current_timeout <<= 1;
689 if (count >= 0xF)
690 break;
691 }
692
693 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400694 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
695 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700696 count = 0xE;
697 }
698
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200699 return count;
700}
701
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300702static void sdhci_set_transfer_irqs(struct sdhci_host *host)
703{
704 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
705 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
706
707 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100708 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300709 else
Russell Kingb537f942014-04-25 12:56:01 +0100710 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
711
712 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
713 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300714}
715
Aisheng Dongb45e6682014-08-27 15:26:29 +0800716static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200717{
718 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800719
720 if (host->ops->set_timeout) {
721 host->ops->set_timeout(host, cmd);
722 } else {
723 count = sdhci_calc_timeout(host, cmd);
724 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
725 }
726}
727
728static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
729{
Pierre Ossman2134a922008-06-28 18:28:51 +0200730 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500731 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200732 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200733
734 WARN_ON(host->data);
735
Aisheng Dongb45e6682014-08-27 15:26:29 +0800736 if (data || (cmd->flags & MMC_RSP_BUSY))
737 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500738
739 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200740 return;
741
742 /* Sanity checks */
743 BUG_ON(data->blksz * data->blocks > 524288);
744 BUG_ON(data->blksz > host->mmc->max_blk_size);
745 BUG_ON(data->blocks > 65535);
746
747 host->data = data;
748 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400749 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200750
Richard Röjforsa13abc72009-09-22 16:45:30 -0700751 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100752 host->flags |= SDHCI_REQ_USE_DMA;
753
Pierre Ossman2134a922008-06-28 18:28:51 +0200754 /*
755 * FIXME: This doesn't account for merging when mapping the
756 * scatterlist.
757 */
758 if (host->flags & SDHCI_REQ_USE_DMA) {
759 int broken, i;
760 struct scatterlist *sg;
761
762 broken = 0;
763 if (host->flags & SDHCI_USE_ADMA) {
764 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
765 broken = 1;
766 } else {
767 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
768 broken = 1;
769 }
770
771 if (unlikely(broken)) {
772 for_each_sg(data->sg, sg, data->sg_len, i) {
773 if (sg->length & 0x3) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100774 DBG("Reverting to PIO because of transfer size (%d)\n",
Pierre Ossman2134a922008-06-28 18:28:51 +0200775 sg->length);
776 host->flags &= ~SDHCI_REQ_USE_DMA;
777 break;
778 }
779 }
780 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100781 }
782
783 /*
784 * The assumption here being that alignment is the same after
785 * translation to device address space.
786 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200787 if (host->flags & SDHCI_REQ_USE_DMA) {
788 int broken, i;
789 struct scatterlist *sg;
790
791 broken = 0;
792 if (host->flags & SDHCI_USE_ADMA) {
793 /*
794 * As we use 3 byte chunks to work around
795 * alignment problems, we need to check this
796 * quirk.
797 */
798 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
799 broken = 1;
800 } else {
801 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
802 broken = 1;
803 }
804
805 if (unlikely(broken)) {
806 for_each_sg(data->sg, sg, data->sg_len, i) {
807 if (sg->offset & 0x3) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100808 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200809 host->flags &= ~SDHCI_REQ_USE_DMA;
810 break;
811 }
812 }
813 }
814 }
815
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200816 if (host->flags & SDHCI_REQ_USE_DMA) {
817 if (host->flags & SDHCI_USE_ADMA) {
818 ret = sdhci_adma_table_pre(host, data);
819 if (ret) {
820 /*
821 * This only happens when someone fed
822 * us an invalid request.
823 */
824 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200825 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200826 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300827 sdhci_writel(host, host->adma_addr,
828 SDHCI_ADMA_ADDRESS);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200829 if (host->flags & SDHCI_USE_64_BIT_DMA)
830 sdhci_writel(host,
831 (u64)host->adma_addr >> 32,
832 SDHCI_ADMA_ADDRESS_HI);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200833 }
834 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300835 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200836
Haibo Chend31911b2015-08-25 10:02:11 +0800837 sg_cnt = sdhci_pre_dma_transfer(host, data);
Jiri Slaby62a7f362015-06-12 11:45:02 +0200838 if (sg_cnt <= 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200839 /*
840 * This only happens when someone fed
841 * us an invalid request.
842 */
843 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200844 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200845 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200846 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300847 sdhci_writel(host, sg_dma_address(data->sg),
848 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200849 }
850 }
851 }
852
Pierre Ossman2134a922008-06-28 18:28:51 +0200853 /*
854 * Always adjust the DMA selection as some controllers
855 * (e.g. JMicron) can't do PIO properly when the selection
856 * is ADMA.
857 */
858 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300859 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200860 ctrl &= ~SDHCI_CTRL_DMA_MASK;
861 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200862 (host->flags & SDHCI_USE_ADMA)) {
863 if (host->flags & SDHCI_USE_64_BIT_DMA)
864 ctrl |= SDHCI_CTRL_ADMA64;
865 else
866 ctrl |= SDHCI_CTRL_ADMA32;
867 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200868 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200869 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300870 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100871 }
872
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200873 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200874 int flags;
875
876 flags = SG_MITER_ATOMIC;
877 if (host->data->flags & MMC_DATA_READ)
878 flags |= SG_MITER_TO_SG;
879 else
880 flags |= SG_MITER_FROM_SG;
881 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200882 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800883 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700884
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300885 sdhci_set_transfer_irqs(host);
886
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400887 /* Set the DMA boundary value and block size */
888 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
889 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300890 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700891}
892
893static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500894 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700895{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800896 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500897 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700898
Dong Aisheng2b558c12013-10-30 22:09:48 +0800899 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800900 if (host->quirks2 &
901 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
902 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
903 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800904 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800905 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
906 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800907 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800908 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700909 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800910 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700911
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200912 WARN_ON(!host->data);
913
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800914 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
915 mode = SDHCI_TRNS_BLK_CNT_EN;
916
Andrei Warkentine89d4562011-05-23 15:06:37 -0500917 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800918 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500919 /*
920 * If we are sending CMD23, CMD12 never gets sent
921 * on successful completion (so no Auto-CMD12).
922 */
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800923 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
924 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500925 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500926 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
927 mode |= SDHCI_TRNS_AUTO_CMD23;
928 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
929 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700930 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500931
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700932 if (data->flags & MMC_DATA_READ)
933 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100934 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700935 mode |= SDHCI_TRNS_DMA;
936
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300937 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800938}
939
940static void sdhci_finish_data(struct sdhci_host *host)
941{
942 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800943
944 BUG_ON(!host->data);
945
946 data = host->data;
947 host->data = NULL;
948
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100949 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200950 if (host->flags & SDHCI_USE_ADMA)
951 sdhci_adma_table_post(host, data);
952 else {
Haibo Chend31911b2015-08-25 10:02:11 +0800953 if (data->host_cookie == COOKIE_MAPPED) {
Haibo Chen348487c2014-12-09 17:04:05 +0800954 dma_unmap_sg(mmc_dev(host->mmc),
955 data->sg, data->sg_len,
956 (data->flags & MMC_DATA_READ) ?
Pierre Ossman2134a922008-06-28 18:28:51 +0200957 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Haibo Chend31911b2015-08-25 10:02:11 +0800958 data->host_cookie = COOKIE_UNMAPPED;
959 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200960 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800961 }
962
963 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200964 * The specification states that the block count register must
965 * be updated, but it does not specify at what point in the
966 * data flow. That makes the register entirely useless to read
967 * back so we have to assume that nothing made it to the card
968 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800969 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200970 if (data->error)
971 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800972 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200973 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800974
Andrei Warkentine89d4562011-05-23 15:06:37 -0500975 /*
976 * Need to send CMD12 if -
977 * a) open-ended multiblock transfer (no CMD23)
978 * b) error in multiblock transfer
979 */
980 if (data->stop &&
981 (data->error ||
982 !host->mrq->sbc)) {
983
Pierre Ossmand129bce2006-03-24 03:18:17 -0800984 /*
985 * The controller needs a reset of internal state machines
986 * upon error conditions.
987 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200988 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100989 sdhci_do_reset(host, SDHCI_RESET_CMD);
990 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800991 }
992
993 sdhci_send_command(host, data->stop);
994 } else
995 tasklet_schedule(&host->finish_tasklet);
996}
997
Dong Aishengc0e551292013-09-13 19:11:31 +0800998void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800999{
1000 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001001 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001002 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001003
1004 WARN_ON(host->cmd);
1005
Pierre Ossmand129bce2006-03-24 03:18:17 -08001006 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001007 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001008
1009 mask = SDHCI_CMD_INHIBIT;
1010 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1011 mask |= SDHCI_DATA_INHIBIT;
1012
1013 /* We shouldn't wait for data inihibit for stop commands, even
1014 though they might use busy signaling */
1015 if (host->mrq->data && (cmd == host->mrq->data->stop))
1016 mask &= ~SDHCI_DATA_INHIBIT;
1017
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001018 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001019 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001020 pr_err("%s: Controller never released inhibit bit(s).\n",
1021 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001022 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001023 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001024 tasklet_schedule(&host->finish_tasklet);
1025 return;
1026 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001027 timeout--;
1028 mdelay(1);
1029 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001031 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001032 if (!cmd->data && cmd->busy_timeout > 9000)
1033 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001034 else
1035 timeout += 10 * HZ;
1036 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001037
1038 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001039 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001040
Andrei Warkentina3c77782011-04-11 16:13:42 -05001041 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001042
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001043 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001044
Andrei Warkentine89d4562011-05-23 15:06:37 -05001045 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001046
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301048 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001049 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001050 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001051 tasklet_schedule(&host->finish_tasklet);
1052 return;
1053 }
1054
1055 if (!(cmd->flags & MMC_RSP_PRESENT))
1056 flags = SDHCI_CMD_RESP_NONE;
1057 else if (cmd->flags & MMC_RSP_136)
1058 flags = SDHCI_CMD_RESP_LONG;
1059 else if (cmd->flags & MMC_RSP_BUSY)
1060 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1061 else
1062 flags = SDHCI_CMD_RESP_SHORT;
1063
1064 if (cmd->flags & MMC_RSP_CRC)
1065 flags |= SDHCI_CMD_CRC;
1066 if (cmd->flags & MMC_RSP_OPCODE)
1067 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301068
1069 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301070 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1071 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072 flags |= SDHCI_CMD_DATA;
1073
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001074 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001075}
Dong Aishengc0e551292013-09-13 19:11:31 +08001076EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001077
1078static void sdhci_finish_command(struct sdhci_host *host)
1079{
1080 int i;
1081
1082 BUG_ON(host->cmd == NULL);
1083
1084 if (host->cmd->flags & MMC_RSP_PRESENT) {
1085 if (host->cmd->flags & MMC_RSP_136) {
1086 /* CRC is stripped so we need to do some shifting. */
1087 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001088 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001089 SDHCI_RESPONSE + (3-i)*4) << 8;
1090 if (i != 3)
1091 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001092 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001093 SDHCI_RESPONSE + (3-i)*4-1);
1094 }
1095 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001096 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001097 }
1098 }
1099
Pierre Ossman17b04292007-07-22 22:18:46 +02001100 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001101
Andrei Warkentine89d4562011-05-23 15:06:37 -05001102 /* Finished CMD23, now send actual command. */
1103 if (host->cmd == host->mrq->sbc) {
1104 host->cmd = NULL;
1105 sdhci_send_command(host, host->mrq->cmd);
1106 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001107
Andrei Warkentine89d4562011-05-23 15:06:37 -05001108 /* Processed actual command. */
1109 if (host->data && host->data_early)
1110 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001111
Andrei Warkentine89d4562011-05-23 15:06:37 -05001112 if (!host->cmd->data)
1113 tasklet_schedule(&host->finish_tasklet);
1114
1115 host->cmd = NULL;
1116 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001117}
1118
Kevin Liu52983382013-01-31 11:31:37 +08001119static u16 sdhci_get_preset_value(struct sdhci_host *host)
1120{
Russell Kingd975f122014-04-25 12:59:31 +01001121 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001122
Russell Kingd975f122014-04-25 12:59:31 +01001123 switch (host->timing) {
1124 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001125 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1126 break;
Russell Kingd975f122014-04-25 12:59:31 +01001127 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001128 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1129 break;
Russell Kingd975f122014-04-25 12:59:31 +01001130 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001131 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1132 break;
Russell Kingd975f122014-04-25 12:59:31 +01001133 case MMC_TIMING_UHS_SDR104:
1134 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001135 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1136 break;
Russell Kingd975f122014-04-25 12:59:31 +01001137 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001138 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001139 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1140 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001141 case MMC_TIMING_MMC_HS400:
1142 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1143 break;
Kevin Liu52983382013-01-31 11:31:37 +08001144 default:
1145 pr_warn("%s: Invalid UHS-I mode selected\n",
1146 mmc_hostname(host->mmc));
1147 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1148 break;
1149 }
1150 return preset;
1151}
1152
Russell King17710592014-04-25 12:58:55 +01001153void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001154{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301155 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001156 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301157 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001158 unsigned long timeout;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001159 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001160
Russell King1650d0c2014-04-25 12:58:50 +01001161 host->mmc->actual_clock = 0;
1162
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001163 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
ludovic.desroches@atmel.comaf951762015-09-17 10:16:19 +02001164 if (host->quirks2 & SDHCI_QUIRK2_NEED_DELAY_AFTER_INT_CLK_RST)
1165 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001166
1167 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001168 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001169
Zhangfei Gao85105c52010-08-06 07:10:01 +08001170 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001171 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001172 u16 pre_val;
1173
1174 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1175 pre_val = sdhci_get_preset_value(host);
1176 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1177 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1178 if (host->clk_mul &&
1179 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1180 clk = SDHCI_PROG_CLOCK_MODE;
1181 real_div = div + 1;
1182 clk_mul = host->clk_mul;
1183 } else {
1184 real_div = max_t(int, 1, div << 1);
1185 }
1186 goto clock_set;
1187 }
1188
Arindam Nathc3ed3872011-05-05 12:19:06 +05301189 /*
1190 * Check if the Host Controller supports Programmable Clock
1191 * Mode.
1192 */
1193 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001194 for (div = 1; div <= 1024; div++) {
1195 if ((host->max_clk * host->clk_mul / div)
1196 <= clock)
1197 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001198 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001199 if ((host->max_clk * host->clk_mul / div) <= clock) {
1200 /*
1201 * Set Programmable Clock Mode in the Clock
1202 * Control register.
1203 */
1204 clk = SDHCI_PROG_CLOCK_MODE;
1205 real_div = div;
1206 clk_mul = host->clk_mul;
1207 div--;
1208 } else {
1209 /*
1210 * Divisor can be too small to reach clock
1211 * speed requirement. Then use the base clock.
1212 */
1213 switch_base_clk = true;
1214 }
1215 }
1216
1217 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301218 /* Version 3.00 divisors must be a multiple of 2. */
1219 if (host->max_clk <= clock)
1220 div = 1;
1221 else {
1222 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1223 div += 2) {
1224 if ((host->max_clk / div) <= clock)
1225 break;
1226 }
1227 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001228 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301229 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301230 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1231 && !div && host->max_clk <= 25000000)
1232 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001233 }
1234 } else {
1235 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001236 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001237 if ((host->max_clk / div) <= clock)
1238 break;
1239 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001240 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301241 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001242 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001243
Kevin Liu52983382013-01-31 11:31:37 +08001244clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001245 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001246 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301247 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001248 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1249 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001250 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001251 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001252
Chris Ball27f6cb12009-09-22 16:45:31 -07001253 /* Wait max 20 ms */
1254 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001255 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001256 & SDHCI_CLOCK_INT_STABLE)) {
1257 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001258 pr_err("%s: Internal clock never stabilised.\n",
1259 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001260 sdhci_dumpregs(host);
1261 return;
1262 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001263 timeout--;
1264 mdelay(1);
1265 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001266
1267 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001268 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001269}
Russell King17710592014-04-25 12:58:55 +01001270EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001271
Russell King24fbb3c2014-04-25 13:00:06 +01001272static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1273 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001274{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001275 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001276 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001277
Russell King24fbb3c2014-04-25 13:00:06 +01001278 if (mode != MMC_POWER_OFF) {
1279 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001280 case MMC_VDD_165_195:
1281 pwr = SDHCI_POWER_180;
1282 break;
1283 case MMC_VDD_29_30:
1284 case MMC_VDD_30_31:
1285 pwr = SDHCI_POWER_300;
1286 break;
1287 case MMC_VDD_32_33:
1288 case MMC_VDD_33_34:
1289 pwr = SDHCI_POWER_330;
1290 break;
1291 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001292 WARN(1, "%s: Invalid vdd %#x\n",
1293 mmc_hostname(host->mmc), vdd);
1294 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001295 }
1296 }
1297
1298 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001299 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001300
Pierre Ossmanae628902009-05-03 20:45:03 +02001301 host->pwr = pwr;
1302
1303 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001304 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001305 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1306 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001307 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001308 } else {
1309 /*
1310 * Spec says that we should clear the power reg before setting
1311 * a new value. Some controllers don't seem to like this though.
1312 */
1313 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1314 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001315
Russell Kinge921a8b2014-04-25 13:00:01 +01001316 /*
1317 * At least the Marvell CaFe chip gets confused if we set the
1318 * voltage and set turn on power at the same time, so set the
1319 * voltage first.
1320 */
1321 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1322 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001323
Russell Kinge921a8b2014-04-25 13:00:01 +01001324 pwr |= SDHCI_POWER_ON;
1325
Pierre Ossmanae628902009-05-03 20:45:03 +02001326 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1327
Russell Kinge921a8b2014-04-25 13:00:01 +01001328 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1329 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001330
Russell Kinge921a8b2014-04-25 13:00:01 +01001331 /*
1332 * Some controllers need an extra 10ms delay of 10ms before
1333 * they can apply clock after applying power
1334 */
1335 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1336 mdelay(10);
1337 }
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001338
1339 if (!IS_ERR(mmc->supply.vmmc)) {
1340 spin_unlock_irq(&host->lock);
1341 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1342 spin_lock_irq(&host->lock);
1343 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001344}
1345
Pierre Ossmand129bce2006-03-24 03:18:17 -08001346/*****************************************************************************\
1347 * *
1348 * MMC callbacks *
1349 * *
1350\*****************************************************************************/
1351
1352static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1353{
1354 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001355 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001356 unsigned long flags;
1357
1358 host = mmc_priv(mmc);
1359
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001360 sdhci_runtime_pm_get(host);
1361
Scott Branden04e079c2015-03-10 11:35:10 -07001362 /* Firstly check card presence */
1363 present = sdhci_do_get_cd(host);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001364
Pierre Ossmand129bce2006-03-24 03:18:17 -08001365 spin_lock_irqsave(&host->lock, flags);
1366
1367 WARN_ON(host->mrq != NULL);
1368
Pierre Ossmanf9134312008-12-21 17:01:48 +01001369#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001370 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001371#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001372
1373 /*
1374 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1375 * requests if Auto-CMD12 is enabled.
1376 */
1377 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001378 if (mrq->stop) {
1379 mrq->data->stop = NULL;
1380 mrq->stop = NULL;
1381 }
1382 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001383
1384 host->mrq = mrq;
1385
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001386 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001387 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001388 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301389 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001390 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001391 sdhci_send_command(host, mrq->sbc);
1392 else
1393 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301394 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001395
Pierre Ossman5f25a662006-10-04 02:15:39 -07001396 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001397 spin_unlock_irqrestore(&host->lock, flags);
1398}
1399
Russell King2317f562014-04-25 12:57:07 +01001400void sdhci_set_bus_width(struct sdhci_host *host, int width)
1401{
1402 u8 ctrl;
1403
1404 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1405 if (width == MMC_BUS_WIDTH_8) {
1406 ctrl &= ~SDHCI_CTRL_4BITBUS;
1407 if (host->version >= SDHCI_SPEC_300)
1408 ctrl |= SDHCI_CTRL_8BITBUS;
1409 } else {
1410 if (host->version >= SDHCI_SPEC_300)
1411 ctrl &= ~SDHCI_CTRL_8BITBUS;
1412 if (width == MMC_BUS_WIDTH_4)
1413 ctrl |= SDHCI_CTRL_4BITBUS;
1414 else
1415 ctrl &= ~SDHCI_CTRL_4BITBUS;
1416 }
1417 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1418}
1419EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1420
Russell King96d7b782014-04-25 12:59:26 +01001421void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1422{
1423 u16 ctrl_2;
1424
1425 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1426 /* Select Bus Speed Mode for host */
1427 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1428 if ((timing == MMC_TIMING_MMC_HS200) ||
1429 (timing == MMC_TIMING_UHS_SDR104))
1430 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1431 else if (timing == MMC_TIMING_UHS_SDR12)
1432 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1433 else if (timing == MMC_TIMING_UHS_SDR25)
1434 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1435 else if (timing == MMC_TIMING_UHS_SDR50)
1436 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1437 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1438 (timing == MMC_TIMING_MMC_DDR52))
1439 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001440 else if (timing == MMC_TIMING_MMC_HS400)
1441 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001442 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1443}
1444EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1445
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001446static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001447{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001448 unsigned long flags;
1449 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001450 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001451
Pierre Ossmand129bce2006-03-24 03:18:17 -08001452 spin_lock_irqsave(&host->lock, flags);
1453
Adrian Hunterceb61432011-12-27 15:48:41 +02001454 if (host->flags & SDHCI_DEVICE_DEAD) {
1455 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001456 if (!IS_ERR(mmc->supply.vmmc) &&
1457 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001458 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001459 return;
1460 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001461
Pierre Ossmand129bce2006-03-24 03:18:17 -08001462 /*
1463 * Reset the chip on each power off.
1464 * Should clear out any weird states.
1465 */
1466 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001467 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001468 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001469 }
1470
Kevin Liu52983382013-01-31 11:31:37 +08001471 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001472 (ios->power_mode == MMC_POWER_UP) &&
1473 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001474 sdhci_enable_preset_value(host, false);
1475
Russell King373073e2014-04-25 12:58:45 +01001476 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001477 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001478 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001479
1480 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1481 host->clock) {
1482 host->timeout_clk = host->mmc->actual_clock ?
1483 host->mmc->actual_clock / 1000 :
1484 host->clock / 1000;
1485 host->mmc->max_busy_timeout =
1486 host->ops->get_max_timeout_count ?
1487 host->ops->get_max_timeout_count(host) :
1488 1 << 27;
1489 host->mmc->max_busy_timeout /= host->timeout_clk;
1490 }
Russell King373073e2014-04-25 12:58:45 +01001491 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001492
Russell King24fbb3c2014-04-25 13:00:06 +01001493 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001494
Philip Rakity643a81f2010-09-23 08:24:32 -07001495 if (host->ops->platform_send_init_74_clocks)
1496 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1497
Russell King2317f562014-04-25 12:57:07 +01001498 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001499
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001500 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001501
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001502 if ((ios->timing == MMC_TIMING_SD_HS ||
1503 ios->timing == MMC_TIMING_MMC_HS)
1504 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001505 ctrl |= SDHCI_CTRL_HISPD;
1506 else
1507 ctrl &= ~SDHCI_CTRL_HISPD;
1508
Arindam Nathd6d50a12011-05-05 12:18:59 +05301509 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301510 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301511
1512 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001513 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1514 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001515 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301516 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301517 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1518 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001519 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301520 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301521
Russell Kingda91a8f2014-04-25 13:00:12 +01001522 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301523 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301524 /*
1525 * We only need to set Driver Strength if the
1526 * preset value enable is not set.
1527 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001528 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301529 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1530 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1531 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001532 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1533 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301534 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1535 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001536 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1537 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1538 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001539 pr_warn("%s: invalid driver type, default to driver type B\n",
1540 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001541 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1542 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301543
1544 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301545 } else {
1546 /*
1547 * According to SDHC Spec v3.00, if the Preset Value
1548 * Enable in the Host Control 2 register is set, we
1549 * need to reset SD Clock Enable before changing High
1550 * Speed Enable to avoid generating clock gliches.
1551 */
Arindam Nath758535c2011-05-05 12:19:00 +05301552
1553 /* Reset SD Clock Enable */
1554 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1555 clk &= ~SDHCI_CLOCK_CARD_EN;
1556 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1557
1558 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1559
1560 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001561 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301562 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301563
Arindam Nath49c468f2011-05-05 12:19:01 +05301564 /* Reset SD Clock Enable */
1565 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1566 clk &= ~SDHCI_CLOCK_CARD_EN;
1567 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1568
Russell King96d7b782014-04-25 12:59:26 +01001569 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001570 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301571
Kevin Liu52983382013-01-31 11:31:37 +08001572 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1573 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1574 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1575 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1576 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001577 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1578 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001579 u16 preset;
1580
1581 sdhci_enable_preset_value(host, true);
1582 preset = sdhci_get_preset_value(host);
1583 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1584 >> SDHCI_PRESET_DRV_SHIFT;
1585 }
1586
Arindam Nath49c468f2011-05-05 12:19:01 +05301587 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001588 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301589 } else
1590 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301591
Leandro Dorileob8352262007-07-25 23:47:04 +02001592 /*
1593 * Some (ENE) controllers go apeshit on some ios operation,
1594 * signalling timeout and CRC errors even on CMD0. Resetting
1595 * it on each ios seems to solve the problem.
1596 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301597 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001598 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001599
Pierre Ossman5f25a662006-10-04 02:15:39 -07001600 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001601 spin_unlock_irqrestore(&host->lock, flags);
1602}
1603
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001604static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1605{
1606 struct sdhci_host *host = mmc_priv(mmc);
1607
1608 sdhci_runtime_pm_get(host);
1609 sdhci_do_set_ios(host, ios);
1610 sdhci_runtime_pm_put(host);
1611}
1612
Kevin Liu94144a42013-02-28 17:35:53 +08001613static int sdhci_do_get_cd(struct sdhci_host *host)
1614{
1615 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1616
1617 if (host->flags & SDHCI_DEVICE_DEAD)
1618 return 0;
1619
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001620 /* If nonremovable, assume that the card is always present. */
1621 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
Kevin Liu94144a42013-02-28 17:35:53 +08001622 return 1;
1623
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001624 /*
1625 * Try slot gpio detect, if defined it take precedence
1626 * over build in controller functionality
1627 */
Kevin Liu94144a42013-02-28 17:35:53 +08001628 if (!IS_ERR_VALUE(gpio_cd))
1629 return !!gpio_cd;
1630
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001631 /* If polling, assume that the card is always present. */
1632 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1633 return 1;
1634
Kevin Liu94144a42013-02-28 17:35:53 +08001635 /* Host native card detect */
1636 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1637}
1638
1639static int sdhci_get_cd(struct mmc_host *mmc)
1640{
1641 struct sdhci_host *host = mmc_priv(mmc);
1642 int ret;
1643
1644 sdhci_runtime_pm_get(host);
1645 ret = sdhci_do_get_cd(host);
1646 sdhci_runtime_pm_put(host);
1647 return ret;
1648}
1649
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001650static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001651{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001652 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001653 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001654
Pierre Ossmand129bce2006-03-24 03:18:17 -08001655 spin_lock_irqsave(&host->lock, flags);
1656
Pierre Ossman1e728592008-04-16 19:13:13 +02001657 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001658 is_readonly = 0;
1659 else if (host->ops->get_ro)
1660 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001661 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001662 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1663 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001664
1665 spin_unlock_irqrestore(&host->lock, flags);
1666
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001667 /* This quirk needs to be replaced by a callback-function later */
1668 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1669 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001670}
1671
Takashi Iwai82b0e232011-04-21 20:26:38 +02001672#define SAMPLE_COUNT 5
1673
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001674static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001675{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001676 int i, ro_count;
1677
Takashi Iwai82b0e232011-04-21 20:26:38 +02001678 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001679 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001680
1681 ro_count = 0;
1682 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001683 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001684 if (++ro_count > SAMPLE_COUNT / 2)
1685 return 1;
1686 }
1687 msleep(30);
1688 }
1689 return 0;
1690}
1691
Adrian Hunter20758b62011-08-29 16:42:12 +03001692static void sdhci_hw_reset(struct mmc_host *mmc)
1693{
1694 struct sdhci_host *host = mmc_priv(mmc);
1695
1696 if (host->ops && host->ops->hw_reset)
1697 host->ops->hw_reset(host);
1698}
1699
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001700static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001701{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001702 struct sdhci_host *host = mmc_priv(mmc);
1703 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001704
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001705 sdhci_runtime_pm_get(host);
1706 ret = sdhci_do_get_ro(host);
1707 sdhci_runtime_pm_put(host);
1708 return ret;
1709}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001710
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001711static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1712{
Russell Kingbe138552014-04-25 12:55:56 +01001713 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001714 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001715 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001716 else
Russell Kingb537f942014-04-25 12:56:01 +01001717 host->ier &= ~SDHCI_INT_CARD_INT;
1718
1719 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1720 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001721 mmiowb();
1722 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001723}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001724
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001725static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1726{
1727 struct sdhci_host *host = mmc_priv(mmc);
1728 unsigned long flags;
1729
Russell Kingef104332014-04-25 12:55:41 +01001730 sdhci_runtime_pm_get(host);
1731
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001732 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001733 if (enable)
1734 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1735 else
1736 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1737
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001738 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001739 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001740
1741 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001742}
1743
Philip Rakity6231f3d2012-07-23 15:56:23 -07001744static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001745 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001746{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001747 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001748 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001749 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001750
1751 /*
1752 * Signal Voltage Switching is only applicable for Host Controllers
1753 * v3.00 and above.
1754 */
1755 if (host->version < SDHCI_SPEC_300)
1756 return 0;
1757
Philip Rakity6231f3d2012-07-23 15:56:23 -07001758 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001759
Fabio Estevam21f59982013-02-14 10:35:03 -02001760 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001761 case MMC_SIGNAL_VOLTAGE_330:
1762 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1763 ctrl &= ~SDHCI_CTRL_VDD_180;
1764 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1765
Tim Kryger3a48edc2014-06-13 10:13:56 -07001766 if (!IS_ERR(mmc->supply.vqmmc)) {
1767 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1768 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001769 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001770 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1771 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001772 return -EIO;
1773 }
1774 }
1775 /* Wait for 5ms */
1776 usleep_range(5000, 5500);
1777
1778 /* 3.3V regulator output should be stable within 5 ms */
1779 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1780 if (!(ctrl & SDHCI_CTRL_VDD_180))
1781 return 0;
1782
Joe Perches66061102014-09-12 14:56:56 -07001783 pr_warn("%s: 3.3V regulator output did not became stable\n",
1784 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001785
1786 return -EAGAIN;
1787 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001788 if (!IS_ERR(mmc->supply.vqmmc)) {
1789 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001790 1700000, 1950000);
1791 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001792 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1793 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001794 return -EIO;
1795 }
1796 }
1797
1798 /*
1799 * Enable 1.8V Signal Enable in the Host Control2
1800 * register
1801 */
1802 ctrl |= SDHCI_CTRL_VDD_180;
1803 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1804
Vincent Yang9d967a62015-01-20 16:05:15 +08001805 /* Some controller need to do more when switching */
1806 if (host->ops->voltage_switch)
1807 host->ops->voltage_switch(host);
1808
Kevin Liu20b92a32012-12-17 19:29:26 +08001809 /* 1.8V regulator output should be stable within 5 ms */
1810 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1811 if (ctrl & SDHCI_CTRL_VDD_180)
1812 return 0;
1813
Joe Perches66061102014-09-12 14:56:56 -07001814 pr_warn("%s: 1.8V regulator output did not became stable\n",
1815 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001816
1817 return -EAGAIN;
1818 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001819 if (!IS_ERR(mmc->supply.vqmmc)) {
1820 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1821 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001822 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001823 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1824 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001825 return -EIO;
1826 }
1827 }
1828 return 0;
1829 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301830 /* No signal voltage switch required */
1831 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001832 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301833}
1834
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001835static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001836 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001837{
1838 struct sdhci_host *host = mmc_priv(mmc);
1839 int err;
1840
1841 if (host->version < SDHCI_SPEC_300)
1842 return 0;
1843 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001844 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001845 sdhci_runtime_pm_put(host);
1846 return err;
1847}
1848
Kevin Liu20b92a32012-12-17 19:29:26 +08001849static int sdhci_card_busy(struct mmc_host *mmc)
1850{
1851 struct sdhci_host *host = mmc_priv(mmc);
1852 u32 present_state;
1853
1854 sdhci_runtime_pm_get(host);
1855 /* Check whether DAT[3:0] is 0000 */
1856 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1857 sdhci_runtime_pm_put(host);
1858
1859 return !(present_state & SDHCI_DATA_LVL_MASK);
1860}
1861
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001862static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1863{
1864 struct sdhci_host *host = mmc_priv(mmc);
1865 unsigned long flags;
1866
1867 spin_lock_irqsave(&host->lock, flags);
1868 host->flags |= SDHCI_HS400_TUNING;
1869 spin_unlock_irqrestore(&host->lock, flags);
1870
1871 return 0;
1872}
1873
Girish K S069c9f12012-01-06 09:56:39 +05301874static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301875{
Russell King4b6f37d2014-04-25 12:59:36 +01001876 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301877 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301878 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301879 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001880 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001881 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001882 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301883
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001884 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001885 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301886
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001887 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1888 host->flags &= ~SDHCI_HS400_TUNING;
1889
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001890 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1891 tuning_count = host->tuning_count;
1892
Arindam Nathb513ea22011-05-05 12:19:04 +05301893 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001894 * The Host Controller needs tuning in case of SDR104 and DDR50
1895 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1896 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301897 * If the Host Controller supports the HS200 mode then the
1898 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301899 */
Russell King4b6f37d2014-04-25 12:59:36 +01001900 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001901 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001902 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001903 err = -EINVAL;
1904 goto out_unlock;
1905
Russell King4b6f37d2014-04-25 12:59:36 +01001906 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001907 /*
1908 * Periodic re-tuning for HS400 is not expected to be needed, so
1909 * disable it here.
1910 */
1911 if (hs400_tuning)
1912 tuning_count = 0;
1913 break;
1914
Russell King4b6f37d2014-04-25 12:59:36 +01001915 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001916 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001917 break;
Girish K S069c9f12012-01-06 09:56:39 +05301918
Russell King4b6f37d2014-04-25 12:59:36 +01001919 case MMC_TIMING_UHS_SDR50:
1920 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1921 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1922 break;
1923 /* FALLTHROUGH */
1924
1925 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001926 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301927 }
1928
Dong Aisheng45251812013-09-13 19:11:30 +08001929 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001930 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001931 err = host->ops->platform_execute_tuning(host, opcode);
1932 sdhci_runtime_pm_put(host);
1933 return err;
1934 }
1935
Russell King4b6f37d2014-04-25 12:59:36 +01001936 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1937 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001938 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1939 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301940 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1941
1942 /*
1943 * As per the Host Controller spec v3.00, tuning command
1944 * generates Buffer Read Ready interrupt, so enable that.
1945 *
1946 * Note: The spec clearly says that when tuning sequence
1947 * is being performed, the controller does not generate
1948 * interrupts other than Buffer Read Ready interrupt. But
1949 * to make sure we don't hit a controller bug, we _only_
1950 * enable Buffer Read Ready interrupt here.
1951 */
Russell Kingb537f942014-04-25 12:56:01 +01001952 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1953 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301954
1955 /*
1956 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1957 * of loops reaches 40 times or a timeout of 150ms occurs.
1958 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301959 do {
1960 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001961 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301962
Girish K S069c9f12012-01-06 09:56:39 +05301963 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301964 cmd.arg = 0;
1965 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1966 cmd.retries = 0;
1967 cmd.data = NULL;
1968 cmd.error = 0;
1969
Al Cooper7ce45e92014-05-09 11:34:07 -04001970 if (tuning_loop_counter-- == 0)
1971 break;
1972
Arindam Nathb513ea22011-05-05 12:19:04 +05301973 mrq.cmd = &cmd;
1974 host->mrq = &mrq;
1975
1976 /*
1977 * In response to CMD19, the card sends 64 bytes of tuning
1978 * block to the Host Controller. So we set the block size
1979 * to 64 here.
1980 */
Girish K S069c9f12012-01-06 09:56:39 +05301981 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1982 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1983 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1984 SDHCI_BLOCK_SIZE);
1985 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1986 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1987 SDHCI_BLOCK_SIZE);
1988 } else {
1989 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1990 SDHCI_BLOCK_SIZE);
1991 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301992
1993 /*
1994 * The tuning block is sent by the card to the host controller.
1995 * So we set the TRNS_READ bit in the Transfer Mode register.
1996 * This also takes care of setting DMA Enable and Multi Block
1997 * Select in the same register to 0.
1998 */
1999 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2000
2001 sdhci_send_command(host, &cmd);
2002
2003 host->cmd = NULL;
2004 host->mrq = NULL;
2005
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002006 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302007 /* Wait for Buffer Read Ready interrupt */
2008 wait_event_interruptible_timeout(host->buf_ready_int,
2009 (host->tuning_done == 1),
2010 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002011 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302012
2013 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002014 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 +05302015 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2016 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2017 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2018 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2019
2020 err = -EIO;
2021 goto out;
2022 }
2023
2024 host->tuning_done = 0;
2025
2026 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07002027
2028 /* eMMC spec does not require a delay between tuning cycles */
2029 if (opcode == MMC_SEND_TUNING_BLOCK)
2030 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302031 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2032
2033 /*
2034 * The Host Driver has exhausted the maximum number of loops allowed,
2035 * so use fixed sampling frequency.
2036 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002037 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302038 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2039 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002040 }
2041 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002042 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002043 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302044 }
2045
2046out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002047 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002048 /*
2049 * In case tuning fails, host controllers which support
2050 * re-tuning can try tuning again at a later time, when the
2051 * re-tuning timer expires. So for these controllers, we
2052 * return 0. Since there might be other controllers who do not
2053 * have this capability, we return error for them.
2054 */
2055 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302056 }
2057
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002058 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302059
Russell Kingb537f942014-04-25 12:56:01 +01002060 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2061 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002062out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002063 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002064 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302065
2066 return err;
2067}
2068
Adrian Huntercb849642015-02-06 14:12:59 +02002069static int sdhci_select_drive_strength(struct mmc_card *card,
2070 unsigned int max_dtr, int host_drv,
2071 int card_drv, int *drv_type)
2072{
2073 struct sdhci_host *host = mmc_priv(card->host);
2074
2075 if (!host->ops->select_drive_strength)
2076 return 0;
2077
2078 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2079 card_drv, drv_type);
2080}
Kevin Liu52983382013-01-31 11:31:37 +08002081
2082static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302083{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302084 /* Host Controller v3.00 defines preset value registers */
2085 if (host->version < SDHCI_SPEC_300)
2086 return;
2087
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302088 /*
2089 * We only enable or disable Preset Value if they are not already
2090 * enabled or disabled respectively. Otherwise, we bail out.
2091 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002092 if (host->preset_enabled != enable) {
2093 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2094
2095 if (enable)
2096 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2097 else
2098 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2099
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302100 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002101
2102 if (enable)
2103 host->flags |= SDHCI_PV_ENABLED;
2104 else
2105 host->flags &= ~SDHCI_PV_ENABLED;
2106
2107 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302108 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002109}
2110
Haibo Chen348487c2014-12-09 17:04:05 +08002111static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2112 int err)
2113{
2114 struct sdhci_host *host = mmc_priv(mmc);
2115 struct mmc_data *data = mrq->data;
2116
2117 if (host->flags & SDHCI_REQ_USE_DMA) {
Haibo Chend31911b2015-08-25 10:02:11 +08002118 if (data->host_cookie == COOKIE_GIVEN ||
2119 data->host_cookie == COOKIE_MAPPED)
Haibo Chen348487c2014-12-09 17:04:05 +08002120 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2121 data->flags & MMC_DATA_WRITE ?
2122 DMA_TO_DEVICE : DMA_FROM_DEVICE);
Haibo Chend31911b2015-08-25 10:02:11 +08002123 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002124 }
2125}
2126
2127static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Haibo Chend31911b2015-08-25 10:02:11 +08002128 struct mmc_data *data)
Haibo Chen348487c2014-12-09 17:04:05 +08002129{
2130 int sg_count;
2131
Haibo Chend31911b2015-08-25 10:02:11 +08002132 if (data->host_cookie == COOKIE_MAPPED) {
2133 data->host_cookie = COOKIE_GIVEN;
2134 return data->sg_count;
Haibo Chen348487c2014-12-09 17:04:05 +08002135 }
2136
Haibo Chend31911b2015-08-25 10:02:11 +08002137 WARN_ON(data->host_cookie == COOKIE_GIVEN);
Haibo Chen348487c2014-12-09 17:04:05 +08002138
Haibo Chend31911b2015-08-25 10:02:11 +08002139 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2140 data->flags & MMC_DATA_WRITE ?
2141 DMA_TO_DEVICE : DMA_FROM_DEVICE);
Haibo Chen348487c2014-12-09 17:04:05 +08002142
2143 if (sg_count == 0)
Haibo Chend31911b2015-08-25 10:02:11 +08002144 return -ENOSPC;
Haibo Chen348487c2014-12-09 17:04:05 +08002145
Haibo Chend31911b2015-08-25 10:02:11 +08002146 data->sg_count = sg_count;
2147 data->host_cookie = COOKIE_MAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002148
2149 return sg_count;
2150}
2151
2152static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2153 bool is_first_req)
2154{
2155 struct sdhci_host *host = mmc_priv(mmc);
2156
Haibo Chend31911b2015-08-25 10:02:11 +08002157 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002158
2159 if (host->flags & SDHCI_REQ_USE_DMA)
Haibo Chend31911b2015-08-25 10:02:11 +08002160 sdhci_pre_dma_transfer(host, mrq->data);
Haibo Chen348487c2014-12-09 17:04:05 +08002161}
2162
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002163static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002164{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002165 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002166 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002167 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002168
Christian Daudt722e1282013-06-20 14:26:36 -07002169 /* First check if client has provided their own card event */
2170 if (host->ops->card_event)
2171 host->ops->card_event(host);
2172
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002173 present = sdhci_do_get_cd(host);
2174
Pierre Ossmand129bce2006-03-24 03:18:17 -08002175 spin_lock_irqsave(&host->lock, flags);
2176
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002177 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002178 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302179 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002180 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302181 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002182 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002183
Russell King03231f92014-04-25 12:57:12 +01002184 sdhci_do_reset(host, SDHCI_RESET_CMD);
2185 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002186
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002187 host->mrq->cmd->error = -ENOMEDIUM;
2188 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002189 }
2190
2191 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002192}
2193
2194static const struct mmc_host_ops sdhci_ops = {
2195 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002196 .post_req = sdhci_post_req,
2197 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002198 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002199 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002200 .get_ro = sdhci_get_ro,
2201 .hw_reset = sdhci_hw_reset,
2202 .enable_sdio_irq = sdhci_enable_sdio_irq,
2203 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002204 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002205 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002206 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002207 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002208 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002209};
2210
2211/*****************************************************************************\
2212 * *
2213 * Tasklets *
2214 * *
2215\*****************************************************************************/
2216
Pierre Ossmand129bce2006-03-24 03:18:17 -08002217static void sdhci_tasklet_finish(unsigned long param)
2218{
2219 struct sdhci_host *host;
2220 unsigned long flags;
2221 struct mmc_request *mrq;
2222
2223 host = (struct sdhci_host*)param;
2224
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002225 spin_lock_irqsave(&host->lock, flags);
2226
Chris Ball0c9c99a2011-04-27 17:35:31 -04002227 /*
2228 * If this tasklet gets rescheduled while running, it will
2229 * be run again afterwards but without any active request.
2230 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002231 if (!host->mrq) {
2232 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002233 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002234 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002235
2236 del_timer(&host->timer);
2237
2238 mrq = host->mrq;
2239
Pierre Ossmand129bce2006-03-24 03:18:17 -08002240 /*
2241 * The controller needs a reset of internal state machines
2242 * upon error conditions.
2243 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002244 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002245 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002246 (mrq->sbc && mrq->sbc->error) ||
2247 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2248 (mrq->data->stop && mrq->data->stop->error))) ||
2249 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002250
2251 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002252 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002253 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002254 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002255
2256 /* Spec says we should do both at the same time, but Ricoh
2257 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002258 sdhci_do_reset(host, SDHCI_RESET_CMD);
2259 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002260 }
2261
2262 host->mrq = NULL;
2263 host->cmd = NULL;
2264 host->data = NULL;
2265
Pierre Ossmanf9134312008-12-21 17:01:48 +01002266#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002267 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002268#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002269
Pierre Ossman5f25a662006-10-04 02:15:39 -07002270 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002271 spin_unlock_irqrestore(&host->lock, flags);
2272
2273 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002274 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002275}
2276
2277static void sdhci_timeout_timer(unsigned long data)
2278{
2279 struct sdhci_host *host;
2280 unsigned long flags;
2281
2282 host = (struct sdhci_host*)data;
2283
2284 spin_lock_irqsave(&host->lock, flags);
2285
2286 if (host->mrq) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002287 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2288 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002289 sdhci_dumpregs(host);
2290
2291 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002292 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002293 sdhci_finish_data(host);
2294 } else {
2295 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002296 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002297 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002298 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002299
2300 tasklet_schedule(&host->finish_tasklet);
2301 }
2302 }
2303
Pierre Ossman5f25a662006-10-04 02:15:39 -07002304 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002305 spin_unlock_irqrestore(&host->lock, flags);
2306}
2307
2308/*****************************************************************************\
2309 * *
2310 * Interrupt handling *
2311 * *
2312\*****************************************************************************/
2313
Adrian Hunter61541392014-09-24 10:27:27 +03002314static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002315{
2316 BUG_ON(intmask == 0);
2317
2318 if (!host->cmd) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002319 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2320 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002321 sdhci_dumpregs(host);
2322 return;
2323 }
2324
Pierre Ossman43b58b32007-07-25 23:15:27 +02002325 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002326 host->cmd->error = -ETIMEDOUT;
2327 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2328 SDHCI_INT_INDEX))
2329 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002330
Pierre Ossmane8095172008-07-25 01:09:08 +02002331 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002332 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002333 return;
2334 }
2335
2336 /*
2337 * The host can send and interrupt when the busy state has
2338 * ended, allowing us to wait without wasting CPU cycles.
2339 * Unfortunately this is overloaded on the "data complete"
2340 * interrupt, so we need to take some care when handling
2341 * it.
2342 *
2343 * Note: The 1.0 specification is a bit ambiguous about this
2344 * feature so there might be some problems with older
2345 * controllers.
2346 */
2347 if (host->cmd->flags & MMC_RSP_BUSY) {
2348 if (host->cmd->data)
Marek Vasut2e4456f2015-11-18 10:47:02 +01002349 DBG("Cannot wait for busy signal when also doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002350 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2351 && !host->busy_handle) {
2352 /* Mark that command complete before busy is ended */
2353 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002354 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002355 }
Ben Dooksf9454052009-02-20 20:33:08 +03002356
2357 /* The controller does not support the end-of-busy IRQ,
2358 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002359 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2360 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2361 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002362 }
2363
2364 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002365 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002366}
2367
George G. Davis0957c332010-02-18 12:32:12 -05002368#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002369static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002370{
2371 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002372 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002373
2374 sdhci_dumpregs(host);
2375
2376 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002377 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002378
Adrian Huntere57a5f62014-11-04 12:42:46 +02002379 if (host->flags & SDHCI_USE_64_BIT_DMA)
2380 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2381 name, desc, le32_to_cpu(dma_desc->addr_hi),
2382 le32_to_cpu(dma_desc->addr_lo),
2383 le16_to_cpu(dma_desc->len),
2384 le16_to_cpu(dma_desc->cmd));
2385 else
2386 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2387 name, desc, le32_to_cpu(dma_desc->addr_lo),
2388 le16_to_cpu(dma_desc->len),
2389 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002390
Adrian Hunter76fe3792014-11-04 12:42:42 +02002391 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002392
Adrian Hunter05452302014-11-04 12:42:45 +02002393 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002394 break;
2395 }
2396}
2397#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002398static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002399#endif
2400
Pierre Ossmand129bce2006-03-24 03:18:17 -08002401static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2402{
Girish K S069c9f12012-01-06 09:56:39 +05302403 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002404 BUG_ON(intmask == 0);
2405
Arindam Nathb513ea22011-05-05 12:19:04 +05302406 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2407 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302408 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2409 if (command == MMC_SEND_TUNING_BLOCK ||
2410 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302411 host->tuning_done = 1;
2412 wake_up(&host->buf_ready_int);
2413 return;
2414 }
2415 }
2416
Pierre Ossmand129bce2006-03-24 03:18:17 -08002417 if (!host->data) {
2418 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002419 * The "data complete" interrupt is also used to
2420 * indicate that a busy state has ended. See comment
2421 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002422 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002423 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002424 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2425 host->cmd->error = -ETIMEDOUT;
2426 tasklet_schedule(&host->finish_tasklet);
2427 return;
2428 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002429 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002430 /*
2431 * Some cards handle busy-end interrupt
2432 * before the command completed, so make
2433 * sure we do things in the proper order.
2434 */
2435 if (host->busy_handle)
2436 sdhci_finish_command(host);
2437 else
2438 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002439 return;
2440 }
2441 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002442
Marek Vasut2e4456f2015-11-18 10:47:02 +01002443 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2444 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002445 sdhci_dumpregs(host);
2446
2447 return;
2448 }
2449
2450 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002451 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002452 else if (intmask & SDHCI_INT_DATA_END_BIT)
2453 host->data->error = -EILSEQ;
2454 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2455 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2456 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002457 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002458 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302459 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002460 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002461 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002462 if (host->ops->adma_workaround)
2463 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002464 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002465
Pierre Ossman17b04292007-07-22 22:18:46 +02002466 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002467 sdhci_finish_data(host);
2468 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002469 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002470 sdhci_transfer_pio(host);
2471
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002472 /*
2473 * We currently don't do anything fancy with DMA
2474 * boundaries, but as we can't disable the feature
2475 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002476 *
2477 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2478 * should return a valid address to continue from, but as
2479 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002480 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002481 if (intmask & SDHCI_INT_DMA_END) {
2482 u32 dmastart, dmanow;
2483 dmastart = sg_dma_address(host->data->sg);
2484 dmanow = dmastart + host->data->bytes_xfered;
2485 /*
2486 * Force update to the next DMA block boundary.
2487 */
2488 dmanow = (dmanow &
2489 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2490 SDHCI_DEFAULT_BOUNDARY_SIZE;
2491 host->data->bytes_xfered = dmanow - dmastart;
2492 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2493 " next 0x%08x\n",
2494 mmc_hostname(host->mmc), dmastart,
2495 host->data->bytes_xfered, dmanow);
2496 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2497 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002498
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002499 if (intmask & SDHCI_INT_DATA_END) {
2500 if (host->cmd) {
2501 /*
2502 * Data managed to finish before the
2503 * command completed. Make sure we do
2504 * things in the proper order.
2505 */
2506 host->data_early = 1;
2507 } else {
2508 sdhci_finish_data(host);
2509 }
2510 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002511 }
2512}
2513
David Howells7d12e782006-10-05 14:55:46 +01002514static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002515{
Russell King781e9892014-04-25 12:55:46 +01002516 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002517 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002518 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002519 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002520
2521 spin_lock(&host->lock);
2522
Russell Kingbe138552014-04-25 12:55:56 +01002523 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002524 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002525 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002526 }
2527
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002528 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002529 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002530 result = IRQ_NONE;
2531 goto out;
2532 }
2533
Russell King41005002014-04-25 12:55:36 +01002534 do {
2535 /* Clear selected interrupts. */
2536 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2537 SDHCI_INT_BUS_POWER);
2538 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002539
Russell King41005002014-04-25 12:55:36 +01002540 DBG("*** %s got interrupt: 0x%08x\n",
2541 mmc_hostname(host->mmc), intmask);
2542
2543 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2544 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2545 SDHCI_CARD_PRESENT;
2546
2547 /*
2548 * There is a observation on i.mx esdhc. INSERT
2549 * bit will be immediately set again when it gets
2550 * cleared, if a card is inserted. We have to mask
2551 * the irq to prevent interrupt storm which will
2552 * freeze the system. And the REMOVE gets the
2553 * same situation.
2554 *
2555 * More testing are needed here to ensure it works
2556 * for other platforms though.
2557 */
Russell Kingb537f942014-04-25 12:56:01 +01002558 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2559 SDHCI_INT_CARD_REMOVE);
2560 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2561 SDHCI_INT_CARD_INSERT;
2562 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2563 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002564
2565 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2566 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002567
2568 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2569 SDHCI_INT_CARD_REMOVE);
2570 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002571 }
2572
2573 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002574 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2575 &intmask);
Russell King41005002014-04-25 12:55:36 +01002576
2577 if (intmask & SDHCI_INT_DATA_MASK)
2578 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2579
2580 if (intmask & SDHCI_INT_BUS_POWER)
2581 pr_err("%s: Card is consuming too much power!\n",
2582 mmc_hostname(host->mmc));
2583
Russell King781e9892014-04-25 12:55:46 +01002584 if (intmask & SDHCI_INT_CARD_INT) {
2585 sdhci_enable_sdio_irq_nolock(host, false);
2586 host->thread_isr |= SDHCI_INT_CARD_INT;
2587 result = IRQ_WAKE_THREAD;
2588 }
Russell King41005002014-04-25 12:55:36 +01002589
2590 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2591 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2592 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2593 SDHCI_INT_CARD_INT);
2594
2595 if (intmask) {
2596 unexpected |= intmask;
2597 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2598 }
2599
Russell King781e9892014-04-25 12:55:46 +01002600 if (result == IRQ_NONE)
2601 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002602
2603 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002604 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002605out:
2606 spin_unlock(&host->lock);
2607
Alexander Stein6379b232012-03-14 09:52:10 +01002608 if (unexpected) {
2609 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2610 mmc_hostname(host->mmc), unexpected);
2611 sdhci_dumpregs(host);
2612 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002613
Pierre Ossmand129bce2006-03-24 03:18:17 -08002614 return result;
2615}
2616
Russell King781e9892014-04-25 12:55:46 +01002617static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2618{
2619 struct sdhci_host *host = dev_id;
2620 unsigned long flags;
2621 u32 isr;
2622
2623 spin_lock_irqsave(&host->lock, flags);
2624 isr = host->thread_isr;
2625 host->thread_isr = 0;
2626 spin_unlock_irqrestore(&host->lock, flags);
2627
Russell King3560db82014-04-25 12:55:51 +01002628 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2629 sdhci_card_event(host->mmc);
2630 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2631 }
2632
Russell King781e9892014-04-25 12:55:46 +01002633 if (isr & SDHCI_INT_CARD_INT) {
2634 sdio_run_irqs(host->mmc);
2635
2636 spin_lock_irqsave(&host->lock, flags);
2637 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2638 sdhci_enable_sdio_irq_nolock(host, true);
2639 spin_unlock_irqrestore(&host->lock, flags);
2640 }
2641
2642 return isr ? IRQ_HANDLED : IRQ_NONE;
2643}
2644
Pierre Ossmand129bce2006-03-24 03:18:17 -08002645/*****************************************************************************\
2646 * *
2647 * Suspend/resume *
2648 * *
2649\*****************************************************************************/
2650
2651#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002652void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2653{
2654 u8 val;
2655 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2656 | SDHCI_WAKE_ON_INT;
2657
2658 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2659 val |= mask ;
2660 /* Avoid fake wake up */
2661 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2662 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2663 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2664}
2665EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2666
Fabio Estevam0b10f472014-08-30 14:53:13 -03002667static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002668{
2669 u8 val;
2670 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2671 | SDHCI_WAKE_ON_INT;
2672
2673 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2674 val &= ~mask;
2675 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2676}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002677
Manuel Lauss29495aa2011-11-03 11:09:45 +01002678int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002679{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002680 sdhci_disable_card_detection(host);
2681
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002682 mmc_retune_timer_stop(host->mmc);
2683 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302684
Kevin Liuad080d72013-01-05 17:21:33 +08002685 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002686 host->ier = 0;
2687 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2688 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002689 free_irq(host->irq, host);
2690 } else {
2691 sdhci_enable_irq_wakeups(host);
2692 enable_irq_wake(host->irq);
2693 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002694 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002695}
2696
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002697EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002698
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002699int sdhci_resume_host(struct sdhci_host *host)
2700{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002701 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002702
Richard Röjforsa13abc72009-09-22 16:45:30 -07002703 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002704 if (host->ops->enable_dma)
2705 host->ops->enable_dma(host);
2706 }
2707
Adrian Hunter6308d292012-02-07 14:48:54 +02002708 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2709 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2710 /* Card keeps power but host controller does not */
2711 sdhci_init(host, 0);
2712 host->pwr = 0;
2713 host->clock = 0;
2714 sdhci_do_set_ios(host, &host->mmc->ios);
2715 } else {
2716 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2717 mmiowb();
2718 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002719
Haibo Chen14a7b41642015-09-15 18:32:58 +08002720 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2721 ret = request_threaded_irq(host->irq, sdhci_irq,
2722 sdhci_thread_irq, IRQF_SHARED,
2723 mmc_hostname(host->mmc), host);
2724 if (ret)
2725 return ret;
2726 } else {
2727 sdhci_disable_irq_wakeups(host);
2728 disable_irq_wake(host->irq);
2729 }
2730
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002731 sdhci_enable_card_detection(host);
2732
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002733 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002734}
2735
2736EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002737
2738static int sdhci_runtime_pm_get(struct sdhci_host *host)
2739{
2740 return pm_runtime_get_sync(host->mmc->parent);
2741}
2742
2743static int sdhci_runtime_pm_put(struct sdhci_host *host)
2744{
2745 pm_runtime_mark_last_busy(host->mmc->parent);
2746 return pm_runtime_put_autosuspend(host->mmc->parent);
2747}
2748
Adrian Hunterf0710a52013-05-06 12:17:32 +03002749static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2750{
Adrian Hunter5c671c42015-11-26 14:00:50 +02002751 if (host->bus_on)
Adrian Hunterf0710a52013-05-06 12:17:32 +03002752 return;
2753 host->bus_on = true;
2754 pm_runtime_get_noresume(host->mmc->parent);
2755}
2756
2757static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2758{
Adrian Hunter5c671c42015-11-26 14:00:50 +02002759 if (!host->bus_on)
Adrian Hunterf0710a52013-05-06 12:17:32 +03002760 return;
2761 host->bus_on = false;
2762 pm_runtime_put_noidle(host->mmc->parent);
2763}
2764
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002765int sdhci_runtime_suspend_host(struct sdhci_host *host)
2766{
2767 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002768
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002769 mmc_retune_timer_stop(host->mmc);
2770 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002771
2772 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002773 host->ier &= SDHCI_INT_CARD_INT;
2774 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2775 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002776 spin_unlock_irqrestore(&host->lock, flags);
2777
Russell King781e9892014-04-25 12:55:46 +01002778 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002779
2780 spin_lock_irqsave(&host->lock, flags);
2781 host->runtime_suspended = true;
2782 spin_unlock_irqrestore(&host->lock, flags);
2783
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002784 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002785}
2786EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2787
2788int sdhci_runtime_resume_host(struct sdhci_host *host)
2789{
2790 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002791 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002792
2793 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2794 if (host->ops->enable_dma)
2795 host->ops->enable_dma(host);
2796 }
2797
2798 sdhci_init(host, 0);
2799
2800 /* Force clock and power re-program */
2801 host->pwr = 0;
2802 host->clock = 0;
Jisheng Zhang3396e732015-01-29 17:42:12 +08002803 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002804 sdhci_do_set_ios(host, &host->mmc->ios);
2805
Kevin Liu52983382013-01-31 11:31:37 +08002806 if ((host_flags & SDHCI_PV_ENABLED) &&
2807 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2808 spin_lock_irqsave(&host->lock, flags);
2809 sdhci_enable_preset_value(host, true);
2810 spin_unlock_irqrestore(&host->lock, flags);
2811 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002812
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002813 spin_lock_irqsave(&host->lock, flags);
2814
2815 host->runtime_suspended = false;
2816
2817 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002818 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002819 sdhci_enable_sdio_irq_nolock(host, true);
2820
2821 /* Enable Card Detection */
2822 sdhci_enable_card_detection(host);
2823
2824 spin_unlock_irqrestore(&host->lock, flags);
2825
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002826 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002827}
2828EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2829
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002830#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002831
Pierre Ossmand129bce2006-03-24 03:18:17 -08002832/*****************************************************************************\
2833 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002834 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002835 * *
2836\*****************************************************************************/
2837
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002838struct sdhci_host *sdhci_alloc_host(struct device *dev,
2839 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002840{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002841 struct mmc_host *mmc;
2842 struct sdhci_host *host;
2843
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002844 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002845
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002846 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002847 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002848 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002849
2850 host = mmc_priv(mmc);
2851 host->mmc = mmc;
2852
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002853 return host;
2854}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002855
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002856EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002857
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002858int sdhci_add_host(struct sdhci_host *host)
2859{
2860 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002861 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302862 u32 max_current_caps;
2863 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002864 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08002865 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002866 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002867
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002868 WARN_ON(host == NULL);
2869 if (host == NULL)
2870 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002871
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002872 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002873
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002874 if (debug_quirks)
2875 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002876 if (debug_quirks2)
2877 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002878
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002879 override_timeout_clk = host->timeout_clk;
2880
Russell King03231f92014-04-25 12:57:12 +01002881 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002882
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002883 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002884 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2885 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002886 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002887 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
2888 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002889 }
2890
Arindam Nathf2119df2011-05-05 12:18:57 +05302891 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002892 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002893
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002894 if (host->version >= SDHCI_SPEC_300)
2895 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2896 host->caps1 :
2897 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302898
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002899 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002900 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302901 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002902 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002903 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002904 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002905
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002906 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002907 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002908 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002909 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002910 }
2911
Arindam Nathf2119df2011-05-05 12:18:57 +05302912 if ((host->version >= SDHCI_SPEC_200) &&
2913 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002914 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002915
2916 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2917 (host->flags & SDHCI_USE_ADMA)) {
2918 DBG("Disabling ADMA as it is marked broken\n");
2919 host->flags &= ~SDHCI_USE_ADMA;
2920 }
2921
Adrian Huntere57a5f62014-11-04 12:42:46 +02002922 /*
2923 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2924 * and *must* do 64-bit DMA. A driver has the opportunity to change
2925 * that during the first call to ->enable_dma(). Similarly
2926 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2927 * implement.
2928 */
2929 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT)
2930 host->flags |= SDHCI_USE_64_BIT_DMA;
2931
Richard Röjforsa13abc72009-09-22 16:45:30 -07002932 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002933 if (host->ops->enable_dma) {
2934 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07002935 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002936 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002937 host->flags &=
2938 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002939 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002940 }
2941 }
2942
Adrian Huntere57a5f62014-11-04 12:42:46 +02002943 /* SDMA does not support 64-bit DMA */
2944 if (host->flags & SDHCI_USE_64_BIT_DMA)
2945 host->flags &= ~SDHCI_USE_SDMA;
2946
Pierre Ossman2134a922008-06-28 18:28:51 +02002947 if (host->flags & SDHCI_USE_ADMA) {
2948 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002949 * The DMA descriptor table size is calculated as the maximum
2950 * number of segments times 2, to allow for an alignment
2951 * descriptor for each segment, plus 1 for a nop end descriptor,
2952 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002953 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02002954 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2955 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2956 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002957 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002958 } else {
2959 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2960 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002961 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002962 }
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002963 host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +02002964 host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002965 &host->adma_addr,
2966 GFP_KERNEL);
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002967 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002968 host->align_buffer = kmalloc(host->align_buffer_sz, GFP_KERNEL);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002969 if (!host->adma_table || !host->align_buffer) {
Peng Fan7ac02032015-06-22 11:41:23 +08002970 if (host->adma_table)
2971 dma_free_coherent(mmc_dev(mmc),
2972 host->adma_table_sz,
2973 host->adma_table,
2974 host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02002975 kfree(host->align_buffer);
Joe Perches66061102014-09-12 14:56:56 -07002976 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002977 mmc_hostname(mmc));
2978 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002979 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01002980 host->align_buffer = NULL;
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002981 } else if (host->adma_addr & (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07002982 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2983 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002984 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002985 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002986 host->adma_table, host->adma_addr);
Russell Kingd1e49f72014-04-25 12:58:34 +01002987 kfree(host->align_buffer);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002988 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01002989 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02002990 }
2991 }
2992
Pierre Ossman76591502008-07-21 00:32:11 +02002993 /*
2994 * If we use DMA, then it's up to the caller to set the DMA
2995 * mask, but PIO does not need the hw shim so we set a new
2996 * mask here in that case.
2997 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002998 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002999 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003000 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003001 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003002
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003003 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05303004 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003005 >> SDHCI_CLOCK_BASE_SHIFT;
3006 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303007 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003008 >> SDHCI_CLOCK_BASE_SHIFT;
3009
Pierre Ossmand129bce2006-03-24 03:18:17 -08003010 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003011 if (host->max_clk == 0 || host->quirks &
3012 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003013 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003014 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3015 mmc_hostname(mmc));
Ben Dooks4240ff02009-03-17 00:13:57 +03003016 return -ENODEV;
3017 }
3018 host->max_clk = host->ops->get_max_clock(host);
3019 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003020
3021 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303022 * In case of Host Controller v3.00, find out whether clock
3023 * multiplier is supported.
3024 */
3025 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3026 SDHCI_CLOCK_MUL_SHIFT;
3027
3028 /*
3029 * In case the value in Clock Multiplier is 0, then programmable
3030 * clock mode is not supported, otherwise the actual clock
3031 * multiplier is one more than the value of Clock Multiplier
3032 * in the Capabilities Register.
3033 */
3034 if (host->clk_mul)
3035 host->clk_mul += 1;
3036
3037 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003038 * Set host parameters.
3039 */
3040 mmc->ops = &sdhci_ops;
Dong Aisheng59241752015-07-22 20:53:07 +08003041 max_clk = host->max_clk;
3042
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003043 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003044 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303045 else if (host->version >= SDHCI_SPEC_300) {
3046 if (host->clk_mul) {
3047 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003048 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303049 } else
3050 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3051 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003052 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003053
Dong Aisheng59241752015-07-22 20:53:07 +08003054 if (!mmc->f_max || (mmc->f_max && (mmc->f_max > max_clk)))
3055 mmc->f_max = max_clk;
3056
Aisheng Dong28aab052014-08-27 15:26:31 +08003057 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3058 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3059 SDHCI_TIMEOUT_CLK_SHIFT;
3060 if (host->timeout_clk == 0) {
3061 if (host->ops->get_timeout_clock) {
3062 host->timeout_clk =
3063 host->ops->get_timeout_clock(host);
3064 } else {
3065 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3066 mmc_hostname(mmc));
3067 return -ENODEV;
3068 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003069 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003070
Aisheng Dong28aab052014-08-27 15:26:31 +08003071 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3072 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003073
Aisheng Dong28aab052014-08-27 15:26:31 +08003074 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003075 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003076 mmc->max_busy_timeout /= host->timeout_clk;
3077 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003078
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003079 if (override_timeout_clk)
3080 host->timeout_clk = override_timeout_clk;
3081
Andrei Warkentine89d4562011-05-23 15:06:37 -05003082 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003083 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003084
3085 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3086 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003087
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003088 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003089 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003090 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003091 !(host->flags & SDHCI_USE_SDMA)) &&
3092 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003093 host->flags |= SDHCI_AUTO_CMD23;
3094 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3095 } else {
3096 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3097 }
3098
Philip Rakity15ec4462010-11-19 16:48:39 -05003099 /*
3100 * A controller may support 8-bit width, but the board itself
3101 * might not have the pins brought out. Boards that support
3102 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3103 * their platform code before calling sdhci_add_host(), and we
3104 * won't assume 8-bit width for hosts without that CAP.
3105 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003106 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003107 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003108
Jerry Huang63ef5d82012-10-25 13:47:19 +08003109 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3110 mmc->caps &= ~MMC_CAP_CMD23;
3111
Arindam Nathf2119df2011-05-05 12:18:57 +05303112 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003113 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003114
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003115 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Ivan T. Ivanovc31d22e2015-07-06 15:16:20 +03003116 !(mmc->caps & MMC_CAP_NONREMOVABLE) &&
3117 IS_ERR_VALUE(mmc_gpio_get_cd(host->mmc)))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003118 mmc->caps |= MMC_CAP_NEEDS_POLL;
3119
Tim Kryger3a48edc2014-06-13 10:13:56 -07003120 /* If there are external regulators, get them */
3121 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3122 return -EPROBE_DEFER;
3123
Philip Rakity6231f3d2012-07-23 15:56:23 -07003124 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003125 if (!IS_ERR(mmc->supply.vqmmc)) {
3126 ret = regulator_enable(mmc->supply.vqmmc);
3127 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3128 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003129 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3130 SDHCI_SUPPORT_SDR50 |
3131 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003132 if (ret) {
3133 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3134 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003135 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003136 }
Kevin Liu8363c372012-11-17 17:55:51 -05003137 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003138
Daniel Drake6a661802012-11-25 13:01:19 -05003139 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3140 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3141 SDHCI_SUPPORT_DDR50);
3142
Al Cooper4188bba2012-03-16 15:54:17 -04003143 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3144 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3145 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303146 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3147
3148 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003149 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303150 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003151 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3152 * field can be promoted to support HS200.
3153 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003154 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003155 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003156 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303157 mmc->caps |= MMC_CAP_UHS_SDR50;
3158
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003159 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3160 (caps[1] & SDHCI_SUPPORT_HS400))
3161 mmc->caps2 |= MMC_CAP2_HS400;
3162
Adrian Hunter549c0b12014-11-06 15:19:05 +02003163 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3164 (IS_ERR(mmc->supply.vqmmc) ||
3165 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3166 1300000)))
3167 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3168
Micky Ching9107ebb2014-02-21 18:40:35 +08003169 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3170 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303171 mmc->caps |= MMC_CAP_UHS_DDR50;
3172
Girish K S069c9f12012-01-06 09:56:39 +05303173 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303174 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3175 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3176
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003177 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303178 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003179 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303180
Arindam Nathd6d50a12011-05-05 12:18:59 +05303181 /* Driver Type(s) (A, C, D) supported by the host */
3182 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3183 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3184 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3185 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3186 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3187 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3188
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303189 /* Initial value for re-tuning timer count */
3190 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3191 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3192
3193 /*
3194 * In case Re-tuning Timer is not disabled, the actual value of
3195 * re-tuning timer will be 2 ^ (n - 1).
3196 */
3197 if (host->tuning_count)
3198 host->tuning_count = 1 << (host->tuning_count - 1);
3199
3200 /* Re-tuning mode supported by the Host Controller */
3201 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3202 SDHCI_RETUNING_MODE_SHIFT;
3203
Takashi Iwai8f230f42010-12-08 10:04:30 +01003204 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003205
Arindam Nathf2119df2011-05-05 12:18:57 +05303206 /*
3207 * According to SD Host Controller spec v3.00, if the Host System
3208 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3209 * the value is meaningful only if Voltage Support in the Capabilities
3210 * register is set. The actual current value is 4 times the register
3211 * value.
3212 */
3213 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003214 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003215 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003216 if (curr > 0) {
3217
3218 /* convert to SDHCI_MAX_CURRENT format */
3219 curr = curr/1000; /* convert to mA */
3220 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3221
3222 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3223 max_current_caps =
3224 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3225 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3226 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3227 }
3228 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303229
3230 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003231 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303232
Aaron Lu55c46652012-07-04 13:31:48 +08003233 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303234 SDHCI_MAX_CURRENT_330_MASK) >>
3235 SDHCI_MAX_CURRENT_330_SHIFT) *
3236 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303237 }
3238 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003239 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303240
Aaron Lu55c46652012-07-04 13:31:48 +08003241 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303242 SDHCI_MAX_CURRENT_300_MASK) >>
3243 SDHCI_MAX_CURRENT_300_SHIFT) *
3244 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303245 }
3246 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003247 ocr_avail |= MMC_VDD_165_195;
3248
Aaron Lu55c46652012-07-04 13:31:48 +08003249 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303250 SDHCI_MAX_CURRENT_180_MASK) >>
3251 SDHCI_MAX_CURRENT_180_SHIFT) *
3252 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303253 }
3254
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003255 /* If OCR set by host, use it instead. */
3256 if (host->ocr_mask)
3257 ocr_avail = host->ocr_mask;
3258
3259 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003260 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003261 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003262
Takashi Iwai8f230f42010-12-08 10:04:30 +01003263 mmc->ocr_avail = ocr_avail;
3264 mmc->ocr_avail_sdio = ocr_avail;
3265 if (host->ocr_avail_sdio)
3266 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3267 mmc->ocr_avail_sd = ocr_avail;
3268 if (host->ocr_avail_sd)
3269 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3270 else /* normal SD controllers don't support 1.8V */
3271 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3272 mmc->ocr_avail_mmc = ocr_avail;
3273 if (host->ocr_avail_mmc)
3274 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003275
3276 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003277 pr_err("%s: Hardware doesn't report any support voltages.\n",
3278 mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003279 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003280 }
3281
Pierre Ossmand129bce2006-03-24 03:18:17 -08003282 spin_lock_init(&host->lock);
3283
3284 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003285 * Maximum number of segments. Depends on if the hardware
3286 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003287 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003288 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003289 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003290 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003291 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003292 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003293 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003294
3295 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003296 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3297 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3298 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003299 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003300 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003301
3302 /*
3303 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003304 * of bytes. When doing hardware scatter/gather, each entry cannot
3305 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003306 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003307 if (host->flags & SDHCI_USE_ADMA) {
3308 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3309 mmc->max_seg_size = 65535;
3310 else
3311 mmc->max_seg_size = 65536;
3312 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003313 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003314 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003315
3316 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003317 * Maximum block size. This varies from controller to controller and
3318 * is specified in the capabilities register.
3319 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003320 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3321 mmc->max_blk_size = 2;
3322 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303323 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003324 SDHCI_MAX_BLOCK_SHIFT;
3325 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003326 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3327 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003328 mmc->max_blk_size = 0;
3329 }
3330 }
3331
3332 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003333
3334 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003335 * Maximum block count.
3336 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003337 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003338
3339 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003340 * Init tasklets.
3341 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003342 tasklet_init(&host->finish_tasklet,
3343 sdhci_tasklet_finish, (unsigned long)host);
3344
Al Viroe4cad1b2006-10-10 22:47:07 +01003345 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003346
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003347 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303348
Shawn Guo2af502c2013-07-05 14:38:55 +08003349 sdhci_init(host, 0);
3350
Russell King781e9892014-04-25 12:55:46 +01003351 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3352 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003353 if (ret) {
3354 pr_err("%s: Failed to request IRQ %d: %d\n",
3355 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003356 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003357 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003358
Pierre Ossmand129bce2006-03-24 03:18:17 -08003359#ifdef CONFIG_MMC_DEBUG
3360 sdhci_dumpregs(host);
3361#endif
3362
Pierre Ossmanf9134312008-12-21 17:01:48 +01003363#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003364 snprintf(host->led_name, sizeof(host->led_name),
3365 "%s::", mmc_hostname(mmc));
3366 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003367 host->led.brightness = LED_OFF;
3368 host->led.default_trigger = mmc_hostname(mmc);
3369 host->led.brightness_set = sdhci_led_control;
3370
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003371 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003372 if (ret) {
3373 pr_err("%s: Failed to register LED device: %d\n",
3374 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003375 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003376 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003377#endif
3378
Pierre Ossman5f25a662006-10-04 02:15:39 -07003379 mmiowb();
3380
Pierre Ossmand129bce2006-03-24 03:18:17 -08003381 mmc_add_host(mmc);
3382
Girish K Sa3c76eb2011-10-11 11:44:09 +05303383 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003384 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003385 (host->flags & SDHCI_USE_ADMA) ?
3386 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003387 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003388
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003389 sdhci_enable_card_detection(host);
3390
Pierre Ossmand129bce2006-03-24 03:18:17 -08003391 return 0;
3392
Pierre Ossmanf9134312008-12-21 17:01:48 +01003393#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003394reset:
Russell King03231f92014-04-25 12:57:12 +01003395 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003396 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3397 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003398 free_irq(host->irq, host);
3399#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003400untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003401 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003402
3403 return ret;
3404}
3405
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003406EXPORT_SYMBOL_GPL(sdhci_add_host);
3407
Pierre Ossman1e728592008-04-16 19:13:13 +02003408void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003409{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003410 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003411 unsigned long flags;
3412
3413 if (dead) {
3414 spin_lock_irqsave(&host->lock, flags);
3415
3416 host->flags |= SDHCI_DEVICE_DEAD;
3417
3418 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303419 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003420 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003421
3422 host->mrq->cmd->error = -ENOMEDIUM;
3423 tasklet_schedule(&host->finish_tasklet);
3424 }
3425
3426 spin_unlock_irqrestore(&host->lock, flags);
3427 }
3428
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003429 sdhci_disable_card_detection(host);
3430
Markus Mayer4e743f12014-07-03 13:27:42 -07003431 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003432
Pierre Ossmanf9134312008-12-21 17:01:48 +01003433#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003434 led_classdev_unregister(&host->led);
3435#endif
3436
Pierre Ossman1e728592008-04-16 19:13:13 +02003437 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003438 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003439
Russell Kingb537f942014-04-25 12:56:01 +01003440 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3441 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003442 free_irq(host->irq, host);
3443
3444 del_timer_sync(&host->timer);
3445
Pierre Ossmand129bce2006-03-24 03:18:17 -08003446 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003447
Tim Kryger3a48edc2014-06-13 10:13:56 -07003448 if (!IS_ERR(mmc->supply.vqmmc))
3449 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003450
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003451 if (host->adma_table)
Adrian Hunter76fe3792014-11-04 12:42:42 +02003452 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003453 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003454 kfree(host->align_buffer);
3455
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003456 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003457 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003458}
3459
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003460EXPORT_SYMBOL_GPL(sdhci_remove_host);
3461
3462void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003463{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003464 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003465}
3466
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003467EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003468
3469/*****************************************************************************\
3470 * *
3471 * Driver init/exit *
3472 * *
3473\*****************************************************************************/
3474
3475static int __init sdhci_drv_init(void)
3476{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303477 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003478 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303479 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003480
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003481 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003482}
3483
3484static void __exit sdhci_drv_exit(void)
3485{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003486}
3487
3488module_init(sdhci_drv_init);
3489module_exit(sdhci_drv_exit);
3490
Pierre Ossmandf673b22006-06-30 02:22:31 -07003491module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003492module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003493
Pierre Ossman32710e82009-04-08 20:14:54 +02003494MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003495MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003496MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003497
Pierre Ossmandf673b22006-06-30 02:22:31 -07003498MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003499MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");