blob: 73edf44ad1ac060ea86ab35bd8d3eefe4a3dd7b3 [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Aaron Lu473b0952012-07-03 17:27:49 +080030#include <linux/mmc/card.h>
Corneliu Doban85cc1c32015-02-09 16:06:29 -080031#include <linux/mmc/sdio.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080032#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080033
Pierre Ossmand129bce2006-03-24 03:18:17 -080034#include "sdhci.h"
35
36#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmand129bce2006-03-24 03:18:17 -080038#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010039 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080040
Pierre Ossmanf9134312008-12-21 17:01:48 +010041#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
42 defined(CONFIG_MMC_SDHCI_MODULE))
43#define SDHCI_USE_LEDS_CLASS
44#endif
45
Arindam Nathb513ea22011-05-05 12:19:04 +053046#define MAX_TUNING_LOOP 40
47
Pierre Ossmandf673b22006-06-30 02:22:31 -070048static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030049static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070050
Pierre Ossmand129bce2006-03-24 03:18:17 -080051static void sdhci_finish_data(struct sdhci_host *);
52
Pierre Ossmand129bce2006-03-24 03:18:17 -080053static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053054static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Kevin Liu52983382013-01-31 11:31:37 +080055static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Scott Branden04e079c2015-03-10 11:35:10 -070056static int sdhci_do_get_cd(struct sdhci_host *host);
Pierre Ossmand129bce2006-03-24 03:18:17 -080057
58static void sdhci_dumpregs(struct sdhci_host *host)
59{
Girish K Sa3c76eb2011-10-11 11:44:09 +053060 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070061 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080062
Girish K Sa3c76eb2011-10-11 11:44:09 +053063 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030064 sdhci_readl(host, SDHCI_DMA_ADDRESS),
65 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053066 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030067 sdhci_readw(host, SDHCI_BLOCK_SIZE),
68 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053069 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030070 sdhci_readl(host, SDHCI_ARGUMENT),
71 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053072 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030073 sdhci_readl(host, SDHCI_PRESENT_STATE),
74 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053075 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030076 sdhci_readb(host, SDHCI_POWER_CONTROL),
77 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053078 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030079 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
80 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053081 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030082 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
83 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +053084 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030085 sdhci_readl(host, SDHCI_INT_ENABLE),
86 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053087 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030088 sdhci_readw(host, SDHCI_ACMD12_ERR),
89 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +053090 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030091 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -050092 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +053093 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -050094 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030095 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053096 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +053097 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -080098
Adrian Huntere57a5f62014-11-04 12:42:46 +020099 if (host->flags & SDHCI_USE_ADMA) {
100 if (host->flags & SDHCI_USE_64_BIT_DMA)
101 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
102 readl(host->ioaddr + SDHCI_ADMA_ERROR),
103 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
104 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
105 else
106 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
107 readl(host->ioaddr + SDHCI_ADMA_ERROR),
108 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
109 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100110
Girish K Sa3c76eb2011-10-11 11:44:09 +0530111 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800112}
113
114/*****************************************************************************\
115 * *
116 * Low level functions *
117 * *
118\*****************************************************************************/
119
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300120static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
121{
Russell King5b4f1f62014-04-25 12:57:02 +0100122 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300123
Adrian Hunterc79396c2011-12-27 15:48:42 +0200124 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100125 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300126 return;
127
Russell King5b4f1f62014-04-25 12:57:02 +0100128 if (enable) {
129 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
130 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800131
Russell King5b4f1f62014-04-25 12:57:02 +0100132 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
133 SDHCI_INT_CARD_INSERT;
134 } else {
135 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
136 }
Russell Kingb537f942014-04-25 12:56:01 +0100137
138 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
139 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300140}
141
142static void sdhci_enable_card_detection(struct sdhci_host *host)
143{
144 sdhci_set_card_detection(host, true);
145}
146
147static void sdhci_disable_card_detection(struct sdhci_host *host)
148{
149 sdhci_set_card_detection(host, false);
150}
151
Ulf Hansson02d0b682016-04-11 15:32:41 +0200152static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
153{
154 if (host->bus_on)
155 return;
156 host->bus_on = true;
157 pm_runtime_get_noresume(host->mmc->parent);
158}
159
160static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
161{
162 if (!host->bus_on)
163 return;
164 host->bus_on = false;
165 pm_runtime_put_noidle(host->mmc->parent);
166}
167
Russell King03231f92014-04-25 12:57:12 +0100168void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800169{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700170 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800171
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300172 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800173
Adrian Hunterf0710a52013-05-06 12:17:32 +0300174 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800175 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300176 /* Reset-all turns off SD Bus Power */
177 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
178 sdhci_runtime_pm_bus_off(host);
179 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800180
Pierre Ossmane16514d82006-06-30 02:22:24 -0700181 /* Wait max 100 ms */
182 timeout = 100;
183
184 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300185 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700186 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530187 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700188 mmc_hostname(host->mmc), (int)mask);
189 sdhci_dumpregs(host);
190 return;
191 }
192 timeout--;
193 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800194 }
Russell King03231f92014-04-25 12:57:12 +0100195}
196EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300197
Russell King03231f92014-04-25 12:57:12 +0100198static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
199{
200 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Ivan T. Ivanov135b0a22015-07-06 15:16:21 +0300201 if (!sdhci_do_get_cd(host))
Russell King03231f92014-04-25 12:57:12 +0100202 return;
203 }
204
205 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800206
Russell Kingda91a8f2014-04-25 13:00:12 +0100207 if (mask & SDHCI_RESET_ALL) {
208 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
209 if (host->ops->enable_dma)
210 host->ops->enable_dma(host);
211 }
212
213 /* Resetting the controller clears many */
214 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800215 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800216}
217
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800218static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
219
220static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800221{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800222 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100223 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800224 else
Russell King03231f92014-04-25 12:57:12 +0100225 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226
Russell Kingb537f942014-04-25 12:56:01 +0100227 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
228 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
229 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
230 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
231 SDHCI_INT_RESPONSE;
232
233 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
234 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800235
236 if (soft) {
237 /* force clock reconfiguration */
238 host->clock = 0;
239 sdhci_set_ios(host->mmc, &host->mmc->ios);
240 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300241}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800242
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300243static void sdhci_reinit(struct sdhci_host *host)
244{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800245 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300246 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800247}
248
249static void sdhci_activate_led(struct sdhci_host *host)
250{
251 u8 ctrl;
252
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300253 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800254 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300255 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800256}
257
258static void sdhci_deactivate_led(struct sdhci_host *host)
259{
260 u8 ctrl;
261
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300262 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800263 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300264 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800265}
266
Pierre Ossmanf9134312008-12-21 17:01:48 +0100267#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100268static void sdhci_led_control(struct led_classdev *led,
269 enum led_brightness brightness)
270{
271 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
272 unsigned long flags;
273
274 spin_lock_irqsave(&host->lock, flags);
275
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300276 if (host->runtime_suspended)
277 goto out;
278
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100279 if (brightness == LED_OFF)
280 sdhci_deactivate_led(host);
281 else
282 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300283out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100284 spin_unlock_irqrestore(&host->lock, flags);
285}
286#endif
287
Pierre Ossmand129bce2006-03-24 03:18:17 -0800288/*****************************************************************************\
289 * *
290 * Core functions *
291 * *
292\*****************************************************************************/
293
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100294static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800295{
Pierre Ossman76591502008-07-21 00:32:11 +0200296 unsigned long flags;
297 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700298 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200299 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800300
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100301 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800302
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100303 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200304 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800305
Pierre Ossman76591502008-07-21 00:32:11 +0200306 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100308 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300309 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800310
Pierre Ossman76591502008-07-21 00:32:11 +0200311 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800312
Pierre Ossman76591502008-07-21 00:32:11 +0200313 blksize -= len;
314 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200315
Pierre Ossman76591502008-07-21 00:32:11 +0200316 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800317
Pierre Ossman76591502008-07-21 00:32:11 +0200318 while (len) {
319 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300320 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200321 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800322 }
Pierre Ossman76591502008-07-21 00:32:11 +0200323
324 *buf = scratch & 0xFF;
325
326 buf++;
327 scratch >>= 8;
328 chunk--;
329 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800330 }
331 }
Pierre Ossman76591502008-07-21 00:32:11 +0200332
333 sg_miter_stop(&host->sg_miter);
334
335 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100336}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800337
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100338static void sdhci_write_block_pio(struct sdhci_host *host)
339{
Pierre Ossman76591502008-07-21 00:32:11 +0200340 unsigned long flags;
341 size_t blksize, len, chunk;
342 u32 scratch;
343 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100344
345 DBG("PIO writing\n");
346
347 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200348 chunk = 0;
349 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100350
Pierre Ossman76591502008-07-21 00:32:11 +0200351 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100352
353 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300354 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100355
Pierre Ossman76591502008-07-21 00:32:11 +0200356 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200357
Pierre Ossman76591502008-07-21 00:32:11 +0200358 blksize -= len;
359 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100360
Pierre Ossman76591502008-07-21 00:32:11 +0200361 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100362
Pierre Ossman76591502008-07-21 00:32:11 +0200363 while (len) {
364 scratch |= (u32)*buf << (chunk * 8);
365
366 buf++;
367 chunk++;
368 len--;
369
370 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300371 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200372 chunk = 0;
373 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100374 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100375 }
376 }
Pierre Ossman76591502008-07-21 00:32:11 +0200377
378 sg_miter_stop(&host->sg_miter);
379
380 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381}
382
383static void sdhci_transfer_pio(struct sdhci_host *host)
384{
385 u32 mask;
386
387 BUG_ON(!host->data);
388
Pierre Ossman76591502008-07-21 00:32:11 +0200389 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100390 return;
391
392 if (host->data->flags & MMC_DATA_READ)
393 mask = SDHCI_DATA_AVAILABLE;
394 else
395 mask = SDHCI_SPACE_AVAILABLE;
396
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200397 /*
398 * Some controllers (JMicron JMB38x) mess up the buffer bits
399 * for transfers < 4 bytes. As long as it is just one block,
400 * we can ignore the bits.
401 */
402 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
403 (host->data->blocks == 1))
404 mask = ~0;
405
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300406 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300407 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
408 udelay(100);
409
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100410 if (host->data->flags & MMC_DATA_READ)
411 sdhci_read_block_pio(host);
412 else
413 sdhci_write_block_pio(host);
414
Pierre Ossman76591502008-07-21 00:32:11 +0200415 host->blocks--;
416 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100417 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100418 }
419
420 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800421}
422
Russell King48857d92016-01-26 13:40:16 +0000423static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000424 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000425{
426 int sg_count;
427
Russell King94538e52016-01-26 13:40:37 +0000428 /*
429 * If the data buffers are already mapped, return the previous
430 * dma_map_sg() result.
431 */
432 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000433 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000434
435 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
436 data->flags & MMC_DATA_WRITE ?
437 DMA_TO_DEVICE : DMA_FROM_DEVICE);
438
439 if (sg_count == 0)
440 return -ENOSPC;
441
442 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000443 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000444
445 return sg_count;
446}
447
Pierre Ossman2134a922008-06-28 18:28:51 +0200448static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
449{
450 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800451 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200452}
453
454static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
455{
Cong Wang482fce92011-11-27 13:27:00 +0800456 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200457 local_irq_restore(*flags);
458}
459
Adrian Huntere57a5f62014-11-04 12:42:46 +0200460static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
461 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800462{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200463 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800464
Adrian Huntere57a5f62014-11-04 12:42:46 +0200465 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200466 dma_desc->cmd = cpu_to_le16(cmd);
467 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200468 dma_desc->addr_lo = cpu_to_le32((u32)addr);
469
470 if (host->flags & SDHCI_USE_64_BIT_DMA)
471 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800472}
473
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200474static void sdhci_adma_mark_end(void *desc)
475{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200476 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200477
Adrian Huntere57a5f62014-11-04 12:42:46 +0200478 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200479 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200480}
481
Russell King60c64762016-01-26 13:40:22 +0000482static void sdhci_adma_table_pre(struct sdhci_host *host,
483 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200484{
Pierre Ossman2134a922008-06-28 18:28:51 +0200485 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200486 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000487 dma_addr_t addr, align_addr;
488 void *desc, *align;
489 char *buffer;
490 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200491
492 /*
493 * The spec does not specify endianness of descriptor table.
494 * We currently guess that it is LE.
495 */
496
Russell King60c64762016-01-26 13:40:22 +0000497 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200498
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200499 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200500 align = host->align_buffer;
501
502 align_addr = host->align_addr;
503
504 for_each_sg(data->sg, sg, host->sg_count, i) {
505 addr = sg_dma_address(sg);
506 len = sg_dma_len(sg);
507
508 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000509 * The SDHCI specification states that ADMA addresses must
510 * be 32-bit aligned. If they aren't, then we use a bounce
511 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200512 * alignment.
513 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200514 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
515 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200516 if (offset) {
517 if (data->flags & MMC_DATA_WRITE) {
518 buffer = sdhci_kmap_atomic(sg, &flags);
519 memcpy(align, buffer, offset);
520 sdhci_kunmap_atomic(buffer, &flags);
521 }
522
Ben Dooks118cd172010-03-05 13:43:26 -0800523 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200524 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200525 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200526
527 BUG_ON(offset > 65536);
528
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200529 align += SDHCI_ADMA2_ALIGN;
530 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200531
Adrian Hunter76fe3792014-11-04 12:42:42 +0200532 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200533
534 addr += offset;
535 len -= offset;
536 }
537
Pierre Ossman2134a922008-06-28 18:28:51 +0200538 BUG_ON(len > 65536);
539
Adrian Hunter347ea322015-11-26 14:00:48 +0200540 if (len) {
541 /* tran, valid */
542 sdhci_adma_write_desc(host, desc, addr, len,
543 ADMA2_TRAN_VALID);
544 desc += host->desc_sz;
545 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200546
547 /*
548 * If this triggers then we have a calculation bug
549 * somewhere. :/
550 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200551 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200552 }
553
Thomas Abraham70764a92010-05-26 14:42:04 -0700554 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000555 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200556 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200557 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200558 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700559 }
560 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000561 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200562 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700563 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200564}
565
566static void sdhci_adma_table_post(struct sdhci_host *host,
567 struct mmc_data *data)
568{
Pierre Ossman2134a922008-06-28 18:28:51 +0200569 struct scatterlist *sg;
570 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200571 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200572 char *buffer;
573 unsigned long flags;
574
Russell King47fa9612016-01-26 13:40:06 +0000575 if (data->flags & MMC_DATA_READ) {
576 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100577
Russell King47fa9612016-01-26 13:40:06 +0000578 /* Do a quick scan of the SG list for any unaligned mappings */
579 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200580 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000581 has_unaligned = true;
582 break;
583 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200584
Russell King47fa9612016-01-26 13:40:06 +0000585 if (has_unaligned) {
586 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000587 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200588
Russell King47fa9612016-01-26 13:40:06 +0000589 align = host->align_buffer;
590
591 for_each_sg(data->sg, sg, host->sg_count, i) {
592 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
593 size = SDHCI_ADMA2_ALIGN -
594 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
595
596 buffer = sdhci_kmap_atomic(sg, &flags);
597 memcpy(buffer, align, size);
598 sdhci_kunmap_atomic(buffer, &flags);
599
600 align += SDHCI_ADMA2_ALIGN;
601 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200602 }
603 }
604 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200605}
606
Andrei Warkentina3c77782011-04-11 16:13:42 -0500607static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800608{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700609 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500610 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700611 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800612
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200613 /*
614 * If the host controller provides us with an incorrect timeout
615 * value, just skip the check and use 0xE. The hardware may take
616 * longer to time out, but that's much better than having a too-short
617 * timeout value.
618 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200619 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200620 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200621
Andrei Warkentina3c77782011-04-11 16:13:42 -0500622 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100623 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500624 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800625
Andrei Warkentina3c77782011-04-11 16:13:42 -0500626 /* timeout in us */
627 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100628 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300629 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000630 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000631 if (host->clock && data->timeout_clks) {
632 unsigned long long val;
633
634 /*
635 * data->timeout_clks is in units of clock cycles.
636 * host->clock is in Hz. target_timeout is in us.
637 * Hence, us = 1000000 * cycles / Hz. Round up.
638 */
639 val = 1000000 * data->timeout_clks;
640 if (do_div(val, host->clock))
641 target_timeout++;
642 target_timeout += val;
643 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300644 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700645
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700646 /*
647 * Figure out needed cycles.
648 * We do this in steps in order to fit inside a 32 bit int.
649 * The first step is the minimum timeout, which will have a
650 * minimum resolution of 6 bits:
651 * (1) 2^13*1000 > 2^22,
652 * (2) host->timeout_clk < 2^16
653 * =>
654 * (1) / (2) > 2^6
655 */
656 count = 0;
657 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
658 while (current_timeout < target_timeout) {
659 count++;
660 current_timeout <<= 1;
661 if (count >= 0xF)
662 break;
663 }
664
665 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400666 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
667 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700668 count = 0xE;
669 }
670
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200671 return count;
672}
673
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300674static void sdhci_set_transfer_irqs(struct sdhci_host *host)
675{
676 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
677 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
678
679 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100680 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300681 else
Russell Kingb537f942014-04-25 12:56:01 +0100682 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
683
684 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
685 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300686}
687
Aisheng Dongb45e6682014-08-27 15:26:29 +0800688static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200689{
690 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800691
692 if (host->ops->set_timeout) {
693 host->ops->set_timeout(host, cmd);
694 } else {
695 count = sdhci_calc_timeout(host, cmd);
696 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
697 }
698}
699
700static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
701{
Pierre Ossman2134a922008-06-28 18:28:51 +0200702 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500703 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200704
705 WARN_ON(host->data);
706
Aisheng Dongb45e6682014-08-27 15:26:29 +0800707 if (data || (cmd->flags & MMC_RSP_BUSY))
708 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500709
710 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200711 return;
712
713 /* Sanity checks */
714 BUG_ON(data->blksz * data->blocks > 524288);
715 BUG_ON(data->blksz > host->mmc->max_blk_size);
716 BUG_ON(data->blocks > 65535);
717
718 host->data = data;
719 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400720 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200721
Russell Kingfce14422016-01-26 13:41:20 +0000722 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200723 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000724 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000725 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200726
Russell Kingfce14422016-01-26 13:41:20 +0000727 host->flags |= SDHCI_REQ_USE_DMA;
728
729 /*
730 * FIXME: This doesn't account for merging when mapping the
731 * scatterlist.
732 *
733 * The assumption here being that alignment and lengths are
734 * the same after DMA mapping to device address space.
735 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000736 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000737 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200738 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000739 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000740 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000741 /*
742 * As we use up to 3 byte chunks to work
743 * around alignment problems, we need to
744 * check the offset as well.
745 */
746 offset_mask = 3;
747 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200748 } else {
749 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000750 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000751 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
752 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200753 }
754
Russell Kingdf953922016-01-26 13:41:14 +0000755 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200756 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000757 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100758 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000759 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200760 host->flags &= ~SDHCI_REQ_USE_DMA;
761 break;
762 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000763 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100764 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200765 host->flags &= ~SDHCI_REQ_USE_DMA;
766 break;
767 }
768 }
769 }
770 }
771
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200772 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000773 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200774
Russell King60c64762016-01-26 13:40:22 +0000775 if (sg_cnt <= 0) {
776 /*
777 * This only happens when someone fed
778 * us an invalid request.
779 */
780 WARN_ON(1);
781 host->flags &= ~SDHCI_REQ_USE_DMA;
782 } else if (host->flags & SDHCI_USE_ADMA) {
783 sdhci_adma_table_pre(host, data, sg_cnt);
784
785 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
786 if (host->flags & SDHCI_USE_64_BIT_DMA)
787 sdhci_writel(host,
788 (u64)host->adma_addr >> 32,
789 SDHCI_ADMA_ADDRESS_HI);
790 } else {
791 WARN_ON(sg_cnt != 1);
792 sdhci_writel(host, sg_dma_address(data->sg),
793 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200794 }
795 }
796
Pierre Ossman2134a922008-06-28 18:28:51 +0200797 /*
798 * Always adjust the DMA selection as some controllers
799 * (e.g. JMicron) can't do PIO properly when the selection
800 * is ADMA.
801 */
802 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300803 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200804 ctrl &= ~SDHCI_CTRL_DMA_MASK;
805 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200806 (host->flags & SDHCI_USE_ADMA)) {
807 if (host->flags & SDHCI_USE_64_BIT_DMA)
808 ctrl |= SDHCI_CTRL_ADMA64;
809 else
810 ctrl |= SDHCI_CTRL_ADMA32;
811 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200812 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200813 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300814 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100815 }
816
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200817 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200818 int flags;
819
820 flags = SG_MITER_ATOMIC;
821 if (host->data->flags & MMC_DATA_READ)
822 flags |= SG_MITER_TO_SG;
823 else
824 flags |= SG_MITER_FROM_SG;
825 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200826 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800827 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700828
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300829 sdhci_set_transfer_irqs(host);
830
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400831 /* Set the DMA boundary value and block size */
832 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
833 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300834 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700835}
836
837static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500838 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700839{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800840 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500841 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700842
Dong Aisheng2b558c12013-10-30 22:09:48 +0800843 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800844 if (host->quirks2 &
845 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
846 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
847 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800848 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800849 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
850 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800851 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800852 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700853 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800854 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700855
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200856 WARN_ON(!host->data);
857
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800858 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
859 mode = SDHCI_TRNS_BLK_CNT_EN;
860
Andrei Warkentine89d4562011-05-23 15:06:37 -0500861 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800862 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500863 /*
864 * If we are sending CMD23, CMD12 never gets sent
865 * on successful completion (so no Auto-CMD12).
866 */
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800867 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
868 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500869 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500870 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
871 mode |= SDHCI_TRNS_AUTO_CMD23;
872 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
873 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700874 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500875
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700876 if (data->flags & MMC_DATA_READ)
877 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100878 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700879 mode |= SDHCI_TRNS_DMA;
880
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300881 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800882}
883
884static void sdhci_finish_data(struct sdhci_host *host)
885{
886 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800887
888 BUG_ON(!host->data);
889
890 data = host->data;
891 host->data = NULL;
892
Russell Kingadd89132016-01-26 13:40:42 +0000893 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
894 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
895 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800896
897 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200898 * The specification states that the block count register must
899 * be updated, but it does not specify at what point in the
900 * data flow. That makes the register entirely useless to read
901 * back so we have to assume that nothing made it to the card
902 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800903 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200904 if (data->error)
905 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800906 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200907 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800908
Andrei Warkentine89d4562011-05-23 15:06:37 -0500909 /*
910 * Need to send CMD12 if -
911 * a) open-ended multiblock transfer (no CMD23)
912 * b) error in multiblock transfer
913 */
914 if (data->stop &&
915 (data->error ||
916 !host->mrq->sbc)) {
917
Pierre Ossmand129bce2006-03-24 03:18:17 -0800918 /*
919 * The controller needs a reset of internal state machines
920 * upon error conditions.
921 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200922 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100923 sdhci_do_reset(host, SDHCI_RESET_CMD);
924 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800925 }
926
927 sdhci_send_command(host, data->stop);
928 } else
929 tasklet_schedule(&host->finish_tasklet);
930}
931
Dong Aishengc0e551292013-09-13 19:11:31 +0800932void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800933{
934 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700935 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700936 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800937
938 WARN_ON(host->cmd);
939
Russell King96776202016-01-26 13:39:34 +0000940 /* Initially, a command has no error */
941 cmd->error = 0;
942
Pierre Ossmand129bce2006-03-24 03:18:17 -0800943 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700944 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700945
946 mask = SDHCI_CMD_INHIBIT;
947 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
948 mask |= SDHCI_DATA_INHIBIT;
949
950 /* We shouldn't wait for data inihibit for stop commands, even
951 though they might use busy signaling */
952 if (host->mrq->data && (cmd == host->mrq->data->stop))
953 mask &= ~SDHCI_DATA_INHIBIT;
954
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300955 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700956 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100957 pr_err("%s: Controller never released inhibit bit(s).\n",
958 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800959 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200960 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800961 tasklet_schedule(&host->finish_tasklet);
962 return;
963 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700964 timeout--;
965 mdelay(1);
966 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800967
Adrian Hunter3e1a6892013-11-14 10:16:20 +0200968 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100969 if (!cmd->data && cmd->busy_timeout > 9000)
970 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +0200971 else
972 timeout += 10 * HZ;
973 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800974
975 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +0900976 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800977
Andrei Warkentina3c77782011-04-11 16:13:42 -0500978 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800979
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300980 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800981
Andrei Warkentine89d4562011-05-23 15:06:37 -0500982 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700983
Pierre Ossmand129bce2006-03-24 03:18:17 -0800984 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530985 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800986 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200987 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800988 tasklet_schedule(&host->finish_tasklet);
989 return;
990 }
991
992 if (!(cmd->flags & MMC_RSP_PRESENT))
993 flags = SDHCI_CMD_RESP_NONE;
994 else if (cmd->flags & MMC_RSP_136)
995 flags = SDHCI_CMD_RESP_LONG;
996 else if (cmd->flags & MMC_RSP_BUSY)
997 flags = SDHCI_CMD_RESP_SHORT_BUSY;
998 else
999 flags = SDHCI_CMD_RESP_SHORT;
1000
1001 if (cmd->flags & MMC_RSP_CRC)
1002 flags |= SDHCI_CMD_CRC;
1003 if (cmd->flags & MMC_RSP_OPCODE)
1004 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301005
1006 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301007 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1008 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009 flags |= SDHCI_CMD_DATA;
1010
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001011 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012}
Dong Aishengc0e551292013-09-13 19:11:31 +08001013EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014
1015static void sdhci_finish_command(struct sdhci_host *host)
1016{
1017 int i;
1018
1019 BUG_ON(host->cmd == NULL);
1020
1021 if (host->cmd->flags & MMC_RSP_PRESENT) {
1022 if (host->cmd->flags & MMC_RSP_136) {
1023 /* CRC is stripped so we need to do some shifting. */
1024 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001025 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001026 SDHCI_RESPONSE + (3-i)*4) << 8;
1027 if (i != 3)
1028 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001029 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030 SDHCI_RESPONSE + (3-i)*4-1);
1031 }
1032 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001033 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001034 }
1035 }
1036
Andrei Warkentine89d4562011-05-23 15:06:37 -05001037 /* Finished CMD23, now send actual command. */
1038 if (host->cmd == host->mrq->sbc) {
1039 host->cmd = NULL;
1040 sdhci_send_command(host, host->mrq->cmd);
1041 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001042
Andrei Warkentine89d4562011-05-23 15:06:37 -05001043 /* Processed actual command. */
1044 if (host->data && host->data_early)
1045 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001046
Andrei Warkentine89d4562011-05-23 15:06:37 -05001047 if (!host->cmd->data)
1048 tasklet_schedule(&host->finish_tasklet);
1049
1050 host->cmd = NULL;
1051 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001052}
1053
Kevin Liu52983382013-01-31 11:31:37 +08001054static u16 sdhci_get_preset_value(struct sdhci_host *host)
1055{
Russell Kingd975f122014-04-25 12:59:31 +01001056 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001057
Russell Kingd975f122014-04-25 12:59:31 +01001058 switch (host->timing) {
1059 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001060 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1061 break;
Russell Kingd975f122014-04-25 12:59:31 +01001062 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001063 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1064 break;
Russell Kingd975f122014-04-25 12:59:31 +01001065 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001066 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1067 break;
Russell Kingd975f122014-04-25 12:59:31 +01001068 case MMC_TIMING_UHS_SDR104:
1069 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001070 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1071 break;
Russell Kingd975f122014-04-25 12:59:31 +01001072 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001073 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001074 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1075 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001076 case MMC_TIMING_MMC_HS400:
1077 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1078 break;
Kevin Liu52983382013-01-31 11:31:37 +08001079 default:
1080 pr_warn("%s: Invalid UHS-I mode selected\n",
1081 mmc_hostname(host->mmc));
1082 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1083 break;
1084 }
1085 return preset;
1086}
1087
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001088u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1089 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001090{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301091 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001092 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301093 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001094 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001095
Zhangfei Gao85105c52010-08-06 07:10:01 +08001096 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001097 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001098 u16 pre_val;
1099
1100 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1101 pre_val = sdhci_get_preset_value(host);
1102 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1103 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1104 if (host->clk_mul &&
1105 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1106 clk = SDHCI_PROG_CLOCK_MODE;
1107 real_div = div + 1;
1108 clk_mul = host->clk_mul;
1109 } else {
1110 real_div = max_t(int, 1, div << 1);
1111 }
1112 goto clock_set;
1113 }
1114
Arindam Nathc3ed3872011-05-05 12:19:06 +05301115 /*
1116 * Check if the Host Controller supports Programmable Clock
1117 * Mode.
1118 */
1119 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001120 for (div = 1; div <= 1024; div++) {
1121 if ((host->max_clk * host->clk_mul / div)
1122 <= clock)
1123 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001124 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001125 if ((host->max_clk * host->clk_mul / div) <= clock) {
1126 /*
1127 * Set Programmable Clock Mode in the Clock
1128 * Control register.
1129 */
1130 clk = SDHCI_PROG_CLOCK_MODE;
1131 real_div = div;
1132 clk_mul = host->clk_mul;
1133 div--;
1134 } else {
1135 /*
1136 * Divisor can be too small to reach clock
1137 * speed requirement. Then use the base clock.
1138 */
1139 switch_base_clk = true;
1140 }
1141 }
1142
1143 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301144 /* Version 3.00 divisors must be a multiple of 2. */
1145 if (host->max_clk <= clock)
1146 div = 1;
1147 else {
1148 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1149 div += 2) {
1150 if ((host->max_clk / div) <= clock)
1151 break;
1152 }
1153 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001154 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301155 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301156 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1157 && !div && host->max_clk <= 25000000)
1158 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001159 }
1160 } else {
1161 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001162 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001163 if ((host->max_clk / div) <= clock)
1164 break;
1165 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001166 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301167 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001168 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001169
Kevin Liu52983382013-01-31 11:31:37 +08001170clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001171 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001172 *actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301173 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001174 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1175 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001176
1177 return clk;
1178}
1179EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1180
1181void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1182{
1183 u16 clk;
1184 unsigned long timeout;
1185
1186 host->mmc->actual_clock = 0;
1187
1188 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001189
1190 if (clock == 0)
1191 return;
1192
1193 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1194
Pierre Ossmand129bce2006-03-24 03:18:17 -08001195 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001196 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001197
Chris Ball27f6cb12009-09-22 16:45:31 -07001198 /* Wait max 20 ms */
1199 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001200 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001201 & SDHCI_CLOCK_INT_STABLE)) {
1202 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001203 pr_err("%s: Internal clock never stabilised.\n",
1204 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001205 sdhci_dumpregs(host);
1206 return;
1207 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001208 timeout--;
1209 mdelay(1);
1210 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001211
1212 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001213 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001214}
Russell King17710592014-04-25 12:58:55 +01001215EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001216
Adrian Hunter1dceb042016-03-29 12:45:43 +03001217static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1218 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001219{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001220 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001221
1222 spin_unlock_irq(&host->lock);
1223 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1224 spin_lock_irq(&host->lock);
1225
1226 if (mode != MMC_POWER_OFF)
1227 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1228 else
1229 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1230}
1231
1232void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1233 unsigned short vdd)
1234{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001235 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001236
Russell King24fbb3c2014-04-25 13:00:06 +01001237 if (mode != MMC_POWER_OFF) {
1238 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001239 case MMC_VDD_165_195:
1240 pwr = SDHCI_POWER_180;
1241 break;
1242 case MMC_VDD_29_30:
1243 case MMC_VDD_30_31:
1244 pwr = SDHCI_POWER_300;
1245 break;
1246 case MMC_VDD_32_33:
1247 case MMC_VDD_33_34:
1248 pwr = SDHCI_POWER_330;
1249 break;
1250 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001251 WARN(1, "%s: Invalid vdd %#x\n",
1252 mmc_hostname(host->mmc), vdd);
1253 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001254 }
1255 }
1256
1257 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001258 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001259
Pierre Ossmanae628902009-05-03 20:45:03 +02001260 host->pwr = pwr;
1261
1262 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001263 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001264 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1265 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001266 } else {
1267 /*
1268 * Spec says that we should clear the power reg before setting
1269 * a new value. Some controllers don't seem to like this though.
1270 */
1271 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1272 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001273
Russell Kinge921a8b2014-04-25 13:00:01 +01001274 /*
1275 * At least the Marvell CaFe chip gets confused if we set the
1276 * voltage and set turn on power at the same time, so set the
1277 * voltage first.
1278 */
1279 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1280 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001281
Russell Kinge921a8b2014-04-25 13:00:01 +01001282 pwr |= SDHCI_POWER_ON;
1283
Pierre Ossmanae628902009-05-03 20:45:03 +02001284 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1285
Russell Kinge921a8b2014-04-25 13:00:01 +01001286 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1287 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001288
Russell Kinge921a8b2014-04-25 13:00:01 +01001289 /*
1290 * Some controllers need an extra 10ms delay of 10ms before
1291 * they can apply clock after applying power
1292 */
1293 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1294 mdelay(10);
1295 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001296}
1297EXPORT_SYMBOL_GPL(sdhci_set_power);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001298
Adrian Hunter1dceb042016-03-29 12:45:43 +03001299static void __sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1300 unsigned short vdd)
1301{
1302 struct mmc_host *mmc = host->mmc;
1303
1304 if (host->ops->set_power)
1305 host->ops->set_power(host, mode, vdd);
1306 else if (!IS_ERR(mmc->supply.vmmc))
1307 sdhci_set_power_reg(host, mode, vdd);
1308 else
1309 sdhci_set_power(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001310}
1311
Pierre Ossmand129bce2006-03-24 03:18:17 -08001312/*****************************************************************************\
1313 * *
1314 * MMC callbacks *
1315 * *
1316\*****************************************************************************/
1317
1318static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1319{
1320 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001321 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001322 unsigned long flags;
1323
1324 host = mmc_priv(mmc);
1325
Scott Branden04e079c2015-03-10 11:35:10 -07001326 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001327 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001328
Pierre Ossmand129bce2006-03-24 03:18:17 -08001329 spin_lock_irqsave(&host->lock, flags);
1330
1331 WARN_ON(host->mrq != NULL);
1332
Pierre Ossmanf9134312008-12-21 17:01:48 +01001333#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001334 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001335#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001336
1337 /*
1338 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1339 * requests if Auto-CMD12 is enabled.
1340 */
1341 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001342 if (mrq->stop) {
1343 mrq->data->stop = NULL;
1344 mrq->stop = NULL;
1345 }
1346 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001347
1348 host->mrq = mrq;
1349
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001350 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001351 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001352 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301353 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001354 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001355 sdhci_send_command(host, mrq->sbc);
1356 else
1357 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301358 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001359
Pierre Ossman5f25a662006-10-04 02:15:39 -07001360 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001361 spin_unlock_irqrestore(&host->lock, flags);
1362}
1363
Russell King2317f562014-04-25 12:57:07 +01001364void sdhci_set_bus_width(struct sdhci_host *host, int width)
1365{
1366 u8 ctrl;
1367
1368 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1369 if (width == MMC_BUS_WIDTH_8) {
1370 ctrl &= ~SDHCI_CTRL_4BITBUS;
1371 if (host->version >= SDHCI_SPEC_300)
1372 ctrl |= SDHCI_CTRL_8BITBUS;
1373 } else {
1374 if (host->version >= SDHCI_SPEC_300)
1375 ctrl &= ~SDHCI_CTRL_8BITBUS;
1376 if (width == MMC_BUS_WIDTH_4)
1377 ctrl |= SDHCI_CTRL_4BITBUS;
1378 else
1379 ctrl &= ~SDHCI_CTRL_4BITBUS;
1380 }
1381 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1382}
1383EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1384
Russell King96d7b782014-04-25 12:59:26 +01001385void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1386{
1387 u16 ctrl_2;
1388
1389 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1390 /* Select Bus Speed Mode for host */
1391 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1392 if ((timing == MMC_TIMING_MMC_HS200) ||
1393 (timing == MMC_TIMING_UHS_SDR104))
1394 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1395 else if (timing == MMC_TIMING_UHS_SDR12)
1396 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1397 else if (timing == MMC_TIMING_UHS_SDR25)
1398 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1399 else if (timing == MMC_TIMING_UHS_SDR50)
1400 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1401 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1402 (timing == MMC_TIMING_MMC_DDR52))
1403 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001404 else if (timing == MMC_TIMING_MMC_HS400)
1405 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001406 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1407}
1408EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1409
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001410static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001411{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001412 unsigned long flags;
1413 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001414 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001415
Pierre Ossmand129bce2006-03-24 03:18:17 -08001416 spin_lock_irqsave(&host->lock, flags);
1417
Adrian Hunterceb61432011-12-27 15:48:41 +02001418 if (host->flags & SDHCI_DEVICE_DEAD) {
1419 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001420 if (!IS_ERR(mmc->supply.vmmc) &&
1421 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001422 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001423 return;
1424 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001425
Pierre Ossmand129bce2006-03-24 03:18:17 -08001426 /*
1427 * Reset the chip on each power off.
1428 * Should clear out any weird states.
1429 */
1430 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001431 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001432 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001433 }
1434
Kevin Liu52983382013-01-31 11:31:37 +08001435 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001436 (ios->power_mode == MMC_POWER_UP) &&
1437 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001438 sdhci_enable_preset_value(host, false);
1439
Russell King373073e2014-04-25 12:58:45 +01001440 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001441 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001442 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001443
1444 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1445 host->clock) {
1446 host->timeout_clk = host->mmc->actual_clock ?
1447 host->mmc->actual_clock / 1000 :
1448 host->clock / 1000;
1449 host->mmc->max_busy_timeout =
1450 host->ops->get_max_timeout_count ?
1451 host->ops->get_max_timeout_count(host) :
1452 1 << 27;
1453 host->mmc->max_busy_timeout /= host->timeout_clk;
1454 }
Russell King373073e2014-04-25 12:58:45 +01001455 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001456
Adrian Hunter1dceb042016-03-29 12:45:43 +03001457 __sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001458
Philip Rakity643a81f2010-09-23 08:24:32 -07001459 if (host->ops->platform_send_init_74_clocks)
1460 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1461
Russell King2317f562014-04-25 12:57:07 +01001462 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001463
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001464 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001465
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001466 if ((ios->timing == MMC_TIMING_SD_HS ||
1467 ios->timing == MMC_TIMING_MMC_HS)
1468 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001469 ctrl |= SDHCI_CTRL_HISPD;
1470 else
1471 ctrl &= ~SDHCI_CTRL_HISPD;
1472
Arindam Nathd6d50a12011-05-05 12:18:59 +05301473 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301474 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301475
1476 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001477 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1478 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001479 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301480 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301481 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1482 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001483 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301484 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301485
Russell Kingda91a8f2014-04-25 13:00:12 +01001486 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301487 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301488 /*
1489 * We only need to set Driver Strength if the
1490 * preset value enable is not set.
1491 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001492 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301493 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1494 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1495 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001496 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1497 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301498 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1499 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001500 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1501 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1502 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001503 pr_warn("%s: invalid driver type, default to driver type B\n",
1504 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001505 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1506 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301507
1508 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301509 } else {
1510 /*
1511 * According to SDHC Spec v3.00, if the Preset Value
1512 * Enable in the Host Control 2 register is set, we
1513 * need to reset SD Clock Enable before changing High
1514 * Speed Enable to avoid generating clock gliches.
1515 */
Arindam Nath758535c2011-05-05 12:19:00 +05301516
1517 /* Reset SD Clock Enable */
1518 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1519 clk &= ~SDHCI_CLOCK_CARD_EN;
1520 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1521
1522 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1523
1524 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001525 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301526 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301527
Arindam Nath49c468f2011-05-05 12:19:01 +05301528 /* Reset SD Clock Enable */
1529 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1530 clk &= ~SDHCI_CLOCK_CARD_EN;
1531 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1532
Russell King96d7b782014-04-25 12:59:26 +01001533 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001534 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301535
Kevin Liu52983382013-01-31 11:31:37 +08001536 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1537 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1538 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1539 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1540 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001541 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1542 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001543 u16 preset;
1544
1545 sdhci_enable_preset_value(host, true);
1546 preset = sdhci_get_preset_value(host);
1547 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1548 >> SDHCI_PRESET_DRV_SHIFT;
1549 }
1550
Arindam Nath49c468f2011-05-05 12:19:01 +05301551 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001552 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301553 } else
1554 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301555
Leandro Dorileob8352262007-07-25 23:47:04 +02001556 /*
1557 * Some (ENE) controllers go apeshit on some ios operation,
1558 * signalling timeout and CRC errors even on CMD0. Resetting
1559 * it on each ios seems to solve the problem.
1560 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301561 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001562 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001563
Pierre Ossman5f25a662006-10-04 02:15:39 -07001564 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001565 spin_unlock_irqrestore(&host->lock, flags);
1566}
1567
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001568static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1569{
1570 struct sdhci_host *host = mmc_priv(mmc);
1571
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001572 sdhci_do_set_ios(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001573}
1574
Kevin Liu94144a42013-02-28 17:35:53 +08001575static int sdhci_do_get_cd(struct sdhci_host *host)
1576{
1577 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1578
1579 if (host->flags & SDHCI_DEVICE_DEAD)
1580 return 0;
1581
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001582 /* If nonremovable, assume that the card is always present. */
1583 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
Kevin Liu94144a42013-02-28 17:35:53 +08001584 return 1;
1585
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001586 /*
1587 * Try slot gpio detect, if defined it take precedence
1588 * over build in controller functionality
1589 */
Kevin Liu94144a42013-02-28 17:35:53 +08001590 if (!IS_ERR_VALUE(gpio_cd))
1591 return !!gpio_cd;
1592
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001593 /* If polling, assume that the card is always present. */
1594 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1595 return 1;
1596
Kevin Liu94144a42013-02-28 17:35:53 +08001597 /* Host native card detect */
1598 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1599}
1600
1601static int sdhci_get_cd(struct mmc_host *mmc)
1602{
1603 struct sdhci_host *host = mmc_priv(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08001604
Ulf Hansson15e82072016-04-07 10:56:39 +02001605 return sdhci_do_get_cd(host);
Kevin Liu94144a42013-02-28 17:35:53 +08001606}
1607
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001608static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001609{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001610 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001611 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001612
Pierre Ossmand129bce2006-03-24 03:18:17 -08001613 spin_lock_irqsave(&host->lock, flags);
1614
Pierre Ossman1e728592008-04-16 19:13:13 +02001615 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001616 is_readonly = 0;
1617 else if (host->ops->get_ro)
1618 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001619 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001620 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1621 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001622
1623 spin_unlock_irqrestore(&host->lock, flags);
1624
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001625 /* This quirk needs to be replaced by a callback-function later */
1626 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1627 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001628}
1629
Takashi Iwai82b0e232011-04-21 20:26:38 +02001630#define SAMPLE_COUNT 5
1631
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001632static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001633{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001634 int i, ro_count;
1635
Takashi Iwai82b0e232011-04-21 20:26:38 +02001636 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001637 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001638
1639 ro_count = 0;
1640 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001641 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001642 if (++ro_count > SAMPLE_COUNT / 2)
1643 return 1;
1644 }
1645 msleep(30);
1646 }
1647 return 0;
1648}
1649
Adrian Hunter20758b62011-08-29 16:42:12 +03001650static void sdhci_hw_reset(struct mmc_host *mmc)
1651{
1652 struct sdhci_host *host = mmc_priv(mmc);
1653
1654 if (host->ops && host->ops->hw_reset)
1655 host->ops->hw_reset(host);
1656}
1657
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001658static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001659{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001660 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001661
Ulf Hansson15e82072016-04-07 10:56:39 +02001662 return sdhci_do_get_ro(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001663}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001664
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001665static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1666{
Russell Kingbe138552014-04-25 12:55:56 +01001667 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001668 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001669 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001670 else
Russell Kingb537f942014-04-25 12:56:01 +01001671 host->ier &= ~SDHCI_INT_CARD_INT;
1672
1673 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1674 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001675 mmiowb();
1676 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001677}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001678
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001679static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1680{
1681 struct sdhci_host *host = mmc_priv(mmc);
1682 unsigned long flags;
1683
1684 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001685 if (enable)
1686 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1687 else
1688 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1689
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001690 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001691 spin_unlock_irqrestore(&host->lock, flags);
1692}
1693
Philip Rakity6231f3d2012-07-23 15:56:23 -07001694static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001695 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001696{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001697 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001698 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001699 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001700
1701 /*
1702 * Signal Voltage Switching is only applicable for Host Controllers
1703 * v3.00 and above.
1704 */
1705 if (host->version < SDHCI_SPEC_300)
1706 return 0;
1707
Philip Rakity6231f3d2012-07-23 15:56:23 -07001708 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001709
Fabio Estevam21f59982013-02-14 10:35:03 -02001710 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001711 case MMC_SIGNAL_VOLTAGE_330:
1712 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1713 ctrl &= ~SDHCI_CTRL_VDD_180;
1714 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1715
Tim Kryger3a48edc2014-06-13 10:13:56 -07001716 if (!IS_ERR(mmc->supply.vqmmc)) {
1717 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1718 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001719 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001720 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1721 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001722 return -EIO;
1723 }
1724 }
1725 /* Wait for 5ms */
1726 usleep_range(5000, 5500);
1727
1728 /* 3.3V regulator output should be stable within 5 ms */
1729 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1730 if (!(ctrl & SDHCI_CTRL_VDD_180))
1731 return 0;
1732
Joe Perches66061102014-09-12 14:56:56 -07001733 pr_warn("%s: 3.3V regulator output did not became stable\n",
1734 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001735
1736 return -EAGAIN;
1737 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001738 if (!IS_ERR(mmc->supply.vqmmc)) {
1739 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001740 1700000, 1950000);
1741 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001742 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1743 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001744 return -EIO;
1745 }
1746 }
1747
1748 /*
1749 * Enable 1.8V Signal Enable in the Host Control2
1750 * register
1751 */
1752 ctrl |= SDHCI_CTRL_VDD_180;
1753 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1754
Vincent Yang9d967a62015-01-20 16:05:15 +08001755 /* Some controller need to do more when switching */
1756 if (host->ops->voltage_switch)
1757 host->ops->voltage_switch(host);
1758
Kevin Liu20b92a32012-12-17 19:29:26 +08001759 /* 1.8V regulator output should be stable within 5 ms */
1760 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1761 if (ctrl & SDHCI_CTRL_VDD_180)
1762 return 0;
1763
Joe Perches66061102014-09-12 14:56:56 -07001764 pr_warn("%s: 1.8V regulator output did not became stable\n",
1765 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001766
1767 return -EAGAIN;
1768 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001769 if (!IS_ERR(mmc->supply.vqmmc)) {
1770 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1771 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001772 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001773 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1774 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001775 return -EIO;
1776 }
1777 }
1778 return 0;
1779 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301780 /* No signal voltage switch required */
1781 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001782 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301783}
1784
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001785static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001786 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001787{
1788 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001789
1790 if (host->version < SDHCI_SPEC_300)
1791 return 0;
Ulf Hansson15e82072016-04-07 10:56:39 +02001792
1793 return sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001794}
1795
Kevin Liu20b92a32012-12-17 19:29:26 +08001796static int sdhci_card_busy(struct mmc_host *mmc)
1797{
1798 struct sdhci_host *host = mmc_priv(mmc);
1799 u32 present_state;
1800
Kevin Liu20b92a32012-12-17 19:29:26 +08001801 /* Check whether DAT[3:0] is 0000 */
1802 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08001803
1804 return !(present_state & SDHCI_DATA_LVL_MASK);
1805}
1806
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001807static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1808{
1809 struct sdhci_host *host = mmc_priv(mmc);
1810 unsigned long flags;
1811
1812 spin_lock_irqsave(&host->lock, flags);
1813 host->flags |= SDHCI_HS400_TUNING;
1814 spin_unlock_irqrestore(&host->lock, flags);
1815
1816 return 0;
1817}
1818
Girish K S069c9f12012-01-06 09:56:39 +05301819static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301820{
Russell King4b6f37d2014-04-25 12:59:36 +01001821 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301822 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301823 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301824 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001825 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001826 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001827 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301828
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001829 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301830
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001831 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1832 host->flags &= ~SDHCI_HS400_TUNING;
1833
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001834 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1835 tuning_count = host->tuning_count;
1836
Arindam Nathb513ea22011-05-05 12:19:04 +05301837 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001838 * The Host Controller needs tuning in case of SDR104 and DDR50
1839 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1840 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301841 * If the Host Controller supports the HS200 mode then the
1842 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301843 */
Russell King4b6f37d2014-04-25 12:59:36 +01001844 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001845 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001846 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001847 err = -EINVAL;
1848 goto out_unlock;
1849
Russell King4b6f37d2014-04-25 12:59:36 +01001850 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001851 /*
1852 * Periodic re-tuning for HS400 is not expected to be needed, so
1853 * disable it here.
1854 */
1855 if (hs400_tuning)
1856 tuning_count = 0;
1857 break;
1858
Russell King4b6f37d2014-04-25 12:59:36 +01001859 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001860 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001861 break;
Girish K S069c9f12012-01-06 09:56:39 +05301862
Russell King4b6f37d2014-04-25 12:59:36 +01001863 case MMC_TIMING_UHS_SDR50:
1864 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1865 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1866 break;
1867 /* FALLTHROUGH */
1868
1869 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001870 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301871 }
1872
Dong Aisheng45251812013-09-13 19:11:30 +08001873 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001874 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001875 err = host->ops->platform_execute_tuning(host, opcode);
Dong Aisheng45251812013-09-13 19:11:30 +08001876 return err;
1877 }
1878
Russell King4b6f37d2014-04-25 12:59:36 +01001879 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1880 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001881 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1882 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301883 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1884
1885 /*
1886 * As per the Host Controller spec v3.00, tuning command
1887 * generates Buffer Read Ready interrupt, so enable that.
1888 *
1889 * Note: The spec clearly says that when tuning sequence
1890 * is being performed, the controller does not generate
1891 * interrupts other than Buffer Read Ready interrupt. But
1892 * to make sure we don't hit a controller bug, we _only_
1893 * enable Buffer Read Ready interrupt here.
1894 */
Russell Kingb537f942014-04-25 12:56:01 +01001895 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1896 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301897
1898 /*
1899 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1900 * of loops reaches 40 times or a timeout of 150ms occurs.
1901 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301902 do {
1903 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001904 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301905
Girish K S069c9f12012-01-06 09:56:39 +05301906 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301907 cmd.arg = 0;
1908 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1909 cmd.retries = 0;
1910 cmd.data = NULL;
1911 cmd.error = 0;
1912
Al Cooper7ce45e92014-05-09 11:34:07 -04001913 if (tuning_loop_counter-- == 0)
1914 break;
1915
Arindam Nathb513ea22011-05-05 12:19:04 +05301916 mrq.cmd = &cmd;
1917 host->mrq = &mrq;
1918
1919 /*
1920 * In response to CMD19, the card sends 64 bytes of tuning
1921 * block to the Host Controller. So we set the block size
1922 * to 64 here.
1923 */
Girish K S069c9f12012-01-06 09:56:39 +05301924 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1925 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1926 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1927 SDHCI_BLOCK_SIZE);
1928 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1929 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1930 SDHCI_BLOCK_SIZE);
1931 } else {
1932 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1933 SDHCI_BLOCK_SIZE);
1934 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301935
1936 /*
1937 * The tuning block is sent by the card to the host controller.
1938 * So we set the TRNS_READ bit in the Transfer Mode register.
1939 * This also takes care of setting DMA Enable and Multi Block
1940 * Select in the same register to 0.
1941 */
1942 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1943
1944 sdhci_send_command(host, &cmd);
1945
1946 host->cmd = NULL;
1947 host->mrq = NULL;
1948
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001949 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301950 /* Wait for Buffer Read Ready interrupt */
1951 wait_event_interruptible_timeout(host->buf_ready_int,
1952 (host->tuning_done == 1),
1953 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001954 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301955
1956 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001957 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 +05301958 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1959 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1960 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1961 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1962
1963 err = -EIO;
1964 goto out;
1965 }
1966
1967 host->tuning_done = 0;
1968
1969 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07001970
1971 /* eMMC spec does not require a delay between tuning cycles */
1972 if (opcode == MMC_SEND_TUNING_BLOCK)
1973 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05301974 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1975
1976 /*
1977 * The Host Driver has exhausted the maximum number of loops allowed,
1978 * so use fixed sampling frequency.
1979 */
Al Cooper7ce45e92014-05-09 11:34:07 -04001980 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05301981 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1982 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04001983 }
1984 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001985 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08001986 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05301987 }
1988
1989out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001990 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03001991 /*
1992 * In case tuning fails, host controllers which support
1993 * re-tuning can try tuning again at a later time, when the
1994 * re-tuning timer expires. So for these controllers, we
1995 * return 0. Since there might be other controllers who do not
1996 * have this capability, we return error for them.
1997 */
1998 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301999 }
2000
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002001 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302002
Russell Kingb537f942014-04-25 12:56:01 +01002003 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2004 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002005out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002006 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302007 return err;
2008}
2009
Adrian Huntercb849642015-02-06 14:12:59 +02002010static int sdhci_select_drive_strength(struct mmc_card *card,
2011 unsigned int max_dtr, int host_drv,
2012 int card_drv, int *drv_type)
2013{
2014 struct sdhci_host *host = mmc_priv(card->host);
2015
2016 if (!host->ops->select_drive_strength)
2017 return 0;
2018
2019 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2020 card_drv, drv_type);
2021}
Kevin Liu52983382013-01-31 11:31:37 +08002022
2023static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302024{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302025 /* Host Controller v3.00 defines preset value registers */
2026 if (host->version < SDHCI_SPEC_300)
2027 return;
2028
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302029 /*
2030 * We only enable or disable Preset Value if they are not already
2031 * enabled or disabled respectively. Otherwise, we bail out.
2032 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002033 if (host->preset_enabled != enable) {
2034 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2035
2036 if (enable)
2037 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2038 else
2039 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2040
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302041 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002042
2043 if (enable)
2044 host->flags |= SDHCI_PV_ENABLED;
2045 else
2046 host->flags &= ~SDHCI_PV_ENABLED;
2047
2048 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302049 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002050}
2051
Haibo Chen348487c2014-12-09 17:04:05 +08002052static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2053 int err)
2054{
2055 struct sdhci_host *host = mmc_priv(mmc);
2056 struct mmc_data *data = mrq->data;
2057
Russell Kingf48f0392016-01-26 13:40:32 +00002058 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002059 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2060 data->flags & MMC_DATA_WRITE ?
2061 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2062
2063 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002064}
2065
Haibo Chen348487c2014-12-09 17:04:05 +08002066static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2067 bool is_first_req)
2068{
2069 struct sdhci_host *host = mmc_priv(mmc);
2070
Haibo Chend31911b2015-08-25 10:02:11 +08002071 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002072
2073 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002074 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002075}
2076
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002077static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002078{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002079 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002080 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002081 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002082
Christian Daudt722e1282013-06-20 14:26:36 -07002083 /* First check if client has provided their own card event */
2084 if (host->ops->card_event)
2085 host->ops->card_event(host);
2086
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002087 present = sdhci_do_get_cd(host);
2088
Pierre Ossmand129bce2006-03-24 03:18:17 -08002089 spin_lock_irqsave(&host->lock, flags);
2090
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002091 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002092 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302093 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002094 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302095 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002096 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002097
Russell King03231f92014-04-25 12:57:12 +01002098 sdhci_do_reset(host, SDHCI_RESET_CMD);
2099 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002100
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002101 host->mrq->cmd->error = -ENOMEDIUM;
2102 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002103 }
2104
2105 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002106}
2107
2108static const struct mmc_host_ops sdhci_ops = {
2109 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002110 .post_req = sdhci_post_req,
2111 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002112 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002113 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002114 .get_ro = sdhci_get_ro,
2115 .hw_reset = sdhci_hw_reset,
2116 .enable_sdio_irq = sdhci_enable_sdio_irq,
2117 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002118 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002119 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002120 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002121 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002122 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002123};
2124
2125/*****************************************************************************\
2126 * *
2127 * Tasklets *
2128 * *
2129\*****************************************************************************/
2130
Pierre Ossmand129bce2006-03-24 03:18:17 -08002131static void sdhci_tasklet_finish(unsigned long param)
2132{
2133 struct sdhci_host *host;
2134 unsigned long flags;
2135 struct mmc_request *mrq;
2136
2137 host = (struct sdhci_host*)param;
2138
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002139 spin_lock_irqsave(&host->lock, flags);
2140
Chris Ball0c9c99a2011-04-27 17:35:31 -04002141 /*
2142 * If this tasklet gets rescheduled while running, it will
2143 * be run again afterwards but without any active request.
2144 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002145 if (!host->mrq) {
2146 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002147 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002148 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002149
2150 del_timer(&host->timer);
2151
2152 mrq = host->mrq;
2153
Pierre Ossmand129bce2006-03-24 03:18:17 -08002154 /*
Russell King054cedf2016-01-26 13:40:42 +00002155 * Always unmap the data buffers if they were mapped by
2156 * sdhci_prepare_data() whenever we finish with a request.
2157 * This avoids leaking DMA mappings on error.
2158 */
2159 if (host->flags & SDHCI_REQ_USE_DMA) {
2160 struct mmc_data *data = mrq->data;
2161
2162 if (data && data->host_cookie == COOKIE_MAPPED) {
2163 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2164 (data->flags & MMC_DATA_READ) ?
2165 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2166 data->host_cookie = COOKIE_UNMAPPED;
2167 }
2168 }
2169
2170 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002171 * The controller needs a reset of internal state machines
2172 * upon error conditions.
2173 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002174 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002175 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002176 (mrq->sbc && mrq->sbc->error) ||
2177 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2178 (mrq->data->stop && mrq->data->stop->error))) ||
2179 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002180
2181 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002182 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002183 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002184 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002185
2186 /* Spec says we should do both at the same time, but Ricoh
2187 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002188 sdhci_do_reset(host, SDHCI_RESET_CMD);
2189 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002190 }
2191
2192 host->mrq = NULL;
2193 host->cmd = NULL;
2194 host->data = NULL;
2195
Pierre Ossmanf9134312008-12-21 17:01:48 +01002196#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002197 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002198#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002199
Pierre Ossman5f25a662006-10-04 02:15:39 -07002200 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002201 spin_unlock_irqrestore(&host->lock, flags);
2202
2203 mmc_request_done(host->mmc, mrq);
2204}
2205
2206static void sdhci_timeout_timer(unsigned long data)
2207{
2208 struct sdhci_host *host;
2209 unsigned long flags;
2210
2211 host = (struct sdhci_host*)data;
2212
2213 spin_lock_irqsave(&host->lock, flags);
2214
2215 if (host->mrq) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002216 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2217 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002218 sdhci_dumpregs(host);
2219
2220 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002221 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002222 sdhci_finish_data(host);
2223 } else {
2224 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002225 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002226 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002227 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002228
2229 tasklet_schedule(&host->finish_tasklet);
2230 }
2231 }
2232
Pierre Ossman5f25a662006-10-04 02:15:39 -07002233 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002234 spin_unlock_irqrestore(&host->lock, flags);
2235}
2236
2237/*****************************************************************************\
2238 * *
2239 * Interrupt handling *
2240 * *
2241\*****************************************************************************/
2242
Adrian Hunter61541392014-09-24 10:27:27 +03002243static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002244{
2245 BUG_ON(intmask == 0);
2246
2247 if (!host->cmd) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002248 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2249 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002250 sdhci_dumpregs(host);
2251 return;
2252 }
2253
Russell Kingec014cb2016-01-26 13:39:39 +00002254 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2255 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2256 if (intmask & SDHCI_INT_TIMEOUT)
2257 host->cmd->error = -ETIMEDOUT;
2258 else
2259 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002260
Russell King71fcbda2016-01-26 13:39:45 +00002261 /*
2262 * If this command initiates a data phase and a response
2263 * CRC error is signalled, the card can start transferring
2264 * data - the card may have received the command without
2265 * error. We must not terminate the mmc_request early.
2266 *
2267 * If the card did not receive the command or returned an
2268 * error which prevented it sending data, the data phase
2269 * will time out.
2270 */
2271 if (host->cmd->data &&
2272 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2273 SDHCI_INT_CRC) {
2274 host->cmd = NULL;
2275 return;
2276 }
2277
Pierre Ossmand129bce2006-03-24 03:18:17 -08002278 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002279 return;
2280 }
2281
2282 /*
2283 * The host can send and interrupt when the busy state has
2284 * ended, allowing us to wait without wasting CPU cycles.
2285 * Unfortunately this is overloaded on the "data complete"
2286 * interrupt, so we need to take some care when handling
2287 * it.
2288 *
2289 * Note: The 1.0 specification is a bit ambiguous about this
2290 * feature so there might be some problems with older
2291 * controllers.
2292 */
2293 if (host->cmd->flags & MMC_RSP_BUSY) {
2294 if (host->cmd->data)
Marek Vasut2e4456f2015-11-18 10:47:02 +01002295 DBG("Cannot wait for busy signal when also doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002296 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2297 && !host->busy_handle) {
2298 /* Mark that command complete before busy is ended */
2299 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002300 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002301 }
Ben Dooksf9454052009-02-20 20:33:08 +03002302
2303 /* The controller does not support the end-of-busy IRQ,
2304 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002305 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2306 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2307 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002308 }
2309
2310 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002311 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002312}
2313
George G. Davis0957c332010-02-18 12:32:12 -05002314#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002315static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002316{
2317 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002318 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002319
2320 sdhci_dumpregs(host);
2321
2322 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002323 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002324
Adrian Huntere57a5f62014-11-04 12:42:46 +02002325 if (host->flags & SDHCI_USE_64_BIT_DMA)
2326 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2327 name, desc, le32_to_cpu(dma_desc->addr_hi),
2328 le32_to_cpu(dma_desc->addr_lo),
2329 le16_to_cpu(dma_desc->len),
2330 le16_to_cpu(dma_desc->cmd));
2331 else
2332 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2333 name, desc, le32_to_cpu(dma_desc->addr_lo),
2334 le16_to_cpu(dma_desc->len),
2335 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002336
Adrian Hunter76fe3792014-11-04 12:42:42 +02002337 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002338
Adrian Hunter05452302014-11-04 12:42:45 +02002339 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002340 break;
2341 }
2342}
2343#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002344static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002345#endif
2346
Pierre Ossmand129bce2006-03-24 03:18:17 -08002347static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2348{
Girish K S069c9f12012-01-06 09:56:39 +05302349 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002350 BUG_ON(intmask == 0);
2351
Arindam Nathb513ea22011-05-05 12:19:04 +05302352 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2353 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302354 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2355 if (command == MMC_SEND_TUNING_BLOCK ||
2356 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302357 host->tuning_done = 1;
2358 wake_up(&host->buf_ready_int);
2359 return;
2360 }
2361 }
2362
Pierre Ossmand129bce2006-03-24 03:18:17 -08002363 if (!host->data) {
2364 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002365 * The "data complete" interrupt is also used to
2366 * indicate that a busy state has ended. See comment
2367 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002368 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002369 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002370 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2371 host->cmd->error = -ETIMEDOUT;
2372 tasklet_schedule(&host->finish_tasklet);
2373 return;
2374 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002375 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002376 /*
2377 * Some cards handle busy-end interrupt
2378 * before the command completed, so make
2379 * sure we do things in the proper order.
2380 */
2381 if (host->busy_handle)
2382 sdhci_finish_command(host);
2383 else
2384 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002385 return;
2386 }
2387 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002388
Marek Vasut2e4456f2015-11-18 10:47:02 +01002389 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2390 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002391 sdhci_dumpregs(host);
2392
2393 return;
2394 }
2395
2396 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002397 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002398 else if (intmask & SDHCI_INT_DATA_END_BIT)
2399 host->data->error = -EILSEQ;
2400 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2401 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2402 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002403 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002404 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302405 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002406 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002407 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002408 if (host->ops->adma_workaround)
2409 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002410 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002411
Pierre Ossman17b04292007-07-22 22:18:46 +02002412 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002413 sdhci_finish_data(host);
2414 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002415 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002416 sdhci_transfer_pio(host);
2417
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002418 /*
2419 * We currently don't do anything fancy with DMA
2420 * boundaries, but as we can't disable the feature
2421 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002422 *
2423 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2424 * should return a valid address to continue from, but as
2425 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002426 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002427 if (intmask & SDHCI_INT_DMA_END) {
2428 u32 dmastart, dmanow;
2429 dmastart = sg_dma_address(host->data->sg);
2430 dmanow = dmastart + host->data->bytes_xfered;
2431 /*
2432 * Force update to the next DMA block boundary.
2433 */
2434 dmanow = (dmanow &
2435 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2436 SDHCI_DEFAULT_BOUNDARY_SIZE;
2437 host->data->bytes_xfered = dmanow - dmastart;
2438 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2439 " next 0x%08x\n",
2440 mmc_hostname(host->mmc), dmastart,
2441 host->data->bytes_xfered, dmanow);
2442 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2443 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002444
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002445 if (intmask & SDHCI_INT_DATA_END) {
2446 if (host->cmd) {
2447 /*
2448 * Data managed to finish before the
2449 * command completed. Make sure we do
2450 * things in the proper order.
2451 */
2452 host->data_early = 1;
2453 } else {
2454 sdhci_finish_data(host);
2455 }
2456 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002457 }
2458}
2459
David Howells7d12e782006-10-05 14:55:46 +01002460static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002461{
Russell King781e9892014-04-25 12:55:46 +01002462 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002463 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002464 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002465 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002466
2467 spin_lock(&host->lock);
2468
Russell Kingbe138552014-04-25 12:55:56 +01002469 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002470 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002471 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002472 }
2473
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002474 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002475 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002476 result = IRQ_NONE;
2477 goto out;
2478 }
2479
Russell King41005002014-04-25 12:55:36 +01002480 do {
2481 /* Clear selected interrupts. */
2482 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2483 SDHCI_INT_BUS_POWER);
2484 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002485
Russell King41005002014-04-25 12:55:36 +01002486 DBG("*** %s got interrupt: 0x%08x\n",
2487 mmc_hostname(host->mmc), intmask);
2488
2489 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2490 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2491 SDHCI_CARD_PRESENT;
2492
2493 /*
2494 * There is a observation on i.mx esdhc. INSERT
2495 * bit will be immediately set again when it gets
2496 * cleared, if a card is inserted. We have to mask
2497 * the irq to prevent interrupt storm which will
2498 * freeze the system. And the REMOVE gets the
2499 * same situation.
2500 *
2501 * More testing are needed here to ensure it works
2502 * for other platforms though.
2503 */
Russell Kingb537f942014-04-25 12:56:01 +01002504 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2505 SDHCI_INT_CARD_REMOVE);
2506 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2507 SDHCI_INT_CARD_INSERT;
2508 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2509 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002510
2511 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2512 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002513
2514 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2515 SDHCI_INT_CARD_REMOVE);
2516 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002517 }
2518
2519 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002520 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2521 &intmask);
Russell King41005002014-04-25 12:55:36 +01002522
2523 if (intmask & SDHCI_INT_DATA_MASK)
2524 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2525
2526 if (intmask & SDHCI_INT_BUS_POWER)
2527 pr_err("%s: Card is consuming too much power!\n",
2528 mmc_hostname(host->mmc));
2529
Russell King781e9892014-04-25 12:55:46 +01002530 if (intmask & SDHCI_INT_CARD_INT) {
2531 sdhci_enable_sdio_irq_nolock(host, false);
2532 host->thread_isr |= SDHCI_INT_CARD_INT;
2533 result = IRQ_WAKE_THREAD;
2534 }
Russell King41005002014-04-25 12:55:36 +01002535
2536 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2537 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2538 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2539 SDHCI_INT_CARD_INT);
2540
2541 if (intmask) {
2542 unexpected |= intmask;
2543 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2544 }
2545
Russell King781e9892014-04-25 12:55:46 +01002546 if (result == IRQ_NONE)
2547 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002548
2549 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002550 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002551out:
2552 spin_unlock(&host->lock);
2553
Alexander Stein6379b232012-03-14 09:52:10 +01002554 if (unexpected) {
2555 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2556 mmc_hostname(host->mmc), unexpected);
2557 sdhci_dumpregs(host);
2558 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002559
Pierre Ossmand129bce2006-03-24 03:18:17 -08002560 return result;
2561}
2562
Russell King781e9892014-04-25 12:55:46 +01002563static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2564{
2565 struct sdhci_host *host = dev_id;
2566 unsigned long flags;
2567 u32 isr;
2568
2569 spin_lock_irqsave(&host->lock, flags);
2570 isr = host->thread_isr;
2571 host->thread_isr = 0;
2572 spin_unlock_irqrestore(&host->lock, flags);
2573
Russell King3560db82014-04-25 12:55:51 +01002574 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2575 sdhci_card_event(host->mmc);
2576 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2577 }
2578
Russell King781e9892014-04-25 12:55:46 +01002579 if (isr & SDHCI_INT_CARD_INT) {
2580 sdio_run_irqs(host->mmc);
2581
2582 spin_lock_irqsave(&host->lock, flags);
2583 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2584 sdhci_enable_sdio_irq_nolock(host, true);
2585 spin_unlock_irqrestore(&host->lock, flags);
2586 }
2587
2588 return isr ? IRQ_HANDLED : IRQ_NONE;
2589}
2590
Pierre Ossmand129bce2006-03-24 03:18:17 -08002591/*****************************************************************************\
2592 * *
2593 * Suspend/resume *
2594 * *
2595\*****************************************************************************/
2596
2597#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002598void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2599{
2600 u8 val;
2601 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2602 | SDHCI_WAKE_ON_INT;
2603
2604 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2605 val |= mask ;
2606 /* Avoid fake wake up */
2607 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2608 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2609 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2610}
2611EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2612
Fabio Estevam0b10f472014-08-30 14:53:13 -03002613static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002614{
2615 u8 val;
2616 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2617 | SDHCI_WAKE_ON_INT;
2618
2619 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2620 val &= ~mask;
2621 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2622}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002623
Manuel Lauss29495aa2011-11-03 11:09:45 +01002624int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002625{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002626 sdhci_disable_card_detection(host);
2627
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002628 mmc_retune_timer_stop(host->mmc);
2629 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302630
Kevin Liuad080d72013-01-05 17:21:33 +08002631 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002632 host->ier = 0;
2633 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2634 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002635 free_irq(host->irq, host);
2636 } else {
2637 sdhci_enable_irq_wakeups(host);
2638 enable_irq_wake(host->irq);
2639 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002640 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002641}
2642
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002643EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002644
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002645int sdhci_resume_host(struct sdhci_host *host)
2646{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002647 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002648
Richard Röjforsa13abc72009-09-22 16:45:30 -07002649 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002650 if (host->ops->enable_dma)
2651 host->ops->enable_dma(host);
2652 }
2653
Adrian Hunter6308d292012-02-07 14:48:54 +02002654 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2655 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2656 /* Card keeps power but host controller does not */
2657 sdhci_init(host, 0);
2658 host->pwr = 0;
2659 host->clock = 0;
2660 sdhci_do_set_ios(host, &host->mmc->ios);
2661 } else {
2662 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2663 mmiowb();
2664 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002665
Haibo Chen14a7b41642015-09-15 18:32:58 +08002666 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2667 ret = request_threaded_irq(host->irq, sdhci_irq,
2668 sdhci_thread_irq, IRQF_SHARED,
2669 mmc_hostname(host->mmc), host);
2670 if (ret)
2671 return ret;
2672 } else {
2673 sdhci_disable_irq_wakeups(host);
2674 disable_irq_wake(host->irq);
2675 }
2676
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002677 sdhci_enable_card_detection(host);
2678
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002679 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002680}
2681
2682EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002683
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002684int sdhci_runtime_suspend_host(struct sdhci_host *host)
2685{
2686 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002687
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002688 mmc_retune_timer_stop(host->mmc);
2689 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002690
2691 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002692 host->ier &= SDHCI_INT_CARD_INT;
2693 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2694 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002695 spin_unlock_irqrestore(&host->lock, flags);
2696
Russell King781e9892014-04-25 12:55:46 +01002697 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002698
2699 spin_lock_irqsave(&host->lock, flags);
2700 host->runtime_suspended = true;
2701 spin_unlock_irqrestore(&host->lock, flags);
2702
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002703 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002704}
2705EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2706
2707int sdhci_runtime_resume_host(struct sdhci_host *host)
2708{
2709 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002710 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002711
2712 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2713 if (host->ops->enable_dma)
2714 host->ops->enable_dma(host);
2715 }
2716
2717 sdhci_init(host, 0);
2718
2719 /* Force clock and power re-program */
2720 host->pwr = 0;
2721 host->clock = 0;
Jisheng Zhang3396e732015-01-29 17:42:12 +08002722 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002723 sdhci_do_set_ios(host, &host->mmc->ios);
2724
Kevin Liu52983382013-01-31 11:31:37 +08002725 if ((host_flags & SDHCI_PV_ENABLED) &&
2726 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2727 spin_lock_irqsave(&host->lock, flags);
2728 sdhci_enable_preset_value(host, true);
2729 spin_unlock_irqrestore(&host->lock, flags);
2730 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002731
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002732 spin_lock_irqsave(&host->lock, flags);
2733
2734 host->runtime_suspended = false;
2735
2736 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002737 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002738 sdhci_enable_sdio_irq_nolock(host, true);
2739
2740 /* Enable Card Detection */
2741 sdhci_enable_card_detection(host);
2742
2743 spin_unlock_irqrestore(&host->lock, flags);
2744
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002745 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002746}
2747EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2748
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002749#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002750
Pierre Ossmand129bce2006-03-24 03:18:17 -08002751/*****************************************************************************\
2752 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002753 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002754 * *
2755\*****************************************************************************/
2756
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002757struct sdhci_host *sdhci_alloc_host(struct device *dev,
2758 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002759{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002760 struct mmc_host *mmc;
2761 struct sdhci_host *host;
2762
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002763 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002764
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002765 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002766 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002767 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002768
2769 host = mmc_priv(mmc);
2770 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002771 host->mmc_host_ops = sdhci_ops;
2772 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002773
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002774 return host;
2775}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002776
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002777EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002778
Alexandre Courbot7b913692016-03-07 11:07:55 +09002779static int sdhci_set_dma_mask(struct sdhci_host *host)
2780{
2781 struct mmc_host *mmc = host->mmc;
2782 struct device *dev = mmc_dev(mmc);
2783 int ret = -EINVAL;
2784
2785 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
2786 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2787
2788 /* Try 64-bit mask if hardware is capable of it */
2789 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2790 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2791 if (ret) {
2792 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
2793 mmc_hostname(mmc));
2794 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2795 }
2796 }
2797
2798 /* 32-bit mask as default & fallback */
2799 if (ret) {
2800 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2801 if (ret)
2802 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
2803 mmc_hostname(mmc));
2804 }
2805
2806 return ret;
2807}
2808
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002809int sdhci_add_host(struct sdhci_host *host)
2810{
2811 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002812 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302813 u32 max_current_caps;
2814 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002815 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08002816 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002817 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002818
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002819 WARN_ON(host == NULL);
2820 if (host == NULL)
2821 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002822
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002823 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002824
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002825 if (debug_quirks)
2826 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002827 if (debug_quirks2)
2828 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002829
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002830 override_timeout_clk = host->timeout_clk;
2831
Russell King03231f92014-04-25 12:57:12 +01002832 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002833
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002834 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002835 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2836 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002837 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002838 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
2839 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002840 }
2841
Arindam Nathf2119df2011-05-05 12:18:57 +05302842 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002843 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002844
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002845 if (host->version >= SDHCI_SPEC_300)
2846 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2847 host->caps1 :
2848 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302849
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002850 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002851 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302852 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002853 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002854 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002855 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002856
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002857 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002858 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002859 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002860 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002861 }
2862
Arindam Nathf2119df2011-05-05 12:18:57 +05302863 if ((host->version >= SDHCI_SPEC_200) &&
2864 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002865 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002866
2867 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2868 (host->flags & SDHCI_USE_ADMA)) {
2869 DBG("Disabling ADMA as it is marked broken\n");
2870 host->flags &= ~SDHCI_USE_ADMA;
2871 }
2872
Adrian Huntere57a5f62014-11-04 12:42:46 +02002873 /*
2874 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2875 * and *must* do 64-bit DMA. A driver has the opportunity to change
2876 * that during the first call to ->enable_dma(). Similarly
2877 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2878 * implement.
2879 */
Al Cooper5eaa7472016-02-10 15:25:39 -05002880 if (caps[0] & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02002881 host->flags |= SDHCI_USE_64_BIT_DMA;
2882
Richard Röjforsa13abc72009-09-22 16:45:30 -07002883 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09002884 ret = sdhci_set_dma_mask(host);
2885
2886 if (!ret && host->ops->enable_dma)
2887 ret = host->ops->enable_dma(host);
2888
2889 if (ret) {
2890 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
2891 mmc_hostname(mmc));
2892 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2893
2894 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002895 }
2896 }
2897
Adrian Huntere57a5f62014-11-04 12:42:46 +02002898 /* SDMA does not support 64-bit DMA */
2899 if (host->flags & SDHCI_USE_64_BIT_DMA)
2900 host->flags &= ~SDHCI_USE_SDMA;
2901
Pierre Ossman2134a922008-06-28 18:28:51 +02002902 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00002903 dma_addr_t dma;
2904 void *buf;
2905
Pierre Ossman2134a922008-06-28 18:28:51 +02002906 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002907 * The DMA descriptor table size is calculated as the maximum
2908 * number of segments times 2, to allow for an alignment
2909 * descriptor for each segment, plus 1 for a nop end descriptor,
2910 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002911 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02002912 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2913 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2914 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002915 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002916 } else {
2917 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2918 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002919 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002920 }
Russell Kinge66e61c2016-01-26 13:39:55 +00002921
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002922 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00002923 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
2924 host->adma_table_sz, &dma, GFP_KERNEL);
2925 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07002926 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002927 mmc_hostname(mmc));
2928 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00002929 } else if ((dma + host->align_buffer_sz) &
2930 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07002931 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2932 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002933 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00002934 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
2935 host->adma_table_sz, buf, dma);
2936 } else {
2937 host->align_buffer = buf;
2938 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00002939
Russell Kinge66e61c2016-01-26 13:39:55 +00002940 host->adma_table = buf + host->align_buffer_sz;
2941 host->adma_addr = dma + host->align_buffer_sz;
2942 }
Pierre Ossman2134a922008-06-28 18:28:51 +02002943 }
2944
Pierre Ossman76591502008-07-21 00:32:11 +02002945 /*
2946 * If we use DMA, then it's up to the caller to set the DMA
2947 * mask, but PIO does not need the hw shim so we set a new
2948 * mask here in that case.
2949 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002950 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002951 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07002952 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02002953 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002954
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002955 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302956 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002957 >> SDHCI_CLOCK_BASE_SHIFT;
2958 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302959 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002960 >> SDHCI_CLOCK_BASE_SHIFT;
2961
Pierre Ossmand129bce2006-03-24 03:18:17 -08002962 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002963 if (host->max_clk == 0 || host->quirks &
2964 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002965 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002966 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
2967 mmc_hostname(mmc));
Ben Dooks4240ff02009-03-17 00:13:57 +03002968 return -ENODEV;
2969 }
2970 host->max_clk = host->ops->get_max_clock(host);
2971 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002972
2973 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302974 * In case of Host Controller v3.00, find out whether clock
2975 * multiplier is supported.
2976 */
2977 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2978 SDHCI_CLOCK_MUL_SHIFT;
2979
2980 /*
2981 * In case the value in Clock Multiplier is 0, then programmable
2982 * clock mode is not supported, otherwise the actual clock
2983 * multiplier is one more than the value of Clock Multiplier
2984 * in the Capabilities Register.
2985 */
2986 if (host->clk_mul)
2987 host->clk_mul += 1;
2988
2989 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002990 * Set host parameters.
2991 */
Dong Aisheng59241752015-07-22 20:53:07 +08002992 max_clk = host->max_clk;
2993
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002994 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002995 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302996 else if (host->version >= SDHCI_SPEC_300) {
2997 if (host->clk_mul) {
2998 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08002999 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303000 } else
3001 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3002 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003003 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003004
Adrian Hunterd310ae42016-04-12 14:25:07 +03003005 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003006 mmc->f_max = max_clk;
3007
Aisheng Dong28aab052014-08-27 15:26:31 +08003008 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3009 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3010 SDHCI_TIMEOUT_CLK_SHIFT;
3011 if (host->timeout_clk == 0) {
3012 if (host->ops->get_timeout_clock) {
3013 host->timeout_clk =
3014 host->ops->get_timeout_clock(host);
3015 } else {
3016 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3017 mmc_hostname(mmc));
3018 return -ENODEV;
3019 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003020 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003021
Aisheng Dong28aab052014-08-27 15:26:31 +08003022 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3023 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003024
Adrian Hunter99513622016-03-07 13:33:55 +02003025 if (override_timeout_clk)
3026 host->timeout_clk = override_timeout_clk;
3027
Aisheng Dong28aab052014-08-27 15:26:31 +08003028 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003029 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003030 mmc->max_busy_timeout /= host->timeout_clk;
3031 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003032
Andrei Warkentine89d4562011-05-23 15:06:37 -05003033 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003034 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003035
3036 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3037 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003038
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003039 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003040 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003041 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003042 !(host->flags & SDHCI_USE_SDMA)) &&
3043 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003044 host->flags |= SDHCI_AUTO_CMD23;
3045 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3046 } else {
3047 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3048 }
3049
Philip Rakity15ec4462010-11-19 16:48:39 -05003050 /*
3051 * A controller may support 8-bit width, but the board itself
3052 * might not have the pins brought out. Boards that support
3053 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3054 * their platform code before calling sdhci_add_host(), and we
3055 * won't assume 8-bit width for hosts without that CAP.
3056 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003057 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003058 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003059
Jerry Huang63ef5d82012-10-25 13:47:19 +08003060 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3061 mmc->caps &= ~MMC_CAP_CMD23;
3062
Arindam Nathf2119df2011-05-05 12:18:57 +05303063 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003064 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003065
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003066 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Ivan T. Ivanovc31d22e2015-07-06 15:16:20 +03003067 !(mmc->caps & MMC_CAP_NONREMOVABLE) &&
3068 IS_ERR_VALUE(mmc_gpio_get_cd(host->mmc)))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003069 mmc->caps |= MMC_CAP_NEEDS_POLL;
3070
Tim Kryger3a48edc2014-06-13 10:13:56 -07003071 /* If there are external regulators, get them */
3072 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3073 return -EPROBE_DEFER;
3074
Philip Rakity6231f3d2012-07-23 15:56:23 -07003075 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003076 if (!IS_ERR(mmc->supply.vqmmc)) {
3077 ret = regulator_enable(mmc->supply.vqmmc);
3078 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3079 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003080 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3081 SDHCI_SUPPORT_SDR50 |
3082 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003083 if (ret) {
3084 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3085 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003086 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003087 }
Kevin Liu8363c372012-11-17 17:55:51 -05003088 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003089
Daniel Drake6a661802012-11-25 13:01:19 -05003090 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3091 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3092 SDHCI_SUPPORT_DDR50);
3093
Al Cooper4188bba2012-03-16 15:54:17 -04003094 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3095 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3096 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303097 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3098
3099 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003100 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303101 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003102 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3103 * field can be promoted to support HS200.
3104 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003105 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003106 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003107 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303108 mmc->caps |= MMC_CAP_UHS_SDR50;
3109
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003110 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3111 (caps[1] & SDHCI_SUPPORT_HS400))
3112 mmc->caps2 |= MMC_CAP2_HS400;
3113
Adrian Hunter549c0b12014-11-06 15:19:05 +02003114 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3115 (IS_ERR(mmc->supply.vqmmc) ||
3116 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3117 1300000)))
3118 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3119
Micky Ching9107ebb2014-02-21 18:40:35 +08003120 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3121 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303122 mmc->caps |= MMC_CAP_UHS_DDR50;
3123
Girish K S069c9f12012-01-06 09:56:39 +05303124 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303125 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3126 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3127
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003128 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303129 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003130 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303131
Arindam Nathd6d50a12011-05-05 12:18:59 +05303132 /* Driver Type(s) (A, C, D) supported by the host */
3133 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3134 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3135 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3136 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3137 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3138 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3139
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303140 /* Initial value for re-tuning timer count */
3141 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3142 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3143
3144 /*
3145 * In case Re-tuning Timer is not disabled, the actual value of
3146 * re-tuning timer will be 2 ^ (n - 1).
3147 */
3148 if (host->tuning_count)
3149 host->tuning_count = 1 << (host->tuning_count - 1);
3150
3151 /* Re-tuning mode supported by the Host Controller */
3152 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3153 SDHCI_RETUNING_MODE_SHIFT;
3154
Takashi Iwai8f230f42010-12-08 10:04:30 +01003155 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003156
Arindam Nathf2119df2011-05-05 12:18:57 +05303157 /*
3158 * According to SD Host Controller spec v3.00, if the Host System
3159 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3160 * the value is meaningful only if Voltage Support in the Capabilities
3161 * register is set. The actual current value is 4 times the register
3162 * value.
3163 */
3164 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003165 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003166 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003167 if (curr > 0) {
3168
3169 /* convert to SDHCI_MAX_CURRENT format */
3170 curr = curr/1000; /* convert to mA */
3171 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3172
3173 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3174 max_current_caps =
3175 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3176 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3177 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3178 }
3179 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303180
3181 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003182 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303183
Aaron Lu55c46652012-07-04 13:31:48 +08003184 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303185 SDHCI_MAX_CURRENT_330_MASK) >>
3186 SDHCI_MAX_CURRENT_330_SHIFT) *
3187 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303188 }
3189 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003190 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303191
Aaron Lu55c46652012-07-04 13:31:48 +08003192 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303193 SDHCI_MAX_CURRENT_300_MASK) >>
3194 SDHCI_MAX_CURRENT_300_SHIFT) *
3195 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303196 }
3197 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003198 ocr_avail |= MMC_VDD_165_195;
3199
Aaron Lu55c46652012-07-04 13:31:48 +08003200 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303201 SDHCI_MAX_CURRENT_180_MASK) >>
3202 SDHCI_MAX_CURRENT_180_SHIFT) *
3203 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303204 }
3205
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003206 /* If OCR set by host, use it instead. */
3207 if (host->ocr_mask)
3208 ocr_avail = host->ocr_mask;
3209
3210 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003211 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003212 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003213
Takashi Iwai8f230f42010-12-08 10:04:30 +01003214 mmc->ocr_avail = ocr_avail;
3215 mmc->ocr_avail_sdio = ocr_avail;
3216 if (host->ocr_avail_sdio)
3217 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3218 mmc->ocr_avail_sd = ocr_avail;
3219 if (host->ocr_avail_sd)
3220 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3221 else /* normal SD controllers don't support 1.8V */
3222 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3223 mmc->ocr_avail_mmc = ocr_avail;
3224 if (host->ocr_avail_mmc)
3225 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003226
3227 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003228 pr_err("%s: Hardware doesn't report any support voltages.\n",
3229 mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003230 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003231 }
3232
Pierre Ossmand129bce2006-03-24 03:18:17 -08003233 spin_lock_init(&host->lock);
3234
3235 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003236 * Maximum number of segments. Depends on if the hardware
3237 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003238 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003239 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003240 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003241 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003242 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003243 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003244 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003245
3246 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003247 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3248 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3249 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003250 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003251 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003252
3253 /*
3254 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003255 * of bytes. When doing hardware scatter/gather, each entry cannot
3256 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003257 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003258 if (host->flags & SDHCI_USE_ADMA) {
3259 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3260 mmc->max_seg_size = 65535;
3261 else
3262 mmc->max_seg_size = 65536;
3263 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003264 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003265 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003266
3267 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003268 * Maximum block size. This varies from controller to controller and
3269 * is specified in the capabilities register.
3270 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003271 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3272 mmc->max_blk_size = 2;
3273 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303274 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003275 SDHCI_MAX_BLOCK_SHIFT;
3276 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003277 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3278 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003279 mmc->max_blk_size = 0;
3280 }
3281 }
3282
3283 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003284
3285 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003286 * Maximum block count.
3287 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003288 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003289
3290 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003291 * Init tasklets.
3292 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003293 tasklet_init(&host->finish_tasklet,
3294 sdhci_tasklet_finish, (unsigned long)host);
3295
Al Viroe4cad1b2006-10-10 22:47:07 +01003296 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003297
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003298 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303299
Shawn Guo2af502c2013-07-05 14:38:55 +08003300 sdhci_init(host, 0);
3301
Russell King781e9892014-04-25 12:55:46 +01003302 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3303 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003304 if (ret) {
3305 pr_err("%s: Failed to request IRQ %d: %d\n",
3306 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003307 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003308 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003309
Pierre Ossmand129bce2006-03-24 03:18:17 -08003310#ifdef CONFIG_MMC_DEBUG
3311 sdhci_dumpregs(host);
3312#endif
3313
Pierre Ossmanf9134312008-12-21 17:01:48 +01003314#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003315 snprintf(host->led_name, sizeof(host->led_name),
3316 "%s::", mmc_hostname(mmc));
3317 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003318 host->led.brightness = LED_OFF;
3319 host->led.default_trigger = mmc_hostname(mmc);
3320 host->led.brightness_set = sdhci_led_control;
3321
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003322 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003323 if (ret) {
3324 pr_err("%s: Failed to register LED device: %d\n",
3325 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003326 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003327 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003328#endif
3329
Pierre Ossman5f25a662006-10-04 02:15:39 -07003330 mmiowb();
3331
Pierre Ossmand129bce2006-03-24 03:18:17 -08003332 mmc_add_host(mmc);
3333
Girish K Sa3c76eb2011-10-11 11:44:09 +05303334 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003335 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003336 (host->flags & SDHCI_USE_ADMA) ?
3337 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003338 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003339
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003340 sdhci_enable_card_detection(host);
3341
Pierre Ossmand129bce2006-03-24 03:18:17 -08003342 return 0;
3343
Pierre Ossmanf9134312008-12-21 17:01:48 +01003344#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003345reset:
Russell King03231f92014-04-25 12:57:12 +01003346 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003347 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3348 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003349 free_irq(host->irq, host);
3350#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003351untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003352 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003353
3354 return ret;
3355}
3356
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003357EXPORT_SYMBOL_GPL(sdhci_add_host);
3358
Pierre Ossman1e728592008-04-16 19:13:13 +02003359void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003360{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003361 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003362 unsigned long flags;
3363
3364 if (dead) {
3365 spin_lock_irqsave(&host->lock, flags);
3366
3367 host->flags |= SDHCI_DEVICE_DEAD;
3368
3369 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303370 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003371 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003372
3373 host->mrq->cmd->error = -ENOMEDIUM;
3374 tasklet_schedule(&host->finish_tasklet);
3375 }
3376
3377 spin_unlock_irqrestore(&host->lock, flags);
3378 }
3379
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003380 sdhci_disable_card_detection(host);
3381
Markus Mayer4e743f12014-07-03 13:27:42 -07003382 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003383
Pierre Ossmanf9134312008-12-21 17:01:48 +01003384#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003385 led_classdev_unregister(&host->led);
3386#endif
3387
Pierre Ossman1e728592008-04-16 19:13:13 +02003388 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003389 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003390
Russell Kingb537f942014-04-25 12:56:01 +01003391 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3392 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003393 free_irq(host->irq, host);
3394
3395 del_timer_sync(&host->timer);
3396
Pierre Ossmand129bce2006-03-24 03:18:17 -08003397 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003398
Tim Kryger3a48edc2014-06-13 10:13:56 -07003399 if (!IS_ERR(mmc->supply.vqmmc))
3400 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003401
Russell Kingedd63fc2016-01-26 13:39:50 +00003402 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003403 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3404 host->adma_table_sz, host->align_buffer,
3405 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003406
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003407 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003408 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003409}
3410
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003411EXPORT_SYMBOL_GPL(sdhci_remove_host);
3412
3413void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003414{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003415 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003416}
3417
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003418EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003419
3420/*****************************************************************************\
3421 * *
3422 * Driver init/exit *
3423 * *
3424\*****************************************************************************/
3425
3426static int __init sdhci_drv_init(void)
3427{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303428 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003429 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303430 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003431
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003432 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003433}
3434
3435static void __exit sdhci_drv_exit(void)
3436{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003437}
3438
3439module_init(sdhci_drv_init);
3440module_exit(sdhci_drv_exit);
3441
Pierre Ossmandf673b22006-06-30 02:22:31 -07003442module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003443module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003444
Pierre Ossman32710e82009-04-08 20:14:54 +02003445MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003446MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003447MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003448
Pierre Ossmandf673b22006-06-30 02:22:31 -07003449MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003450MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");