blob: 5a7517f4418759ae38a0b1c8e4eca12d64f25fd6 [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 Lu473b095a2012-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{
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200468 void *desc;
469 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200470 dma_addr_t addr;
471 dma_addr_t align_addr;
472 int len, offset;
473
474 struct scatterlist *sg;
475 int i;
476 char *buffer;
477 unsigned long flags;
478
479 /*
480 * The spec does not specify endianness of descriptor table.
481 * We currently guess that it is LE.
482 */
483
Haibo Chend31911b2015-08-25 10:02:11 +0800484 host->sg_count = sdhci_pre_dma_transfer(host, data);
Haibo Chen348487c2014-12-09 17:04:05 +0800485 if (host->sg_count < 0)
Russell Kingedd63fc2016-01-26 13:39:50 +0000486 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200487
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200488 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200489 align = host->align_buffer;
490
491 align_addr = host->align_addr;
492
493 for_each_sg(data->sg, sg, host->sg_count, i) {
494 addr = sg_dma_address(sg);
495 len = sg_dma_len(sg);
496
497 /*
498 * The SDHCI specification states that ADMA
499 * addresses must be 32-bit aligned. If they
500 * aren't, then we use a bounce buffer for
501 * the (up to three) bytes that screw up the
502 * alignment.
503 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200504 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
505 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200506 if (offset) {
507 if (data->flags & MMC_DATA_WRITE) {
508 buffer = sdhci_kmap_atomic(sg, &flags);
509 memcpy(align, buffer, offset);
510 sdhci_kunmap_atomic(buffer, &flags);
511 }
512
Ben Dooks118cd172010-03-05 13:43:26 -0800513 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200514 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200515 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200516
517 BUG_ON(offset > 65536);
518
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200519 align += SDHCI_ADMA2_ALIGN;
520 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200521
Adrian Hunter76fe3792014-11-04 12:42:42 +0200522 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200523
524 addr += offset;
525 len -= offset;
526 }
527
Pierre Ossman2134a922008-06-28 18:28:51 +0200528 BUG_ON(len > 65536);
529
Adrian Hunter347ea322015-11-26 14:00:48 +0200530 if (len) {
531 /* tran, valid */
532 sdhci_adma_write_desc(host, desc, addr, len,
533 ADMA2_TRAN_VALID);
534 desc += host->desc_sz;
535 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200536
537 /*
538 * If this triggers then we have a calculation bug
539 * somewhere. :/
540 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200541 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200542 }
543
Thomas Abraham70764a92010-05-26 14:42:04 -0700544 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
545 /*
546 * Mark the last descriptor as the terminating descriptor
547 */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200548 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200549 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200550 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700551 }
552 } else {
553 /*
554 * Add a terminating entry.
555 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200556
Thomas Abraham70764a92010-05-26 14:42:04 -0700557 /* nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200558 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700559 }
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200560 return 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200561}
562
563static void sdhci_adma_table_post(struct sdhci_host *host,
564 struct mmc_data *data)
565{
566 int direction;
567
568 struct scatterlist *sg;
569 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200570 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200571 char *buffer;
572 unsigned long flags;
Russell Kingde0b65a2014-04-25 12:58:29 +0100573 bool has_unaligned;
Pierre Ossman2134a922008-06-28 18:28:51 +0200574
575 if (data->flags & MMC_DATA_READ)
576 direction = DMA_FROM_DEVICE;
577 else
578 direction = DMA_TO_DEVICE;
579
Russell Kingde0b65a2014-04-25 12:58:29 +0100580 /* Do a quick scan of the SG list for any unaligned mappings */
581 has_unaligned = false;
582 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200583 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell Kingde0b65a2014-04-25 12:58:29 +0100584 has_unaligned = true;
585 break;
586 }
587
588 if (has_unaligned && data->flags & MMC_DATA_READ) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200589 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
590 data->sg_len, direction);
591
592 align = host->align_buffer;
593
594 for_each_sg(data->sg, sg, host->sg_count, i) {
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200595 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
596 size = SDHCI_ADMA2_ALIGN -
597 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
Pierre Ossman2134a922008-06-28 18:28:51 +0200598
599 buffer = sdhci_kmap_atomic(sg, &flags);
600 memcpy(buffer, align, size);
601 sdhci_kunmap_atomic(buffer, &flags);
602
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200603 align += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200604 }
605 }
606 }
607
Haibo Chend31911b2015-08-25 10:02:11 +0800608 if (data->host_cookie == COOKIE_MAPPED) {
Haibo Chen348487c2014-12-09 17:04:05 +0800609 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
610 data->sg_len, direction);
Haibo Chend31911b2015-08-25 10:02:11 +0800611 data->host_cookie = COOKIE_UNMAPPED;
612 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200613}
614
Andrei Warkentina3c77782011-04-11 16:13:42 -0500615static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800616{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700617 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500618 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700619 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800620
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200621 /*
622 * If the host controller provides us with an incorrect timeout
623 * value, just skip the check and use 0xE. The hardware may take
624 * longer to time out, but that's much better than having a too-short
625 * timeout value.
626 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200627 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200628 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200629
Andrei Warkentina3c77782011-04-11 16:13:42 -0500630 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100631 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500632 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800633
Andrei Warkentina3c77782011-04-11 16:13:42 -0500634 /* timeout in us */
635 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100636 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300637 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000638 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000639 if (host->clock && data->timeout_clks) {
640 unsigned long long val;
641
642 /*
643 * data->timeout_clks is in units of clock cycles.
644 * host->clock is in Hz. target_timeout is in us.
645 * Hence, us = 1000000 * cycles / Hz. Round up.
646 */
647 val = 1000000 * data->timeout_clks;
648 if (do_div(val, host->clock))
649 target_timeout++;
650 target_timeout += val;
651 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300652 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700653
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700654 /*
655 * Figure out needed cycles.
656 * We do this in steps in order to fit inside a 32 bit int.
657 * The first step is the minimum timeout, which will have a
658 * minimum resolution of 6 bits:
659 * (1) 2^13*1000 > 2^22,
660 * (2) host->timeout_clk < 2^16
661 * =>
662 * (1) / (2) > 2^6
663 */
664 count = 0;
665 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
666 while (current_timeout < target_timeout) {
667 count++;
668 current_timeout <<= 1;
669 if (count >= 0xF)
670 break;
671 }
672
673 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400674 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
675 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700676 count = 0xE;
677 }
678
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200679 return count;
680}
681
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300682static void sdhci_set_transfer_irqs(struct sdhci_host *host)
683{
684 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
685 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
686
687 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100688 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300689 else
Russell Kingb537f942014-04-25 12:56:01 +0100690 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
691
692 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
693 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300694}
695
Aisheng Dongb45e6682014-08-27 15:26:29 +0800696static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200697{
698 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800699
700 if (host->ops->set_timeout) {
701 host->ops->set_timeout(host, cmd);
702 } else {
703 count = sdhci_calc_timeout(host, cmd);
704 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
705 }
706}
707
708static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
709{
Pierre Ossman2134a922008-06-28 18:28:51 +0200710 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500711 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200712 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200713
714 WARN_ON(host->data);
715
Aisheng Dongb45e6682014-08-27 15:26:29 +0800716 if (data || (cmd->flags & MMC_RSP_BUSY))
717 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500718
719 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200720 return;
721
722 /* Sanity checks */
723 BUG_ON(data->blksz * data->blocks > 524288);
724 BUG_ON(data->blksz > host->mmc->max_blk_size);
725 BUG_ON(data->blocks > 65535);
726
727 host->data = data;
728 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400729 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200730
Richard Röjforsa13abc72009-09-22 16:45:30 -0700731 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100732 host->flags |= SDHCI_REQ_USE_DMA;
733
Pierre Ossman2134a922008-06-28 18:28:51 +0200734 /*
735 * FIXME: This doesn't account for merging when mapping the
736 * scatterlist.
737 */
738 if (host->flags & SDHCI_REQ_USE_DMA) {
739 int broken, i;
740 struct scatterlist *sg;
741
742 broken = 0;
743 if (host->flags & SDHCI_USE_ADMA) {
744 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
745 broken = 1;
746 } else {
747 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
748 broken = 1;
749 }
750
751 if (unlikely(broken)) {
752 for_each_sg(data->sg, sg, data->sg_len, i) {
753 if (sg->length & 0x3) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100754 DBG("Reverting to PIO because of transfer size (%d)\n",
Pierre Ossman2134a922008-06-28 18:28:51 +0200755 sg->length);
756 host->flags &= ~SDHCI_REQ_USE_DMA;
757 break;
758 }
759 }
760 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100761 }
762
763 /*
764 * The assumption here being that alignment is the same after
765 * translation to device address space.
766 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200767 if (host->flags & SDHCI_REQ_USE_DMA) {
768 int broken, i;
769 struct scatterlist *sg;
770
771 broken = 0;
772 if (host->flags & SDHCI_USE_ADMA) {
773 /*
774 * As we use 3 byte chunks to work around
775 * alignment problems, we need to check this
776 * quirk.
777 */
778 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
779 broken = 1;
780 } else {
781 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
782 broken = 1;
783 }
784
785 if (unlikely(broken)) {
786 for_each_sg(data->sg, sg, data->sg_len, i) {
787 if (sg->offset & 0x3) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100788 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200789 host->flags &= ~SDHCI_REQ_USE_DMA;
790 break;
791 }
792 }
793 }
794 }
795
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200796 if (host->flags & SDHCI_REQ_USE_DMA) {
797 if (host->flags & SDHCI_USE_ADMA) {
798 ret = sdhci_adma_table_pre(host, data);
799 if (ret) {
800 /*
801 * This only happens when someone fed
802 * us an invalid request.
803 */
804 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200805 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200806 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300807 sdhci_writel(host, host->adma_addr,
808 SDHCI_ADMA_ADDRESS);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200809 if (host->flags & SDHCI_USE_64_BIT_DMA)
810 sdhci_writel(host,
811 (u64)host->adma_addr >> 32,
812 SDHCI_ADMA_ADDRESS_HI);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200813 }
814 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300815 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200816
Haibo Chend31911b2015-08-25 10:02:11 +0800817 sg_cnt = sdhci_pre_dma_transfer(host, data);
Jiri Slaby62a7f362015-06-12 11:45:02 +0200818 if (sg_cnt <= 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200819 /*
820 * This only happens when someone fed
821 * us an invalid request.
822 */
823 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200824 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200825 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200826 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300827 sdhci_writel(host, sg_dma_address(data->sg),
828 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200829 }
830 }
831 }
832
Pierre Ossman2134a922008-06-28 18:28:51 +0200833 /*
834 * Always adjust the DMA selection as some controllers
835 * (e.g. JMicron) can't do PIO properly when the selection
836 * is ADMA.
837 */
838 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300839 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200840 ctrl &= ~SDHCI_CTRL_DMA_MASK;
841 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200842 (host->flags & SDHCI_USE_ADMA)) {
843 if (host->flags & SDHCI_USE_64_BIT_DMA)
844 ctrl |= SDHCI_CTRL_ADMA64;
845 else
846 ctrl |= SDHCI_CTRL_ADMA32;
847 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200848 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200849 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300850 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100851 }
852
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200853 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200854 int flags;
855
856 flags = SG_MITER_ATOMIC;
857 if (host->data->flags & MMC_DATA_READ)
858 flags |= SG_MITER_TO_SG;
859 else
860 flags |= SG_MITER_FROM_SG;
861 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200862 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800863 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700864
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300865 sdhci_set_transfer_irqs(host);
866
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400867 /* Set the DMA boundary value and block size */
868 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
869 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300870 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700871}
872
873static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500874 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700875{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800876 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500877 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700878
Dong Aisheng2b558c12013-10-30 22:09:48 +0800879 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800880 if (host->quirks2 &
881 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
882 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
883 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800884 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800885 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
886 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800887 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800888 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700889 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800890 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700891
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200892 WARN_ON(!host->data);
893
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800894 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
895 mode = SDHCI_TRNS_BLK_CNT_EN;
896
Andrei Warkentine89d4562011-05-23 15:06:37 -0500897 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800898 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500899 /*
900 * If we are sending CMD23, CMD12 never gets sent
901 * on successful completion (so no Auto-CMD12).
902 */
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800903 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
904 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500905 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500906 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
907 mode |= SDHCI_TRNS_AUTO_CMD23;
908 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
909 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700910 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500911
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700912 if (data->flags & MMC_DATA_READ)
913 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100914 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700915 mode |= SDHCI_TRNS_DMA;
916
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300917 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800918}
919
920static void sdhci_finish_data(struct sdhci_host *host)
921{
922 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800923
924 BUG_ON(!host->data);
925
926 data = host->data;
927 host->data = NULL;
928
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100929 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200930 if (host->flags & SDHCI_USE_ADMA)
931 sdhci_adma_table_post(host, data);
932 else {
Haibo Chend31911b2015-08-25 10:02:11 +0800933 if (data->host_cookie == COOKIE_MAPPED) {
Haibo Chen348487c2014-12-09 17:04:05 +0800934 dma_unmap_sg(mmc_dev(host->mmc),
935 data->sg, data->sg_len,
936 (data->flags & MMC_DATA_READ) ?
Pierre Ossman2134a922008-06-28 18:28:51 +0200937 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Haibo Chend31911b2015-08-25 10:02:11 +0800938 data->host_cookie = COOKIE_UNMAPPED;
939 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200940 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800941 }
942
943 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200944 * The specification states that the block count register must
945 * be updated, but it does not specify at what point in the
946 * data flow. That makes the register entirely useless to read
947 * back so we have to assume that nothing made it to the card
948 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800949 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200950 if (data->error)
951 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800952 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200953 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800954
Andrei Warkentine89d4562011-05-23 15:06:37 -0500955 /*
956 * Need to send CMD12 if -
957 * a) open-ended multiblock transfer (no CMD23)
958 * b) error in multiblock transfer
959 */
960 if (data->stop &&
961 (data->error ||
962 !host->mrq->sbc)) {
963
Pierre Ossmand129bce2006-03-24 03:18:17 -0800964 /*
965 * The controller needs a reset of internal state machines
966 * upon error conditions.
967 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200968 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100969 sdhci_do_reset(host, SDHCI_RESET_CMD);
970 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800971 }
972
973 sdhci_send_command(host, data->stop);
974 } else
975 tasklet_schedule(&host->finish_tasklet);
976}
977
Dong Aishengc0e551292013-09-13 19:11:31 +0800978void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800979{
980 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700981 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700982 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983
984 WARN_ON(host->cmd);
985
Russell King96776202016-01-26 13:39:34 +0000986 /* Initially, a command has no error */
987 cmd->error = 0;
988
Pierre Ossmand129bce2006-03-24 03:18:17 -0800989 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700990 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700991
992 mask = SDHCI_CMD_INHIBIT;
993 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
994 mask |= SDHCI_DATA_INHIBIT;
995
996 /* We shouldn't wait for data inihibit for stop commands, even
997 though they might use busy signaling */
998 if (host->mrq->data && (cmd == host->mrq->data->stop))
999 mask &= ~SDHCI_DATA_INHIBIT;
1000
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001001 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001002 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001003 pr_err("%s: Controller never released inhibit bit(s).\n",
1004 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001005 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001006 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001007 tasklet_schedule(&host->finish_tasklet);
1008 return;
1009 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001010 timeout--;
1011 mdelay(1);
1012 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001013
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001014 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001015 if (!cmd->data && cmd->busy_timeout > 9000)
1016 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001017 else
1018 timeout += 10 * HZ;
1019 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001020
1021 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001022 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001023
Andrei Warkentina3c77782011-04-11 16:13:42 -05001024 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001025
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001026 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001027
Andrei Warkentine89d4562011-05-23 15:06:37 -05001028 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001029
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301031 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001032 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001033 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001034 tasklet_schedule(&host->finish_tasklet);
1035 return;
1036 }
1037
1038 if (!(cmd->flags & MMC_RSP_PRESENT))
1039 flags = SDHCI_CMD_RESP_NONE;
1040 else if (cmd->flags & MMC_RSP_136)
1041 flags = SDHCI_CMD_RESP_LONG;
1042 else if (cmd->flags & MMC_RSP_BUSY)
1043 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1044 else
1045 flags = SDHCI_CMD_RESP_SHORT;
1046
1047 if (cmd->flags & MMC_RSP_CRC)
1048 flags |= SDHCI_CMD_CRC;
1049 if (cmd->flags & MMC_RSP_OPCODE)
1050 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301051
1052 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301053 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1054 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001055 flags |= SDHCI_CMD_DATA;
1056
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001057 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001058}
Dong Aishengc0e551292013-09-13 19:11:31 +08001059EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001060
1061static void sdhci_finish_command(struct sdhci_host *host)
1062{
1063 int i;
1064
1065 BUG_ON(host->cmd == NULL);
1066
1067 if (host->cmd->flags & MMC_RSP_PRESENT) {
1068 if (host->cmd->flags & MMC_RSP_136) {
1069 /* CRC is stripped so we need to do some shifting. */
1070 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001071 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072 SDHCI_RESPONSE + (3-i)*4) << 8;
1073 if (i != 3)
1074 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001075 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001076 SDHCI_RESPONSE + (3-i)*4-1);
1077 }
1078 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001079 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001080 }
1081 }
1082
Andrei Warkentine89d4562011-05-23 15:06:37 -05001083 /* Finished CMD23, now send actual command. */
1084 if (host->cmd == host->mrq->sbc) {
1085 host->cmd = NULL;
1086 sdhci_send_command(host, host->mrq->cmd);
1087 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001088
Andrei Warkentine89d4562011-05-23 15:06:37 -05001089 /* Processed actual command. */
1090 if (host->data && host->data_early)
1091 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001092
Andrei Warkentine89d4562011-05-23 15:06:37 -05001093 if (!host->cmd->data)
1094 tasklet_schedule(&host->finish_tasklet);
1095
1096 host->cmd = NULL;
1097 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001098}
1099
Kevin Liu52983382013-01-31 11:31:37 +08001100static u16 sdhci_get_preset_value(struct sdhci_host *host)
1101{
Russell Kingd975f122014-04-25 12:59:31 +01001102 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001103
Russell Kingd975f122014-04-25 12:59:31 +01001104 switch (host->timing) {
1105 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001106 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1107 break;
Russell Kingd975f122014-04-25 12:59:31 +01001108 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001109 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1110 break;
Russell Kingd975f122014-04-25 12:59:31 +01001111 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001112 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1113 break;
Russell Kingd975f122014-04-25 12:59:31 +01001114 case MMC_TIMING_UHS_SDR104:
1115 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001116 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1117 break;
Russell Kingd975f122014-04-25 12:59:31 +01001118 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001119 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001120 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1121 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001122 case MMC_TIMING_MMC_HS400:
1123 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1124 break;
Kevin Liu52983382013-01-31 11:31:37 +08001125 default:
1126 pr_warn("%s: Invalid UHS-I mode selected\n",
1127 mmc_hostname(host->mmc));
1128 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1129 break;
1130 }
1131 return preset;
1132}
1133
Russell King17710592014-04-25 12:58:55 +01001134void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001135{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301136 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001137 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301138 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001139 unsigned long timeout;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001140 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001141
Russell King1650d0c2014-04-25 12:58:50 +01001142 host->mmc->actual_clock = 0;
1143
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001144 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
ludovic.desroches@atmel.comaf951762015-09-17 10:16:19 +02001145 if (host->quirks2 & SDHCI_QUIRK2_NEED_DELAY_AFTER_INT_CLK_RST)
1146 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001147
1148 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001149 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001150
Zhangfei Gao85105c52010-08-06 07:10:01 +08001151 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001152 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001153 u16 pre_val;
1154
1155 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1156 pre_val = sdhci_get_preset_value(host);
1157 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1158 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1159 if (host->clk_mul &&
1160 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1161 clk = SDHCI_PROG_CLOCK_MODE;
1162 real_div = div + 1;
1163 clk_mul = host->clk_mul;
1164 } else {
1165 real_div = max_t(int, 1, div << 1);
1166 }
1167 goto clock_set;
1168 }
1169
Arindam Nathc3ed3872011-05-05 12:19:06 +05301170 /*
1171 * Check if the Host Controller supports Programmable Clock
1172 * Mode.
1173 */
1174 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001175 for (div = 1; div <= 1024; div++) {
1176 if ((host->max_clk * host->clk_mul / div)
1177 <= clock)
1178 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001179 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001180 if ((host->max_clk * host->clk_mul / div) <= clock) {
1181 /*
1182 * Set Programmable Clock Mode in the Clock
1183 * Control register.
1184 */
1185 clk = SDHCI_PROG_CLOCK_MODE;
1186 real_div = div;
1187 clk_mul = host->clk_mul;
1188 div--;
1189 } else {
1190 /*
1191 * Divisor can be too small to reach clock
1192 * speed requirement. Then use the base clock.
1193 */
1194 switch_base_clk = true;
1195 }
1196 }
1197
1198 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301199 /* Version 3.00 divisors must be a multiple of 2. */
1200 if (host->max_clk <= clock)
1201 div = 1;
1202 else {
1203 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1204 div += 2) {
1205 if ((host->max_clk / div) <= clock)
1206 break;
1207 }
1208 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001209 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301210 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301211 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1212 && !div && host->max_clk <= 25000000)
1213 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001214 }
1215 } else {
1216 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001217 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001218 if ((host->max_clk / div) <= clock)
1219 break;
1220 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001221 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301222 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001223 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001224
Kevin Liu52983382013-01-31 11:31:37 +08001225clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001226 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001227 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301228 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001229 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1230 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001231 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001232 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001233
Chris Ball27f6cb12009-09-22 16:45:31 -07001234 /* Wait max 20 ms */
1235 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001236 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001237 & SDHCI_CLOCK_INT_STABLE)) {
1238 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001239 pr_err("%s: Internal clock never stabilised.\n",
1240 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001241 sdhci_dumpregs(host);
1242 return;
1243 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001244 timeout--;
1245 mdelay(1);
1246 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001247
1248 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001249 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001250}
Russell King17710592014-04-25 12:58:55 +01001251EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001252
Russell King24fbb3c2014-04-25 13:00:06 +01001253static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1254 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001255{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001256 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001257 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001258
Russell King24fbb3c2014-04-25 13:00:06 +01001259 if (mode != MMC_POWER_OFF) {
1260 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001261 case MMC_VDD_165_195:
1262 pwr = SDHCI_POWER_180;
1263 break;
1264 case MMC_VDD_29_30:
1265 case MMC_VDD_30_31:
1266 pwr = SDHCI_POWER_300;
1267 break;
1268 case MMC_VDD_32_33:
1269 case MMC_VDD_33_34:
1270 pwr = SDHCI_POWER_330;
1271 break;
1272 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001273 WARN(1, "%s: Invalid vdd %#x\n",
1274 mmc_hostname(host->mmc), vdd);
1275 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001276 }
1277 }
1278
1279 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001280 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001281
Pierre Ossmanae628902009-05-03 20:45:03 +02001282 host->pwr = pwr;
1283
1284 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001285 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001286 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1287 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001288 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001289 } else {
1290 /*
1291 * Spec says that we should clear the power reg before setting
1292 * a new value. Some controllers don't seem to like this though.
1293 */
1294 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1295 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001296
Russell Kinge921a8b2014-04-25 13:00:01 +01001297 /*
1298 * At least the Marvell CaFe chip gets confused if we set the
1299 * voltage and set turn on power at the same time, so set the
1300 * voltage first.
1301 */
1302 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1303 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001304
Russell Kinge921a8b2014-04-25 13:00:01 +01001305 pwr |= SDHCI_POWER_ON;
1306
Pierre Ossmanae628902009-05-03 20:45:03 +02001307 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1308
Russell Kinge921a8b2014-04-25 13:00:01 +01001309 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1310 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001311
Russell Kinge921a8b2014-04-25 13:00:01 +01001312 /*
1313 * Some controllers need an extra 10ms delay of 10ms before
1314 * they can apply clock after applying power
1315 */
1316 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1317 mdelay(10);
1318 }
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001319
1320 if (!IS_ERR(mmc->supply.vmmc)) {
1321 spin_unlock_irq(&host->lock);
1322 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1323 spin_lock_irq(&host->lock);
1324 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001325}
1326
Pierre Ossmand129bce2006-03-24 03:18:17 -08001327/*****************************************************************************\
1328 * *
1329 * MMC callbacks *
1330 * *
1331\*****************************************************************************/
1332
1333static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1334{
1335 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001336 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001337 unsigned long flags;
1338
1339 host = mmc_priv(mmc);
1340
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001341 sdhci_runtime_pm_get(host);
1342
Scott Branden04e079c2015-03-10 11:35:10 -07001343 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001344 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001345
Pierre Ossmand129bce2006-03-24 03:18:17 -08001346 spin_lock_irqsave(&host->lock, flags);
1347
1348 WARN_ON(host->mrq != NULL);
1349
Pierre Ossmanf9134312008-12-21 17:01:48 +01001350#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001351 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001352#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001353
1354 /*
1355 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1356 * requests if Auto-CMD12 is enabled.
1357 */
1358 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001359 if (mrq->stop) {
1360 mrq->data->stop = NULL;
1361 mrq->stop = NULL;
1362 }
1363 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001364
1365 host->mrq = mrq;
1366
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001367 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001368 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001369 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301370 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001371 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001372 sdhci_send_command(host, mrq->sbc);
1373 else
1374 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301375 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001376
Pierre Ossman5f25a662006-10-04 02:15:39 -07001377 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001378 spin_unlock_irqrestore(&host->lock, flags);
1379}
1380
Russell King2317f562014-04-25 12:57:07 +01001381void sdhci_set_bus_width(struct sdhci_host *host, int width)
1382{
1383 u8 ctrl;
1384
1385 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1386 if (width == MMC_BUS_WIDTH_8) {
1387 ctrl &= ~SDHCI_CTRL_4BITBUS;
1388 if (host->version >= SDHCI_SPEC_300)
1389 ctrl |= SDHCI_CTRL_8BITBUS;
1390 } else {
1391 if (host->version >= SDHCI_SPEC_300)
1392 ctrl &= ~SDHCI_CTRL_8BITBUS;
1393 if (width == MMC_BUS_WIDTH_4)
1394 ctrl |= SDHCI_CTRL_4BITBUS;
1395 else
1396 ctrl &= ~SDHCI_CTRL_4BITBUS;
1397 }
1398 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1399}
1400EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1401
Russell King96d7b782014-04-25 12:59:26 +01001402void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1403{
1404 u16 ctrl_2;
1405
1406 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1407 /* Select Bus Speed Mode for host */
1408 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1409 if ((timing == MMC_TIMING_MMC_HS200) ||
1410 (timing == MMC_TIMING_UHS_SDR104))
1411 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1412 else if (timing == MMC_TIMING_UHS_SDR12)
1413 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1414 else if (timing == MMC_TIMING_UHS_SDR25)
1415 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1416 else if (timing == MMC_TIMING_UHS_SDR50)
1417 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1418 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1419 (timing == MMC_TIMING_MMC_DDR52))
1420 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001421 else if (timing == MMC_TIMING_MMC_HS400)
1422 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001423 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1424}
1425EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1426
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001427static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001428{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001429 unsigned long flags;
1430 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001431 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001432
Pierre Ossmand129bce2006-03-24 03:18:17 -08001433 spin_lock_irqsave(&host->lock, flags);
1434
Adrian Hunterceb61432011-12-27 15:48:41 +02001435 if (host->flags & SDHCI_DEVICE_DEAD) {
1436 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001437 if (!IS_ERR(mmc->supply.vmmc) &&
1438 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001439 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001440 return;
1441 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001442
Pierre Ossmand129bce2006-03-24 03:18:17 -08001443 /*
1444 * Reset the chip on each power off.
1445 * Should clear out any weird states.
1446 */
1447 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001448 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001449 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001450 }
1451
Kevin Liu52983382013-01-31 11:31:37 +08001452 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001453 (ios->power_mode == MMC_POWER_UP) &&
1454 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001455 sdhci_enable_preset_value(host, false);
1456
Russell King373073e2014-04-25 12:58:45 +01001457 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001458 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001459 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001460
1461 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1462 host->clock) {
1463 host->timeout_clk = host->mmc->actual_clock ?
1464 host->mmc->actual_clock / 1000 :
1465 host->clock / 1000;
1466 host->mmc->max_busy_timeout =
1467 host->ops->get_max_timeout_count ?
1468 host->ops->get_max_timeout_count(host) :
1469 1 << 27;
1470 host->mmc->max_busy_timeout /= host->timeout_clk;
1471 }
Russell King373073e2014-04-25 12:58:45 +01001472 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001473
Russell King24fbb3c2014-04-25 13:00:06 +01001474 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001475
Philip Rakity643a81f2010-09-23 08:24:32 -07001476 if (host->ops->platform_send_init_74_clocks)
1477 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1478
Russell King2317f562014-04-25 12:57:07 +01001479 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001480
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001481 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001482
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001483 if ((ios->timing == MMC_TIMING_SD_HS ||
1484 ios->timing == MMC_TIMING_MMC_HS)
1485 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001486 ctrl |= SDHCI_CTRL_HISPD;
1487 else
1488 ctrl &= ~SDHCI_CTRL_HISPD;
1489
Arindam Nathd6d50a12011-05-05 12:18:59 +05301490 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301491 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301492
1493 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001494 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1495 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001496 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301497 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301498 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1499 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001500 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301501 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301502
Russell Kingda91a8f2014-04-25 13:00:12 +01001503 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301504 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301505 /*
1506 * We only need to set Driver Strength if the
1507 * preset value enable is not set.
1508 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001509 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301510 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1511 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1512 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001513 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1514 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301515 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1516 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001517 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1518 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1519 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001520 pr_warn("%s: invalid driver type, default to driver type B\n",
1521 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001522 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1523 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301524
1525 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301526 } else {
1527 /*
1528 * According to SDHC Spec v3.00, if the Preset Value
1529 * Enable in the Host Control 2 register is set, we
1530 * need to reset SD Clock Enable before changing High
1531 * Speed Enable to avoid generating clock gliches.
1532 */
Arindam Nath758535c2011-05-05 12:19:00 +05301533
1534 /* Reset SD Clock Enable */
1535 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1536 clk &= ~SDHCI_CLOCK_CARD_EN;
1537 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1538
1539 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1540
1541 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001542 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301543 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301544
Arindam Nath49c468f2011-05-05 12:19:01 +05301545 /* Reset SD Clock Enable */
1546 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1547 clk &= ~SDHCI_CLOCK_CARD_EN;
1548 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1549
Russell King96d7b782014-04-25 12:59:26 +01001550 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001551 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301552
Kevin Liu52983382013-01-31 11:31:37 +08001553 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1554 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1555 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1556 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1557 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001558 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1559 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001560 u16 preset;
1561
1562 sdhci_enable_preset_value(host, true);
1563 preset = sdhci_get_preset_value(host);
1564 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1565 >> SDHCI_PRESET_DRV_SHIFT;
1566 }
1567
Arindam Nath49c468f2011-05-05 12:19:01 +05301568 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001569 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301570 } else
1571 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301572
Leandro Dorileob8352262007-07-25 23:47:04 +02001573 /*
1574 * Some (ENE) controllers go apeshit on some ios operation,
1575 * signalling timeout and CRC errors even on CMD0. Resetting
1576 * it on each ios seems to solve the problem.
1577 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301578 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001579 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001580
Pierre Ossman5f25a662006-10-04 02:15:39 -07001581 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001582 spin_unlock_irqrestore(&host->lock, flags);
1583}
1584
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001585static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1586{
1587 struct sdhci_host *host = mmc_priv(mmc);
1588
1589 sdhci_runtime_pm_get(host);
1590 sdhci_do_set_ios(host, ios);
1591 sdhci_runtime_pm_put(host);
1592}
1593
Kevin Liu94144a42013-02-28 17:35:53 +08001594static int sdhci_do_get_cd(struct sdhci_host *host)
1595{
1596 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1597
1598 if (host->flags & SDHCI_DEVICE_DEAD)
1599 return 0;
1600
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001601 /* If nonremovable, assume that the card is always present. */
1602 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
Kevin Liu94144a42013-02-28 17:35:53 +08001603 return 1;
1604
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001605 /*
1606 * Try slot gpio detect, if defined it take precedence
1607 * over build in controller functionality
1608 */
Kevin Liu94144a42013-02-28 17:35:53 +08001609 if (!IS_ERR_VALUE(gpio_cd))
1610 return !!gpio_cd;
1611
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001612 /* If polling, assume that the card is always present. */
1613 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1614 return 1;
1615
Kevin Liu94144a42013-02-28 17:35:53 +08001616 /* Host native card detect */
1617 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1618}
1619
1620static int sdhci_get_cd(struct mmc_host *mmc)
1621{
1622 struct sdhci_host *host = mmc_priv(mmc);
1623 int ret;
1624
1625 sdhci_runtime_pm_get(host);
1626 ret = sdhci_do_get_cd(host);
1627 sdhci_runtime_pm_put(host);
1628 return ret;
1629}
1630
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001631static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001632{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001633 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001634 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001635
Pierre Ossmand129bce2006-03-24 03:18:17 -08001636 spin_lock_irqsave(&host->lock, flags);
1637
Pierre Ossman1e728592008-04-16 19:13:13 +02001638 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001639 is_readonly = 0;
1640 else if (host->ops->get_ro)
1641 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001642 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001643 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1644 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001645
1646 spin_unlock_irqrestore(&host->lock, flags);
1647
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001648 /* This quirk needs to be replaced by a callback-function later */
1649 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1650 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001651}
1652
Takashi Iwai82b0e232011-04-21 20:26:38 +02001653#define SAMPLE_COUNT 5
1654
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001655static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001656{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001657 int i, ro_count;
1658
Takashi Iwai82b0e232011-04-21 20:26:38 +02001659 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001660 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001661
1662 ro_count = 0;
1663 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001664 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001665 if (++ro_count > SAMPLE_COUNT / 2)
1666 return 1;
1667 }
1668 msleep(30);
1669 }
1670 return 0;
1671}
1672
Adrian Hunter20758b62011-08-29 16:42:12 +03001673static void sdhci_hw_reset(struct mmc_host *mmc)
1674{
1675 struct sdhci_host *host = mmc_priv(mmc);
1676
1677 if (host->ops && host->ops->hw_reset)
1678 host->ops->hw_reset(host);
1679}
1680
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001681static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001682{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001683 struct sdhci_host *host = mmc_priv(mmc);
1684 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001685
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001686 sdhci_runtime_pm_get(host);
1687 ret = sdhci_do_get_ro(host);
1688 sdhci_runtime_pm_put(host);
1689 return ret;
1690}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001691
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001692static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1693{
Russell Kingbe138552014-04-25 12:55:56 +01001694 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001695 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001696 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001697 else
Russell Kingb537f942014-04-25 12:56:01 +01001698 host->ier &= ~SDHCI_INT_CARD_INT;
1699
1700 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1701 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001702 mmiowb();
1703 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001704}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001705
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001706static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1707{
1708 struct sdhci_host *host = mmc_priv(mmc);
1709 unsigned long flags;
1710
Russell Kingef104332014-04-25 12:55:41 +01001711 sdhci_runtime_pm_get(host);
1712
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001713 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001714 if (enable)
1715 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1716 else
1717 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1718
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001719 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001720 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001721
1722 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001723}
1724
Philip Rakity6231f3d2012-07-23 15:56:23 -07001725static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001726 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001727{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001728 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001729 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001730 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001731
1732 /*
1733 * Signal Voltage Switching is only applicable for Host Controllers
1734 * v3.00 and above.
1735 */
1736 if (host->version < SDHCI_SPEC_300)
1737 return 0;
1738
Philip Rakity6231f3d2012-07-23 15:56:23 -07001739 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001740
Fabio Estevam21f59982013-02-14 10:35:03 -02001741 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001742 case MMC_SIGNAL_VOLTAGE_330:
1743 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1744 ctrl &= ~SDHCI_CTRL_VDD_180;
1745 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1746
Tim Kryger3a48edc2014-06-13 10:13:56 -07001747 if (!IS_ERR(mmc->supply.vqmmc)) {
1748 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1749 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001750 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001751 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1752 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001753 return -EIO;
1754 }
1755 }
1756 /* Wait for 5ms */
1757 usleep_range(5000, 5500);
1758
1759 /* 3.3V regulator output should be stable within 5 ms */
1760 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1761 if (!(ctrl & SDHCI_CTRL_VDD_180))
1762 return 0;
1763
Joe Perches66061102014-09-12 14:56:56 -07001764 pr_warn("%s: 3.3V regulator output did not became stable\n",
1765 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001766
1767 return -EAGAIN;
1768 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001769 if (!IS_ERR(mmc->supply.vqmmc)) {
1770 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001771 1700000, 1950000);
1772 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001773 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1774 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001775 return -EIO;
1776 }
1777 }
1778
1779 /*
1780 * Enable 1.8V Signal Enable in the Host Control2
1781 * register
1782 */
1783 ctrl |= SDHCI_CTRL_VDD_180;
1784 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1785
Vincent Yang9d967a62015-01-20 16:05:15 +08001786 /* Some controller need to do more when switching */
1787 if (host->ops->voltage_switch)
1788 host->ops->voltage_switch(host);
1789
Kevin Liu20b92a32012-12-17 19:29:26 +08001790 /* 1.8V regulator output should be stable within 5 ms */
1791 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1792 if (ctrl & SDHCI_CTRL_VDD_180)
1793 return 0;
1794
Joe Perches66061102014-09-12 14:56:56 -07001795 pr_warn("%s: 1.8V regulator output did not became stable\n",
1796 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001797
1798 return -EAGAIN;
1799 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001800 if (!IS_ERR(mmc->supply.vqmmc)) {
1801 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1802 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001803 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001804 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1805 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001806 return -EIO;
1807 }
1808 }
1809 return 0;
1810 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301811 /* No signal voltage switch required */
1812 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001813 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301814}
1815
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001816static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001817 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001818{
1819 struct sdhci_host *host = mmc_priv(mmc);
1820 int err;
1821
1822 if (host->version < SDHCI_SPEC_300)
1823 return 0;
1824 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001825 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001826 sdhci_runtime_pm_put(host);
1827 return err;
1828}
1829
Kevin Liu20b92a32012-12-17 19:29:26 +08001830static int sdhci_card_busy(struct mmc_host *mmc)
1831{
1832 struct sdhci_host *host = mmc_priv(mmc);
1833 u32 present_state;
1834
1835 sdhci_runtime_pm_get(host);
1836 /* Check whether DAT[3:0] is 0000 */
1837 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1838 sdhci_runtime_pm_put(host);
1839
1840 return !(present_state & SDHCI_DATA_LVL_MASK);
1841}
1842
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001843static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1844{
1845 struct sdhci_host *host = mmc_priv(mmc);
1846 unsigned long flags;
1847
1848 spin_lock_irqsave(&host->lock, flags);
1849 host->flags |= SDHCI_HS400_TUNING;
1850 spin_unlock_irqrestore(&host->lock, flags);
1851
1852 return 0;
1853}
1854
Girish K S069c9f12012-01-06 09:56:39 +05301855static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301856{
Russell King4b6f37d2014-04-25 12:59:36 +01001857 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301858 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301859 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301860 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001861 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001862 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001863 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301864
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001865 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001866 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301867
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001868 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1869 host->flags &= ~SDHCI_HS400_TUNING;
1870
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001871 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1872 tuning_count = host->tuning_count;
1873
Arindam Nathb513ea22011-05-05 12:19:04 +05301874 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001875 * The Host Controller needs tuning in case of SDR104 and DDR50
1876 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1877 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301878 * If the Host Controller supports the HS200 mode then the
1879 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301880 */
Russell King4b6f37d2014-04-25 12:59:36 +01001881 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001882 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001883 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001884 err = -EINVAL;
1885 goto out_unlock;
1886
Russell King4b6f37d2014-04-25 12:59:36 +01001887 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001888 /*
1889 * Periodic re-tuning for HS400 is not expected to be needed, so
1890 * disable it here.
1891 */
1892 if (hs400_tuning)
1893 tuning_count = 0;
1894 break;
1895
Russell King4b6f37d2014-04-25 12:59:36 +01001896 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001897 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001898 break;
Girish K S069c9f12012-01-06 09:56:39 +05301899
Russell King4b6f37d2014-04-25 12:59:36 +01001900 case MMC_TIMING_UHS_SDR50:
1901 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1902 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1903 break;
1904 /* FALLTHROUGH */
1905
1906 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001907 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301908 }
1909
Dong Aisheng45251812013-09-13 19:11:30 +08001910 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001911 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001912 err = host->ops->platform_execute_tuning(host, opcode);
1913 sdhci_runtime_pm_put(host);
1914 return err;
1915 }
1916
Russell King4b6f37d2014-04-25 12:59:36 +01001917 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1918 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001919 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1920 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301921 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1922
1923 /*
1924 * As per the Host Controller spec v3.00, tuning command
1925 * generates Buffer Read Ready interrupt, so enable that.
1926 *
1927 * Note: The spec clearly says that when tuning sequence
1928 * is being performed, the controller does not generate
1929 * interrupts other than Buffer Read Ready interrupt. But
1930 * to make sure we don't hit a controller bug, we _only_
1931 * enable Buffer Read Ready interrupt here.
1932 */
Russell Kingb537f942014-04-25 12:56:01 +01001933 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1934 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301935
1936 /*
1937 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1938 * of loops reaches 40 times or a timeout of 150ms occurs.
1939 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301940 do {
1941 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001942 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301943
Girish K S069c9f12012-01-06 09:56:39 +05301944 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301945 cmd.arg = 0;
1946 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1947 cmd.retries = 0;
1948 cmd.data = NULL;
1949 cmd.error = 0;
1950
Al Cooper7ce45e92014-05-09 11:34:07 -04001951 if (tuning_loop_counter-- == 0)
1952 break;
1953
Arindam Nathb513ea22011-05-05 12:19:04 +05301954 mrq.cmd = &cmd;
1955 host->mrq = &mrq;
1956
1957 /*
1958 * In response to CMD19, the card sends 64 bytes of tuning
1959 * block to the Host Controller. So we set the block size
1960 * to 64 here.
1961 */
Girish K S069c9f12012-01-06 09:56:39 +05301962 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1963 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1964 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1965 SDHCI_BLOCK_SIZE);
1966 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1967 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1968 SDHCI_BLOCK_SIZE);
1969 } else {
1970 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1971 SDHCI_BLOCK_SIZE);
1972 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301973
1974 /*
1975 * The tuning block is sent by the card to the host controller.
1976 * So we set the TRNS_READ bit in the Transfer Mode register.
1977 * This also takes care of setting DMA Enable and Multi Block
1978 * Select in the same register to 0.
1979 */
1980 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1981
1982 sdhci_send_command(host, &cmd);
1983
1984 host->cmd = NULL;
1985 host->mrq = NULL;
1986
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001987 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301988 /* Wait for Buffer Read Ready interrupt */
1989 wait_event_interruptible_timeout(host->buf_ready_int,
1990 (host->tuning_done == 1),
1991 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001992 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301993
1994 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001995 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 +05301996 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1997 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1998 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1999 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2000
2001 err = -EIO;
2002 goto out;
2003 }
2004
2005 host->tuning_done = 0;
2006
2007 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07002008
2009 /* eMMC spec does not require a delay between tuning cycles */
2010 if (opcode == MMC_SEND_TUNING_BLOCK)
2011 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302012 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2013
2014 /*
2015 * The Host Driver has exhausted the maximum number of loops allowed,
2016 * so use fixed sampling frequency.
2017 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002018 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302019 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2020 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002021 }
2022 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002023 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002024 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302025 }
2026
2027out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002028 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002029 /*
2030 * In case tuning fails, host controllers which support
2031 * re-tuning can try tuning again at a later time, when the
2032 * re-tuning timer expires. So for these controllers, we
2033 * return 0. Since there might be other controllers who do not
2034 * have this capability, we return error for them.
2035 */
2036 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302037 }
2038
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002039 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302040
Russell Kingb537f942014-04-25 12:56:01 +01002041 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2042 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002043out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002044 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002045 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302046
2047 return err;
2048}
2049
Adrian Huntercb849642015-02-06 14:12:59 +02002050static int sdhci_select_drive_strength(struct mmc_card *card,
2051 unsigned int max_dtr, int host_drv,
2052 int card_drv, int *drv_type)
2053{
2054 struct sdhci_host *host = mmc_priv(card->host);
2055
2056 if (!host->ops->select_drive_strength)
2057 return 0;
2058
2059 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2060 card_drv, drv_type);
2061}
Kevin Liu52983382013-01-31 11:31:37 +08002062
2063static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302064{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302065 /* Host Controller v3.00 defines preset value registers */
2066 if (host->version < SDHCI_SPEC_300)
2067 return;
2068
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302069 /*
2070 * We only enable or disable Preset Value if they are not already
2071 * enabled or disabled respectively. Otherwise, we bail out.
2072 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002073 if (host->preset_enabled != enable) {
2074 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2075
2076 if (enable)
2077 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2078 else
2079 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2080
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302081 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002082
2083 if (enable)
2084 host->flags |= SDHCI_PV_ENABLED;
2085 else
2086 host->flags &= ~SDHCI_PV_ENABLED;
2087
2088 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302089 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002090}
2091
Haibo Chen348487c2014-12-09 17:04:05 +08002092static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2093 int err)
2094{
2095 struct sdhci_host *host = mmc_priv(mmc);
2096 struct mmc_data *data = mrq->data;
2097
Russell King771a3dc2016-01-26 13:40:53 +00002098 if (data->host_cookie == COOKIE_GIVEN ||
2099 data->host_cookie == COOKIE_MAPPED)
2100 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2101 data->flags & MMC_DATA_WRITE ?
2102 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2103
2104 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002105}
2106
2107static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Haibo Chend31911b2015-08-25 10:02:11 +08002108 struct mmc_data *data)
Haibo Chen348487c2014-12-09 17:04:05 +08002109{
2110 int sg_count;
2111
Haibo Chend31911b2015-08-25 10:02:11 +08002112 if (data->host_cookie == COOKIE_MAPPED) {
2113 data->host_cookie = COOKIE_GIVEN;
2114 return data->sg_count;
Haibo Chen348487c2014-12-09 17:04:05 +08002115 }
2116
Haibo Chend31911b2015-08-25 10:02:11 +08002117 WARN_ON(data->host_cookie == COOKIE_GIVEN);
Haibo Chen348487c2014-12-09 17:04:05 +08002118
Haibo Chend31911b2015-08-25 10:02:11 +08002119 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2120 data->flags & MMC_DATA_WRITE ?
2121 DMA_TO_DEVICE : DMA_FROM_DEVICE);
Haibo Chen348487c2014-12-09 17:04:05 +08002122
2123 if (sg_count == 0)
Haibo Chend31911b2015-08-25 10:02:11 +08002124 return -ENOSPC;
Haibo Chen348487c2014-12-09 17:04:05 +08002125
Haibo Chend31911b2015-08-25 10:02:11 +08002126 data->sg_count = sg_count;
2127 data->host_cookie = COOKIE_MAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002128
2129 return sg_count;
2130}
2131
2132static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2133 bool is_first_req)
2134{
2135 struct sdhci_host *host = mmc_priv(mmc);
2136
Haibo Chend31911b2015-08-25 10:02:11 +08002137 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002138
2139 if (host->flags & SDHCI_REQ_USE_DMA)
Haibo Chend31911b2015-08-25 10:02:11 +08002140 sdhci_pre_dma_transfer(host, mrq->data);
Haibo Chen348487c2014-12-09 17:04:05 +08002141}
2142
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002143static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002144{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002145 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002146 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002147 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002148
Christian Daudt722e1282013-06-20 14:26:36 -07002149 /* First check if client has provided their own card event */
2150 if (host->ops->card_event)
2151 host->ops->card_event(host);
2152
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002153 present = sdhci_do_get_cd(host);
2154
Pierre Ossmand129bce2006-03-24 03:18:17 -08002155 spin_lock_irqsave(&host->lock, flags);
2156
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002157 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002158 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302159 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002160 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302161 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002162 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002163
Russell King03231f92014-04-25 12:57:12 +01002164 sdhci_do_reset(host, SDHCI_RESET_CMD);
2165 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002166
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002167 host->mrq->cmd->error = -ENOMEDIUM;
2168 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002169 }
2170
2171 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002172}
2173
2174static const struct mmc_host_ops sdhci_ops = {
2175 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002176 .post_req = sdhci_post_req,
2177 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002178 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002179 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002180 .get_ro = sdhci_get_ro,
2181 .hw_reset = sdhci_hw_reset,
2182 .enable_sdio_irq = sdhci_enable_sdio_irq,
2183 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002184 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002185 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002186 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002187 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002188 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002189};
2190
2191/*****************************************************************************\
2192 * *
2193 * Tasklets *
2194 * *
2195\*****************************************************************************/
2196
Pierre Ossmand129bce2006-03-24 03:18:17 -08002197static void sdhci_tasklet_finish(unsigned long param)
2198{
2199 struct sdhci_host *host;
2200 unsigned long flags;
2201 struct mmc_request *mrq;
2202
2203 host = (struct sdhci_host*)param;
2204
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002205 spin_lock_irqsave(&host->lock, flags);
2206
Chris Ball0c9c99a2011-04-27 17:35:31 -04002207 /*
2208 * If this tasklet gets rescheduled while running, it will
2209 * be run again afterwards but without any active request.
2210 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002211 if (!host->mrq) {
2212 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002213 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002214 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002215
2216 del_timer(&host->timer);
2217
2218 mrq = host->mrq;
2219
Pierre Ossmand129bce2006-03-24 03:18:17 -08002220 /*
Russell King054cedf2016-01-26 13:40:42 +00002221 * Always unmap the data buffers if they were mapped by
2222 * sdhci_prepare_data() whenever we finish with a request.
2223 * This avoids leaking DMA mappings on error.
2224 */
2225 if (host->flags & SDHCI_REQ_USE_DMA) {
2226 struct mmc_data *data = mrq->data;
2227
2228 if (data && data->host_cookie == COOKIE_MAPPED) {
2229 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2230 (data->flags & MMC_DATA_READ) ?
2231 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2232 data->host_cookie = COOKIE_UNMAPPED;
2233 }
2234 }
2235
2236 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002237 * The controller needs a reset of internal state machines
2238 * upon error conditions.
2239 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002240 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002241 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002242 (mrq->sbc && mrq->sbc->error) ||
2243 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2244 (mrq->data->stop && mrq->data->stop->error))) ||
2245 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002246
2247 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002248 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002249 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002250 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002251
2252 /* Spec says we should do both at the same time, but Ricoh
2253 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002254 sdhci_do_reset(host, SDHCI_RESET_CMD);
2255 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002256 }
2257
2258 host->mrq = NULL;
2259 host->cmd = NULL;
2260 host->data = NULL;
2261
Pierre Ossmanf9134312008-12-21 17:01:48 +01002262#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002263 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002264#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002265
Pierre Ossman5f25a662006-10-04 02:15:39 -07002266 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002267 spin_unlock_irqrestore(&host->lock, flags);
2268
2269 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002270 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002271}
2272
2273static void sdhci_timeout_timer(unsigned long data)
2274{
2275 struct sdhci_host *host;
2276 unsigned long flags;
2277
2278 host = (struct sdhci_host*)data;
2279
2280 spin_lock_irqsave(&host->lock, flags);
2281
2282 if (host->mrq) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002283 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2284 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002285 sdhci_dumpregs(host);
2286
2287 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002288 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002289 sdhci_finish_data(host);
2290 } else {
2291 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002292 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002293 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002294 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002295
2296 tasklet_schedule(&host->finish_tasklet);
2297 }
2298 }
2299
Pierre Ossman5f25a662006-10-04 02:15:39 -07002300 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002301 spin_unlock_irqrestore(&host->lock, flags);
2302}
2303
2304/*****************************************************************************\
2305 * *
2306 * Interrupt handling *
2307 * *
2308\*****************************************************************************/
2309
Adrian Hunter61541392014-09-24 10:27:27 +03002310static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002311{
2312 BUG_ON(intmask == 0);
2313
2314 if (!host->cmd) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002315 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2316 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002317 sdhci_dumpregs(host);
2318 return;
2319 }
2320
Russell Kingec014cb2016-01-26 13:39:39 +00002321 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2322 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2323 if (intmask & SDHCI_INT_TIMEOUT)
2324 host->cmd->error = -ETIMEDOUT;
2325 else
2326 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002327
Russell King71fcbda2016-01-26 13:39:45 +00002328 /*
2329 * If this command initiates a data phase and a response
2330 * CRC error is signalled, the card can start transferring
2331 * data - the card may have received the command without
2332 * error. We must not terminate the mmc_request early.
2333 *
2334 * If the card did not receive the command or returned an
2335 * error which prevented it sending data, the data phase
2336 * will time out.
2337 */
2338 if (host->cmd->data &&
2339 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2340 SDHCI_INT_CRC) {
2341 host->cmd = NULL;
2342 return;
2343 }
2344
Pierre Ossmand129bce2006-03-24 03:18:17 -08002345 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002346 return;
2347 }
2348
2349 /*
2350 * The host can send and interrupt when the busy state has
2351 * ended, allowing us to wait without wasting CPU cycles.
2352 * Unfortunately this is overloaded on the "data complete"
2353 * interrupt, so we need to take some care when handling
2354 * it.
2355 *
2356 * Note: The 1.0 specification is a bit ambiguous about this
2357 * feature so there might be some problems with older
2358 * controllers.
2359 */
2360 if (host->cmd->flags & MMC_RSP_BUSY) {
2361 if (host->cmd->data)
Marek Vasut2e4456f2015-11-18 10:47:02 +01002362 DBG("Cannot wait for busy signal when also doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002363 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2364 && !host->busy_handle) {
2365 /* Mark that command complete before busy is ended */
2366 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002367 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002368 }
Ben Dooksf9454052009-02-20 20:33:08 +03002369
2370 /* The controller does not support the end-of-busy IRQ,
2371 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002372 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2373 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2374 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002375 }
2376
2377 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002378 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002379}
2380
George G. Davis0957c332010-02-18 12:32:12 -05002381#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002382static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002383{
2384 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002385 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002386
2387 sdhci_dumpregs(host);
2388
2389 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002390 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002391
Adrian Huntere57a5f62014-11-04 12:42:46 +02002392 if (host->flags & SDHCI_USE_64_BIT_DMA)
2393 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2394 name, desc, le32_to_cpu(dma_desc->addr_hi),
2395 le32_to_cpu(dma_desc->addr_lo),
2396 le16_to_cpu(dma_desc->len),
2397 le16_to_cpu(dma_desc->cmd));
2398 else
2399 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2400 name, desc, le32_to_cpu(dma_desc->addr_lo),
2401 le16_to_cpu(dma_desc->len),
2402 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002403
Adrian Hunter76fe3792014-11-04 12:42:42 +02002404 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002405
Adrian Hunter05452302014-11-04 12:42:45 +02002406 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002407 break;
2408 }
2409}
2410#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002411static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002412#endif
2413
Pierre Ossmand129bce2006-03-24 03:18:17 -08002414static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2415{
Girish K S069c9f12012-01-06 09:56:39 +05302416 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002417 BUG_ON(intmask == 0);
2418
Arindam Nathb513ea22011-05-05 12:19:04 +05302419 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2420 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302421 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2422 if (command == MMC_SEND_TUNING_BLOCK ||
2423 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302424 host->tuning_done = 1;
2425 wake_up(&host->buf_ready_int);
2426 return;
2427 }
2428 }
2429
Pierre Ossmand129bce2006-03-24 03:18:17 -08002430 if (!host->data) {
2431 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002432 * The "data complete" interrupt is also used to
2433 * indicate that a busy state has ended. See comment
2434 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002435 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002436 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002437 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2438 host->cmd->error = -ETIMEDOUT;
2439 tasklet_schedule(&host->finish_tasklet);
2440 return;
2441 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002442 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002443 /*
2444 * Some cards handle busy-end interrupt
2445 * before the command completed, so make
2446 * sure we do things in the proper order.
2447 */
2448 if (host->busy_handle)
2449 sdhci_finish_command(host);
2450 else
2451 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002452 return;
2453 }
2454 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002455
Marek Vasut2e4456f2015-11-18 10:47:02 +01002456 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2457 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002458 sdhci_dumpregs(host);
2459
2460 return;
2461 }
2462
2463 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002464 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002465 else if (intmask & SDHCI_INT_DATA_END_BIT)
2466 host->data->error = -EILSEQ;
2467 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2468 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2469 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002470 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002471 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302472 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002473 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002474 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002475 if (host->ops->adma_workaround)
2476 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002477 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002478
Pierre Ossman17b04292007-07-22 22:18:46 +02002479 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002480 sdhci_finish_data(host);
2481 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002482 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002483 sdhci_transfer_pio(host);
2484
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002485 /*
2486 * We currently don't do anything fancy with DMA
2487 * boundaries, but as we can't disable the feature
2488 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002489 *
2490 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2491 * should return a valid address to continue from, but as
2492 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002493 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002494 if (intmask & SDHCI_INT_DMA_END) {
2495 u32 dmastart, dmanow;
2496 dmastart = sg_dma_address(host->data->sg);
2497 dmanow = dmastart + host->data->bytes_xfered;
2498 /*
2499 * Force update to the next DMA block boundary.
2500 */
2501 dmanow = (dmanow &
2502 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2503 SDHCI_DEFAULT_BOUNDARY_SIZE;
2504 host->data->bytes_xfered = dmanow - dmastart;
2505 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2506 " next 0x%08x\n",
2507 mmc_hostname(host->mmc), dmastart,
2508 host->data->bytes_xfered, dmanow);
2509 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2510 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002511
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002512 if (intmask & SDHCI_INT_DATA_END) {
2513 if (host->cmd) {
2514 /*
2515 * Data managed to finish before the
2516 * command completed. Make sure we do
2517 * things in the proper order.
2518 */
2519 host->data_early = 1;
2520 } else {
2521 sdhci_finish_data(host);
2522 }
2523 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002524 }
2525}
2526
David Howells7d12e782006-10-05 14:55:46 +01002527static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002528{
Russell King781e9892014-04-25 12:55:46 +01002529 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002530 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002531 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002532 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002533
2534 spin_lock(&host->lock);
2535
Russell Kingbe138552014-04-25 12:55:56 +01002536 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002537 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002538 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002539 }
2540
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002541 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002542 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002543 result = IRQ_NONE;
2544 goto out;
2545 }
2546
Russell King41005002014-04-25 12:55:36 +01002547 do {
2548 /* Clear selected interrupts. */
2549 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2550 SDHCI_INT_BUS_POWER);
2551 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002552
Russell King41005002014-04-25 12:55:36 +01002553 DBG("*** %s got interrupt: 0x%08x\n",
2554 mmc_hostname(host->mmc), intmask);
2555
2556 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2557 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2558 SDHCI_CARD_PRESENT;
2559
2560 /*
2561 * There is a observation on i.mx esdhc. INSERT
2562 * bit will be immediately set again when it gets
2563 * cleared, if a card is inserted. We have to mask
2564 * the irq to prevent interrupt storm which will
2565 * freeze the system. And the REMOVE gets the
2566 * same situation.
2567 *
2568 * More testing are needed here to ensure it works
2569 * for other platforms though.
2570 */
Russell Kingb537f942014-04-25 12:56:01 +01002571 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2572 SDHCI_INT_CARD_REMOVE);
2573 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2574 SDHCI_INT_CARD_INSERT;
2575 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2576 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002577
2578 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2579 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002580
2581 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2582 SDHCI_INT_CARD_REMOVE);
2583 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002584 }
2585
2586 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002587 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2588 &intmask);
Russell King41005002014-04-25 12:55:36 +01002589
2590 if (intmask & SDHCI_INT_DATA_MASK)
2591 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2592
2593 if (intmask & SDHCI_INT_BUS_POWER)
2594 pr_err("%s: Card is consuming too much power!\n",
2595 mmc_hostname(host->mmc));
2596
Russell King781e9892014-04-25 12:55:46 +01002597 if (intmask & SDHCI_INT_CARD_INT) {
2598 sdhci_enable_sdio_irq_nolock(host, false);
2599 host->thread_isr |= SDHCI_INT_CARD_INT;
2600 result = IRQ_WAKE_THREAD;
2601 }
Russell King41005002014-04-25 12:55:36 +01002602
2603 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2604 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2605 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2606 SDHCI_INT_CARD_INT);
2607
2608 if (intmask) {
2609 unexpected |= intmask;
2610 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2611 }
2612
Russell King781e9892014-04-25 12:55:46 +01002613 if (result == IRQ_NONE)
2614 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002615
2616 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002617 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002618out:
2619 spin_unlock(&host->lock);
2620
Alexander Stein6379b232012-03-14 09:52:10 +01002621 if (unexpected) {
2622 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2623 mmc_hostname(host->mmc), unexpected);
2624 sdhci_dumpregs(host);
2625 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002626
Pierre Ossmand129bce2006-03-24 03:18:17 -08002627 return result;
2628}
2629
Russell King781e9892014-04-25 12:55:46 +01002630static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2631{
2632 struct sdhci_host *host = dev_id;
2633 unsigned long flags;
2634 u32 isr;
2635
2636 spin_lock_irqsave(&host->lock, flags);
2637 isr = host->thread_isr;
2638 host->thread_isr = 0;
2639 spin_unlock_irqrestore(&host->lock, flags);
2640
Russell King3560db82014-04-25 12:55:51 +01002641 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2642 sdhci_card_event(host->mmc);
2643 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2644 }
2645
Russell King781e9892014-04-25 12:55:46 +01002646 if (isr & SDHCI_INT_CARD_INT) {
2647 sdio_run_irqs(host->mmc);
2648
2649 spin_lock_irqsave(&host->lock, flags);
2650 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2651 sdhci_enable_sdio_irq_nolock(host, true);
2652 spin_unlock_irqrestore(&host->lock, flags);
2653 }
2654
2655 return isr ? IRQ_HANDLED : IRQ_NONE;
2656}
2657
Pierre Ossmand129bce2006-03-24 03:18:17 -08002658/*****************************************************************************\
2659 * *
2660 * Suspend/resume *
2661 * *
2662\*****************************************************************************/
2663
2664#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002665void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2666{
2667 u8 val;
2668 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2669 | SDHCI_WAKE_ON_INT;
2670
2671 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2672 val |= mask ;
2673 /* Avoid fake wake up */
2674 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2675 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2676 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2677}
2678EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2679
Fabio Estevam0b10f472014-08-30 14:53:13 -03002680static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002681{
2682 u8 val;
2683 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2684 | SDHCI_WAKE_ON_INT;
2685
2686 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2687 val &= ~mask;
2688 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2689}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002690
Manuel Lauss29495aa2011-11-03 11:09:45 +01002691int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002692{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002693 sdhci_disable_card_detection(host);
2694
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002695 mmc_retune_timer_stop(host->mmc);
2696 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302697
Kevin Liuad080d72013-01-05 17:21:33 +08002698 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002699 host->ier = 0;
2700 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2701 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002702 free_irq(host->irq, host);
2703 } else {
2704 sdhci_enable_irq_wakeups(host);
2705 enable_irq_wake(host->irq);
2706 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002707 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002708}
2709
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002710EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002711
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002712int sdhci_resume_host(struct sdhci_host *host)
2713{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002714 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002715
Richard Röjforsa13abc72009-09-22 16:45:30 -07002716 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002717 if (host->ops->enable_dma)
2718 host->ops->enable_dma(host);
2719 }
2720
Adrian Hunter6308d292012-02-07 14:48:54 +02002721 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2722 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2723 /* Card keeps power but host controller does not */
2724 sdhci_init(host, 0);
2725 host->pwr = 0;
2726 host->clock = 0;
2727 sdhci_do_set_ios(host, &host->mmc->ios);
2728 } else {
2729 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2730 mmiowb();
2731 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002732
Haibo Chen14a7b41642015-09-15 18:32:58 +08002733 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2734 ret = request_threaded_irq(host->irq, sdhci_irq,
2735 sdhci_thread_irq, IRQF_SHARED,
2736 mmc_hostname(host->mmc), host);
2737 if (ret)
2738 return ret;
2739 } else {
2740 sdhci_disable_irq_wakeups(host);
2741 disable_irq_wake(host->irq);
2742 }
2743
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002744 sdhci_enable_card_detection(host);
2745
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002746 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002747}
2748
2749EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002750
2751static int sdhci_runtime_pm_get(struct sdhci_host *host)
2752{
2753 return pm_runtime_get_sync(host->mmc->parent);
2754}
2755
2756static int sdhci_runtime_pm_put(struct sdhci_host *host)
2757{
2758 pm_runtime_mark_last_busy(host->mmc->parent);
2759 return pm_runtime_put_autosuspend(host->mmc->parent);
2760}
2761
Adrian Hunterf0710a52013-05-06 12:17:32 +03002762static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2763{
Adrian Hunter5c671c42015-11-26 14:00:50 +02002764 if (host->bus_on)
Adrian Hunterf0710a52013-05-06 12:17:32 +03002765 return;
2766 host->bus_on = true;
2767 pm_runtime_get_noresume(host->mmc->parent);
2768}
2769
2770static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2771{
Adrian Hunter5c671c42015-11-26 14:00:50 +02002772 if (!host->bus_on)
Adrian Hunterf0710a52013-05-06 12:17:32 +03002773 return;
2774 host->bus_on = false;
2775 pm_runtime_put_noidle(host->mmc->parent);
2776}
2777
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002778int sdhci_runtime_suspend_host(struct sdhci_host *host)
2779{
2780 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002781
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002782 mmc_retune_timer_stop(host->mmc);
2783 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002784
2785 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002786 host->ier &= SDHCI_INT_CARD_INT;
2787 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2788 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002789 spin_unlock_irqrestore(&host->lock, flags);
2790
Russell King781e9892014-04-25 12:55:46 +01002791 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002792
2793 spin_lock_irqsave(&host->lock, flags);
2794 host->runtime_suspended = true;
2795 spin_unlock_irqrestore(&host->lock, flags);
2796
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002797 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002798}
2799EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2800
2801int sdhci_runtime_resume_host(struct sdhci_host *host)
2802{
2803 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002804 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002805
2806 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2807 if (host->ops->enable_dma)
2808 host->ops->enable_dma(host);
2809 }
2810
2811 sdhci_init(host, 0);
2812
2813 /* Force clock and power re-program */
2814 host->pwr = 0;
2815 host->clock = 0;
Jisheng Zhang3396e732015-01-29 17:42:12 +08002816 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002817 sdhci_do_set_ios(host, &host->mmc->ios);
2818
Kevin Liu52983382013-01-31 11:31:37 +08002819 if ((host_flags & SDHCI_PV_ENABLED) &&
2820 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2821 spin_lock_irqsave(&host->lock, flags);
2822 sdhci_enable_preset_value(host, true);
2823 spin_unlock_irqrestore(&host->lock, flags);
2824 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002825
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002826 spin_lock_irqsave(&host->lock, flags);
2827
2828 host->runtime_suspended = false;
2829
2830 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002831 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002832 sdhci_enable_sdio_irq_nolock(host, true);
2833
2834 /* Enable Card Detection */
2835 sdhci_enable_card_detection(host);
2836
2837 spin_unlock_irqrestore(&host->lock, flags);
2838
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002839 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002840}
2841EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2842
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002843#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002844
Pierre Ossmand129bce2006-03-24 03:18:17 -08002845/*****************************************************************************\
2846 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002847 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002848 * *
2849\*****************************************************************************/
2850
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002851struct sdhci_host *sdhci_alloc_host(struct device *dev,
2852 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002853{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002854 struct mmc_host *mmc;
2855 struct sdhci_host *host;
2856
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002857 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002858
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002859 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002860 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002861 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002862
2863 host = mmc_priv(mmc);
2864 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002865 host->mmc_host_ops = sdhci_ops;
2866 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002867
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002868 return host;
2869}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002870
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002871EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002872
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002873int sdhci_add_host(struct sdhci_host *host)
2874{
2875 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002876 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302877 u32 max_current_caps;
2878 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002879 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08002880 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002881 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002882
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002883 WARN_ON(host == NULL);
2884 if (host == NULL)
2885 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002886
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002887 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002888
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002889 if (debug_quirks)
2890 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002891 if (debug_quirks2)
2892 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002893
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002894 override_timeout_clk = host->timeout_clk;
2895
Russell King03231f92014-04-25 12:57:12 +01002896 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002897
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002898 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002899 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2900 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002901 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002902 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
2903 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002904 }
2905
Arindam Nathf2119df2011-05-05 12:18:57 +05302906 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002907 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002908
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002909 if (host->version >= SDHCI_SPEC_300)
2910 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2911 host->caps1 :
2912 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302913
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002914 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002915 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302916 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002917 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002918 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002919 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002920
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002921 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002922 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002923 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002924 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002925 }
2926
Arindam Nathf2119df2011-05-05 12:18:57 +05302927 if ((host->version >= SDHCI_SPEC_200) &&
2928 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002929 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002930
2931 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2932 (host->flags & SDHCI_USE_ADMA)) {
2933 DBG("Disabling ADMA as it is marked broken\n");
2934 host->flags &= ~SDHCI_USE_ADMA;
2935 }
2936
Adrian Huntere57a5f62014-11-04 12:42:46 +02002937 /*
2938 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2939 * and *must* do 64-bit DMA. A driver has the opportunity to change
2940 * that during the first call to ->enable_dma(). Similarly
2941 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2942 * implement.
2943 */
Al Cooper5eaa7472016-02-10 15:25:39 -05002944 if (caps[0] & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02002945 host->flags |= SDHCI_USE_64_BIT_DMA;
2946
Richard Röjforsa13abc72009-09-22 16:45:30 -07002947 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002948 if (host->ops->enable_dma) {
2949 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07002950 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002951 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002952 host->flags &=
2953 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002954 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002955 }
2956 }
2957
Adrian Huntere57a5f62014-11-04 12:42:46 +02002958 /* SDMA does not support 64-bit DMA */
2959 if (host->flags & SDHCI_USE_64_BIT_DMA)
2960 host->flags &= ~SDHCI_USE_SDMA;
2961
Pierre Ossman2134a922008-06-28 18:28:51 +02002962 if (host->flags & SDHCI_USE_ADMA) {
2963 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002964 * The DMA descriptor table size is calculated as the maximum
2965 * number of segments times 2, to allow for an alignment
2966 * descriptor for each segment, plus 1 for a nop end descriptor,
2967 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002968 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02002969 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2970 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2971 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002972 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002973 } else {
2974 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2975 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002976 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002977 }
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002978 host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +02002979 host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002980 &host->adma_addr,
2981 GFP_KERNEL);
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002982 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kingedd63fc2016-01-26 13:39:50 +00002983 host->align_buffer = dma_alloc_coherent(mmc_dev(mmc),
2984 host->align_buffer_sz,
2985 &host->align_addr,
2986 GFP_KERNEL);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002987 if (!host->adma_table || !host->align_buffer) {
Peng Fan7ac02032015-06-22 11:41:23 +08002988 if (host->adma_table)
2989 dma_free_coherent(mmc_dev(mmc),
2990 host->adma_table_sz,
2991 host->adma_table,
2992 host->adma_addr);
Russell Kingedd63fc2016-01-26 13:39:50 +00002993 if (host->align_buffer)
2994 dma_free_coherent(mmc_dev(mmc),
2995 host->align_buffer_sz,
2996 host->align_buffer,
2997 host->align_addr);
Joe Perches66061102014-09-12 14:56:56 -07002998 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002999 mmc_hostname(mmc));
3000 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003001 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01003002 host->align_buffer = NULL;
Adrian Hunter04a5ae62015-11-26 14:00:49 +02003003 } else if (host->adma_addr & (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07003004 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3005 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003006 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter76fe3792014-11-04 12:42:42 +02003007 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003008 host->adma_table, host->adma_addr);
Russell Kingedd63fc2016-01-26 13:39:50 +00003009 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz,
3010 host->align_buffer, host->align_addr);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003011 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01003012 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003013 }
Russell Kingedd63fc2016-01-26 13:39:50 +00003014
3015 /* dma_alloc_coherent returns page aligned and sized buffers */
3016 BUG_ON(host->align_addr & SDHCI_ADMA2_MASK);
Pierre Ossman2134a922008-06-28 18:28:51 +02003017 }
3018
Pierre Ossman76591502008-07-21 00:32:11 +02003019 /*
3020 * If we use DMA, then it's up to the caller to set the DMA
3021 * mask, but PIO does not need the hw shim so we set a new
3022 * mask here in that case.
3023 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003024 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003025 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003026 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003027 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003028
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003029 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05303030 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003031 >> SDHCI_CLOCK_BASE_SHIFT;
3032 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303033 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003034 >> SDHCI_CLOCK_BASE_SHIFT;
3035
Pierre Ossmand129bce2006-03-24 03:18:17 -08003036 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003037 if (host->max_clk == 0 || host->quirks &
3038 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003039 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003040 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3041 mmc_hostname(mmc));
Ben Dooks4240ff02009-03-17 00:13:57 +03003042 return -ENODEV;
3043 }
3044 host->max_clk = host->ops->get_max_clock(host);
3045 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003046
3047 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303048 * In case of Host Controller v3.00, find out whether clock
3049 * multiplier is supported.
3050 */
3051 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3052 SDHCI_CLOCK_MUL_SHIFT;
3053
3054 /*
3055 * In case the value in Clock Multiplier is 0, then programmable
3056 * clock mode is not supported, otherwise the actual clock
3057 * multiplier is one more than the value of Clock Multiplier
3058 * in the Capabilities Register.
3059 */
3060 if (host->clk_mul)
3061 host->clk_mul += 1;
3062
3063 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003064 * Set host parameters.
3065 */
Dong Aisheng59241752015-07-22 20:53:07 +08003066 max_clk = host->max_clk;
3067
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003068 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003069 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303070 else if (host->version >= SDHCI_SPEC_300) {
3071 if (host->clk_mul) {
3072 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003073 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303074 } else
3075 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3076 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003077 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003078
Dong Aisheng59241752015-07-22 20:53:07 +08003079 if (!mmc->f_max || (mmc->f_max && (mmc->f_max > max_clk)))
3080 mmc->f_max = max_clk;
3081
Aisheng Dong28aab052014-08-27 15:26:31 +08003082 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3083 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3084 SDHCI_TIMEOUT_CLK_SHIFT;
3085 if (host->timeout_clk == 0) {
3086 if (host->ops->get_timeout_clock) {
3087 host->timeout_clk =
3088 host->ops->get_timeout_clock(host);
3089 } else {
3090 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3091 mmc_hostname(mmc));
3092 return -ENODEV;
3093 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003094 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003095
Aisheng Dong28aab052014-08-27 15:26:31 +08003096 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3097 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003098
Aisheng Dong28aab052014-08-27 15:26:31 +08003099 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003100 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003101 mmc->max_busy_timeout /= host->timeout_clk;
3102 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003103
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003104 if (override_timeout_clk)
3105 host->timeout_clk = override_timeout_clk;
3106
Andrei Warkentine89d4562011-05-23 15:06:37 -05003107 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003108 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003109
3110 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3111 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003112
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003113 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003114 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003115 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003116 !(host->flags & SDHCI_USE_SDMA)) &&
3117 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003118 host->flags |= SDHCI_AUTO_CMD23;
3119 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3120 } else {
3121 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3122 }
3123
Philip Rakity15ec4462010-11-19 16:48:39 -05003124 /*
3125 * A controller may support 8-bit width, but the board itself
3126 * might not have the pins brought out. Boards that support
3127 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3128 * their platform code before calling sdhci_add_host(), and we
3129 * won't assume 8-bit width for hosts without that CAP.
3130 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003131 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003132 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003133
Jerry Huang63ef5d82012-10-25 13:47:19 +08003134 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3135 mmc->caps &= ~MMC_CAP_CMD23;
3136
Arindam Nathf2119df2011-05-05 12:18:57 +05303137 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003138 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003139
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003140 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Ivan T. Ivanovc31d22e2015-07-06 15:16:20 +03003141 !(mmc->caps & MMC_CAP_NONREMOVABLE) &&
3142 IS_ERR_VALUE(mmc_gpio_get_cd(host->mmc)))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003143 mmc->caps |= MMC_CAP_NEEDS_POLL;
3144
Tim Kryger3a48edc2014-06-13 10:13:56 -07003145 /* If there are external regulators, get them */
3146 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3147 return -EPROBE_DEFER;
3148
Philip Rakity6231f3d2012-07-23 15:56:23 -07003149 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003150 if (!IS_ERR(mmc->supply.vqmmc)) {
3151 ret = regulator_enable(mmc->supply.vqmmc);
3152 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3153 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003154 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3155 SDHCI_SUPPORT_SDR50 |
3156 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003157 if (ret) {
3158 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3159 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003160 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003161 }
Kevin Liu8363c372012-11-17 17:55:51 -05003162 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003163
Daniel Drake6a661802012-11-25 13:01:19 -05003164 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3165 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3166 SDHCI_SUPPORT_DDR50);
3167
Al Cooper4188bba2012-03-16 15:54:17 -04003168 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3169 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3170 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303171 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3172
3173 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003174 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303175 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003176 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3177 * field can be promoted to support HS200.
3178 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003179 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003180 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003181 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303182 mmc->caps |= MMC_CAP_UHS_SDR50;
3183
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003184 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3185 (caps[1] & SDHCI_SUPPORT_HS400))
3186 mmc->caps2 |= MMC_CAP2_HS400;
3187
Adrian Hunter549c0b12014-11-06 15:19:05 +02003188 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3189 (IS_ERR(mmc->supply.vqmmc) ||
3190 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3191 1300000)))
3192 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3193
Micky Ching9107ebb2014-02-21 18:40:35 +08003194 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3195 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303196 mmc->caps |= MMC_CAP_UHS_DDR50;
3197
Girish K S069c9f12012-01-06 09:56:39 +05303198 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303199 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3200 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3201
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003202 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303203 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003204 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303205
Arindam Nathd6d50a12011-05-05 12:18:59 +05303206 /* Driver Type(s) (A, C, D) supported by the host */
3207 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3208 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3209 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3210 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3211 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3212 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3213
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303214 /* Initial value for re-tuning timer count */
3215 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3216 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3217
3218 /*
3219 * In case Re-tuning Timer is not disabled, the actual value of
3220 * re-tuning timer will be 2 ^ (n - 1).
3221 */
3222 if (host->tuning_count)
3223 host->tuning_count = 1 << (host->tuning_count - 1);
3224
3225 /* Re-tuning mode supported by the Host Controller */
3226 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3227 SDHCI_RETUNING_MODE_SHIFT;
3228
Takashi Iwai8f230f42010-12-08 10:04:30 +01003229 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003230
Arindam Nathf2119df2011-05-05 12:18:57 +05303231 /*
3232 * According to SD Host Controller spec v3.00, if the Host System
3233 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3234 * the value is meaningful only if Voltage Support in the Capabilities
3235 * register is set. The actual current value is 4 times the register
3236 * value.
3237 */
3238 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003239 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003240 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003241 if (curr > 0) {
3242
3243 /* convert to SDHCI_MAX_CURRENT format */
3244 curr = curr/1000; /* convert to mA */
3245 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3246
3247 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3248 max_current_caps =
3249 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3250 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3251 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3252 }
3253 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303254
3255 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003256 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303257
Aaron Lu55c46652012-07-04 13:31:48 +08003258 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303259 SDHCI_MAX_CURRENT_330_MASK) >>
3260 SDHCI_MAX_CURRENT_330_SHIFT) *
3261 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303262 }
3263 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003264 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303265
Aaron Lu55c46652012-07-04 13:31:48 +08003266 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303267 SDHCI_MAX_CURRENT_300_MASK) >>
3268 SDHCI_MAX_CURRENT_300_SHIFT) *
3269 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303270 }
3271 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003272 ocr_avail |= MMC_VDD_165_195;
3273
Aaron Lu55c46652012-07-04 13:31:48 +08003274 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303275 SDHCI_MAX_CURRENT_180_MASK) >>
3276 SDHCI_MAX_CURRENT_180_SHIFT) *
3277 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303278 }
3279
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003280 /* If OCR set by host, use it instead. */
3281 if (host->ocr_mask)
3282 ocr_avail = host->ocr_mask;
3283
3284 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003285 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003286 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003287
Takashi Iwai8f230f42010-12-08 10:04:30 +01003288 mmc->ocr_avail = ocr_avail;
3289 mmc->ocr_avail_sdio = ocr_avail;
3290 if (host->ocr_avail_sdio)
3291 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3292 mmc->ocr_avail_sd = ocr_avail;
3293 if (host->ocr_avail_sd)
3294 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3295 else /* normal SD controllers don't support 1.8V */
3296 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3297 mmc->ocr_avail_mmc = ocr_avail;
3298 if (host->ocr_avail_mmc)
3299 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003300
3301 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003302 pr_err("%s: Hardware doesn't report any support voltages.\n",
3303 mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003304 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003305 }
3306
Pierre Ossmand129bce2006-03-24 03:18:17 -08003307 spin_lock_init(&host->lock);
3308
3309 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003310 * Maximum number of segments. Depends on if the hardware
3311 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003312 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003313 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003314 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003315 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003316 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003317 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003318 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003319
3320 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003321 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3322 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3323 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003324 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003325 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003326
3327 /*
3328 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003329 * of bytes. When doing hardware scatter/gather, each entry cannot
3330 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003331 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003332 if (host->flags & SDHCI_USE_ADMA) {
3333 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3334 mmc->max_seg_size = 65535;
3335 else
3336 mmc->max_seg_size = 65536;
3337 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003338 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003339 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003340
3341 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003342 * Maximum block size. This varies from controller to controller and
3343 * is specified in the capabilities register.
3344 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003345 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3346 mmc->max_blk_size = 2;
3347 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303348 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003349 SDHCI_MAX_BLOCK_SHIFT;
3350 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003351 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3352 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003353 mmc->max_blk_size = 0;
3354 }
3355 }
3356
3357 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003358
3359 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003360 * Maximum block count.
3361 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003362 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003363
3364 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003365 * Init tasklets.
3366 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003367 tasklet_init(&host->finish_tasklet,
3368 sdhci_tasklet_finish, (unsigned long)host);
3369
Al Viroe4cad1b2006-10-10 22:47:07 +01003370 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003371
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003372 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303373
Shawn Guo2af502c2013-07-05 14:38:55 +08003374 sdhci_init(host, 0);
3375
Russell King781e9892014-04-25 12:55:46 +01003376 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3377 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003378 if (ret) {
3379 pr_err("%s: Failed to request IRQ %d: %d\n",
3380 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003381 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003382 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003383
Pierre Ossmand129bce2006-03-24 03:18:17 -08003384#ifdef CONFIG_MMC_DEBUG
3385 sdhci_dumpregs(host);
3386#endif
3387
Pierre Ossmanf9134312008-12-21 17:01:48 +01003388#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003389 snprintf(host->led_name, sizeof(host->led_name),
3390 "%s::", mmc_hostname(mmc));
3391 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003392 host->led.brightness = LED_OFF;
3393 host->led.default_trigger = mmc_hostname(mmc);
3394 host->led.brightness_set = sdhci_led_control;
3395
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003396 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003397 if (ret) {
3398 pr_err("%s: Failed to register LED device: %d\n",
3399 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003400 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003401 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003402#endif
3403
Pierre Ossman5f25a662006-10-04 02:15:39 -07003404 mmiowb();
3405
Pierre Ossmand129bce2006-03-24 03:18:17 -08003406 mmc_add_host(mmc);
3407
Girish K Sa3c76eb2011-10-11 11:44:09 +05303408 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003409 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003410 (host->flags & SDHCI_USE_ADMA) ?
3411 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003412 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003413
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003414 sdhci_enable_card_detection(host);
3415
Pierre Ossmand129bce2006-03-24 03:18:17 -08003416 return 0;
3417
Pierre Ossmanf9134312008-12-21 17:01:48 +01003418#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003419reset:
Russell King03231f92014-04-25 12:57:12 +01003420 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003421 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3422 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003423 free_irq(host->irq, host);
3424#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003425untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003426 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003427
3428 return ret;
3429}
3430
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003431EXPORT_SYMBOL_GPL(sdhci_add_host);
3432
Pierre Ossman1e728592008-04-16 19:13:13 +02003433void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003434{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003435 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003436 unsigned long flags;
3437
3438 if (dead) {
3439 spin_lock_irqsave(&host->lock, flags);
3440
3441 host->flags |= SDHCI_DEVICE_DEAD;
3442
3443 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303444 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003445 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003446
3447 host->mrq->cmd->error = -ENOMEDIUM;
3448 tasklet_schedule(&host->finish_tasklet);
3449 }
3450
3451 spin_unlock_irqrestore(&host->lock, flags);
3452 }
3453
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003454 sdhci_disable_card_detection(host);
3455
Markus Mayer4e743f12014-07-03 13:27:42 -07003456 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003457
Pierre Ossmanf9134312008-12-21 17:01:48 +01003458#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003459 led_classdev_unregister(&host->led);
3460#endif
3461
Pierre Ossman1e728592008-04-16 19:13:13 +02003462 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003463 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003464
Russell Kingb537f942014-04-25 12:56:01 +01003465 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3466 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003467 free_irq(host->irq, host);
3468
3469 del_timer_sync(&host->timer);
3470
Pierre Ossmand129bce2006-03-24 03:18:17 -08003471 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003472
Tim Kryger3a48edc2014-06-13 10:13:56 -07003473 if (!IS_ERR(mmc->supply.vqmmc))
3474 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003475
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003476 if (host->adma_table)
Adrian Hunter76fe3792014-11-04 12:42:42 +02003477 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003478 host->adma_table, host->adma_addr);
Russell Kingedd63fc2016-01-26 13:39:50 +00003479 if (host->align_buffer)
3480 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz,
3481 host->align_buffer, host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003482
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003483 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003484 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003485}
3486
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003487EXPORT_SYMBOL_GPL(sdhci_remove_host);
3488
3489void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003490{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003491 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003492}
3493
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003494EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003495
3496/*****************************************************************************\
3497 * *
3498 * Driver init/exit *
3499 * *
3500\*****************************************************************************/
3501
3502static int __init sdhci_drv_init(void)
3503{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303504 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003505 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303506 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003507
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003508 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003509}
3510
3511static void __exit sdhci_drv_exit(void)
3512{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003513}
3514
3515module_init(sdhci_drv_init);
3516module_exit(sdhci_drv_exit);
3517
Pierre Ossmandf673b22006-06-30 02:22:31 -07003518module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003519module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003520
Pierre Ossman32710e82009-04-08 20:14:54 +02003521MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003522MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003523MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003524
Pierre Ossmandf673b22006-06-30 02:22:31 -07003525MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003526MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");