blob: 54ab050bc47bcf4a41905e2e0febed35389a41b1 [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Aaron Lu473b0952012-07-03 17:27:49 +080030#include <linux/mmc/card.h>
Corneliu Doban85cc1c32015-02-09 16:06:29 -080031#include <linux/mmc/sdio.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080032#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080033
Pierre Ossmand129bce2006-03-24 03:18:17 -080034#include "sdhci.h"
35
36#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmand129bce2006-03-24 03:18:17 -080038#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010039 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080040
Pierre Ossmanf9134312008-12-21 17:01:48 +010041#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
42 defined(CONFIG_MMC_SDHCI_MODULE))
43#define SDHCI_USE_LEDS_CLASS
44#endif
45
Arindam Nathb513ea22011-05-05 12:19:04 +053046#define MAX_TUNING_LOOP 40
47
Pierre Ossmandf673b22006-06-30 02:22:31 -070048static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030049static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070050
Pierre Ossmand129bce2006-03-24 03:18:17 -080051static void sdhci_finish_data(struct sdhci_host *);
52
Pierre Ossmand129bce2006-03-24 03:18:17 -080053static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053054static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Kevin Liu52983382013-01-31 11:31:37 +080055static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Haibo Chen348487c2014-12-09 17:04:05 +080056static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Haibo Chend31911b2015-08-25 10:02:11 +080057 struct mmc_data *data);
Scott Branden04e079cf2015-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{
Pierre Ossman2134a922008-06-28 18:28:51 +0200468 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200469 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000470 dma_addr_t addr, align_addr;
471 void *desc, *align;
472 char *buffer;
473 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200474
475 /*
476 * The spec does not specify endianness of descriptor table.
477 * We currently guess that it is LE.
478 */
479
Haibo Chend31911b2015-08-25 10:02:11 +0800480 host->sg_count = sdhci_pre_dma_transfer(host, data);
Haibo Chen348487c2014-12-09 17:04:05 +0800481 if (host->sg_count < 0)
Russell Kingedd63fc2016-01-26 13:39:50 +0000482 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200483
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200484 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200485 align = host->align_buffer;
486
487 align_addr = host->align_addr;
488
489 for_each_sg(data->sg, sg, host->sg_count, i) {
490 addr = sg_dma_address(sg);
491 len = sg_dma_len(sg);
492
493 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000494 * The SDHCI specification states that ADMA addresses must
495 * be 32-bit aligned. If they aren't, then we use a bounce
496 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200497 * alignment.
498 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200499 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
500 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200501 if (offset) {
502 if (data->flags & MMC_DATA_WRITE) {
503 buffer = sdhci_kmap_atomic(sg, &flags);
504 memcpy(align, buffer, offset);
505 sdhci_kunmap_atomic(buffer, &flags);
506 }
507
Ben Dooks118cd172010-03-05 13:43:26 -0800508 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200509 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200510 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200511
512 BUG_ON(offset > 65536);
513
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200514 align += SDHCI_ADMA2_ALIGN;
515 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200516
Adrian Hunter76fe3792014-11-04 12:42:42 +0200517 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200518
519 addr += offset;
520 len -= offset;
521 }
522
Pierre Ossman2134a922008-06-28 18:28:51 +0200523 BUG_ON(len > 65536);
524
Adrian Hunter347ea322015-11-26 14:00:48 +0200525 if (len) {
526 /* tran, valid */
527 sdhci_adma_write_desc(host, desc, addr, len,
528 ADMA2_TRAN_VALID);
529 desc += host->desc_sz;
530 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200531
532 /*
533 * If this triggers then we have a calculation bug
534 * somewhere. :/
535 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200536 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200537 }
538
Thomas Abraham70764a92010-05-26 14:42:04 -0700539 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000540 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200541 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200542 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200543 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700544 }
545 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000546 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200547 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700548 }
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200549 return 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200550}
551
552static void sdhci_adma_table_post(struct sdhci_host *host,
553 struct mmc_data *data)
554{
Pierre Ossman2134a922008-06-28 18:28:51 +0200555 struct scatterlist *sg;
556 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200557 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200558 char *buffer;
559 unsigned long flags;
560
Russell King47fa9612016-01-26 13:40:06 +0000561 if (data->flags & MMC_DATA_READ) {
562 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100563
Russell King47fa9612016-01-26 13:40:06 +0000564 /* Do a quick scan of the SG list for any unaligned mappings */
565 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200566 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000567 has_unaligned = true;
568 break;
569 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200570
Russell King47fa9612016-01-26 13:40:06 +0000571 if (has_unaligned) {
572 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000573 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200574
Russell King47fa9612016-01-26 13:40:06 +0000575 align = host->align_buffer;
576
577 for_each_sg(data->sg, sg, host->sg_count, i) {
578 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
579 size = SDHCI_ADMA2_ALIGN -
580 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
581
582 buffer = sdhci_kmap_atomic(sg, &flags);
583 memcpy(buffer, align, size);
584 sdhci_kunmap_atomic(buffer, &flags);
585
586 align += SDHCI_ADMA2_ALIGN;
587 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200588 }
589 }
590 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200591}
592
Andrei Warkentina3c77782011-04-11 16:13:42 -0500593static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800594{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700595 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500596 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700597 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800598
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200599 /*
600 * If the host controller provides us with an incorrect timeout
601 * value, just skip the check and use 0xE. The hardware may take
602 * longer to time out, but that's much better than having a too-short
603 * timeout value.
604 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200605 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200606 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200607
Andrei Warkentina3c77782011-04-11 16:13:42 -0500608 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100609 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500610 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800611
Andrei Warkentina3c77782011-04-11 16:13:42 -0500612 /* timeout in us */
613 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100614 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300615 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000616 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000617 if (host->clock && data->timeout_clks) {
618 unsigned long long val;
619
620 /*
621 * data->timeout_clks is in units of clock cycles.
622 * host->clock is in Hz. target_timeout is in us.
623 * Hence, us = 1000000 * cycles / Hz. Round up.
624 */
625 val = 1000000 * data->timeout_clks;
626 if (do_div(val, host->clock))
627 target_timeout++;
628 target_timeout += val;
629 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300630 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700631
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700632 /*
633 * Figure out needed cycles.
634 * We do this in steps in order to fit inside a 32 bit int.
635 * The first step is the minimum timeout, which will have a
636 * minimum resolution of 6 bits:
637 * (1) 2^13*1000 > 2^22,
638 * (2) host->timeout_clk < 2^16
639 * =>
640 * (1) / (2) > 2^6
641 */
642 count = 0;
643 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
644 while (current_timeout < target_timeout) {
645 count++;
646 current_timeout <<= 1;
647 if (count >= 0xF)
648 break;
649 }
650
651 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400652 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
653 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700654 count = 0xE;
655 }
656
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200657 return count;
658}
659
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300660static void sdhci_set_transfer_irqs(struct sdhci_host *host)
661{
662 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
663 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
664
665 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100666 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300667 else
Russell Kingb537f942014-04-25 12:56:01 +0100668 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
669
670 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
671 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300672}
673
Aisheng Dongb45e6682014-08-27 15:26:29 +0800674static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200675{
676 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800677
678 if (host->ops->set_timeout) {
679 host->ops->set_timeout(host, cmd);
680 } else {
681 count = sdhci_calc_timeout(host, cmd);
682 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
683 }
684}
685
686static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
687{
Pierre Ossman2134a922008-06-28 18:28:51 +0200688 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500689 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200690 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200691
692 WARN_ON(host->data);
693
Aisheng Dongb45e6682014-08-27 15:26:29 +0800694 if (data || (cmd->flags & MMC_RSP_BUSY))
695 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500696
697 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200698 return;
699
700 /* Sanity checks */
701 BUG_ON(data->blksz * data->blocks > 524288);
702 BUG_ON(data->blksz > host->mmc->max_blk_size);
703 BUG_ON(data->blocks > 65535);
704
705 host->data = data;
706 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400707 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200708
Richard Röjforsa13abc72009-09-22 16:45:30 -0700709 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100710 host->flags |= SDHCI_REQ_USE_DMA;
711
Pierre Ossman2134a922008-06-28 18:28:51 +0200712 /*
713 * FIXME: This doesn't account for merging when mapping the
714 * scatterlist.
715 */
716 if (host->flags & SDHCI_REQ_USE_DMA) {
717 int broken, i;
718 struct scatterlist *sg;
719
720 broken = 0;
721 if (host->flags & SDHCI_USE_ADMA) {
722 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
723 broken = 1;
724 } else {
725 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
726 broken = 1;
727 }
728
729 if (unlikely(broken)) {
730 for_each_sg(data->sg, sg, data->sg_len, i) {
731 if (sg->length & 0x3) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100732 DBG("Reverting to PIO because of transfer size (%d)\n",
Pierre Ossman2134a922008-06-28 18:28:51 +0200733 sg->length);
734 host->flags &= ~SDHCI_REQ_USE_DMA;
735 break;
736 }
737 }
738 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100739 }
740
741 /*
742 * The assumption here being that alignment is the same after
743 * translation to device address space.
744 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200745 if (host->flags & SDHCI_REQ_USE_DMA) {
746 int broken, i;
747 struct scatterlist *sg;
748
749 broken = 0;
750 if (host->flags & SDHCI_USE_ADMA) {
751 /*
752 * As we use 3 byte chunks to work around
753 * alignment problems, we need to check this
754 * quirk.
755 */
756 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
757 broken = 1;
758 } else {
759 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
760 broken = 1;
761 }
762
763 if (unlikely(broken)) {
764 for_each_sg(data->sg, sg, data->sg_len, i) {
765 if (sg->offset & 0x3) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100766 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200767 host->flags &= ~SDHCI_REQ_USE_DMA;
768 break;
769 }
770 }
771 }
772 }
773
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200774 if (host->flags & SDHCI_REQ_USE_DMA) {
775 if (host->flags & SDHCI_USE_ADMA) {
776 ret = sdhci_adma_table_pre(host, data);
777 if (ret) {
778 /*
779 * This only happens when someone fed
780 * us an invalid request.
781 */
782 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200783 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200784 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300785 sdhci_writel(host, host->adma_addr,
786 SDHCI_ADMA_ADDRESS);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200787 if (host->flags & SDHCI_USE_64_BIT_DMA)
788 sdhci_writel(host,
789 (u64)host->adma_addr >> 32,
790 SDHCI_ADMA_ADDRESS_HI);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200791 }
792 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300793 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200794
Haibo Chend31911b2015-08-25 10:02:11 +0800795 sg_cnt = sdhci_pre_dma_transfer(host, data);
Jiri Slaby62a7f362015-06-12 11:45:02 +0200796 if (sg_cnt <= 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200797 /*
798 * This only happens when someone fed
799 * us an invalid request.
800 */
801 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200802 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200803 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200804 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300805 sdhci_writel(host, sg_dma_address(data->sg),
806 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200807 }
808 }
809 }
810
Pierre Ossman2134a922008-06-28 18:28:51 +0200811 /*
812 * Always adjust the DMA selection as some controllers
813 * (e.g. JMicron) can't do PIO properly when the selection
814 * is ADMA.
815 */
816 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300817 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200818 ctrl &= ~SDHCI_CTRL_DMA_MASK;
819 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200820 (host->flags & SDHCI_USE_ADMA)) {
821 if (host->flags & SDHCI_USE_64_BIT_DMA)
822 ctrl |= SDHCI_CTRL_ADMA64;
823 else
824 ctrl |= SDHCI_CTRL_ADMA32;
825 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200826 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200827 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300828 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100829 }
830
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200831 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200832 int flags;
833
834 flags = SG_MITER_ATOMIC;
835 if (host->data->flags & MMC_DATA_READ)
836 flags |= SG_MITER_TO_SG;
837 else
838 flags |= SG_MITER_FROM_SG;
839 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200840 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800841 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700842
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300843 sdhci_set_transfer_irqs(host);
844
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400845 /* Set the DMA boundary value and block size */
846 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
847 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300848 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700849}
850
851static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500852 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700853{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800854 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500855 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700856
Dong Aisheng2b558c12013-10-30 22:09:48 +0800857 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800858 if (host->quirks2 &
859 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
860 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
861 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800862 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800863 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
864 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800865 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800866 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700867 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800868 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700869
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200870 WARN_ON(!host->data);
871
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800872 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
873 mode = SDHCI_TRNS_BLK_CNT_EN;
874
Andrei Warkentine89d4562011-05-23 15:06:37 -0500875 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800876 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500877 /*
878 * If we are sending CMD23, CMD12 never gets sent
879 * on successful completion (so no Auto-CMD12).
880 */
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800881 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
882 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500883 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500884 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
885 mode |= SDHCI_TRNS_AUTO_CMD23;
886 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
887 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700888 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500889
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700890 if (data->flags & MMC_DATA_READ)
891 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100892 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700893 mode |= SDHCI_TRNS_DMA;
894
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300895 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800896}
897
898static void sdhci_finish_data(struct sdhci_host *host)
899{
900 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800901
902 BUG_ON(!host->data);
903
904 data = host->data;
905 host->data = NULL;
906
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100907 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200908 if (host->flags & SDHCI_USE_ADMA)
909 sdhci_adma_table_post(host, data);
Russell Kingf55c98f2016-01-26 13:40:11 +0000910
911 if (data->host_cookie == COOKIE_MAPPED) {
912 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
913 (data->flags & MMC_DATA_READ) ?
914 DMA_FROM_DEVICE : DMA_TO_DEVICE);
915 data->host_cookie = COOKIE_UNMAPPED;
Pierre Ossman2134a922008-06-28 18:28:51 +0200916 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800917 }
918
919 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200920 * The specification states that the block count register must
921 * be updated, but it does not specify at what point in the
922 * data flow. That makes the register entirely useless to read
923 * back so we have to assume that nothing made it to the card
924 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800925 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200926 if (data->error)
927 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800928 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200929 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800930
Andrei Warkentine89d4562011-05-23 15:06:37 -0500931 /*
932 * Need to send CMD12 if -
933 * a) open-ended multiblock transfer (no CMD23)
934 * b) error in multiblock transfer
935 */
936 if (data->stop &&
937 (data->error ||
938 !host->mrq->sbc)) {
939
Pierre Ossmand129bce2006-03-24 03:18:17 -0800940 /*
941 * The controller needs a reset of internal state machines
942 * upon error conditions.
943 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200944 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100945 sdhci_do_reset(host, SDHCI_RESET_CMD);
946 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800947 }
948
949 sdhci_send_command(host, data->stop);
950 } else
951 tasklet_schedule(&host->finish_tasklet);
952}
953
Dong Aishengc0e551292013-09-13 19:11:31 +0800954void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800955{
956 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700957 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700958 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800959
960 WARN_ON(host->cmd);
961
Russell King96776202016-01-26 13:39:34 +0000962 /* Initially, a command has no error */
963 cmd->error = 0;
964
Pierre Ossmand129bce2006-03-24 03:18:17 -0800965 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700966 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700967
968 mask = SDHCI_CMD_INHIBIT;
969 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
970 mask |= SDHCI_DATA_INHIBIT;
971
972 /* We shouldn't wait for data inihibit for stop commands, even
973 though they might use busy signaling */
974 if (host->mrq->data && (cmd == host->mrq->data->stop))
975 mask &= ~SDHCI_DATA_INHIBIT;
976
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300977 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700978 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100979 pr_err("%s: Controller never released inhibit bit(s).\n",
980 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800981 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200982 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983 tasklet_schedule(&host->finish_tasklet);
984 return;
985 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700986 timeout--;
987 mdelay(1);
988 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800989
Adrian Hunter3e1a6892013-11-14 10:16:20 +0200990 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100991 if (!cmd->data && cmd->busy_timeout > 9000)
992 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +0200993 else
994 timeout += 10 * HZ;
995 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996
997 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +0900998 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800999
Andrei Warkentina3c77782011-04-11 16:13:42 -05001000 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001001
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001002 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001003
Andrei Warkentine89d4562011-05-23 15:06:37 -05001004 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001005
Pierre Ossmand129bce2006-03-24 03:18:17 -08001006 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301007 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001008 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001009 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001010 tasklet_schedule(&host->finish_tasklet);
1011 return;
1012 }
1013
1014 if (!(cmd->flags & MMC_RSP_PRESENT))
1015 flags = SDHCI_CMD_RESP_NONE;
1016 else if (cmd->flags & MMC_RSP_136)
1017 flags = SDHCI_CMD_RESP_LONG;
1018 else if (cmd->flags & MMC_RSP_BUSY)
1019 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1020 else
1021 flags = SDHCI_CMD_RESP_SHORT;
1022
1023 if (cmd->flags & MMC_RSP_CRC)
1024 flags |= SDHCI_CMD_CRC;
1025 if (cmd->flags & MMC_RSP_OPCODE)
1026 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301027
1028 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301029 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1030 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001031 flags |= SDHCI_CMD_DATA;
1032
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001033 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001034}
Dong Aishengc0e551292013-09-13 19:11:31 +08001035EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001036
1037static void sdhci_finish_command(struct sdhci_host *host)
1038{
1039 int i;
1040
1041 BUG_ON(host->cmd == NULL);
1042
1043 if (host->cmd->flags & MMC_RSP_PRESENT) {
1044 if (host->cmd->flags & MMC_RSP_136) {
1045 /* CRC is stripped so we need to do some shifting. */
1046 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001047 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001048 SDHCI_RESPONSE + (3-i)*4) << 8;
1049 if (i != 3)
1050 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001051 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001052 SDHCI_RESPONSE + (3-i)*4-1);
1053 }
1054 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001055 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001056 }
1057 }
1058
Andrei Warkentine89d4562011-05-23 15:06:37 -05001059 /* Finished CMD23, now send actual command. */
1060 if (host->cmd == host->mrq->sbc) {
1061 host->cmd = NULL;
1062 sdhci_send_command(host, host->mrq->cmd);
1063 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001064
Andrei Warkentine89d4562011-05-23 15:06:37 -05001065 /* Processed actual command. */
1066 if (host->data && host->data_early)
1067 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001068
Andrei Warkentine89d4562011-05-23 15:06:37 -05001069 if (!host->cmd->data)
1070 tasklet_schedule(&host->finish_tasklet);
1071
1072 host->cmd = NULL;
1073 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001074}
1075
Kevin Liu52983382013-01-31 11:31:37 +08001076static u16 sdhci_get_preset_value(struct sdhci_host *host)
1077{
Russell Kingd975f122014-04-25 12:59:31 +01001078 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001079
Russell Kingd975f122014-04-25 12:59:31 +01001080 switch (host->timing) {
1081 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001082 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1083 break;
Russell Kingd975f122014-04-25 12:59:31 +01001084 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001085 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1086 break;
Russell Kingd975f122014-04-25 12:59:31 +01001087 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001088 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1089 break;
Russell Kingd975f122014-04-25 12:59:31 +01001090 case MMC_TIMING_UHS_SDR104:
1091 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001092 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1093 break;
Russell Kingd975f122014-04-25 12:59:31 +01001094 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001095 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001096 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1097 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001098 case MMC_TIMING_MMC_HS400:
1099 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1100 break;
Kevin Liu52983382013-01-31 11:31:37 +08001101 default:
1102 pr_warn("%s: Invalid UHS-I mode selected\n",
1103 mmc_hostname(host->mmc));
1104 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1105 break;
1106 }
1107 return preset;
1108}
1109
Russell King17710592014-04-25 12:58:55 +01001110void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001111{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301112 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001113 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301114 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001115 unsigned long timeout;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001116 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001117
Russell King1650d0c2014-04-25 12:58:50 +01001118 host->mmc->actual_clock = 0;
1119
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001120 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
ludovic.desroches@atmel.comaf951762015-09-17 10:16:19 +02001121 if (host->quirks2 & SDHCI_QUIRK2_NEED_DELAY_AFTER_INT_CLK_RST)
1122 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001123
1124 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001125 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001126
Zhangfei Gao85105c52010-08-06 07:10:01 +08001127 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001128 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001129 u16 pre_val;
1130
1131 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1132 pre_val = sdhci_get_preset_value(host);
1133 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1134 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1135 if (host->clk_mul &&
1136 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1137 clk = SDHCI_PROG_CLOCK_MODE;
1138 real_div = div + 1;
1139 clk_mul = host->clk_mul;
1140 } else {
1141 real_div = max_t(int, 1, div << 1);
1142 }
1143 goto clock_set;
1144 }
1145
Arindam Nathc3ed3872011-05-05 12:19:06 +05301146 /*
1147 * Check if the Host Controller supports Programmable Clock
1148 * Mode.
1149 */
1150 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001151 for (div = 1; div <= 1024; div++) {
1152 if ((host->max_clk * host->clk_mul / div)
1153 <= clock)
1154 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001155 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001156 if ((host->max_clk * host->clk_mul / div) <= clock) {
1157 /*
1158 * Set Programmable Clock Mode in the Clock
1159 * Control register.
1160 */
1161 clk = SDHCI_PROG_CLOCK_MODE;
1162 real_div = div;
1163 clk_mul = host->clk_mul;
1164 div--;
1165 } else {
1166 /*
1167 * Divisor can be too small to reach clock
1168 * speed requirement. Then use the base clock.
1169 */
1170 switch_base_clk = true;
1171 }
1172 }
1173
1174 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301175 /* Version 3.00 divisors must be a multiple of 2. */
1176 if (host->max_clk <= clock)
1177 div = 1;
1178 else {
1179 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1180 div += 2) {
1181 if ((host->max_clk / div) <= clock)
1182 break;
1183 }
1184 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001185 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301186 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301187 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1188 && !div && host->max_clk <= 25000000)
1189 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001190 }
1191 } else {
1192 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001193 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001194 if ((host->max_clk / div) <= clock)
1195 break;
1196 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001197 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301198 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001199 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001200
Kevin Liu52983382013-01-31 11:31:37 +08001201clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001202 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001203 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301204 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001205 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1206 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001207 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001208 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001209
Chris Ball27f6cb12009-09-22 16:45:31 -07001210 /* Wait max 20 ms */
1211 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001212 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001213 & SDHCI_CLOCK_INT_STABLE)) {
1214 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001215 pr_err("%s: Internal clock never stabilised.\n",
1216 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001217 sdhci_dumpregs(host);
1218 return;
1219 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001220 timeout--;
1221 mdelay(1);
1222 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001223
1224 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001225 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001226}
Russell King17710592014-04-25 12:58:55 +01001227EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001228
Russell King24fbb3c2014-04-25 13:00:06 +01001229static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1230 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001231{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001232 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001233 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001234
Russell King24fbb3c2014-04-25 13:00:06 +01001235 if (mode != MMC_POWER_OFF) {
1236 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001237 case MMC_VDD_165_195:
1238 pwr = SDHCI_POWER_180;
1239 break;
1240 case MMC_VDD_29_30:
1241 case MMC_VDD_30_31:
1242 pwr = SDHCI_POWER_300;
1243 break;
1244 case MMC_VDD_32_33:
1245 case MMC_VDD_33_34:
1246 pwr = SDHCI_POWER_330;
1247 break;
1248 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001249 WARN(1, "%s: Invalid vdd %#x\n",
1250 mmc_hostname(host->mmc), vdd);
1251 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001252 }
1253 }
1254
1255 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001256 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001257
Pierre Ossmanae628902009-05-03 20:45:03 +02001258 host->pwr = pwr;
1259
1260 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001261 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001262 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1263 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001264 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001265 } else {
1266 /*
1267 * Spec says that we should clear the power reg before setting
1268 * a new value. Some controllers don't seem to like this though.
1269 */
1270 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1271 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001272
Russell Kinge921a8b2014-04-25 13:00:01 +01001273 /*
1274 * At least the Marvell CaFe chip gets confused if we set the
1275 * voltage and set turn on power at the same time, so set the
1276 * voltage first.
1277 */
1278 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1279 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001280
Russell Kinge921a8b2014-04-25 13:00:01 +01001281 pwr |= SDHCI_POWER_ON;
1282
Pierre Ossmanae628902009-05-03 20:45:03 +02001283 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1284
Russell Kinge921a8b2014-04-25 13:00:01 +01001285 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1286 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001287
Russell Kinge921a8b2014-04-25 13:00:01 +01001288 /*
1289 * Some controllers need an extra 10ms delay of 10ms before
1290 * they can apply clock after applying power
1291 */
1292 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1293 mdelay(10);
1294 }
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001295
1296 if (!IS_ERR(mmc->supply.vmmc)) {
1297 spin_unlock_irq(&host->lock);
1298 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1299 spin_lock_irq(&host->lock);
1300 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001301}
1302
Pierre Ossmand129bce2006-03-24 03:18:17 -08001303/*****************************************************************************\
1304 * *
1305 * MMC callbacks *
1306 * *
1307\*****************************************************************************/
1308
1309static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1310{
1311 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001312 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001313 unsigned long flags;
1314
1315 host = mmc_priv(mmc);
1316
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001317 sdhci_runtime_pm_get(host);
1318
Scott Branden04e079cf2015-03-10 11:35:10 -07001319 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001320 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001321
Pierre Ossmand129bce2006-03-24 03:18:17 -08001322 spin_lock_irqsave(&host->lock, flags);
1323
1324 WARN_ON(host->mrq != NULL);
1325
Pierre Ossmanf9134312008-12-21 17:01:48 +01001326#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001327 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001328#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001329
1330 /*
1331 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1332 * requests if Auto-CMD12 is enabled.
1333 */
1334 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001335 if (mrq->stop) {
1336 mrq->data->stop = NULL;
1337 mrq->stop = NULL;
1338 }
1339 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001340
1341 host->mrq = mrq;
1342
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001343 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001344 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001345 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301346 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001347 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001348 sdhci_send_command(host, mrq->sbc);
1349 else
1350 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301351 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001352
Pierre Ossman5f25a662006-10-04 02:15:39 -07001353 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001354 spin_unlock_irqrestore(&host->lock, flags);
1355}
1356
Russell King2317f562014-04-25 12:57:07 +01001357void sdhci_set_bus_width(struct sdhci_host *host, int width)
1358{
1359 u8 ctrl;
1360
1361 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1362 if (width == MMC_BUS_WIDTH_8) {
1363 ctrl &= ~SDHCI_CTRL_4BITBUS;
1364 if (host->version >= SDHCI_SPEC_300)
1365 ctrl |= SDHCI_CTRL_8BITBUS;
1366 } else {
1367 if (host->version >= SDHCI_SPEC_300)
1368 ctrl &= ~SDHCI_CTRL_8BITBUS;
1369 if (width == MMC_BUS_WIDTH_4)
1370 ctrl |= SDHCI_CTRL_4BITBUS;
1371 else
1372 ctrl &= ~SDHCI_CTRL_4BITBUS;
1373 }
1374 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1375}
1376EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1377
Russell King96d7b782014-04-25 12:59:26 +01001378void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1379{
1380 u16 ctrl_2;
1381
1382 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1383 /* Select Bus Speed Mode for host */
1384 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1385 if ((timing == MMC_TIMING_MMC_HS200) ||
1386 (timing == MMC_TIMING_UHS_SDR104))
1387 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1388 else if (timing == MMC_TIMING_UHS_SDR12)
1389 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1390 else if (timing == MMC_TIMING_UHS_SDR25)
1391 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1392 else if (timing == MMC_TIMING_UHS_SDR50)
1393 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1394 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1395 (timing == MMC_TIMING_MMC_DDR52))
1396 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001397 else if (timing == MMC_TIMING_MMC_HS400)
1398 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001399 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1400}
1401EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1402
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001403static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001404{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001405 unsigned long flags;
1406 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001407 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001408
Pierre Ossmand129bce2006-03-24 03:18:17 -08001409 spin_lock_irqsave(&host->lock, flags);
1410
Adrian Hunterceb61432011-12-27 15:48:41 +02001411 if (host->flags & SDHCI_DEVICE_DEAD) {
1412 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001413 if (!IS_ERR(mmc->supply.vmmc) &&
1414 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001415 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001416 return;
1417 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001418
Pierre Ossmand129bce2006-03-24 03:18:17 -08001419 /*
1420 * Reset the chip on each power off.
1421 * Should clear out any weird states.
1422 */
1423 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001424 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001425 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001426 }
1427
Kevin Liu52983382013-01-31 11:31:37 +08001428 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001429 (ios->power_mode == MMC_POWER_UP) &&
1430 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001431 sdhci_enable_preset_value(host, false);
1432
Russell King373073e2014-04-25 12:58:45 +01001433 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001434 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001435 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001436
1437 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1438 host->clock) {
1439 host->timeout_clk = host->mmc->actual_clock ?
1440 host->mmc->actual_clock / 1000 :
1441 host->clock / 1000;
1442 host->mmc->max_busy_timeout =
1443 host->ops->get_max_timeout_count ?
1444 host->ops->get_max_timeout_count(host) :
1445 1 << 27;
1446 host->mmc->max_busy_timeout /= host->timeout_clk;
1447 }
Russell King373073e2014-04-25 12:58:45 +01001448 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001449
Russell King24fbb3c2014-04-25 13:00:06 +01001450 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001451
Philip Rakity643a81f2010-09-23 08:24:32 -07001452 if (host->ops->platform_send_init_74_clocks)
1453 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1454
Russell King2317f562014-04-25 12:57:07 +01001455 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001456
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001457 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001458
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001459 if ((ios->timing == MMC_TIMING_SD_HS ||
1460 ios->timing == MMC_TIMING_MMC_HS)
1461 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001462 ctrl |= SDHCI_CTRL_HISPD;
1463 else
1464 ctrl &= ~SDHCI_CTRL_HISPD;
1465
Arindam Nathd6d50a12011-05-05 12:18:59 +05301466 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301467 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301468
1469 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001470 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1471 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001472 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301473 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301474 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1475 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001476 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301477 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301478
Russell Kingda91a8f2014-04-25 13:00:12 +01001479 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301480 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301481 /*
1482 * We only need to set Driver Strength if the
1483 * preset value enable is not set.
1484 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001485 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301486 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1487 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1488 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001489 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1490 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301491 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1492 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001493 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1494 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1495 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001496 pr_warn("%s: invalid driver type, default to driver type B\n",
1497 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001498 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1499 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301500
1501 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301502 } else {
1503 /*
1504 * According to SDHC Spec v3.00, if the Preset Value
1505 * Enable in the Host Control 2 register is set, we
1506 * need to reset SD Clock Enable before changing High
1507 * Speed Enable to avoid generating clock gliches.
1508 */
Arindam Nath758535c2011-05-05 12:19:00 +05301509
1510 /* Reset SD Clock Enable */
1511 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1512 clk &= ~SDHCI_CLOCK_CARD_EN;
1513 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1514
1515 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1516
1517 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001518 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301519 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301520
Arindam Nath49c468f2011-05-05 12:19:01 +05301521 /* Reset SD Clock Enable */
1522 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1523 clk &= ~SDHCI_CLOCK_CARD_EN;
1524 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1525
Russell King96d7b782014-04-25 12:59:26 +01001526 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001527 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301528
Kevin Liu52983382013-01-31 11:31:37 +08001529 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1530 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1531 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1532 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1533 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001534 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1535 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001536 u16 preset;
1537
1538 sdhci_enable_preset_value(host, true);
1539 preset = sdhci_get_preset_value(host);
1540 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1541 >> SDHCI_PRESET_DRV_SHIFT;
1542 }
1543
Arindam Nath49c468f2011-05-05 12:19:01 +05301544 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001545 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301546 } else
1547 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301548
Leandro Dorileob8352262007-07-25 23:47:04 +02001549 /*
1550 * Some (ENE) controllers go apeshit on some ios operation,
1551 * signalling timeout and CRC errors even on CMD0. Resetting
1552 * it on each ios seems to solve the problem.
1553 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301554 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001555 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001556
Pierre Ossman5f25a662006-10-04 02:15:39 -07001557 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001558 spin_unlock_irqrestore(&host->lock, flags);
1559}
1560
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001561static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1562{
1563 struct sdhci_host *host = mmc_priv(mmc);
1564
1565 sdhci_runtime_pm_get(host);
1566 sdhci_do_set_ios(host, ios);
1567 sdhci_runtime_pm_put(host);
1568}
1569
Kevin Liu94144a42013-02-28 17:35:53 +08001570static int sdhci_do_get_cd(struct sdhci_host *host)
1571{
1572 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1573
1574 if (host->flags & SDHCI_DEVICE_DEAD)
1575 return 0;
1576
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001577 /* If nonremovable, assume that the card is always present. */
1578 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
Kevin Liu94144a42013-02-28 17:35:53 +08001579 return 1;
1580
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001581 /*
1582 * Try slot gpio detect, if defined it take precedence
1583 * over build in controller functionality
1584 */
Kevin Liu94144a42013-02-28 17:35:53 +08001585 if (!IS_ERR_VALUE(gpio_cd))
1586 return !!gpio_cd;
1587
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001588 /* If polling, assume that the card is always present. */
1589 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1590 return 1;
1591
Kevin Liu94144a42013-02-28 17:35:53 +08001592 /* Host native card detect */
1593 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1594}
1595
1596static int sdhci_get_cd(struct mmc_host *mmc)
1597{
1598 struct sdhci_host *host = mmc_priv(mmc);
1599 int ret;
1600
1601 sdhci_runtime_pm_get(host);
1602 ret = sdhci_do_get_cd(host);
1603 sdhci_runtime_pm_put(host);
1604 return ret;
1605}
1606
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001607static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001608{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001609 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001610 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001611
Pierre Ossmand129bce2006-03-24 03:18:17 -08001612 spin_lock_irqsave(&host->lock, flags);
1613
Pierre Ossman1e728592008-04-16 19:13:13 +02001614 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001615 is_readonly = 0;
1616 else if (host->ops->get_ro)
1617 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001618 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001619 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1620 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001621
1622 spin_unlock_irqrestore(&host->lock, flags);
1623
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001624 /* This quirk needs to be replaced by a callback-function later */
1625 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1626 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001627}
1628
Takashi Iwai82b0e232011-04-21 20:26:38 +02001629#define SAMPLE_COUNT 5
1630
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001631static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001632{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001633 int i, ro_count;
1634
Takashi Iwai82b0e232011-04-21 20:26:38 +02001635 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001636 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001637
1638 ro_count = 0;
1639 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001640 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001641 if (++ro_count > SAMPLE_COUNT / 2)
1642 return 1;
1643 }
1644 msleep(30);
1645 }
1646 return 0;
1647}
1648
Adrian Hunter20758b62011-08-29 16:42:12 +03001649static void sdhci_hw_reset(struct mmc_host *mmc)
1650{
1651 struct sdhci_host *host = mmc_priv(mmc);
1652
1653 if (host->ops && host->ops->hw_reset)
1654 host->ops->hw_reset(host);
1655}
1656
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001657static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001658{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001659 struct sdhci_host *host = mmc_priv(mmc);
1660 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001661
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001662 sdhci_runtime_pm_get(host);
1663 ret = sdhci_do_get_ro(host);
1664 sdhci_runtime_pm_put(host);
1665 return ret;
1666}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001667
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001668static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1669{
Russell Kingbe138552014-04-25 12:55:56 +01001670 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001671 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001672 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001673 else
Russell Kingb537f942014-04-25 12:56:01 +01001674 host->ier &= ~SDHCI_INT_CARD_INT;
1675
1676 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1677 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001678 mmiowb();
1679 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001680}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001681
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001682static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1683{
1684 struct sdhci_host *host = mmc_priv(mmc);
1685 unsigned long flags;
1686
Russell Kingef104332014-04-25 12:55:41 +01001687 sdhci_runtime_pm_get(host);
1688
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001689 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001690 if (enable)
1691 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1692 else
1693 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1694
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001695 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001696 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001697
1698 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001699}
1700
Philip Rakity6231f3d2012-07-23 15:56:23 -07001701static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001702 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001703{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001704 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001705 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001706 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001707
1708 /*
1709 * Signal Voltage Switching is only applicable for Host Controllers
1710 * v3.00 and above.
1711 */
1712 if (host->version < SDHCI_SPEC_300)
1713 return 0;
1714
Philip Rakity6231f3d2012-07-23 15:56:23 -07001715 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001716
Fabio Estevam21f59982013-02-14 10:35:03 -02001717 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001718 case MMC_SIGNAL_VOLTAGE_330:
1719 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1720 ctrl &= ~SDHCI_CTRL_VDD_180;
1721 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1722
Tim Kryger3a48edc2014-06-13 10:13:56 -07001723 if (!IS_ERR(mmc->supply.vqmmc)) {
1724 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1725 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001726 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001727 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1728 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001729 return -EIO;
1730 }
1731 }
1732 /* Wait for 5ms */
1733 usleep_range(5000, 5500);
1734
1735 /* 3.3V regulator output should be stable within 5 ms */
1736 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1737 if (!(ctrl & SDHCI_CTRL_VDD_180))
1738 return 0;
1739
Joe Perches66061102014-09-12 14:56:56 -07001740 pr_warn("%s: 3.3V regulator output did not became stable\n",
1741 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001742
1743 return -EAGAIN;
1744 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001745 if (!IS_ERR(mmc->supply.vqmmc)) {
1746 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001747 1700000, 1950000);
1748 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001749 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1750 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001751 return -EIO;
1752 }
1753 }
1754
1755 /*
1756 * Enable 1.8V Signal Enable in the Host Control2
1757 * register
1758 */
1759 ctrl |= SDHCI_CTRL_VDD_180;
1760 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1761
Vincent Yang9d967a62015-01-20 16:05:15 +08001762 /* Some controller need to do more when switching */
1763 if (host->ops->voltage_switch)
1764 host->ops->voltage_switch(host);
1765
Kevin Liu20b92a32012-12-17 19:29:26 +08001766 /* 1.8V regulator output should be stable within 5 ms */
1767 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1768 if (ctrl & SDHCI_CTRL_VDD_180)
1769 return 0;
1770
Joe Perches66061102014-09-12 14:56:56 -07001771 pr_warn("%s: 1.8V regulator output did not became stable\n",
1772 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001773
1774 return -EAGAIN;
1775 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001776 if (!IS_ERR(mmc->supply.vqmmc)) {
1777 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1778 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001779 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001780 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1781 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001782 return -EIO;
1783 }
1784 }
1785 return 0;
1786 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301787 /* No signal voltage switch required */
1788 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001789 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301790}
1791
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001792static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001793 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001794{
1795 struct sdhci_host *host = mmc_priv(mmc);
1796 int err;
1797
1798 if (host->version < SDHCI_SPEC_300)
1799 return 0;
1800 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001801 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001802 sdhci_runtime_pm_put(host);
1803 return err;
1804}
1805
Kevin Liu20b92a32012-12-17 19:29:26 +08001806static int sdhci_card_busy(struct mmc_host *mmc)
1807{
1808 struct sdhci_host *host = mmc_priv(mmc);
1809 u32 present_state;
1810
1811 sdhci_runtime_pm_get(host);
1812 /* Check whether DAT[3:0] is 0000 */
1813 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1814 sdhci_runtime_pm_put(host);
1815
1816 return !(present_state & SDHCI_DATA_LVL_MASK);
1817}
1818
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001819static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1820{
1821 struct sdhci_host *host = mmc_priv(mmc);
1822 unsigned long flags;
1823
1824 spin_lock_irqsave(&host->lock, flags);
1825 host->flags |= SDHCI_HS400_TUNING;
1826 spin_unlock_irqrestore(&host->lock, flags);
1827
1828 return 0;
1829}
1830
Girish K S069c9f12012-01-06 09:56:39 +05301831static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301832{
Russell King4b6f37d2014-04-25 12:59:36 +01001833 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301834 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301835 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301836 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001837 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001838 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001839 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301840
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001841 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001842 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301843
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001844 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1845 host->flags &= ~SDHCI_HS400_TUNING;
1846
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001847 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1848 tuning_count = host->tuning_count;
1849
Arindam Nathb513ea22011-05-05 12:19:04 +05301850 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001851 * The Host Controller needs tuning in case of SDR104 and DDR50
1852 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1853 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301854 * If the Host Controller supports the HS200 mode then the
1855 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301856 */
Russell King4b6f37d2014-04-25 12:59:36 +01001857 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001858 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001859 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001860 err = -EINVAL;
1861 goto out_unlock;
1862
Russell King4b6f37d2014-04-25 12:59:36 +01001863 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001864 /*
1865 * Periodic re-tuning for HS400 is not expected to be needed, so
1866 * disable it here.
1867 */
1868 if (hs400_tuning)
1869 tuning_count = 0;
1870 break;
1871
Russell King4b6f37d2014-04-25 12:59:36 +01001872 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001873 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001874 break;
Girish K S069c9f12012-01-06 09:56:39 +05301875
Russell King4b6f37d2014-04-25 12:59:36 +01001876 case MMC_TIMING_UHS_SDR50:
1877 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1878 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1879 break;
1880 /* FALLTHROUGH */
1881
1882 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001883 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301884 }
1885
Dong Aisheng45251812013-09-13 19:11:30 +08001886 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001887 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001888 err = host->ops->platform_execute_tuning(host, opcode);
1889 sdhci_runtime_pm_put(host);
1890 return err;
1891 }
1892
Russell King4b6f37d2014-04-25 12:59:36 +01001893 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1894 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001895 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1896 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301897 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1898
1899 /*
1900 * As per the Host Controller spec v3.00, tuning command
1901 * generates Buffer Read Ready interrupt, so enable that.
1902 *
1903 * Note: The spec clearly says that when tuning sequence
1904 * is being performed, the controller does not generate
1905 * interrupts other than Buffer Read Ready interrupt. But
1906 * to make sure we don't hit a controller bug, we _only_
1907 * enable Buffer Read Ready interrupt here.
1908 */
Russell Kingb537f942014-04-25 12:56:01 +01001909 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1910 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301911
1912 /*
1913 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1914 * of loops reaches 40 times or a timeout of 150ms occurs.
1915 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301916 do {
1917 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001918 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301919
Girish K S069c9f12012-01-06 09:56:39 +05301920 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301921 cmd.arg = 0;
1922 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1923 cmd.retries = 0;
1924 cmd.data = NULL;
1925 cmd.error = 0;
1926
Al Cooper7ce45e92014-05-09 11:34:07 -04001927 if (tuning_loop_counter-- == 0)
1928 break;
1929
Arindam Nathb513ea22011-05-05 12:19:04 +05301930 mrq.cmd = &cmd;
1931 host->mrq = &mrq;
1932
1933 /*
1934 * In response to CMD19, the card sends 64 bytes of tuning
1935 * block to the Host Controller. So we set the block size
1936 * to 64 here.
1937 */
Girish K S069c9f12012-01-06 09:56:39 +05301938 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1939 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1940 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1941 SDHCI_BLOCK_SIZE);
1942 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1943 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1944 SDHCI_BLOCK_SIZE);
1945 } else {
1946 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1947 SDHCI_BLOCK_SIZE);
1948 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301949
1950 /*
1951 * The tuning block is sent by the card to the host controller.
1952 * So we set the TRNS_READ bit in the Transfer Mode register.
1953 * This also takes care of setting DMA Enable and Multi Block
1954 * Select in the same register to 0.
1955 */
1956 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1957
1958 sdhci_send_command(host, &cmd);
1959
1960 host->cmd = NULL;
1961 host->mrq = NULL;
1962
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001963 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301964 /* Wait for Buffer Read Ready interrupt */
1965 wait_event_interruptible_timeout(host->buf_ready_int,
1966 (host->tuning_done == 1),
1967 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001968 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301969
1970 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001971 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 +05301972 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1973 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1974 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1975 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1976
1977 err = -EIO;
1978 goto out;
1979 }
1980
1981 host->tuning_done = 0;
1982
1983 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07001984
1985 /* eMMC spec does not require a delay between tuning cycles */
1986 if (opcode == MMC_SEND_TUNING_BLOCK)
1987 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05301988 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1989
1990 /*
1991 * The Host Driver has exhausted the maximum number of loops allowed,
1992 * so use fixed sampling frequency.
1993 */
Al Cooper7ce45e92014-05-09 11:34:07 -04001994 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05301995 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1996 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04001997 }
1998 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001999 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002000 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302001 }
2002
2003out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002004 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002005 /*
2006 * In case tuning fails, host controllers which support
2007 * re-tuning can try tuning again at a later time, when the
2008 * re-tuning timer expires. So for these controllers, we
2009 * return 0. Since there might be other controllers who do not
2010 * have this capability, we return error for them.
2011 */
2012 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302013 }
2014
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002015 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302016
Russell Kingb537f942014-04-25 12:56:01 +01002017 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2018 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002019out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002020 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002021 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302022
2023 return err;
2024}
2025
Adrian Huntercb849642015-02-06 14:12:59 +02002026static int sdhci_select_drive_strength(struct mmc_card *card,
2027 unsigned int max_dtr, int host_drv,
2028 int card_drv, int *drv_type)
2029{
2030 struct sdhci_host *host = mmc_priv(card->host);
2031
2032 if (!host->ops->select_drive_strength)
2033 return 0;
2034
2035 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2036 card_drv, drv_type);
2037}
Kevin Liu52983382013-01-31 11:31:37 +08002038
2039static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302040{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302041 /* Host Controller v3.00 defines preset value registers */
2042 if (host->version < SDHCI_SPEC_300)
2043 return;
2044
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302045 /*
2046 * We only enable or disable Preset Value if they are not already
2047 * enabled or disabled respectively. Otherwise, we bail out.
2048 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002049 if (host->preset_enabled != enable) {
2050 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2051
2052 if (enable)
2053 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2054 else
2055 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2056
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302057 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002058
2059 if (enable)
2060 host->flags |= SDHCI_PV_ENABLED;
2061 else
2062 host->flags &= ~SDHCI_PV_ENABLED;
2063
2064 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302065 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002066}
2067
Haibo Chen348487c2014-12-09 17:04:05 +08002068static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2069 int err)
2070{
2071 struct sdhci_host *host = mmc_priv(mmc);
2072 struct mmc_data *data = mrq->data;
2073
Russell King771a3dc2016-01-26 13:40:53 +00002074 if (data->host_cookie == COOKIE_GIVEN ||
2075 data->host_cookie == COOKIE_MAPPED)
2076 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2077 data->flags & MMC_DATA_WRITE ?
2078 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2079
2080 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002081}
2082
2083static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Haibo Chend31911b2015-08-25 10:02:11 +08002084 struct mmc_data *data)
Haibo Chen348487c2014-12-09 17:04:05 +08002085{
2086 int sg_count;
2087
Haibo Chend31911b2015-08-25 10:02:11 +08002088 if (data->host_cookie == COOKIE_MAPPED) {
2089 data->host_cookie = COOKIE_GIVEN;
2090 return data->sg_count;
Haibo Chen348487c2014-12-09 17:04:05 +08002091 }
2092
Haibo Chend31911b2015-08-25 10:02:11 +08002093 WARN_ON(data->host_cookie == COOKIE_GIVEN);
Haibo Chen348487c2014-12-09 17:04:05 +08002094
Haibo Chend31911b2015-08-25 10:02:11 +08002095 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2096 data->flags & MMC_DATA_WRITE ?
2097 DMA_TO_DEVICE : DMA_FROM_DEVICE);
Haibo Chen348487c2014-12-09 17:04:05 +08002098
2099 if (sg_count == 0)
Haibo Chend31911b2015-08-25 10:02:11 +08002100 return -ENOSPC;
Haibo Chen348487c2014-12-09 17:04:05 +08002101
Haibo Chend31911b2015-08-25 10:02:11 +08002102 data->sg_count = sg_count;
2103 data->host_cookie = COOKIE_MAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002104
2105 return sg_count;
2106}
2107
2108static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2109 bool is_first_req)
2110{
2111 struct sdhci_host *host = mmc_priv(mmc);
2112
Haibo Chend31911b2015-08-25 10:02:11 +08002113 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002114
2115 if (host->flags & SDHCI_REQ_USE_DMA)
Haibo Chend31911b2015-08-25 10:02:11 +08002116 sdhci_pre_dma_transfer(host, mrq->data);
Haibo Chen348487c2014-12-09 17:04:05 +08002117}
2118
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002119static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002120{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002121 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002122 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002123 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002124
Christian Daudt722e1282013-06-20 14:26:36 -07002125 /* First check if client has provided their own card event */
2126 if (host->ops->card_event)
2127 host->ops->card_event(host);
2128
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002129 present = sdhci_do_get_cd(host);
2130
Pierre Ossmand129bce2006-03-24 03:18:17 -08002131 spin_lock_irqsave(&host->lock, flags);
2132
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002133 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002134 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302135 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002136 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302137 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002138 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002139
Russell King03231f92014-04-25 12:57:12 +01002140 sdhci_do_reset(host, SDHCI_RESET_CMD);
2141 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002142
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002143 host->mrq->cmd->error = -ENOMEDIUM;
2144 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002145 }
2146
2147 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002148}
2149
2150static const struct mmc_host_ops sdhci_ops = {
2151 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002152 .post_req = sdhci_post_req,
2153 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002154 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002155 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002156 .get_ro = sdhci_get_ro,
2157 .hw_reset = sdhci_hw_reset,
2158 .enable_sdio_irq = sdhci_enable_sdio_irq,
2159 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002160 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002161 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002162 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002163 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002164 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002165};
2166
2167/*****************************************************************************\
2168 * *
2169 * Tasklets *
2170 * *
2171\*****************************************************************************/
2172
Pierre Ossmand129bce2006-03-24 03:18:17 -08002173static void sdhci_tasklet_finish(unsigned long param)
2174{
2175 struct sdhci_host *host;
2176 unsigned long flags;
2177 struct mmc_request *mrq;
2178
2179 host = (struct sdhci_host*)param;
2180
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002181 spin_lock_irqsave(&host->lock, flags);
2182
Chris Ball0c9c99a2011-04-27 17:35:31 -04002183 /*
2184 * If this tasklet gets rescheduled while running, it will
2185 * be run again afterwards but without any active request.
2186 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002187 if (!host->mrq) {
2188 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002189 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002190 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002191
2192 del_timer(&host->timer);
2193
2194 mrq = host->mrq;
2195
Pierre Ossmand129bce2006-03-24 03:18:17 -08002196 /*
Russell King054cedf2016-01-26 13:40:42 +00002197 * Always unmap the data buffers if they were mapped by
2198 * sdhci_prepare_data() whenever we finish with a request.
2199 * This avoids leaking DMA mappings on error.
2200 */
2201 if (host->flags & SDHCI_REQ_USE_DMA) {
2202 struct mmc_data *data = mrq->data;
2203
2204 if (data && data->host_cookie == COOKIE_MAPPED) {
2205 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2206 (data->flags & MMC_DATA_READ) ?
2207 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2208 data->host_cookie = COOKIE_UNMAPPED;
2209 }
2210 }
2211
2212 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002213 * The controller needs a reset of internal state machines
2214 * upon error conditions.
2215 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002216 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002217 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002218 (mrq->sbc && mrq->sbc->error) ||
2219 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2220 (mrq->data->stop && mrq->data->stop->error))) ||
2221 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002222
2223 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002224 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002225 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002226 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002227
2228 /* Spec says we should do both at the same time, but Ricoh
2229 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002230 sdhci_do_reset(host, SDHCI_RESET_CMD);
2231 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002232 }
2233
2234 host->mrq = NULL;
2235 host->cmd = NULL;
2236 host->data = NULL;
2237
Pierre Ossmanf9134312008-12-21 17:01:48 +01002238#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002239 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002240#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002241
Pierre Ossman5f25a662006-10-04 02:15:39 -07002242 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002243 spin_unlock_irqrestore(&host->lock, flags);
2244
2245 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002246 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002247}
2248
2249static void sdhci_timeout_timer(unsigned long data)
2250{
2251 struct sdhci_host *host;
2252 unsigned long flags;
2253
2254 host = (struct sdhci_host*)data;
2255
2256 spin_lock_irqsave(&host->lock, flags);
2257
2258 if (host->mrq) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002259 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2260 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002261 sdhci_dumpregs(host);
2262
2263 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002264 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002265 sdhci_finish_data(host);
2266 } else {
2267 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002268 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002269 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002270 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002271
2272 tasklet_schedule(&host->finish_tasklet);
2273 }
2274 }
2275
Pierre Ossman5f25a662006-10-04 02:15:39 -07002276 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002277 spin_unlock_irqrestore(&host->lock, flags);
2278}
2279
2280/*****************************************************************************\
2281 * *
2282 * Interrupt handling *
2283 * *
2284\*****************************************************************************/
2285
Adrian Hunter61541392014-09-24 10:27:27 +03002286static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002287{
2288 BUG_ON(intmask == 0);
2289
2290 if (!host->cmd) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002291 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2292 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002293 sdhci_dumpregs(host);
2294 return;
2295 }
2296
Russell Kingec014cb2016-01-26 13:39:39 +00002297 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2298 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2299 if (intmask & SDHCI_INT_TIMEOUT)
2300 host->cmd->error = -ETIMEDOUT;
2301 else
2302 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002303
Russell King71fcbda2016-01-26 13:39:45 +00002304 /*
2305 * If this command initiates a data phase and a response
2306 * CRC error is signalled, the card can start transferring
2307 * data - the card may have received the command without
2308 * error. We must not terminate the mmc_request early.
2309 *
2310 * If the card did not receive the command or returned an
2311 * error which prevented it sending data, the data phase
2312 * will time out.
2313 */
2314 if (host->cmd->data &&
2315 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2316 SDHCI_INT_CRC) {
2317 host->cmd = NULL;
2318 return;
2319 }
2320
Pierre Ossmand129bce2006-03-24 03:18:17 -08002321 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002322 return;
2323 }
2324
2325 /*
2326 * The host can send and interrupt when the busy state has
2327 * ended, allowing us to wait without wasting CPU cycles.
2328 * Unfortunately this is overloaded on the "data complete"
2329 * interrupt, so we need to take some care when handling
2330 * it.
2331 *
2332 * Note: The 1.0 specification is a bit ambiguous about this
2333 * feature so there might be some problems with older
2334 * controllers.
2335 */
2336 if (host->cmd->flags & MMC_RSP_BUSY) {
2337 if (host->cmd->data)
Marek Vasut2e4456f2015-11-18 10:47:02 +01002338 DBG("Cannot wait for busy signal when also doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002339 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2340 && !host->busy_handle) {
2341 /* Mark that command complete before busy is ended */
2342 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002343 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002344 }
Ben Dooksf9454052009-02-20 20:33:08 +03002345
2346 /* The controller does not support the end-of-busy IRQ,
2347 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002348 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2349 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2350 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002351 }
2352
2353 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002354 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002355}
2356
George G. Davis0957c332010-02-18 12:32:12 -05002357#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002358static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002359{
2360 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002361 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002362
2363 sdhci_dumpregs(host);
2364
2365 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002366 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002367
Adrian Huntere57a5f62014-11-04 12:42:46 +02002368 if (host->flags & SDHCI_USE_64_BIT_DMA)
2369 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2370 name, desc, le32_to_cpu(dma_desc->addr_hi),
2371 le32_to_cpu(dma_desc->addr_lo),
2372 le16_to_cpu(dma_desc->len),
2373 le16_to_cpu(dma_desc->cmd));
2374 else
2375 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2376 name, desc, le32_to_cpu(dma_desc->addr_lo),
2377 le16_to_cpu(dma_desc->len),
2378 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002379
Adrian Hunter76fe3792014-11-04 12:42:42 +02002380 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002381
Adrian Hunter05452302014-11-04 12:42:45 +02002382 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002383 break;
2384 }
2385}
2386#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002387static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002388#endif
2389
Pierre Ossmand129bce2006-03-24 03:18:17 -08002390static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2391{
Girish K S069c9f12012-01-06 09:56:39 +05302392 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002393 BUG_ON(intmask == 0);
2394
Arindam Nathb513ea22011-05-05 12:19:04 +05302395 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2396 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302397 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2398 if (command == MMC_SEND_TUNING_BLOCK ||
2399 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302400 host->tuning_done = 1;
2401 wake_up(&host->buf_ready_int);
2402 return;
2403 }
2404 }
2405
Pierre Ossmand129bce2006-03-24 03:18:17 -08002406 if (!host->data) {
2407 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002408 * The "data complete" interrupt is also used to
2409 * indicate that a busy state has ended. See comment
2410 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002411 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002412 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002413 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2414 host->cmd->error = -ETIMEDOUT;
2415 tasklet_schedule(&host->finish_tasklet);
2416 return;
2417 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002418 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002419 /*
2420 * Some cards handle busy-end interrupt
2421 * before the command completed, so make
2422 * sure we do things in the proper order.
2423 */
2424 if (host->busy_handle)
2425 sdhci_finish_command(host);
2426 else
2427 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002428 return;
2429 }
2430 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002431
Marek Vasut2e4456f2015-11-18 10:47:02 +01002432 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2433 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002434 sdhci_dumpregs(host);
2435
2436 return;
2437 }
2438
2439 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002440 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002441 else if (intmask & SDHCI_INT_DATA_END_BIT)
2442 host->data->error = -EILSEQ;
2443 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2444 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2445 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002446 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002447 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302448 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002449 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002450 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002451 if (host->ops->adma_workaround)
2452 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002453 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002454
Pierre Ossman17b04292007-07-22 22:18:46 +02002455 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002456 sdhci_finish_data(host);
2457 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002458 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002459 sdhci_transfer_pio(host);
2460
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002461 /*
2462 * We currently don't do anything fancy with DMA
2463 * boundaries, but as we can't disable the feature
2464 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002465 *
2466 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2467 * should return a valid address to continue from, but as
2468 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002469 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002470 if (intmask & SDHCI_INT_DMA_END) {
2471 u32 dmastart, dmanow;
2472 dmastart = sg_dma_address(host->data->sg);
2473 dmanow = dmastart + host->data->bytes_xfered;
2474 /*
2475 * Force update to the next DMA block boundary.
2476 */
2477 dmanow = (dmanow &
2478 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2479 SDHCI_DEFAULT_BOUNDARY_SIZE;
2480 host->data->bytes_xfered = dmanow - dmastart;
2481 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2482 " next 0x%08x\n",
2483 mmc_hostname(host->mmc), dmastart,
2484 host->data->bytes_xfered, dmanow);
2485 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2486 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002487
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002488 if (intmask & SDHCI_INT_DATA_END) {
2489 if (host->cmd) {
2490 /*
2491 * Data managed to finish before the
2492 * command completed. Make sure we do
2493 * things in the proper order.
2494 */
2495 host->data_early = 1;
2496 } else {
2497 sdhci_finish_data(host);
2498 }
2499 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002500 }
2501}
2502
David Howells7d12e782006-10-05 14:55:46 +01002503static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002504{
Russell King781e9892014-04-25 12:55:46 +01002505 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002506 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002507 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002508 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002509
2510 spin_lock(&host->lock);
2511
Russell Kingbe138552014-04-25 12:55:56 +01002512 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002513 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002514 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002515 }
2516
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002517 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002518 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002519 result = IRQ_NONE;
2520 goto out;
2521 }
2522
Russell King41005002014-04-25 12:55:36 +01002523 do {
2524 /* Clear selected interrupts. */
2525 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2526 SDHCI_INT_BUS_POWER);
2527 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002528
Russell King41005002014-04-25 12:55:36 +01002529 DBG("*** %s got interrupt: 0x%08x\n",
2530 mmc_hostname(host->mmc), intmask);
2531
2532 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2533 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2534 SDHCI_CARD_PRESENT;
2535
2536 /*
2537 * There is a observation on i.mx esdhc. INSERT
2538 * bit will be immediately set again when it gets
2539 * cleared, if a card is inserted. We have to mask
2540 * the irq to prevent interrupt storm which will
2541 * freeze the system. And the REMOVE gets the
2542 * same situation.
2543 *
2544 * More testing are needed here to ensure it works
2545 * for other platforms though.
2546 */
Russell Kingb537f942014-04-25 12:56:01 +01002547 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2548 SDHCI_INT_CARD_REMOVE);
2549 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2550 SDHCI_INT_CARD_INSERT;
2551 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2552 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002553
2554 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2555 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002556
2557 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2558 SDHCI_INT_CARD_REMOVE);
2559 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002560 }
2561
2562 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002563 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2564 &intmask);
Russell King41005002014-04-25 12:55:36 +01002565
2566 if (intmask & SDHCI_INT_DATA_MASK)
2567 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2568
2569 if (intmask & SDHCI_INT_BUS_POWER)
2570 pr_err("%s: Card is consuming too much power!\n",
2571 mmc_hostname(host->mmc));
2572
Russell King781e9892014-04-25 12:55:46 +01002573 if (intmask & SDHCI_INT_CARD_INT) {
2574 sdhci_enable_sdio_irq_nolock(host, false);
2575 host->thread_isr |= SDHCI_INT_CARD_INT;
2576 result = IRQ_WAKE_THREAD;
2577 }
Russell King41005002014-04-25 12:55:36 +01002578
2579 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2580 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2581 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2582 SDHCI_INT_CARD_INT);
2583
2584 if (intmask) {
2585 unexpected |= intmask;
2586 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2587 }
2588
Russell King781e9892014-04-25 12:55:46 +01002589 if (result == IRQ_NONE)
2590 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002591
2592 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002593 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002594out:
2595 spin_unlock(&host->lock);
2596
Alexander Stein6379b232012-03-14 09:52:10 +01002597 if (unexpected) {
2598 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2599 mmc_hostname(host->mmc), unexpected);
2600 sdhci_dumpregs(host);
2601 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002602
Pierre Ossmand129bce2006-03-24 03:18:17 -08002603 return result;
2604}
2605
Russell King781e9892014-04-25 12:55:46 +01002606static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2607{
2608 struct sdhci_host *host = dev_id;
2609 unsigned long flags;
2610 u32 isr;
2611
2612 spin_lock_irqsave(&host->lock, flags);
2613 isr = host->thread_isr;
2614 host->thread_isr = 0;
2615 spin_unlock_irqrestore(&host->lock, flags);
2616
Russell King3560db82014-04-25 12:55:51 +01002617 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2618 sdhci_card_event(host->mmc);
2619 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2620 }
2621
Russell King781e9892014-04-25 12:55:46 +01002622 if (isr & SDHCI_INT_CARD_INT) {
2623 sdio_run_irqs(host->mmc);
2624
2625 spin_lock_irqsave(&host->lock, flags);
2626 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2627 sdhci_enable_sdio_irq_nolock(host, true);
2628 spin_unlock_irqrestore(&host->lock, flags);
2629 }
2630
2631 return isr ? IRQ_HANDLED : IRQ_NONE;
2632}
2633
Pierre Ossmand129bce2006-03-24 03:18:17 -08002634/*****************************************************************************\
2635 * *
2636 * Suspend/resume *
2637 * *
2638\*****************************************************************************/
2639
2640#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002641void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2642{
2643 u8 val;
2644 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2645 | SDHCI_WAKE_ON_INT;
2646
2647 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2648 val |= mask ;
2649 /* Avoid fake wake up */
2650 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2651 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2652 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2653}
2654EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2655
Fabio Estevam0b10f472014-08-30 14:53:13 -03002656static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002657{
2658 u8 val;
2659 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2660 | SDHCI_WAKE_ON_INT;
2661
2662 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2663 val &= ~mask;
2664 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2665}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002666
Manuel Lauss29495aa2011-11-03 11:09:45 +01002667int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002668{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002669 sdhci_disable_card_detection(host);
2670
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002671 mmc_retune_timer_stop(host->mmc);
2672 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302673
Kevin Liuad080d72013-01-05 17:21:33 +08002674 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002675 host->ier = 0;
2676 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2677 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002678 free_irq(host->irq, host);
2679 } else {
2680 sdhci_enable_irq_wakeups(host);
2681 enable_irq_wake(host->irq);
2682 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002683 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002684}
2685
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002686EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002687
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002688int sdhci_resume_host(struct sdhci_host *host)
2689{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002690 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002691
Richard Röjforsa13abc72009-09-22 16:45:30 -07002692 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002693 if (host->ops->enable_dma)
2694 host->ops->enable_dma(host);
2695 }
2696
Adrian Hunter6308d292012-02-07 14:48:54 +02002697 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2698 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2699 /* Card keeps power but host controller does not */
2700 sdhci_init(host, 0);
2701 host->pwr = 0;
2702 host->clock = 0;
2703 sdhci_do_set_ios(host, &host->mmc->ios);
2704 } else {
2705 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2706 mmiowb();
2707 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002708
Haibo Chen14a7b41642015-09-15 18:32:58 +08002709 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2710 ret = request_threaded_irq(host->irq, sdhci_irq,
2711 sdhci_thread_irq, IRQF_SHARED,
2712 mmc_hostname(host->mmc), host);
2713 if (ret)
2714 return ret;
2715 } else {
2716 sdhci_disable_irq_wakeups(host);
2717 disable_irq_wake(host->irq);
2718 }
2719
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002720 sdhci_enable_card_detection(host);
2721
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002722 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002723}
2724
2725EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002726
2727static int sdhci_runtime_pm_get(struct sdhci_host *host)
2728{
2729 return pm_runtime_get_sync(host->mmc->parent);
2730}
2731
2732static int sdhci_runtime_pm_put(struct sdhci_host *host)
2733{
2734 pm_runtime_mark_last_busy(host->mmc->parent);
2735 return pm_runtime_put_autosuspend(host->mmc->parent);
2736}
2737
Adrian Hunterf0710a52013-05-06 12:17:32 +03002738static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2739{
Adrian Hunter5c671c42015-11-26 14:00:50 +02002740 if (host->bus_on)
Adrian Hunterf0710a52013-05-06 12:17:32 +03002741 return;
2742 host->bus_on = true;
2743 pm_runtime_get_noresume(host->mmc->parent);
2744}
2745
2746static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2747{
Adrian Hunter5c671c42015-11-26 14:00:50 +02002748 if (!host->bus_on)
Adrian Hunterf0710a52013-05-06 12:17:32 +03002749 return;
2750 host->bus_on = false;
2751 pm_runtime_put_noidle(host->mmc->parent);
2752}
2753
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002754int sdhci_runtime_suspend_host(struct sdhci_host *host)
2755{
2756 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002757
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002758 mmc_retune_timer_stop(host->mmc);
2759 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002760
2761 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002762 host->ier &= SDHCI_INT_CARD_INT;
2763 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2764 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002765 spin_unlock_irqrestore(&host->lock, flags);
2766
Russell King781e9892014-04-25 12:55:46 +01002767 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002768
2769 spin_lock_irqsave(&host->lock, flags);
2770 host->runtime_suspended = true;
2771 spin_unlock_irqrestore(&host->lock, flags);
2772
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002773 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002774}
2775EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2776
2777int sdhci_runtime_resume_host(struct sdhci_host *host)
2778{
2779 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002780 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002781
2782 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2783 if (host->ops->enable_dma)
2784 host->ops->enable_dma(host);
2785 }
2786
2787 sdhci_init(host, 0);
2788
2789 /* Force clock and power re-program */
2790 host->pwr = 0;
2791 host->clock = 0;
Jisheng Zhang3396e732015-01-29 17:42:12 +08002792 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002793 sdhci_do_set_ios(host, &host->mmc->ios);
2794
Kevin Liu52983382013-01-31 11:31:37 +08002795 if ((host_flags & SDHCI_PV_ENABLED) &&
2796 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2797 spin_lock_irqsave(&host->lock, flags);
2798 sdhci_enable_preset_value(host, true);
2799 spin_unlock_irqrestore(&host->lock, flags);
2800 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002801
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002802 spin_lock_irqsave(&host->lock, flags);
2803
2804 host->runtime_suspended = false;
2805
2806 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002807 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002808 sdhci_enable_sdio_irq_nolock(host, true);
2809
2810 /* Enable Card Detection */
2811 sdhci_enable_card_detection(host);
2812
2813 spin_unlock_irqrestore(&host->lock, flags);
2814
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002815 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002816}
2817EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2818
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002819#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002820
Pierre Ossmand129bce2006-03-24 03:18:17 -08002821/*****************************************************************************\
2822 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002823 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002824 * *
2825\*****************************************************************************/
2826
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002827struct sdhci_host *sdhci_alloc_host(struct device *dev,
2828 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002829{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002830 struct mmc_host *mmc;
2831 struct sdhci_host *host;
2832
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002833 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002834
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002835 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002836 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002837 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002838
2839 host = mmc_priv(mmc);
2840 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002841 host->mmc_host_ops = sdhci_ops;
2842 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002843
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002844 return host;
2845}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002846
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002847EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002848
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002849int sdhci_add_host(struct sdhci_host *host)
2850{
2851 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002852 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302853 u32 max_current_caps;
2854 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002855 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08002856 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002857 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002858
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002859 WARN_ON(host == NULL);
2860 if (host == NULL)
2861 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002862
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002863 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002864
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002865 if (debug_quirks)
2866 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002867 if (debug_quirks2)
2868 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002869
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002870 override_timeout_clk = host->timeout_clk;
2871
Russell King03231f92014-04-25 12:57:12 +01002872 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002873
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002874 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002875 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2876 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002877 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002878 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
2879 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002880 }
2881
Arindam Nathf2119df2011-05-05 12:18:57 +05302882 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002883 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002884
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002885 if (host->version >= SDHCI_SPEC_300)
2886 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2887 host->caps1 :
2888 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302889
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002890 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002891 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302892 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002893 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002894 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002895 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002896
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002897 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002898 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002899 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002900 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002901 }
2902
Arindam Nathf2119df2011-05-05 12:18:57 +05302903 if ((host->version >= SDHCI_SPEC_200) &&
2904 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002905 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002906
2907 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2908 (host->flags & SDHCI_USE_ADMA)) {
2909 DBG("Disabling ADMA as it is marked broken\n");
2910 host->flags &= ~SDHCI_USE_ADMA;
2911 }
2912
Adrian Huntere57a5f62014-11-04 12:42:46 +02002913 /*
2914 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2915 * and *must* do 64-bit DMA. A driver has the opportunity to change
2916 * that during the first call to ->enable_dma(). Similarly
2917 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2918 * implement.
2919 */
Al Cooper5eaa7472016-02-10 15:25:39 -05002920 if (caps[0] & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02002921 host->flags |= SDHCI_USE_64_BIT_DMA;
2922
Richard Röjforsa13abc72009-09-22 16:45:30 -07002923 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002924 if (host->ops->enable_dma) {
2925 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07002926 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002927 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002928 host->flags &=
2929 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002930 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002931 }
2932 }
2933
Adrian Huntere57a5f62014-11-04 12:42:46 +02002934 /* SDMA does not support 64-bit DMA */
2935 if (host->flags & SDHCI_USE_64_BIT_DMA)
2936 host->flags &= ~SDHCI_USE_SDMA;
2937
Pierre Ossman2134a922008-06-28 18:28:51 +02002938 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00002939 dma_addr_t dma;
2940 void *buf;
2941
Pierre Ossman2134a922008-06-28 18:28:51 +02002942 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002943 * The DMA descriptor table size is calculated as the maximum
2944 * number of segments times 2, to allow for an alignment
2945 * descriptor for each segment, plus 1 for a nop end descriptor,
2946 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002947 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02002948 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2949 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2950 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002951 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002952 } else {
2953 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2954 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002955 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002956 }
Russell Kinge66e61c2016-01-26 13:39:55 +00002957
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002958 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00002959 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
2960 host->adma_table_sz, &dma, GFP_KERNEL);
2961 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07002962 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002963 mmc_hostname(mmc));
2964 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00002965 } else if ((dma + host->align_buffer_sz) &
2966 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07002967 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2968 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002969 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00002970 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
2971 host->adma_table_sz, buf, dma);
2972 } else {
2973 host->align_buffer = buf;
2974 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00002975
Russell Kinge66e61c2016-01-26 13:39:55 +00002976 host->adma_table = buf + host->align_buffer_sz;
2977 host->adma_addr = dma + host->align_buffer_sz;
2978 }
Pierre Ossman2134a922008-06-28 18:28:51 +02002979 }
2980
Pierre Ossman76591502008-07-21 00:32:11 +02002981 /*
2982 * If we use DMA, then it's up to the caller to set the DMA
2983 * mask, but PIO does not need the hw shim so we set a new
2984 * mask here in that case.
2985 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002986 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002987 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07002988 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02002989 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002990
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002991 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302992 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002993 >> SDHCI_CLOCK_BASE_SHIFT;
2994 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302995 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002996 >> SDHCI_CLOCK_BASE_SHIFT;
2997
Pierre Ossmand129bce2006-03-24 03:18:17 -08002998 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002999 if (host->max_clk == 0 || host->quirks &
3000 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003001 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003002 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3003 mmc_hostname(mmc));
Ben Dooks4240ff02009-03-17 00:13:57 +03003004 return -ENODEV;
3005 }
3006 host->max_clk = host->ops->get_max_clock(host);
3007 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003008
3009 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303010 * In case of Host Controller v3.00, find out whether clock
3011 * multiplier is supported.
3012 */
3013 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3014 SDHCI_CLOCK_MUL_SHIFT;
3015
3016 /*
3017 * In case the value in Clock Multiplier is 0, then programmable
3018 * clock mode is not supported, otherwise the actual clock
3019 * multiplier is one more than the value of Clock Multiplier
3020 * in the Capabilities Register.
3021 */
3022 if (host->clk_mul)
3023 host->clk_mul += 1;
3024
3025 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003026 * Set host parameters.
3027 */
Dong Aisheng59241752015-07-22 20:53:07 +08003028 max_clk = host->max_clk;
3029
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003030 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003031 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303032 else if (host->version >= SDHCI_SPEC_300) {
3033 if (host->clk_mul) {
3034 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003035 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303036 } else
3037 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3038 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003039 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003040
Dong Aisheng59241752015-07-22 20:53:07 +08003041 if (!mmc->f_max || (mmc->f_max && (mmc->f_max > max_clk)))
3042 mmc->f_max = max_clk;
3043
Aisheng Dong28aab052014-08-27 15:26:31 +08003044 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3045 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3046 SDHCI_TIMEOUT_CLK_SHIFT;
3047 if (host->timeout_clk == 0) {
3048 if (host->ops->get_timeout_clock) {
3049 host->timeout_clk =
3050 host->ops->get_timeout_clock(host);
3051 } else {
3052 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3053 mmc_hostname(mmc));
3054 return -ENODEV;
3055 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003056 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003057
Aisheng Dong28aab052014-08-27 15:26:31 +08003058 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3059 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003060
Aisheng Dong28aab052014-08-27 15:26:31 +08003061 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003062 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003063 mmc->max_busy_timeout /= host->timeout_clk;
3064 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003065
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003066 if (override_timeout_clk)
3067 host->timeout_clk = override_timeout_clk;
3068
Andrei Warkentine89d4562011-05-23 15:06:37 -05003069 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003070 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003071
3072 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3073 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003074
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003075 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003076 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003077 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003078 !(host->flags & SDHCI_USE_SDMA)) &&
3079 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003080 host->flags |= SDHCI_AUTO_CMD23;
3081 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3082 } else {
3083 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3084 }
3085
Philip Rakity15ec4462010-11-19 16:48:39 -05003086 /*
3087 * A controller may support 8-bit width, but the board itself
3088 * might not have the pins brought out. Boards that support
3089 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3090 * their platform code before calling sdhci_add_host(), and we
3091 * won't assume 8-bit width for hosts without that CAP.
3092 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003093 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003094 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003095
Jerry Huang63ef5d82012-10-25 13:47:19 +08003096 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3097 mmc->caps &= ~MMC_CAP_CMD23;
3098
Arindam Nathf2119df2011-05-05 12:18:57 +05303099 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003100 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003101
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003102 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Ivan T. Ivanovc31d22e2015-07-06 15:16:20 +03003103 !(mmc->caps & MMC_CAP_NONREMOVABLE) &&
3104 IS_ERR_VALUE(mmc_gpio_get_cd(host->mmc)))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003105 mmc->caps |= MMC_CAP_NEEDS_POLL;
3106
Tim Kryger3a48edc2014-06-13 10:13:56 -07003107 /* If there are external regulators, get them */
3108 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3109 return -EPROBE_DEFER;
3110
Philip Rakity6231f3d2012-07-23 15:56:23 -07003111 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003112 if (!IS_ERR(mmc->supply.vqmmc)) {
3113 ret = regulator_enable(mmc->supply.vqmmc);
3114 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3115 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003116 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3117 SDHCI_SUPPORT_SDR50 |
3118 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003119 if (ret) {
3120 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3121 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003122 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003123 }
Kevin Liu8363c372012-11-17 17:55:51 -05003124 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003125
Daniel Drake6a661802012-11-25 13:01:19 -05003126 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3127 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3128 SDHCI_SUPPORT_DDR50);
3129
Al Cooper4188bba2012-03-16 15:54:17 -04003130 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3131 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3132 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303133 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3134
3135 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003136 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303137 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003138 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3139 * field can be promoted to support HS200.
3140 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003141 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003142 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003143 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303144 mmc->caps |= MMC_CAP_UHS_SDR50;
3145
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003146 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3147 (caps[1] & SDHCI_SUPPORT_HS400))
3148 mmc->caps2 |= MMC_CAP2_HS400;
3149
Adrian Hunter549c0b12014-11-06 15:19:05 +02003150 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3151 (IS_ERR(mmc->supply.vqmmc) ||
3152 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3153 1300000)))
3154 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3155
Micky Ching9107ebb2014-02-21 18:40:35 +08003156 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3157 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303158 mmc->caps |= MMC_CAP_UHS_DDR50;
3159
Girish K S069c9f12012-01-06 09:56:39 +05303160 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303161 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3162 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3163
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003164 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303165 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003166 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303167
Arindam Nathd6d50a12011-05-05 12:18:59 +05303168 /* Driver Type(s) (A, C, D) supported by the host */
3169 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3170 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3171 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3172 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3173 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3174 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3175
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303176 /* Initial value for re-tuning timer count */
3177 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3178 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3179
3180 /*
3181 * In case Re-tuning Timer is not disabled, the actual value of
3182 * re-tuning timer will be 2 ^ (n - 1).
3183 */
3184 if (host->tuning_count)
3185 host->tuning_count = 1 << (host->tuning_count - 1);
3186
3187 /* Re-tuning mode supported by the Host Controller */
3188 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3189 SDHCI_RETUNING_MODE_SHIFT;
3190
Takashi Iwai8f230f42010-12-08 10:04:30 +01003191 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003192
Arindam Nathf2119df2011-05-05 12:18:57 +05303193 /*
3194 * According to SD Host Controller spec v3.00, if the Host System
3195 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3196 * the value is meaningful only if Voltage Support in the Capabilities
3197 * register is set. The actual current value is 4 times the register
3198 * value.
3199 */
3200 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003201 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003202 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003203 if (curr > 0) {
3204
3205 /* convert to SDHCI_MAX_CURRENT format */
3206 curr = curr/1000; /* convert to mA */
3207 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3208
3209 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3210 max_current_caps =
3211 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3212 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3213 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3214 }
3215 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303216
3217 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003218 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303219
Aaron Lu55c46652012-07-04 13:31:48 +08003220 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303221 SDHCI_MAX_CURRENT_330_MASK) >>
3222 SDHCI_MAX_CURRENT_330_SHIFT) *
3223 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303224 }
3225 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003226 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303227
Aaron Lu55c46652012-07-04 13:31:48 +08003228 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303229 SDHCI_MAX_CURRENT_300_MASK) >>
3230 SDHCI_MAX_CURRENT_300_SHIFT) *
3231 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303232 }
3233 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003234 ocr_avail |= MMC_VDD_165_195;
3235
Aaron Lu55c46652012-07-04 13:31:48 +08003236 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303237 SDHCI_MAX_CURRENT_180_MASK) >>
3238 SDHCI_MAX_CURRENT_180_SHIFT) *
3239 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303240 }
3241
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003242 /* If OCR set by host, use it instead. */
3243 if (host->ocr_mask)
3244 ocr_avail = host->ocr_mask;
3245
3246 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003247 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003248 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003249
Takashi Iwai8f230f42010-12-08 10:04:30 +01003250 mmc->ocr_avail = ocr_avail;
3251 mmc->ocr_avail_sdio = ocr_avail;
3252 if (host->ocr_avail_sdio)
3253 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3254 mmc->ocr_avail_sd = ocr_avail;
3255 if (host->ocr_avail_sd)
3256 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3257 else /* normal SD controllers don't support 1.8V */
3258 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3259 mmc->ocr_avail_mmc = ocr_avail;
3260 if (host->ocr_avail_mmc)
3261 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003262
3263 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003264 pr_err("%s: Hardware doesn't report any support voltages.\n",
3265 mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003266 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003267 }
3268
Pierre Ossmand129bce2006-03-24 03:18:17 -08003269 spin_lock_init(&host->lock);
3270
3271 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003272 * Maximum number of segments. Depends on if the hardware
3273 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003274 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003275 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003276 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003277 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003278 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003279 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003280 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003281
3282 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003283 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3284 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3285 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003286 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003287 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003288
3289 /*
3290 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003291 * of bytes. When doing hardware scatter/gather, each entry cannot
3292 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003293 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003294 if (host->flags & SDHCI_USE_ADMA) {
3295 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3296 mmc->max_seg_size = 65535;
3297 else
3298 mmc->max_seg_size = 65536;
3299 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003300 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003301 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003302
3303 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003304 * Maximum block size. This varies from controller to controller and
3305 * is specified in the capabilities register.
3306 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003307 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3308 mmc->max_blk_size = 2;
3309 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303310 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003311 SDHCI_MAX_BLOCK_SHIFT;
3312 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003313 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3314 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003315 mmc->max_blk_size = 0;
3316 }
3317 }
3318
3319 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003320
3321 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003322 * Maximum block count.
3323 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003324 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003325
3326 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003327 * Init tasklets.
3328 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003329 tasklet_init(&host->finish_tasklet,
3330 sdhci_tasklet_finish, (unsigned long)host);
3331
Al Viroe4cad1b2006-10-10 22:47:07 +01003332 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003333
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003334 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303335
Shawn Guo2af502c2013-07-05 14:38:55 +08003336 sdhci_init(host, 0);
3337
Russell King781e9892014-04-25 12:55:46 +01003338 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3339 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003340 if (ret) {
3341 pr_err("%s: Failed to request IRQ %d: %d\n",
3342 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003343 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003344 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003345
Pierre Ossmand129bce2006-03-24 03:18:17 -08003346#ifdef CONFIG_MMC_DEBUG
3347 sdhci_dumpregs(host);
3348#endif
3349
Pierre Ossmanf9134312008-12-21 17:01:48 +01003350#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003351 snprintf(host->led_name, sizeof(host->led_name),
3352 "%s::", mmc_hostname(mmc));
3353 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003354 host->led.brightness = LED_OFF;
3355 host->led.default_trigger = mmc_hostname(mmc);
3356 host->led.brightness_set = sdhci_led_control;
3357
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003358 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003359 if (ret) {
3360 pr_err("%s: Failed to register LED device: %d\n",
3361 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003362 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003363 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003364#endif
3365
Pierre Ossman5f25a662006-10-04 02:15:39 -07003366 mmiowb();
3367
Pierre Ossmand129bce2006-03-24 03:18:17 -08003368 mmc_add_host(mmc);
3369
Girish K Sa3c76eb2011-10-11 11:44:09 +05303370 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003371 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003372 (host->flags & SDHCI_USE_ADMA) ?
3373 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003374 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003375
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003376 sdhci_enable_card_detection(host);
3377
Pierre Ossmand129bce2006-03-24 03:18:17 -08003378 return 0;
3379
Pierre Ossmanf9134312008-12-21 17:01:48 +01003380#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003381reset:
Russell King03231f92014-04-25 12:57:12 +01003382 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003383 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3384 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003385 free_irq(host->irq, host);
3386#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003387untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003388 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003389
3390 return ret;
3391}
3392
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003393EXPORT_SYMBOL_GPL(sdhci_add_host);
3394
Pierre Ossman1e728592008-04-16 19:13:13 +02003395void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003396{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003397 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003398 unsigned long flags;
3399
3400 if (dead) {
3401 spin_lock_irqsave(&host->lock, flags);
3402
3403 host->flags |= SDHCI_DEVICE_DEAD;
3404
3405 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303406 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003407 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003408
3409 host->mrq->cmd->error = -ENOMEDIUM;
3410 tasklet_schedule(&host->finish_tasklet);
3411 }
3412
3413 spin_unlock_irqrestore(&host->lock, flags);
3414 }
3415
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003416 sdhci_disable_card_detection(host);
3417
Markus Mayer4e743f12014-07-03 13:27:42 -07003418 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003419
Pierre Ossmanf9134312008-12-21 17:01:48 +01003420#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003421 led_classdev_unregister(&host->led);
3422#endif
3423
Pierre Ossman1e728592008-04-16 19:13:13 +02003424 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003425 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003426
Russell Kingb537f942014-04-25 12:56:01 +01003427 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3428 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003429 free_irq(host->irq, host);
3430
3431 del_timer_sync(&host->timer);
3432
Pierre Ossmand129bce2006-03-24 03:18:17 -08003433 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003434
Tim Kryger3a48edc2014-06-13 10:13:56 -07003435 if (!IS_ERR(mmc->supply.vqmmc))
3436 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003437
Russell Kingedd63fc2016-01-26 13:39:50 +00003438 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003439 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3440 host->adma_table_sz, host->align_buffer,
3441 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003442
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003443 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003444 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003445}
3446
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003447EXPORT_SYMBOL_GPL(sdhci_remove_host);
3448
3449void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003450{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003451 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003452}
3453
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003454EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003455
3456/*****************************************************************************\
3457 * *
3458 * Driver init/exit *
3459 * *
3460\*****************************************************************************/
3461
3462static int __init sdhci_drv_init(void)
3463{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303464 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003465 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303466 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003467
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003468 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003469}
3470
3471static void __exit sdhci_drv_exit(void)
3472{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003473}
3474
3475module_init(sdhci_drv_init);
3476module_exit(sdhci_drv_exit);
3477
Pierre Ossmandf673b22006-06-30 02:22:31 -07003478module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003479module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003480
Pierre Ossman32710e82009-04-08 20:14:54 +02003481MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003482MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003483MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003484
Pierre Ossmandf673b22006-06-30 02:22:31 -07003485MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003486MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");