blob: 6fe0e782fd36b8843f587670e2d308571b1e3a9d [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 {
638 target_timeout = data->timeout_ns / 1000;
639 if (host->clock)
640 target_timeout += data->timeout_clks / host->clock;
641 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700642
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700643 /*
644 * Figure out needed cycles.
645 * We do this in steps in order to fit inside a 32 bit int.
646 * The first step is the minimum timeout, which will have a
647 * minimum resolution of 6 bits:
648 * (1) 2^13*1000 > 2^22,
649 * (2) host->timeout_clk < 2^16
650 * =>
651 * (1) / (2) > 2^6
652 */
653 count = 0;
654 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
655 while (current_timeout < target_timeout) {
656 count++;
657 current_timeout <<= 1;
658 if (count >= 0xF)
659 break;
660 }
661
662 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400663 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
664 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700665 count = 0xE;
666 }
667
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200668 return count;
669}
670
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300671static void sdhci_set_transfer_irqs(struct sdhci_host *host)
672{
673 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
674 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
675
676 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100677 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300678 else
Russell Kingb537f942014-04-25 12:56:01 +0100679 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
680
681 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
682 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300683}
684
Aisheng Dongb45e6682014-08-27 15:26:29 +0800685static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200686{
687 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800688
689 if (host->ops->set_timeout) {
690 host->ops->set_timeout(host, cmd);
691 } else {
692 count = sdhci_calc_timeout(host, cmd);
693 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
694 }
695}
696
697static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
698{
Pierre Ossman2134a922008-06-28 18:28:51 +0200699 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500700 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200701 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200702
703 WARN_ON(host->data);
704
Aisheng Dongb45e6682014-08-27 15:26:29 +0800705 if (data || (cmd->flags & MMC_RSP_BUSY))
706 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500707
708 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200709 return;
710
711 /* Sanity checks */
712 BUG_ON(data->blksz * data->blocks > 524288);
713 BUG_ON(data->blksz > host->mmc->max_blk_size);
714 BUG_ON(data->blocks > 65535);
715
716 host->data = data;
717 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400718 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200719
Richard Röjforsa13abc72009-09-22 16:45:30 -0700720 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100721 host->flags |= SDHCI_REQ_USE_DMA;
722
Pierre Ossman2134a922008-06-28 18:28:51 +0200723 /*
724 * FIXME: This doesn't account for merging when mapping the
725 * scatterlist.
726 */
727 if (host->flags & SDHCI_REQ_USE_DMA) {
728 int broken, i;
729 struct scatterlist *sg;
730
731 broken = 0;
732 if (host->flags & SDHCI_USE_ADMA) {
733 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
734 broken = 1;
735 } else {
736 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
737 broken = 1;
738 }
739
740 if (unlikely(broken)) {
741 for_each_sg(data->sg, sg, data->sg_len, i) {
742 if (sg->length & 0x3) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100743 DBG("Reverting to PIO because of transfer size (%d)\n",
Pierre Ossman2134a922008-06-28 18:28:51 +0200744 sg->length);
745 host->flags &= ~SDHCI_REQ_USE_DMA;
746 break;
747 }
748 }
749 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100750 }
751
752 /*
753 * The assumption here being that alignment is the same after
754 * translation to device address space.
755 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200756 if (host->flags & SDHCI_REQ_USE_DMA) {
757 int broken, i;
758 struct scatterlist *sg;
759
760 broken = 0;
761 if (host->flags & SDHCI_USE_ADMA) {
762 /*
763 * As we use 3 byte chunks to work around
764 * alignment problems, we need to check this
765 * quirk.
766 */
767 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
768 broken = 1;
769 } else {
770 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
771 broken = 1;
772 }
773
774 if (unlikely(broken)) {
775 for_each_sg(data->sg, sg, data->sg_len, i) {
776 if (sg->offset & 0x3) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100777 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200778 host->flags &= ~SDHCI_REQ_USE_DMA;
779 break;
780 }
781 }
782 }
783 }
784
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200785 if (host->flags & SDHCI_REQ_USE_DMA) {
786 if (host->flags & SDHCI_USE_ADMA) {
787 ret = sdhci_adma_table_pre(host, data);
788 if (ret) {
789 /*
790 * This only happens when someone fed
791 * us an invalid request.
792 */
793 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200794 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200795 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300796 sdhci_writel(host, host->adma_addr,
797 SDHCI_ADMA_ADDRESS);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200798 if (host->flags & SDHCI_USE_64_BIT_DMA)
799 sdhci_writel(host,
800 (u64)host->adma_addr >> 32,
801 SDHCI_ADMA_ADDRESS_HI);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200802 }
803 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300804 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200805
Haibo Chend31911b2015-08-25 10:02:11 +0800806 sg_cnt = sdhci_pre_dma_transfer(host, data);
Jiri Slaby62a7f362015-06-12 11:45:02 +0200807 if (sg_cnt <= 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200808 /*
809 * This only happens when someone fed
810 * us an invalid request.
811 */
812 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200813 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200814 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200815 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300816 sdhci_writel(host, sg_dma_address(data->sg),
817 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200818 }
819 }
820 }
821
Pierre Ossman2134a922008-06-28 18:28:51 +0200822 /*
823 * Always adjust the DMA selection as some controllers
824 * (e.g. JMicron) can't do PIO properly when the selection
825 * is ADMA.
826 */
827 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300828 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200829 ctrl &= ~SDHCI_CTRL_DMA_MASK;
830 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200831 (host->flags & SDHCI_USE_ADMA)) {
832 if (host->flags & SDHCI_USE_64_BIT_DMA)
833 ctrl |= SDHCI_CTRL_ADMA64;
834 else
835 ctrl |= SDHCI_CTRL_ADMA32;
836 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200837 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200838 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300839 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100840 }
841
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200842 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200843 int flags;
844
845 flags = SG_MITER_ATOMIC;
846 if (host->data->flags & MMC_DATA_READ)
847 flags |= SG_MITER_TO_SG;
848 else
849 flags |= SG_MITER_FROM_SG;
850 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200851 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800852 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700853
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300854 sdhci_set_transfer_irqs(host);
855
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400856 /* Set the DMA boundary value and block size */
857 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
858 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300859 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700860}
861
862static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500863 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700864{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800865 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500866 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700867
Dong Aisheng2b558c12013-10-30 22:09:48 +0800868 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800869 if (host->quirks2 &
870 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
871 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
872 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800873 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800874 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
875 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800876 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800877 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700878 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800879 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700880
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200881 WARN_ON(!host->data);
882
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800883 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
884 mode = SDHCI_TRNS_BLK_CNT_EN;
885
Andrei Warkentine89d4562011-05-23 15:06:37 -0500886 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800887 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500888 /*
889 * If we are sending CMD23, CMD12 never gets sent
890 * on successful completion (so no Auto-CMD12).
891 */
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800892 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
893 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500894 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500895 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
896 mode |= SDHCI_TRNS_AUTO_CMD23;
897 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
898 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700899 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500900
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700901 if (data->flags & MMC_DATA_READ)
902 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100903 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700904 mode |= SDHCI_TRNS_DMA;
905
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300906 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800907}
908
909static void sdhci_finish_data(struct sdhci_host *host)
910{
911 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800912
913 BUG_ON(!host->data);
914
915 data = host->data;
916 host->data = NULL;
917
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100918 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200919 if (host->flags & SDHCI_USE_ADMA)
920 sdhci_adma_table_post(host, data);
921 else {
Haibo Chend31911b2015-08-25 10:02:11 +0800922 if (data->host_cookie == COOKIE_MAPPED) {
Haibo Chen348487c2014-12-09 17:04:05 +0800923 dma_unmap_sg(mmc_dev(host->mmc),
924 data->sg, data->sg_len,
925 (data->flags & MMC_DATA_READ) ?
Pierre Ossman2134a922008-06-28 18:28:51 +0200926 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Haibo Chend31911b2015-08-25 10:02:11 +0800927 data->host_cookie = COOKIE_UNMAPPED;
928 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200929 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800930 }
931
932 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200933 * The specification states that the block count register must
934 * be updated, but it does not specify at what point in the
935 * data flow. That makes the register entirely useless to read
936 * back so we have to assume that nothing made it to the card
937 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800938 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200939 if (data->error)
940 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800941 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200942 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800943
Andrei Warkentine89d4562011-05-23 15:06:37 -0500944 /*
945 * Need to send CMD12 if -
946 * a) open-ended multiblock transfer (no CMD23)
947 * b) error in multiblock transfer
948 */
949 if (data->stop &&
950 (data->error ||
951 !host->mrq->sbc)) {
952
Pierre Ossmand129bce2006-03-24 03:18:17 -0800953 /*
954 * The controller needs a reset of internal state machines
955 * upon error conditions.
956 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200957 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100958 sdhci_do_reset(host, SDHCI_RESET_CMD);
959 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800960 }
961
962 sdhci_send_command(host, data->stop);
963 } else
964 tasklet_schedule(&host->finish_tasklet);
965}
966
Dong Aishengc0e551292013-09-13 19:11:31 +0800967void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800968{
969 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700970 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700971 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800972
973 WARN_ON(host->cmd);
974
Russell King96776202016-01-26 13:39:34 +0000975 /* Initially, a command has no error */
976 cmd->error = 0;
977
Pierre Ossmand129bce2006-03-24 03:18:17 -0800978 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700979 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700980
981 mask = SDHCI_CMD_INHIBIT;
982 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
983 mask |= SDHCI_DATA_INHIBIT;
984
985 /* We shouldn't wait for data inihibit for stop commands, even
986 though they might use busy signaling */
987 if (host->mrq->data && (cmd == host->mrq->data->stop))
988 mask &= ~SDHCI_DATA_INHIBIT;
989
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300990 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700991 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100992 pr_err("%s: Controller never released inhibit bit(s).\n",
993 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200995 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996 tasklet_schedule(&host->finish_tasklet);
997 return;
998 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700999 timeout--;
1000 mdelay(1);
1001 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001002
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001003 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001004 if (!cmd->data && cmd->busy_timeout > 9000)
1005 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001006 else
1007 timeout += 10 * HZ;
1008 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009
1010 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001011 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012
Andrei Warkentina3c77782011-04-11 16:13:42 -05001013 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001015 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001016
Andrei Warkentine89d4562011-05-23 15:06:37 -05001017 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001018
Pierre Ossmand129bce2006-03-24 03:18:17 -08001019 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301020 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001021 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001022 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001023 tasklet_schedule(&host->finish_tasklet);
1024 return;
1025 }
1026
1027 if (!(cmd->flags & MMC_RSP_PRESENT))
1028 flags = SDHCI_CMD_RESP_NONE;
1029 else if (cmd->flags & MMC_RSP_136)
1030 flags = SDHCI_CMD_RESP_LONG;
1031 else if (cmd->flags & MMC_RSP_BUSY)
1032 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1033 else
1034 flags = SDHCI_CMD_RESP_SHORT;
1035
1036 if (cmd->flags & MMC_RSP_CRC)
1037 flags |= SDHCI_CMD_CRC;
1038 if (cmd->flags & MMC_RSP_OPCODE)
1039 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301040
1041 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301042 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1043 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001044 flags |= SDHCI_CMD_DATA;
1045
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001046 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047}
Dong Aishengc0e551292013-09-13 19:11:31 +08001048EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001049
1050static void sdhci_finish_command(struct sdhci_host *host)
1051{
1052 int i;
1053
1054 BUG_ON(host->cmd == NULL);
1055
1056 if (host->cmd->flags & MMC_RSP_PRESENT) {
1057 if (host->cmd->flags & MMC_RSP_136) {
1058 /* CRC is stripped so we need to do some shifting. */
1059 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001060 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001061 SDHCI_RESPONSE + (3-i)*4) << 8;
1062 if (i != 3)
1063 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001064 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001065 SDHCI_RESPONSE + (3-i)*4-1);
1066 }
1067 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001068 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001069 }
1070 }
1071
Andrei Warkentine89d4562011-05-23 15:06:37 -05001072 /* Finished CMD23, now send actual command. */
1073 if (host->cmd == host->mrq->sbc) {
1074 host->cmd = NULL;
1075 sdhci_send_command(host, host->mrq->cmd);
1076 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001077
Andrei Warkentine89d4562011-05-23 15:06:37 -05001078 /* Processed actual command. */
1079 if (host->data && host->data_early)
1080 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001081
Andrei Warkentine89d4562011-05-23 15:06:37 -05001082 if (!host->cmd->data)
1083 tasklet_schedule(&host->finish_tasklet);
1084
1085 host->cmd = NULL;
1086 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001087}
1088
Kevin Liu52983382013-01-31 11:31:37 +08001089static u16 sdhci_get_preset_value(struct sdhci_host *host)
1090{
Russell Kingd975f122014-04-25 12:59:31 +01001091 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001092
Russell Kingd975f122014-04-25 12:59:31 +01001093 switch (host->timing) {
1094 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001095 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1096 break;
Russell Kingd975f122014-04-25 12:59:31 +01001097 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001098 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1099 break;
Russell Kingd975f122014-04-25 12:59:31 +01001100 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001101 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1102 break;
Russell Kingd975f122014-04-25 12:59:31 +01001103 case MMC_TIMING_UHS_SDR104:
1104 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001105 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1106 break;
Russell Kingd975f122014-04-25 12:59:31 +01001107 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001108 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001109 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1110 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001111 case MMC_TIMING_MMC_HS400:
1112 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1113 break;
Kevin Liu52983382013-01-31 11:31:37 +08001114 default:
1115 pr_warn("%s: Invalid UHS-I mode selected\n",
1116 mmc_hostname(host->mmc));
1117 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1118 break;
1119 }
1120 return preset;
1121}
1122
Russell King17710592014-04-25 12:58:55 +01001123void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001124{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301125 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001126 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301127 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001128 unsigned long timeout;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001129 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001130
Russell King1650d0c2014-04-25 12:58:50 +01001131 host->mmc->actual_clock = 0;
1132
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001133 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
ludovic.desroches@atmel.comaf951762015-09-17 10:16:19 +02001134 if (host->quirks2 & SDHCI_QUIRK2_NEED_DELAY_AFTER_INT_CLK_RST)
1135 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001136
1137 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001138 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001139
Zhangfei Gao85105c52010-08-06 07:10:01 +08001140 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001141 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001142 u16 pre_val;
1143
1144 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1145 pre_val = sdhci_get_preset_value(host);
1146 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1147 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1148 if (host->clk_mul &&
1149 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1150 clk = SDHCI_PROG_CLOCK_MODE;
1151 real_div = div + 1;
1152 clk_mul = host->clk_mul;
1153 } else {
1154 real_div = max_t(int, 1, div << 1);
1155 }
1156 goto clock_set;
1157 }
1158
Arindam Nathc3ed3872011-05-05 12:19:06 +05301159 /*
1160 * Check if the Host Controller supports Programmable Clock
1161 * Mode.
1162 */
1163 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001164 for (div = 1; div <= 1024; div++) {
1165 if ((host->max_clk * host->clk_mul / div)
1166 <= clock)
1167 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001168 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001169 if ((host->max_clk * host->clk_mul / div) <= clock) {
1170 /*
1171 * Set Programmable Clock Mode in the Clock
1172 * Control register.
1173 */
1174 clk = SDHCI_PROG_CLOCK_MODE;
1175 real_div = div;
1176 clk_mul = host->clk_mul;
1177 div--;
1178 } else {
1179 /*
1180 * Divisor can be too small to reach clock
1181 * speed requirement. Then use the base clock.
1182 */
1183 switch_base_clk = true;
1184 }
1185 }
1186
1187 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301188 /* Version 3.00 divisors must be a multiple of 2. */
1189 if (host->max_clk <= clock)
1190 div = 1;
1191 else {
1192 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1193 div += 2) {
1194 if ((host->max_clk / div) <= clock)
1195 break;
1196 }
1197 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001198 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301199 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301200 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1201 && !div && host->max_clk <= 25000000)
1202 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001203 }
1204 } else {
1205 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001206 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001207 if ((host->max_clk / div) <= clock)
1208 break;
1209 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001210 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301211 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001212 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001213
Kevin Liu52983382013-01-31 11:31:37 +08001214clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001215 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001216 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301217 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001218 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1219 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001220 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001221 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001222
Chris Ball27f6cb12009-09-22 16:45:31 -07001223 /* Wait max 20 ms */
1224 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001225 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001226 & SDHCI_CLOCK_INT_STABLE)) {
1227 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001228 pr_err("%s: Internal clock never stabilised.\n",
1229 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001230 sdhci_dumpregs(host);
1231 return;
1232 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001233 timeout--;
1234 mdelay(1);
1235 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001236
1237 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001238 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001239}
Russell King17710592014-04-25 12:58:55 +01001240EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001241
Russell King24fbb3c2014-04-25 13:00:06 +01001242static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1243 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001244{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001245 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001246 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001247
Russell King24fbb3c2014-04-25 13:00:06 +01001248 if (mode != MMC_POWER_OFF) {
1249 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001250 case MMC_VDD_165_195:
1251 pwr = SDHCI_POWER_180;
1252 break;
1253 case MMC_VDD_29_30:
1254 case MMC_VDD_30_31:
1255 pwr = SDHCI_POWER_300;
1256 break;
1257 case MMC_VDD_32_33:
1258 case MMC_VDD_33_34:
1259 pwr = SDHCI_POWER_330;
1260 break;
1261 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001262 WARN(1, "%s: Invalid vdd %#x\n",
1263 mmc_hostname(host->mmc), vdd);
1264 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001265 }
1266 }
1267
1268 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001269 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001270
Pierre Ossmanae628902009-05-03 20:45:03 +02001271 host->pwr = pwr;
1272
1273 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001274 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001275 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1276 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001277 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001278 } else {
1279 /*
1280 * Spec says that we should clear the power reg before setting
1281 * a new value. Some controllers don't seem to like this though.
1282 */
1283 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1284 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001285
Russell Kinge921a8b2014-04-25 13:00:01 +01001286 /*
1287 * At least the Marvell CaFe chip gets confused if we set the
1288 * voltage and set turn on power at the same time, so set the
1289 * voltage first.
1290 */
1291 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1292 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001293
Russell Kinge921a8b2014-04-25 13:00:01 +01001294 pwr |= SDHCI_POWER_ON;
1295
Pierre Ossmanae628902009-05-03 20:45:03 +02001296 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1297
Russell Kinge921a8b2014-04-25 13:00:01 +01001298 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1299 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001300
Russell Kinge921a8b2014-04-25 13:00:01 +01001301 /*
1302 * Some controllers need an extra 10ms delay of 10ms before
1303 * they can apply clock after applying power
1304 */
1305 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1306 mdelay(10);
1307 }
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001308
1309 if (!IS_ERR(mmc->supply.vmmc)) {
1310 spin_unlock_irq(&host->lock);
1311 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1312 spin_lock_irq(&host->lock);
1313 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001314}
1315
Pierre Ossmand129bce2006-03-24 03:18:17 -08001316/*****************************************************************************\
1317 * *
1318 * MMC callbacks *
1319 * *
1320\*****************************************************************************/
1321
1322static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1323{
1324 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001325 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001326 unsigned long flags;
1327
1328 host = mmc_priv(mmc);
1329
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001330 sdhci_runtime_pm_get(host);
1331
Scott Branden04e079c2015-03-10 11:35:10 -07001332 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001333 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001334
Pierre Ossmand129bce2006-03-24 03:18:17 -08001335 spin_lock_irqsave(&host->lock, flags);
1336
1337 WARN_ON(host->mrq != NULL);
1338
Pierre Ossmanf9134312008-12-21 17:01:48 +01001339#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001340 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001341#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001342
1343 /*
1344 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1345 * requests if Auto-CMD12 is enabled.
1346 */
1347 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001348 if (mrq->stop) {
1349 mrq->data->stop = NULL;
1350 mrq->stop = NULL;
1351 }
1352 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001353
1354 host->mrq = mrq;
1355
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001356 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001357 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001358 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301359 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001360 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001361 sdhci_send_command(host, mrq->sbc);
1362 else
1363 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301364 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001365
Pierre Ossman5f25a662006-10-04 02:15:39 -07001366 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001367 spin_unlock_irqrestore(&host->lock, flags);
1368}
1369
Russell King2317f562014-04-25 12:57:07 +01001370void sdhci_set_bus_width(struct sdhci_host *host, int width)
1371{
1372 u8 ctrl;
1373
1374 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1375 if (width == MMC_BUS_WIDTH_8) {
1376 ctrl &= ~SDHCI_CTRL_4BITBUS;
1377 if (host->version >= SDHCI_SPEC_300)
1378 ctrl |= SDHCI_CTRL_8BITBUS;
1379 } else {
1380 if (host->version >= SDHCI_SPEC_300)
1381 ctrl &= ~SDHCI_CTRL_8BITBUS;
1382 if (width == MMC_BUS_WIDTH_4)
1383 ctrl |= SDHCI_CTRL_4BITBUS;
1384 else
1385 ctrl &= ~SDHCI_CTRL_4BITBUS;
1386 }
1387 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1388}
1389EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1390
Russell King96d7b782014-04-25 12:59:26 +01001391void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1392{
1393 u16 ctrl_2;
1394
1395 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1396 /* Select Bus Speed Mode for host */
1397 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1398 if ((timing == MMC_TIMING_MMC_HS200) ||
1399 (timing == MMC_TIMING_UHS_SDR104))
1400 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1401 else if (timing == MMC_TIMING_UHS_SDR12)
1402 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1403 else if (timing == MMC_TIMING_UHS_SDR25)
1404 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1405 else if (timing == MMC_TIMING_UHS_SDR50)
1406 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1407 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1408 (timing == MMC_TIMING_MMC_DDR52))
1409 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001410 else if (timing == MMC_TIMING_MMC_HS400)
1411 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001412 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1413}
1414EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1415
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001416static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001417{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001418 unsigned long flags;
1419 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001420 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001421
Pierre Ossmand129bce2006-03-24 03:18:17 -08001422 spin_lock_irqsave(&host->lock, flags);
1423
Adrian Hunterceb61432011-12-27 15:48:41 +02001424 if (host->flags & SDHCI_DEVICE_DEAD) {
1425 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001426 if (!IS_ERR(mmc->supply.vmmc) &&
1427 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001428 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001429 return;
1430 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001431
Pierre Ossmand129bce2006-03-24 03:18:17 -08001432 /*
1433 * Reset the chip on each power off.
1434 * Should clear out any weird states.
1435 */
1436 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001437 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001438 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001439 }
1440
Kevin Liu52983382013-01-31 11:31:37 +08001441 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001442 (ios->power_mode == MMC_POWER_UP) &&
1443 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001444 sdhci_enable_preset_value(host, false);
1445
Russell King373073e2014-04-25 12:58:45 +01001446 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001447 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001448 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001449
1450 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1451 host->clock) {
1452 host->timeout_clk = host->mmc->actual_clock ?
1453 host->mmc->actual_clock / 1000 :
1454 host->clock / 1000;
1455 host->mmc->max_busy_timeout =
1456 host->ops->get_max_timeout_count ?
1457 host->ops->get_max_timeout_count(host) :
1458 1 << 27;
1459 host->mmc->max_busy_timeout /= host->timeout_clk;
1460 }
Russell King373073e2014-04-25 12:58:45 +01001461 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001462
Russell King24fbb3c2014-04-25 13:00:06 +01001463 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001464
Philip Rakity643a81f2010-09-23 08:24:32 -07001465 if (host->ops->platform_send_init_74_clocks)
1466 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1467
Russell King2317f562014-04-25 12:57:07 +01001468 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001469
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001470 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001471
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001472 if ((ios->timing == MMC_TIMING_SD_HS ||
1473 ios->timing == MMC_TIMING_MMC_HS)
1474 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001475 ctrl |= SDHCI_CTRL_HISPD;
1476 else
1477 ctrl &= ~SDHCI_CTRL_HISPD;
1478
Arindam Nathd6d50a12011-05-05 12:18:59 +05301479 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301480 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301481
1482 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001483 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1484 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001485 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301486 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301487 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1488 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001489 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301490 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301491
Russell Kingda91a8f2014-04-25 13:00:12 +01001492 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301493 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301494 /*
1495 * We only need to set Driver Strength if the
1496 * preset value enable is not set.
1497 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001498 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301499 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1500 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1501 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001502 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1503 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301504 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1505 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001506 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1507 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1508 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001509 pr_warn("%s: invalid driver type, default to driver type B\n",
1510 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001511 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1512 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301513
1514 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301515 } else {
1516 /*
1517 * According to SDHC Spec v3.00, if the Preset Value
1518 * Enable in the Host Control 2 register is set, we
1519 * need to reset SD Clock Enable before changing High
1520 * Speed Enable to avoid generating clock gliches.
1521 */
Arindam Nath758535c2011-05-05 12:19:00 +05301522
1523 /* Reset SD Clock Enable */
1524 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1525 clk &= ~SDHCI_CLOCK_CARD_EN;
1526 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1527
1528 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1529
1530 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001531 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301532 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301533
Arindam Nath49c468f2011-05-05 12:19:01 +05301534 /* 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
Russell King96d7b782014-04-25 12:59:26 +01001539 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001540 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301541
Kevin Liu52983382013-01-31 11:31:37 +08001542 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1543 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1544 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1545 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1546 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001547 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1548 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001549 u16 preset;
1550
1551 sdhci_enable_preset_value(host, true);
1552 preset = sdhci_get_preset_value(host);
1553 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1554 >> SDHCI_PRESET_DRV_SHIFT;
1555 }
1556
Arindam Nath49c468f2011-05-05 12:19:01 +05301557 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001558 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301559 } else
1560 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301561
Leandro Dorileob8352262007-07-25 23:47:04 +02001562 /*
1563 * Some (ENE) controllers go apeshit on some ios operation,
1564 * signalling timeout and CRC errors even on CMD0. Resetting
1565 * it on each ios seems to solve the problem.
1566 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301567 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001568 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001569
Pierre Ossman5f25a662006-10-04 02:15:39 -07001570 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001571 spin_unlock_irqrestore(&host->lock, flags);
1572}
1573
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001574static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1575{
1576 struct sdhci_host *host = mmc_priv(mmc);
1577
1578 sdhci_runtime_pm_get(host);
1579 sdhci_do_set_ios(host, ios);
1580 sdhci_runtime_pm_put(host);
1581}
1582
Kevin Liu94144a42013-02-28 17:35:53 +08001583static int sdhci_do_get_cd(struct sdhci_host *host)
1584{
1585 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1586
1587 if (host->flags & SDHCI_DEVICE_DEAD)
1588 return 0;
1589
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001590 /* If nonremovable, assume that the card is always present. */
1591 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
Kevin Liu94144a42013-02-28 17:35:53 +08001592 return 1;
1593
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001594 /*
1595 * Try slot gpio detect, if defined it take precedence
1596 * over build in controller functionality
1597 */
Kevin Liu94144a42013-02-28 17:35:53 +08001598 if (!IS_ERR_VALUE(gpio_cd))
1599 return !!gpio_cd;
1600
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001601 /* If polling, assume that the card is always present. */
1602 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1603 return 1;
1604
Kevin Liu94144a42013-02-28 17:35:53 +08001605 /* Host native card detect */
1606 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1607}
1608
1609static int sdhci_get_cd(struct mmc_host *mmc)
1610{
1611 struct sdhci_host *host = mmc_priv(mmc);
1612 int ret;
1613
1614 sdhci_runtime_pm_get(host);
1615 ret = sdhci_do_get_cd(host);
1616 sdhci_runtime_pm_put(host);
1617 return ret;
1618}
1619
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001620static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001621{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001622 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001623 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001624
Pierre Ossmand129bce2006-03-24 03:18:17 -08001625 spin_lock_irqsave(&host->lock, flags);
1626
Pierre Ossman1e728592008-04-16 19:13:13 +02001627 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001628 is_readonly = 0;
1629 else if (host->ops->get_ro)
1630 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001631 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001632 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1633 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001634
1635 spin_unlock_irqrestore(&host->lock, flags);
1636
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001637 /* This quirk needs to be replaced by a callback-function later */
1638 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1639 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001640}
1641
Takashi Iwai82b0e232011-04-21 20:26:38 +02001642#define SAMPLE_COUNT 5
1643
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001644static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001645{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001646 int i, ro_count;
1647
Takashi Iwai82b0e232011-04-21 20:26:38 +02001648 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001649 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001650
1651 ro_count = 0;
1652 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001653 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001654 if (++ro_count > SAMPLE_COUNT / 2)
1655 return 1;
1656 }
1657 msleep(30);
1658 }
1659 return 0;
1660}
1661
Adrian Hunter20758b62011-08-29 16:42:12 +03001662static void sdhci_hw_reset(struct mmc_host *mmc)
1663{
1664 struct sdhci_host *host = mmc_priv(mmc);
1665
1666 if (host->ops && host->ops->hw_reset)
1667 host->ops->hw_reset(host);
1668}
1669
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001670static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001671{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001672 struct sdhci_host *host = mmc_priv(mmc);
1673 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001674
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001675 sdhci_runtime_pm_get(host);
1676 ret = sdhci_do_get_ro(host);
1677 sdhci_runtime_pm_put(host);
1678 return ret;
1679}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001680
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001681static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1682{
Russell Kingbe138552014-04-25 12:55:56 +01001683 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001684 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001685 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001686 else
Russell Kingb537f942014-04-25 12:56:01 +01001687 host->ier &= ~SDHCI_INT_CARD_INT;
1688
1689 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1690 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001691 mmiowb();
1692 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001693}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001694
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001695static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1696{
1697 struct sdhci_host *host = mmc_priv(mmc);
1698 unsigned long flags;
1699
Russell Kingef104332014-04-25 12:55:41 +01001700 sdhci_runtime_pm_get(host);
1701
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001702 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001703 if (enable)
1704 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1705 else
1706 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1707
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001708 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001709 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001710
1711 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001712}
1713
Philip Rakity6231f3d2012-07-23 15:56:23 -07001714static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001715 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001716{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001717 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001718 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001719 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001720
1721 /*
1722 * Signal Voltage Switching is only applicable for Host Controllers
1723 * v3.00 and above.
1724 */
1725 if (host->version < SDHCI_SPEC_300)
1726 return 0;
1727
Philip Rakity6231f3d2012-07-23 15:56:23 -07001728 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001729
Fabio Estevam21f59982013-02-14 10:35:03 -02001730 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001731 case MMC_SIGNAL_VOLTAGE_330:
1732 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1733 ctrl &= ~SDHCI_CTRL_VDD_180;
1734 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1735
Tim Kryger3a48edc2014-06-13 10:13:56 -07001736 if (!IS_ERR(mmc->supply.vqmmc)) {
1737 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1738 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001739 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001740 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1741 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001742 return -EIO;
1743 }
1744 }
1745 /* Wait for 5ms */
1746 usleep_range(5000, 5500);
1747
1748 /* 3.3V regulator output should be stable within 5 ms */
1749 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1750 if (!(ctrl & SDHCI_CTRL_VDD_180))
1751 return 0;
1752
Joe Perches66061102014-09-12 14:56:56 -07001753 pr_warn("%s: 3.3V regulator output did not became stable\n",
1754 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001755
1756 return -EAGAIN;
1757 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001758 if (!IS_ERR(mmc->supply.vqmmc)) {
1759 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001760 1700000, 1950000);
1761 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001762 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1763 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001764 return -EIO;
1765 }
1766 }
1767
1768 /*
1769 * Enable 1.8V Signal Enable in the Host Control2
1770 * register
1771 */
1772 ctrl |= SDHCI_CTRL_VDD_180;
1773 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1774
Vincent Yang9d967a62015-01-20 16:05:15 +08001775 /* Some controller need to do more when switching */
1776 if (host->ops->voltage_switch)
1777 host->ops->voltage_switch(host);
1778
Kevin Liu20b92a32012-12-17 19:29:26 +08001779 /* 1.8V regulator output should be stable within 5 ms */
1780 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1781 if (ctrl & SDHCI_CTRL_VDD_180)
1782 return 0;
1783
Joe Perches66061102014-09-12 14:56:56 -07001784 pr_warn("%s: 1.8V regulator output did not became stable\n",
1785 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001786
1787 return -EAGAIN;
1788 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001789 if (!IS_ERR(mmc->supply.vqmmc)) {
1790 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1791 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001792 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001793 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1794 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001795 return -EIO;
1796 }
1797 }
1798 return 0;
1799 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301800 /* No signal voltage switch required */
1801 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001802 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301803}
1804
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001805static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001806 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001807{
1808 struct sdhci_host *host = mmc_priv(mmc);
1809 int err;
1810
1811 if (host->version < SDHCI_SPEC_300)
1812 return 0;
1813 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001814 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001815 sdhci_runtime_pm_put(host);
1816 return err;
1817}
1818
Kevin Liu20b92a32012-12-17 19:29:26 +08001819static int sdhci_card_busy(struct mmc_host *mmc)
1820{
1821 struct sdhci_host *host = mmc_priv(mmc);
1822 u32 present_state;
1823
1824 sdhci_runtime_pm_get(host);
1825 /* Check whether DAT[3:0] is 0000 */
1826 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1827 sdhci_runtime_pm_put(host);
1828
1829 return !(present_state & SDHCI_DATA_LVL_MASK);
1830}
1831
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001832static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1833{
1834 struct sdhci_host *host = mmc_priv(mmc);
1835 unsigned long flags;
1836
1837 spin_lock_irqsave(&host->lock, flags);
1838 host->flags |= SDHCI_HS400_TUNING;
1839 spin_unlock_irqrestore(&host->lock, flags);
1840
1841 return 0;
1842}
1843
Girish K S069c9f12012-01-06 09:56:39 +05301844static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301845{
Russell King4b6f37d2014-04-25 12:59:36 +01001846 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301847 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301848 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301849 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001850 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001851 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001852 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301853
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001854 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001855 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301856
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001857 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1858 host->flags &= ~SDHCI_HS400_TUNING;
1859
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001860 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1861 tuning_count = host->tuning_count;
1862
Arindam Nathb513ea22011-05-05 12:19:04 +05301863 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001864 * The Host Controller needs tuning in case of SDR104 and DDR50
1865 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1866 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301867 * If the Host Controller supports the HS200 mode then the
1868 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301869 */
Russell King4b6f37d2014-04-25 12:59:36 +01001870 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001871 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001872 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001873 err = -EINVAL;
1874 goto out_unlock;
1875
Russell King4b6f37d2014-04-25 12:59:36 +01001876 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001877 /*
1878 * Periodic re-tuning for HS400 is not expected to be needed, so
1879 * disable it here.
1880 */
1881 if (hs400_tuning)
1882 tuning_count = 0;
1883 break;
1884
Russell King4b6f37d2014-04-25 12:59:36 +01001885 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001886 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001887 break;
Girish K S069c9f12012-01-06 09:56:39 +05301888
Russell King4b6f37d2014-04-25 12:59:36 +01001889 case MMC_TIMING_UHS_SDR50:
1890 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1891 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1892 break;
1893 /* FALLTHROUGH */
1894
1895 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001896 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301897 }
1898
Dong Aisheng45251812013-09-13 19:11:30 +08001899 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001900 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001901 err = host->ops->platform_execute_tuning(host, opcode);
1902 sdhci_runtime_pm_put(host);
1903 return err;
1904 }
1905
Russell King4b6f37d2014-04-25 12:59:36 +01001906 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1907 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001908 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1909 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301910 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1911
1912 /*
1913 * As per the Host Controller spec v3.00, tuning command
1914 * generates Buffer Read Ready interrupt, so enable that.
1915 *
1916 * Note: The spec clearly says that when tuning sequence
1917 * is being performed, the controller does not generate
1918 * interrupts other than Buffer Read Ready interrupt. But
1919 * to make sure we don't hit a controller bug, we _only_
1920 * enable Buffer Read Ready interrupt here.
1921 */
Russell Kingb537f942014-04-25 12:56:01 +01001922 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1923 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301924
1925 /*
1926 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1927 * of loops reaches 40 times or a timeout of 150ms occurs.
1928 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301929 do {
1930 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001931 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301932
Girish K S069c9f12012-01-06 09:56:39 +05301933 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301934 cmd.arg = 0;
1935 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1936 cmd.retries = 0;
1937 cmd.data = NULL;
1938 cmd.error = 0;
1939
Al Cooper7ce45e92014-05-09 11:34:07 -04001940 if (tuning_loop_counter-- == 0)
1941 break;
1942
Arindam Nathb513ea22011-05-05 12:19:04 +05301943 mrq.cmd = &cmd;
1944 host->mrq = &mrq;
1945
1946 /*
1947 * In response to CMD19, the card sends 64 bytes of tuning
1948 * block to the Host Controller. So we set the block size
1949 * to 64 here.
1950 */
Girish K S069c9f12012-01-06 09:56:39 +05301951 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1952 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1953 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1954 SDHCI_BLOCK_SIZE);
1955 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1956 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1957 SDHCI_BLOCK_SIZE);
1958 } else {
1959 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1960 SDHCI_BLOCK_SIZE);
1961 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301962
1963 /*
1964 * The tuning block is sent by the card to the host controller.
1965 * So we set the TRNS_READ bit in the Transfer Mode register.
1966 * This also takes care of setting DMA Enable and Multi Block
1967 * Select in the same register to 0.
1968 */
1969 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1970
1971 sdhci_send_command(host, &cmd);
1972
1973 host->cmd = NULL;
1974 host->mrq = NULL;
1975
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001976 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301977 /* Wait for Buffer Read Ready interrupt */
1978 wait_event_interruptible_timeout(host->buf_ready_int,
1979 (host->tuning_done == 1),
1980 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001981 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301982
1983 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001984 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 +05301985 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1986 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1987 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1988 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1989
1990 err = -EIO;
1991 goto out;
1992 }
1993
1994 host->tuning_done = 0;
1995
1996 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07001997
1998 /* eMMC spec does not require a delay between tuning cycles */
1999 if (opcode == MMC_SEND_TUNING_BLOCK)
2000 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302001 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2002
2003 /*
2004 * The Host Driver has exhausted the maximum number of loops allowed,
2005 * so use fixed sampling frequency.
2006 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002007 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302008 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2009 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002010 }
2011 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002012 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002013 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302014 }
2015
2016out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002017 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002018 /*
2019 * In case tuning fails, host controllers which support
2020 * re-tuning can try tuning again at a later time, when the
2021 * re-tuning timer expires. So for these controllers, we
2022 * return 0. Since there might be other controllers who do not
2023 * have this capability, we return error for them.
2024 */
2025 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302026 }
2027
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002028 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302029
Russell Kingb537f942014-04-25 12:56:01 +01002030 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2031 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002032out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002033 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002034 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302035
2036 return err;
2037}
2038
Adrian Huntercb849642015-02-06 14:12:59 +02002039static int sdhci_select_drive_strength(struct mmc_card *card,
2040 unsigned int max_dtr, int host_drv,
2041 int card_drv, int *drv_type)
2042{
2043 struct sdhci_host *host = mmc_priv(card->host);
2044
2045 if (!host->ops->select_drive_strength)
2046 return 0;
2047
2048 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2049 card_drv, drv_type);
2050}
Kevin Liu52983382013-01-31 11:31:37 +08002051
2052static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302053{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302054 /* Host Controller v3.00 defines preset value registers */
2055 if (host->version < SDHCI_SPEC_300)
2056 return;
2057
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302058 /*
2059 * We only enable or disable Preset Value if they are not already
2060 * enabled or disabled respectively. Otherwise, we bail out.
2061 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002062 if (host->preset_enabled != enable) {
2063 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2064
2065 if (enable)
2066 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2067 else
2068 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2069
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302070 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002071
2072 if (enable)
2073 host->flags |= SDHCI_PV_ENABLED;
2074 else
2075 host->flags &= ~SDHCI_PV_ENABLED;
2076
2077 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302078 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002079}
2080
Haibo Chen348487c2014-12-09 17:04:05 +08002081static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2082 int err)
2083{
2084 struct sdhci_host *host = mmc_priv(mmc);
2085 struct mmc_data *data = mrq->data;
2086
Russell King771a3dc2016-01-26 13:40:53 +00002087 if (data->host_cookie == COOKIE_GIVEN ||
2088 data->host_cookie == COOKIE_MAPPED)
2089 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2090 data->flags & MMC_DATA_WRITE ?
2091 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2092
2093 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002094}
2095
2096static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Haibo Chend31911b2015-08-25 10:02:11 +08002097 struct mmc_data *data)
Haibo Chen348487c2014-12-09 17:04:05 +08002098{
2099 int sg_count;
2100
Haibo Chend31911b2015-08-25 10:02:11 +08002101 if (data->host_cookie == COOKIE_MAPPED) {
2102 data->host_cookie = COOKIE_GIVEN;
2103 return data->sg_count;
Haibo Chen348487c2014-12-09 17:04:05 +08002104 }
2105
Haibo Chend31911b2015-08-25 10:02:11 +08002106 WARN_ON(data->host_cookie == COOKIE_GIVEN);
Haibo Chen348487c2014-12-09 17:04:05 +08002107
Haibo Chend31911b2015-08-25 10:02:11 +08002108 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2109 data->flags & MMC_DATA_WRITE ?
2110 DMA_TO_DEVICE : DMA_FROM_DEVICE);
Haibo Chen348487c2014-12-09 17:04:05 +08002111
2112 if (sg_count == 0)
Haibo Chend31911b2015-08-25 10:02:11 +08002113 return -ENOSPC;
Haibo Chen348487c2014-12-09 17:04:05 +08002114
Haibo Chend31911b2015-08-25 10:02:11 +08002115 data->sg_count = sg_count;
2116 data->host_cookie = COOKIE_MAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002117
2118 return sg_count;
2119}
2120
2121static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2122 bool is_first_req)
2123{
2124 struct sdhci_host *host = mmc_priv(mmc);
2125
Haibo Chend31911b2015-08-25 10:02:11 +08002126 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002127
2128 if (host->flags & SDHCI_REQ_USE_DMA)
Haibo Chend31911b2015-08-25 10:02:11 +08002129 sdhci_pre_dma_transfer(host, mrq->data);
Haibo Chen348487c2014-12-09 17:04:05 +08002130}
2131
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002132static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002133{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002134 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002135 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002136 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002137
Christian Daudt722e1282013-06-20 14:26:36 -07002138 /* First check if client has provided their own card event */
2139 if (host->ops->card_event)
2140 host->ops->card_event(host);
2141
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002142 present = sdhci_do_get_cd(host);
2143
Pierre Ossmand129bce2006-03-24 03:18:17 -08002144 spin_lock_irqsave(&host->lock, flags);
2145
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002146 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002147 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302148 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002149 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302150 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002151 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002152
Russell King03231f92014-04-25 12:57:12 +01002153 sdhci_do_reset(host, SDHCI_RESET_CMD);
2154 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002155
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002156 host->mrq->cmd->error = -ENOMEDIUM;
2157 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002158 }
2159
2160 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002161}
2162
2163static const struct mmc_host_ops sdhci_ops = {
2164 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002165 .post_req = sdhci_post_req,
2166 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002167 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002168 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002169 .get_ro = sdhci_get_ro,
2170 .hw_reset = sdhci_hw_reset,
2171 .enable_sdio_irq = sdhci_enable_sdio_irq,
2172 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002173 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002174 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002175 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002176 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002177 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002178};
2179
2180/*****************************************************************************\
2181 * *
2182 * Tasklets *
2183 * *
2184\*****************************************************************************/
2185
Pierre Ossmand129bce2006-03-24 03:18:17 -08002186static void sdhci_tasklet_finish(unsigned long param)
2187{
2188 struct sdhci_host *host;
2189 unsigned long flags;
2190 struct mmc_request *mrq;
2191
2192 host = (struct sdhci_host*)param;
2193
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002194 spin_lock_irqsave(&host->lock, flags);
2195
Chris Ball0c9c99a2011-04-27 17:35:31 -04002196 /*
2197 * If this tasklet gets rescheduled while running, it will
2198 * be run again afterwards but without any active request.
2199 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002200 if (!host->mrq) {
2201 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002202 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002203 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002204
2205 del_timer(&host->timer);
2206
2207 mrq = host->mrq;
2208
Pierre Ossmand129bce2006-03-24 03:18:17 -08002209 /*
Russell King054cedf2016-01-26 13:40:42 +00002210 * Always unmap the data buffers if they were mapped by
2211 * sdhci_prepare_data() whenever we finish with a request.
2212 * This avoids leaking DMA mappings on error.
2213 */
2214 if (host->flags & SDHCI_REQ_USE_DMA) {
2215 struct mmc_data *data = mrq->data;
2216
2217 if (data && data->host_cookie == COOKIE_MAPPED) {
2218 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2219 (data->flags & MMC_DATA_READ) ?
2220 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2221 data->host_cookie = COOKIE_UNMAPPED;
2222 }
2223 }
2224
2225 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002226 * The controller needs a reset of internal state machines
2227 * upon error conditions.
2228 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002229 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002230 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002231 (mrq->sbc && mrq->sbc->error) ||
2232 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2233 (mrq->data->stop && mrq->data->stop->error))) ||
2234 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002235
2236 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002237 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002238 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002239 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002240
2241 /* Spec says we should do both at the same time, but Ricoh
2242 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002243 sdhci_do_reset(host, SDHCI_RESET_CMD);
2244 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002245 }
2246
2247 host->mrq = NULL;
2248 host->cmd = NULL;
2249 host->data = NULL;
2250
Pierre Ossmanf9134312008-12-21 17:01:48 +01002251#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002252 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002253#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002254
Pierre Ossman5f25a662006-10-04 02:15:39 -07002255 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002256 spin_unlock_irqrestore(&host->lock, flags);
2257
2258 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002259 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002260}
2261
2262static void sdhci_timeout_timer(unsigned long data)
2263{
2264 struct sdhci_host *host;
2265 unsigned long flags;
2266
2267 host = (struct sdhci_host*)data;
2268
2269 spin_lock_irqsave(&host->lock, flags);
2270
2271 if (host->mrq) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002272 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2273 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002274 sdhci_dumpregs(host);
2275
2276 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002277 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002278 sdhci_finish_data(host);
2279 } else {
2280 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002281 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002282 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002283 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002284
2285 tasklet_schedule(&host->finish_tasklet);
2286 }
2287 }
2288
Pierre Ossman5f25a662006-10-04 02:15:39 -07002289 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002290 spin_unlock_irqrestore(&host->lock, flags);
2291}
2292
2293/*****************************************************************************\
2294 * *
2295 * Interrupt handling *
2296 * *
2297\*****************************************************************************/
2298
Adrian Hunter61541392014-09-24 10:27:27 +03002299static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002300{
2301 BUG_ON(intmask == 0);
2302
2303 if (!host->cmd) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002304 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2305 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002306 sdhci_dumpregs(host);
2307 return;
2308 }
2309
Russell Kingec014cb2016-01-26 13:39:39 +00002310 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2311 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2312 if (intmask & SDHCI_INT_TIMEOUT)
2313 host->cmd->error = -ETIMEDOUT;
2314 else
2315 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002316
Russell King71fcbda2016-01-26 13:39:45 +00002317 /*
2318 * If this command initiates a data phase and a response
2319 * CRC error is signalled, the card can start transferring
2320 * data - the card may have received the command without
2321 * error. We must not terminate the mmc_request early.
2322 *
2323 * If the card did not receive the command or returned an
2324 * error which prevented it sending data, the data phase
2325 * will time out.
2326 */
2327 if (host->cmd->data &&
2328 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2329 SDHCI_INT_CRC) {
2330 host->cmd = NULL;
2331 return;
2332 }
2333
Pierre Ossmand129bce2006-03-24 03:18:17 -08002334 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002335 return;
2336 }
2337
2338 /*
2339 * The host can send and interrupt when the busy state has
2340 * ended, allowing us to wait without wasting CPU cycles.
2341 * Unfortunately this is overloaded on the "data complete"
2342 * interrupt, so we need to take some care when handling
2343 * it.
2344 *
2345 * Note: The 1.0 specification is a bit ambiguous about this
2346 * feature so there might be some problems with older
2347 * controllers.
2348 */
2349 if (host->cmd->flags & MMC_RSP_BUSY) {
2350 if (host->cmd->data)
Marek Vasut2e4456f2015-11-18 10:47:02 +01002351 DBG("Cannot wait for busy signal when also doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002352 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2353 && !host->busy_handle) {
2354 /* Mark that command complete before busy is ended */
2355 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002356 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002357 }
Ben Dooksf9454052009-02-20 20:33:08 +03002358
2359 /* The controller does not support the end-of-busy IRQ,
2360 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002361 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2362 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2363 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002364 }
2365
2366 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002367 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002368}
2369
George G. Davis0957c332010-02-18 12:32:12 -05002370#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002371static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002372{
2373 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002374 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002375
2376 sdhci_dumpregs(host);
2377
2378 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002379 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002380
Adrian Huntere57a5f62014-11-04 12:42:46 +02002381 if (host->flags & SDHCI_USE_64_BIT_DMA)
2382 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2383 name, desc, le32_to_cpu(dma_desc->addr_hi),
2384 le32_to_cpu(dma_desc->addr_lo),
2385 le16_to_cpu(dma_desc->len),
2386 le16_to_cpu(dma_desc->cmd));
2387 else
2388 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2389 name, desc, le32_to_cpu(dma_desc->addr_lo),
2390 le16_to_cpu(dma_desc->len),
2391 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002392
Adrian Hunter76fe3792014-11-04 12:42:42 +02002393 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002394
Adrian Hunter05452302014-11-04 12:42:45 +02002395 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002396 break;
2397 }
2398}
2399#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002400static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002401#endif
2402
Pierre Ossmand129bce2006-03-24 03:18:17 -08002403static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2404{
Girish K S069c9f12012-01-06 09:56:39 +05302405 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002406 BUG_ON(intmask == 0);
2407
Arindam Nathb513ea22011-05-05 12:19:04 +05302408 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2409 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302410 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2411 if (command == MMC_SEND_TUNING_BLOCK ||
2412 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302413 host->tuning_done = 1;
2414 wake_up(&host->buf_ready_int);
2415 return;
2416 }
2417 }
2418
Pierre Ossmand129bce2006-03-24 03:18:17 -08002419 if (!host->data) {
2420 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002421 * The "data complete" interrupt is also used to
2422 * indicate that a busy state has ended. See comment
2423 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002424 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002425 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002426 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2427 host->cmd->error = -ETIMEDOUT;
2428 tasklet_schedule(&host->finish_tasklet);
2429 return;
2430 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002431 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002432 /*
2433 * Some cards handle busy-end interrupt
2434 * before the command completed, so make
2435 * sure we do things in the proper order.
2436 */
2437 if (host->busy_handle)
2438 sdhci_finish_command(host);
2439 else
2440 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002441 return;
2442 }
2443 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002444
Marek Vasut2e4456f2015-11-18 10:47:02 +01002445 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2446 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002447 sdhci_dumpregs(host);
2448
2449 return;
2450 }
2451
2452 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002453 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002454 else if (intmask & SDHCI_INT_DATA_END_BIT)
2455 host->data->error = -EILSEQ;
2456 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2457 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2458 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002459 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002460 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302461 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002462 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002463 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002464 if (host->ops->adma_workaround)
2465 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002466 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002467
Pierre Ossman17b04292007-07-22 22:18:46 +02002468 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002469 sdhci_finish_data(host);
2470 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002471 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002472 sdhci_transfer_pio(host);
2473
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002474 /*
2475 * We currently don't do anything fancy with DMA
2476 * boundaries, but as we can't disable the feature
2477 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002478 *
2479 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2480 * should return a valid address to continue from, but as
2481 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002482 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002483 if (intmask & SDHCI_INT_DMA_END) {
2484 u32 dmastart, dmanow;
2485 dmastart = sg_dma_address(host->data->sg);
2486 dmanow = dmastart + host->data->bytes_xfered;
2487 /*
2488 * Force update to the next DMA block boundary.
2489 */
2490 dmanow = (dmanow &
2491 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2492 SDHCI_DEFAULT_BOUNDARY_SIZE;
2493 host->data->bytes_xfered = dmanow - dmastart;
2494 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2495 " next 0x%08x\n",
2496 mmc_hostname(host->mmc), dmastart,
2497 host->data->bytes_xfered, dmanow);
2498 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2499 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002500
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002501 if (intmask & SDHCI_INT_DATA_END) {
2502 if (host->cmd) {
2503 /*
2504 * Data managed to finish before the
2505 * command completed. Make sure we do
2506 * things in the proper order.
2507 */
2508 host->data_early = 1;
2509 } else {
2510 sdhci_finish_data(host);
2511 }
2512 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002513 }
2514}
2515
David Howells7d12e782006-10-05 14:55:46 +01002516static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002517{
Russell King781e9892014-04-25 12:55:46 +01002518 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002519 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002520 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002521 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002522
2523 spin_lock(&host->lock);
2524
Russell Kingbe138552014-04-25 12:55:56 +01002525 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002526 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002527 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002528 }
2529
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002530 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002531 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002532 result = IRQ_NONE;
2533 goto out;
2534 }
2535
Russell King41005002014-04-25 12:55:36 +01002536 do {
2537 /* Clear selected interrupts. */
2538 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2539 SDHCI_INT_BUS_POWER);
2540 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002541
Russell King41005002014-04-25 12:55:36 +01002542 DBG("*** %s got interrupt: 0x%08x\n",
2543 mmc_hostname(host->mmc), intmask);
2544
2545 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2546 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2547 SDHCI_CARD_PRESENT;
2548
2549 /*
2550 * There is a observation on i.mx esdhc. INSERT
2551 * bit will be immediately set again when it gets
2552 * cleared, if a card is inserted. We have to mask
2553 * the irq to prevent interrupt storm which will
2554 * freeze the system. And the REMOVE gets the
2555 * same situation.
2556 *
2557 * More testing are needed here to ensure it works
2558 * for other platforms though.
2559 */
Russell Kingb537f942014-04-25 12:56:01 +01002560 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2561 SDHCI_INT_CARD_REMOVE);
2562 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2563 SDHCI_INT_CARD_INSERT;
2564 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2565 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002566
2567 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2568 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002569
2570 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2571 SDHCI_INT_CARD_REMOVE);
2572 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002573 }
2574
2575 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002576 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2577 &intmask);
Russell King41005002014-04-25 12:55:36 +01002578
2579 if (intmask & SDHCI_INT_DATA_MASK)
2580 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2581
2582 if (intmask & SDHCI_INT_BUS_POWER)
2583 pr_err("%s: Card is consuming too much power!\n",
2584 mmc_hostname(host->mmc));
2585
Russell King781e9892014-04-25 12:55:46 +01002586 if (intmask & SDHCI_INT_CARD_INT) {
2587 sdhci_enable_sdio_irq_nolock(host, false);
2588 host->thread_isr |= SDHCI_INT_CARD_INT;
2589 result = IRQ_WAKE_THREAD;
2590 }
Russell King41005002014-04-25 12:55:36 +01002591
2592 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2593 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2594 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2595 SDHCI_INT_CARD_INT);
2596
2597 if (intmask) {
2598 unexpected |= intmask;
2599 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2600 }
2601
Russell King781e9892014-04-25 12:55:46 +01002602 if (result == IRQ_NONE)
2603 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002604
2605 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002606 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002607out:
2608 spin_unlock(&host->lock);
2609
Alexander Stein6379b232012-03-14 09:52:10 +01002610 if (unexpected) {
2611 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2612 mmc_hostname(host->mmc), unexpected);
2613 sdhci_dumpregs(host);
2614 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002615
Pierre Ossmand129bce2006-03-24 03:18:17 -08002616 return result;
2617}
2618
Russell King781e9892014-04-25 12:55:46 +01002619static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2620{
2621 struct sdhci_host *host = dev_id;
2622 unsigned long flags;
2623 u32 isr;
2624
2625 spin_lock_irqsave(&host->lock, flags);
2626 isr = host->thread_isr;
2627 host->thread_isr = 0;
2628 spin_unlock_irqrestore(&host->lock, flags);
2629
Russell King3560db82014-04-25 12:55:51 +01002630 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2631 sdhci_card_event(host->mmc);
2632 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2633 }
2634
Russell King781e9892014-04-25 12:55:46 +01002635 if (isr & SDHCI_INT_CARD_INT) {
2636 sdio_run_irqs(host->mmc);
2637
2638 spin_lock_irqsave(&host->lock, flags);
2639 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2640 sdhci_enable_sdio_irq_nolock(host, true);
2641 spin_unlock_irqrestore(&host->lock, flags);
2642 }
2643
2644 return isr ? IRQ_HANDLED : IRQ_NONE;
2645}
2646
Pierre Ossmand129bce2006-03-24 03:18:17 -08002647/*****************************************************************************\
2648 * *
2649 * Suspend/resume *
2650 * *
2651\*****************************************************************************/
2652
2653#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002654void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2655{
2656 u8 val;
2657 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2658 | SDHCI_WAKE_ON_INT;
2659
2660 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2661 val |= mask ;
2662 /* Avoid fake wake up */
2663 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2664 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2665 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2666}
2667EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2668
Fabio Estevam0b10f472014-08-30 14:53:13 -03002669static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002670{
2671 u8 val;
2672 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2673 | SDHCI_WAKE_ON_INT;
2674
2675 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2676 val &= ~mask;
2677 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2678}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002679
Manuel Lauss29495aa2011-11-03 11:09:45 +01002680int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002681{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002682 sdhci_disable_card_detection(host);
2683
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002684 mmc_retune_timer_stop(host->mmc);
2685 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302686
Kevin Liuad080d72013-01-05 17:21:33 +08002687 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002688 host->ier = 0;
2689 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2690 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002691 free_irq(host->irq, host);
2692 } else {
2693 sdhci_enable_irq_wakeups(host);
2694 enable_irq_wake(host->irq);
2695 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002696 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002697}
2698
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002699EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002700
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002701int sdhci_resume_host(struct sdhci_host *host)
2702{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002703 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002704
Richard Röjforsa13abc72009-09-22 16:45:30 -07002705 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002706 if (host->ops->enable_dma)
2707 host->ops->enable_dma(host);
2708 }
2709
Adrian Hunter6308d292012-02-07 14:48:54 +02002710 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2711 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2712 /* Card keeps power but host controller does not */
2713 sdhci_init(host, 0);
2714 host->pwr = 0;
2715 host->clock = 0;
2716 sdhci_do_set_ios(host, &host->mmc->ios);
2717 } else {
2718 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2719 mmiowb();
2720 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002721
Haibo Chen14a7b41642015-09-15 18:32:58 +08002722 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2723 ret = request_threaded_irq(host->irq, sdhci_irq,
2724 sdhci_thread_irq, IRQF_SHARED,
2725 mmc_hostname(host->mmc), host);
2726 if (ret)
2727 return ret;
2728 } else {
2729 sdhci_disable_irq_wakeups(host);
2730 disable_irq_wake(host->irq);
2731 }
2732
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002733 sdhci_enable_card_detection(host);
2734
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002735 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002736}
2737
2738EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002739
2740static int sdhci_runtime_pm_get(struct sdhci_host *host)
2741{
2742 return pm_runtime_get_sync(host->mmc->parent);
2743}
2744
2745static int sdhci_runtime_pm_put(struct sdhci_host *host)
2746{
2747 pm_runtime_mark_last_busy(host->mmc->parent);
2748 return pm_runtime_put_autosuspend(host->mmc->parent);
2749}
2750
Adrian Hunterf0710a52013-05-06 12:17:32 +03002751static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2752{
Adrian Hunter5c671c42015-11-26 14:00:50 +02002753 if (host->bus_on)
Adrian Hunterf0710a52013-05-06 12:17:32 +03002754 return;
2755 host->bus_on = true;
2756 pm_runtime_get_noresume(host->mmc->parent);
2757}
2758
2759static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2760{
Adrian Hunter5c671c42015-11-26 14:00:50 +02002761 if (!host->bus_on)
Adrian Hunterf0710a52013-05-06 12:17:32 +03002762 return;
2763 host->bus_on = false;
2764 pm_runtime_put_noidle(host->mmc->parent);
2765}
2766
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002767int sdhci_runtime_suspend_host(struct sdhci_host *host)
2768{
2769 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002770
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002771 mmc_retune_timer_stop(host->mmc);
2772 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002773
2774 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002775 host->ier &= SDHCI_INT_CARD_INT;
2776 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2777 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002778 spin_unlock_irqrestore(&host->lock, flags);
2779
Russell King781e9892014-04-25 12:55:46 +01002780 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002781
2782 spin_lock_irqsave(&host->lock, flags);
2783 host->runtime_suspended = true;
2784 spin_unlock_irqrestore(&host->lock, flags);
2785
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002786 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002787}
2788EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2789
2790int sdhci_runtime_resume_host(struct sdhci_host *host)
2791{
2792 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002793 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002794
2795 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2796 if (host->ops->enable_dma)
2797 host->ops->enable_dma(host);
2798 }
2799
2800 sdhci_init(host, 0);
2801
2802 /* Force clock and power re-program */
2803 host->pwr = 0;
2804 host->clock = 0;
Jisheng Zhang3396e732015-01-29 17:42:12 +08002805 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002806 sdhci_do_set_ios(host, &host->mmc->ios);
2807
Kevin Liu52983382013-01-31 11:31:37 +08002808 if ((host_flags & SDHCI_PV_ENABLED) &&
2809 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2810 spin_lock_irqsave(&host->lock, flags);
2811 sdhci_enable_preset_value(host, true);
2812 spin_unlock_irqrestore(&host->lock, flags);
2813 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002814
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002815 spin_lock_irqsave(&host->lock, flags);
2816
2817 host->runtime_suspended = false;
2818
2819 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002820 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002821 sdhci_enable_sdio_irq_nolock(host, true);
2822
2823 /* Enable Card Detection */
2824 sdhci_enable_card_detection(host);
2825
2826 spin_unlock_irqrestore(&host->lock, flags);
2827
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002828 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002829}
2830EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2831
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002832#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002833
Pierre Ossmand129bce2006-03-24 03:18:17 -08002834/*****************************************************************************\
2835 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002836 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002837 * *
2838\*****************************************************************************/
2839
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002840struct sdhci_host *sdhci_alloc_host(struct device *dev,
2841 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002842{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002843 struct mmc_host *mmc;
2844 struct sdhci_host *host;
2845
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002846 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002847
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002848 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002849 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002850 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002851
2852 host = mmc_priv(mmc);
2853 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002854 host->mmc_host_ops = sdhci_ops;
2855 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002856
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002857 return host;
2858}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002859
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002860EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002861
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002862int sdhci_add_host(struct sdhci_host *host)
2863{
2864 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002865 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302866 u32 max_current_caps;
2867 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002868 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08002869 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002870 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002871
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002872 WARN_ON(host == NULL);
2873 if (host == NULL)
2874 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002875
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002876 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002877
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002878 if (debug_quirks)
2879 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002880 if (debug_quirks2)
2881 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002882
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002883 override_timeout_clk = host->timeout_clk;
2884
Russell King03231f92014-04-25 12:57:12 +01002885 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002886
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002887 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002888 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2889 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002890 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002891 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
2892 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002893 }
2894
Arindam Nathf2119df2011-05-05 12:18:57 +05302895 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002896 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002897
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002898 if (host->version >= SDHCI_SPEC_300)
2899 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2900 host->caps1 :
2901 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302902
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002903 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002904 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302905 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002906 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002907 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002908 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002909
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002910 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002911 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002912 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002913 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002914 }
2915
Arindam Nathf2119df2011-05-05 12:18:57 +05302916 if ((host->version >= SDHCI_SPEC_200) &&
2917 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002918 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002919
2920 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2921 (host->flags & SDHCI_USE_ADMA)) {
2922 DBG("Disabling ADMA as it is marked broken\n");
2923 host->flags &= ~SDHCI_USE_ADMA;
2924 }
2925
Adrian Huntere57a5f62014-11-04 12:42:46 +02002926 /*
2927 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2928 * and *must* do 64-bit DMA. A driver has the opportunity to change
2929 * that during the first call to ->enable_dma(). Similarly
2930 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2931 * implement.
2932 */
Al Cooper5eaa7472016-02-10 15:25:39 -05002933 if (caps[0] & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02002934 host->flags |= SDHCI_USE_64_BIT_DMA;
2935
Richard Röjforsa13abc72009-09-22 16:45:30 -07002936 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002937 if (host->ops->enable_dma) {
2938 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07002939 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002940 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002941 host->flags &=
2942 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002943 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002944 }
2945 }
2946
Adrian Huntere57a5f62014-11-04 12:42:46 +02002947 /* SDMA does not support 64-bit DMA */
2948 if (host->flags & SDHCI_USE_64_BIT_DMA)
2949 host->flags &= ~SDHCI_USE_SDMA;
2950
Pierre Ossman2134a922008-06-28 18:28:51 +02002951 if (host->flags & SDHCI_USE_ADMA) {
2952 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002953 * The DMA descriptor table size is calculated as the maximum
2954 * number of segments times 2, to allow for an alignment
2955 * descriptor for each segment, plus 1 for a nop end descriptor,
2956 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002957 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02002958 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2959 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2960 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002961 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002962 } else {
2963 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2964 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002965 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002966 }
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002967 host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +02002968 host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002969 &host->adma_addr,
2970 GFP_KERNEL);
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002971 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kingedd63fc2016-01-26 13:39:50 +00002972 host->align_buffer = dma_alloc_coherent(mmc_dev(mmc),
2973 host->align_buffer_sz,
2974 &host->align_addr,
2975 GFP_KERNEL);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002976 if (!host->adma_table || !host->align_buffer) {
Peng Fan7ac02032015-06-22 11:41:23 +08002977 if (host->adma_table)
2978 dma_free_coherent(mmc_dev(mmc),
2979 host->adma_table_sz,
2980 host->adma_table,
2981 host->adma_addr);
Russell Kingedd63fc2016-01-26 13:39:50 +00002982 if (host->align_buffer)
2983 dma_free_coherent(mmc_dev(mmc),
2984 host->align_buffer_sz,
2985 host->align_buffer,
2986 host->align_addr);
Joe Perches66061102014-09-12 14:56:56 -07002987 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002988 mmc_hostname(mmc));
2989 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002990 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01002991 host->align_buffer = NULL;
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002992 } else if (host->adma_addr & (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07002993 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2994 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002995 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002996 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002997 host->adma_table, host->adma_addr);
Russell Kingedd63fc2016-01-26 13:39:50 +00002998 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz,
2999 host->align_buffer, host->align_addr);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003000 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01003001 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003002 }
Russell Kingedd63fc2016-01-26 13:39:50 +00003003
3004 /* dma_alloc_coherent returns page aligned and sized buffers */
3005 BUG_ON(host->align_addr & SDHCI_ADMA2_MASK);
Pierre Ossman2134a922008-06-28 18:28:51 +02003006 }
3007
Pierre Ossman76591502008-07-21 00:32:11 +02003008 /*
3009 * If we use DMA, then it's up to the caller to set the DMA
3010 * mask, but PIO does not need the hw shim so we set a new
3011 * mask here in that case.
3012 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003013 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003014 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003015 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003016 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003017
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003018 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05303019 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003020 >> SDHCI_CLOCK_BASE_SHIFT;
3021 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303022 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003023 >> SDHCI_CLOCK_BASE_SHIFT;
3024
Pierre Ossmand129bce2006-03-24 03:18:17 -08003025 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003026 if (host->max_clk == 0 || host->quirks &
3027 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003028 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003029 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3030 mmc_hostname(mmc));
Ben Dooks4240ff02009-03-17 00:13:57 +03003031 return -ENODEV;
3032 }
3033 host->max_clk = host->ops->get_max_clock(host);
3034 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003035
3036 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303037 * In case of Host Controller v3.00, find out whether clock
3038 * multiplier is supported.
3039 */
3040 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3041 SDHCI_CLOCK_MUL_SHIFT;
3042
3043 /*
3044 * In case the value in Clock Multiplier is 0, then programmable
3045 * clock mode is not supported, otherwise the actual clock
3046 * multiplier is one more than the value of Clock Multiplier
3047 * in the Capabilities Register.
3048 */
3049 if (host->clk_mul)
3050 host->clk_mul += 1;
3051
3052 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003053 * Set host parameters.
3054 */
Dong Aisheng59241752015-07-22 20:53:07 +08003055 max_clk = host->max_clk;
3056
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003057 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003058 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303059 else if (host->version >= SDHCI_SPEC_300) {
3060 if (host->clk_mul) {
3061 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003062 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303063 } else
3064 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3065 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003066 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003067
Dong Aisheng59241752015-07-22 20:53:07 +08003068 if (!mmc->f_max || (mmc->f_max && (mmc->f_max > max_clk)))
3069 mmc->f_max = max_clk;
3070
Aisheng Dong28aab052014-08-27 15:26:31 +08003071 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3072 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3073 SDHCI_TIMEOUT_CLK_SHIFT;
3074 if (host->timeout_clk == 0) {
3075 if (host->ops->get_timeout_clock) {
3076 host->timeout_clk =
3077 host->ops->get_timeout_clock(host);
3078 } else {
3079 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3080 mmc_hostname(mmc));
3081 return -ENODEV;
3082 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003083 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003084
Aisheng Dong28aab052014-08-27 15:26:31 +08003085 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3086 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003087
Aisheng Dong28aab052014-08-27 15:26:31 +08003088 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003089 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003090 mmc->max_busy_timeout /= host->timeout_clk;
3091 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003092
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003093 if (override_timeout_clk)
3094 host->timeout_clk = override_timeout_clk;
3095
Andrei Warkentine89d4562011-05-23 15:06:37 -05003096 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003097 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003098
3099 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3100 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003101
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003102 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003103 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003104 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003105 !(host->flags & SDHCI_USE_SDMA)) &&
3106 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003107 host->flags |= SDHCI_AUTO_CMD23;
3108 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3109 } else {
3110 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3111 }
3112
Philip Rakity15ec4462010-11-19 16:48:39 -05003113 /*
3114 * A controller may support 8-bit width, but the board itself
3115 * might not have the pins brought out. Boards that support
3116 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3117 * their platform code before calling sdhci_add_host(), and we
3118 * won't assume 8-bit width for hosts without that CAP.
3119 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003120 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003121 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003122
Jerry Huang63ef5d82012-10-25 13:47:19 +08003123 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3124 mmc->caps &= ~MMC_CAP_CMD23;
3125
Arindam Nathf2119df2011-05-05 12:18:57 +05303126 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003127 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003128
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003129 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Ivan T. Ivanovc31d22e2015-07-06 15:16:20 +03003130 !(mmc->caps & MMC_CAP_NONREMOVABLE) &&
3131 IS_ERR_VALUE(mmc_gpio_get_cd(host->mmc)))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003132 mmc->caps |= MMC_CAP_NEEDS_POLL;
3133
Tim Kryger3a48edc2014-06-13 10:13:56 -07003134 /* If there are external regulators, get them */
3135 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3136 return -EPROBE_DEFER;
3137
Philip Rakity6231f3d2012-07-23 15:56:23 -07003138 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003139 if (!IS_ERR(mmc->supply.vqmmc)) {
3140 ret = regulator_enable(mmc->supply.vqmmc);
3141 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3142 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003143 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3144 SDHCI_SUPPORT_SDR50 |
3145 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003146 if (ret) {
3147 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3148 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003149 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003150 }
Kevin Liu8363c372012-11-17 17:55:51 -05003151 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003152
Daniel Drake6a661802012-11-25 13:01:19 -05003153 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3154 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3155 SDHCI_SUPPORT_DDR50);
3156
Al Cooper4188bba2012-03-16 15:54:17 -04003157 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3158 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3159 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303160 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3161
3162 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003163 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303164 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003165 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3166 * field can be promoted to support HS200.
3167 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003168 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003169 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003170 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303171 mmc->caps |= MMC_CAP_UHS_SDR50;
3172
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003173 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3174 (caps[1] & SDHCI_SUPPORT_HS400))
3175 mmc->caps2 |= MMC_CAP2_HS400;
3176
Adrian Hunter549c0b12014-11-06 15:19:05 +02003177 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3178 (IS_ERR(mmc->supply.vqmmc) ||
3179 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3180 1300000)))
3181 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3182
Micky Ching9107ebb2014-02-21 18:40:35 +08003183 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3184 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303185 mmc->caps |= MMC_CAP_UHS_DDR50;
3186
Girish K S069c9f12012-01-06 09:56:39 +05303187 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303188 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3189 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3190
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003191 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303192 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003193 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303194
Arindam Nathd6d50a12011-05-05 12:18:59 +05303195 /* Driver Type(s) (A, C, D) supported by the host */
3196 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3197 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3198 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3199 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3200 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3201 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3202
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303203 /* Initial value for re-tuning timer count */
3204 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3205 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3206
3207 /*
3208 * In case Re-tuning Timer is not disabled, the actual value of
3209 * re-tuning timer will be 2 ^ (n - 1).
3210 */
3211 if (host->tuning_count)
3212 host->tuning_count = 1 << (host->tuning_count - 1);
3213
3214 /* Re-tuning mode supported by the Host Controller */
3215 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3216 SDHCI_RETUNING_MODE_SHIFT;
3217
Takashi Iwai8f230f42010-12-08 10:04:30 +01003218 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003219
Arindam Nathf2119df2011-05-05 12:18:57 +05303220 /*
3221 * According to SD Host Controller spec v3.00, if the Host System
3222 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3223 * the value is meaningful only if Voltage Support in the Capabilities
3224 * register is set. The actual current value is 4 times the register
3225 * value.
3226 */
3227 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003228 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003229 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003230 if (curr > 0) {
3231
3232 /* convert to SDHCI_MAX_CURRENT format */
3233 curr = curr/1000; /* convert to mA */
3234 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3235
3236 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3237 max_current_caps =
3238 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3239 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3240 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3241 }
3242 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303243
3244 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003245 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303246
Aaron Lu55c46652012-07-04 13:31:48 +08003247 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303248 SDHCI_MAX_CURRENT_330_MASK) >>
3249 SDHCI_MAX_CURRENT_330_SHIFT) *
3250 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303251 }
3252 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003253 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303254
Aaron Lu55c46652012-07-04 13:31:48 +08003255 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303256 SDHCI_MAX_CURRENT_300_MASK) >>
3257 SDHCI_MAX_CURRENT_300_SHIFT) *
3258 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303259 }
3260 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003261 ocr_avail |= MMC_VDD_165_195;
3262
Aaron Lu55c46652012-07-04 13:31:48 +08003263 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303264 SDHCI_MAX_CURRENT_180_MASK) >>
3265 SDHCI_MAX_CURRENT_180_SHIFT) *
3266 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303267 }
3268
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003269 /* If OCR set by host, use it instead. */
3270 if (host->ocr_mask)
3271 ocr_avail = host->ocr_mask;
3272
3273 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003274 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003275 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003276
Takashi Iwai8f230f42010-12-08 10:04:30 +01003277 mmc->ocr_avail = ocr_avail;
3278 mmc->ocr_avail_sdio = ocr_avail;
3279 if (host->ocr_avail_sdio)
3280 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3281 mmc->ocr_avail_sd = ocr_avail;
3282 if (host->ocr_avail_sd)
3283 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3284 else /* normal SD controllers don't support 1.8V */
3285 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3286 mmc->ocr_avail_mmc = ocr_avail;
3287 if (host->ocr_avail_mmc)
3288 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003289
3290 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003291 pr_err("%s: Hardware doesn't report any support voltages.\n",
3292 mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003293 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003294 }
3295
Pierre Ossmand129bce2006-03-24 03:18:17 -08003296 spin_lock_init(&host->lock);
3297
3298 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003299 * Maximum number of segments. Depends on if the hardware
3300 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003301 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003302 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003303 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003304 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003305 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003306 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003307 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003308
3309 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003310 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3311 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3312 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003313 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003314 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003315
3316 /*
3317 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003318 * of bytes. When doing hardware scatter/gather, each entry cannot
3319 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003320 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003321 if (host->flags & SDHCI_USE_ADMA) {
3322 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3323 mmc->max_seg_size = 65535;
3324 else
3325 mmc->max_seg_size = 65536;
3326 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003327 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003328 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003329
3330 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003331 * Maximum block size. This varies from controller to controller and
3332 * is specified in the capabilities register.
3333 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003334 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3335 mmc->max_blk_size = 2;
3336 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303337 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003338 SDHCI_MAX_BLOCK_SHIFT;
3339 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003340 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3341 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003342 mmc->max_blk_size = 0;
3343 }
3344 }
3345
3346 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003347
3348 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003349 * Maximum block count.
3350 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003351 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003352
3353 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003354 * Init tasklets.
3355 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003356 tasklet_init(&host->finish_tasklet,
3357 sdhci_tasklet_finish, (unsigned long)host);
3358
Al Viroe4cad1b2006-10-10 22:47:07 +01003359 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003360
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003361 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303362
Shawn Guo2af502c2013-07-05 14:38:55 +08003363 sdhci_init(host, 0);
3364
Russell King781e9892014-04-25 12:55:46 +01003365 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3366 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003367 if (ret) {
3368 pr_err("%s: Failed to request IRQ %d: %d\n",
3369 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003370 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003371 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003372
Pierre Ossmand129bce2006-03-24 03:18:17 -08003373#ifdef CONFIG_MMC_DEBUG
3374 sdhci_dumpregs(host);
3375#endif
3376
Pierre Ossmanf9134312008-12-21 17:01:48 +01003377#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003378 snprintf(host->led_name, sizeof(host->led_name),
3379 "%s::", mmc_hostname(mmc));
3380 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003381 host->led.brightness = LED_OFF;
3382 host->led.default_trigger = mmc_hostname(mmc);
3383 host->led.brightness_set = sdhci_led_control;
3384
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003385 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003386 if (ret) {
3387 pr_err("%s: Failed to register LED device: %d\n",
3388 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003389 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003390 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003391#endif
3392
Pierre Ossman5f25a662006-10-04 02:15:39 -07003393 mmiowb();
3394
Pierre Ossmand129bce2006-03-24 03:18:17 -08003395 mmc_add_host(mmc);
3396
Girish K Sa3c76eb2011-10-11 11:44:09 +05303397 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003398 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003399 (host->flags & SDHCI_USE_ADMA) ?
3400 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003401 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003402
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003403 sdhci_enable_card_detection(host);
3404
Pierre Ossmand129bce2006-03-24 03:18:17 -08003405 return 0;
3406
Pierre Ossmanf9134312008-12-21 17:01:48 +01003407#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003408reset:
Russell King03231f92014-04-25 12:57:12 +01003409 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003410 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3411 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003412 free_irq(host->irq, host);
3413#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003414untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003415 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003416
3417 return ret;
3418}
3419
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003420EXPORT_SYMBOL_GPL(sdhci_add_host);
3421
Pierre Ossman1e728592008-04-16 19:13:13 +02003422void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003423{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003424 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003425 unsigned long flags;
3426
3427 if (dead) {
3428 spin_lock_irqsave(&host->lock, flags);
3429
3430 host->flags |= SDHCI_DEVICE_DEAD;
3431
3432 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303433 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003434 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003435
3436 host->mrq->cmd->error = -ENOMEDIUM;
3437 tasklet_schedule(&host->finish_tasklet);
3438 }
3439
3440 spin_unlock_irqrestore(&host->lock, flags);
3441 }
3442
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003443 sdhci_disable_card_detection(host);
3444
Markus Mayer4e743f12014-07-03 13:27:42 -07003445 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003446
Pierre Ossmanf9134312008-12-21 17:01:48 +01003447#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003448 led_classdev_unregister(&host->led);
3449#endif
3450
Pierre Ossman1e728592008-04-16 19:13:13 +02003451 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003452 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003453
Russell Kingb537f942014-04-25 12:56:01 +01003454 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3455 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003456 free_irq(host->irq, host);
3457
3458 del_timer_sync(&host->timer);
3459
Pierre Ossmand129bce2006-03-24 03:18:17 -08003460 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003461
Tim Kryger3a48edc2014-06-13 10:13:56 -07003462 if (!IS_ERR(mmc->supply.vqmmc))
3463 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003464
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003465 if (host->adma_table)
Adrian Hunter76fe3792014-11-04 12:42:42 +02003466 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003467 host->adma_table, host->adma_addr);
Russell Kingedd63fc2016-01-26 13:39:50 +00003468 if (host->align_buffer)
3469 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz,
3470 host->align_buffer, host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003471
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003472 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003473 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003474}
3475
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003476EXPORT_SYMBOL_GPL(sdhci_remove_host);
3477
3478void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003479{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003480 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003481}
3482
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003483EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003484
3485/*****************************************************************************\
3486 * *
3487 * Driver init/exit *
3488 * *
3489\*****************************************************************************/
3490
3491static int __init sdhci_drv_init(void)
3492{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303493 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003494 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303495 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003496
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003497 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003498}
3499
3500static void __exit sdhci_drv_exit(void)
3501{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003502}
3503
3504module_init(sdhci_drv_init);
3505module_exit(sdhci_drv_exit);
3506
Pierre Ossmandf673b22006-06-30 02:22:31 -07003507module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003508module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003509
Pierre Ossman32710e82009-04-08 20:14:54 +02003510MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003511MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003512MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003513
Pierre Ossmandf673b22006-06-30 02:22:31 -07003514MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003515MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");