blob: 97e5f40a831fff8d4ce662484273f471eaed02fa [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Aaron Lu473b0952012-07-03 17:27:49 +080030#include <linux/mmc/card.h>
Corneliu Doban85cc1c32015-02-09 16:06:29 -080031#include <linux/mmc/sdio.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080032#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080033
Pierre Ossmand129bce2006-03-24 03:18:17 -080034#include "sdhci.h"
35
36#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmand129bce2006-03-24 03:18:17 -080038#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010039 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080040
Pierre Ossmanf9134312008-12-21 17:01:48 +010041#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
42 defined(CONFIG_MMC_SDHCI_MODULE))
43#define SDHCI_USE_LEDS_CLASS
44#endif
45
Arindam Nathb513ea22011-05-05 12:19:04 +053046#define MAX_TUNING_LOOP 40
47
Pierre Ossmandf673b22006-06-30 02:22:31 -070048static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030049static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070050
Pierre Ossmand129bce2006-03-24 03:18:17 -080051static void sdhci_finish_data(struct sdhci_host *);
52
Pierre Ossmand129bce2006-03-24 03:18:17 -080053static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053054static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Kevin Liu52983382013-01-31 11:31:37 +080055static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Haibo Chen348487c2014-12-09 17:04:05 +080056static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Haibo Chend31911b2015-08-25 10:02:11 +080057 struct mmc_data *data);
Scott Branden04e079cf2015-03-10 11:35:10 -070058static int sdhci_do_get_cd(struct sdhci_host *host);
Pierre Ossmand129bce2006-03-24 03:18:17 -080059
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +010060#ifdef CONFIG_PM
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030061static int sdhci_runtime_pm_get(struct sdhci_host *host);
62static int sdhci_runtime_pm_put(struct sdhci_host *host);
Adrian Hunterf0710a52013-05-06 12:17:32 +030063static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
64static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030065#else
66static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
67{
68 return 0;
69}
70static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
71{
72 return 0;
73}
Adrian Hunterf0710a52013-05-06 12:17:32 +030074static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
75{
76}
77static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
78{
79}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030080#endif
81
Pierre Ossmand129bce2006-03-24 03:18:17 -080082static void sdhci_dumpregs(struct sdhci_host *host)
83{
Girish K Sa3c76eb2011-10-11 11:44:09 +053084 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070085 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080086
Girish K Sa3c76eb2011-10-11 11:44:09 +053087 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030088 sdhci_readl(host, SDHCI_DMA_ADDRESS),
89 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053090 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030091 sdhci_readw(host, SDHCI_BLOCK_SIZE),
92 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053093 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030094 sdhci_readl(host, SDHCI_ARGUMENT),
95 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053096 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030097 sdhci_readl(host, SDHCI_PRESENT_STATE),
98 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053099 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300100 sdhci_readb(host, SDHCI_POWER_CONTROL),
101 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530102 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300103 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
104 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530105 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
107 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530108 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300109 sdhci_readl(host, SDHCI_INT_ENABLE),
110 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530111 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300112 sdhci_readw(host, SDHCI_ACMD12_ERR),
113 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530114 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300115 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500116 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530117 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500118 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300119 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530120 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530121 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800122
Adrian Huntere57a5f62014-11-04 12:42:46 +0200123 if (host->flags & SDHCI_USE_ADMA) {
124 if (host->flags & SDHCI_USE_64_BIT_DMA)
125 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
126 readl(host->ioaddr + SDHCI_ADMA_ERROR),
127 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
128 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
129 else
130 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
131 readl(host->ioaddr + SDHCI_ADMA_ERROR),
132 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
133 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100134
Girish K Sa3c76eb2011-10-11 11:44:09 +0530135 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800136}
137
138/*****************************************************************************\
139 * *
140 * Low level functions *
141 * *
142\*****************************************************************************/
143
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300144static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
145{
Russell King5b4f1f62014-04-25 12:57:02 +0100146 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300147
Adrian Hunterc79396c2011-12-27 15:48:42 +0200148 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100149 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300150 return;
151
Russell King5b4f1f62014-04-25 12:57:02 +0100152 if (enable) {
153 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
154 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800155
Russell King5b4f1f62014-04-25 12:57:02 +0100156 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
157 SDHCI_INT_CARD_INSERT;
158 } else {
159 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
160 }
Russell Kingb537f942014-04-25 12:56:01 +0100161
162 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
163 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300164}
165
166static void sdhci_enable_card_detection(struct sdhci_host *host)
167{
168 sdhci_set_card_detection(host, true);
169}
170
171static void sdhci_disable_card_detection(struct sdhci_host *host)
172{
173 sdhci_set_card_detection(host, false);
174}
175
Russell King03231f92014-04-25 12:57:12 +0100176void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800177{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700178 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800179
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300180 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800181
Adrian Hunterf0710a52013-05-06 12:17:32 +0300182 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800183 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300184 /* Reset-all turns off SD Bus Power */
185 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
186 sdhci_runtime_pm_bus_off(host);
187 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800188
Pierre Ossmane16514d82006-06-30 02:22:24 -0700189 /* Wait max 100 ms */
190 timeout = 100;
191
192 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300193 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700194 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530195 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700196 mmc_hostname(host->mmc), (int)mask);
197 sdhci_dumpregs(host);
198 return;
199 }
200 timeout--;
201 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800202 }
Russell King03231f92014-04-25 12:57:12 +0100203}
204EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300205
Russell King03231f92014-04-25 12:57:12 +0100206static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
207{
208 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Ivan T. Ivanov135b0a22015-07-06 15:16:21 +0300209 if (!sdhci_do_get_cd(host))
Russell King03231f92014-04-25 12:57:12 +0100210 return;
211 }
212
213 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800214
Russell Kingda91a8f2014-04-25 13:00:12 +0100215 if (mask & SDHCI_RESET_ALL) {
216 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
217 if (host->ops->enable_dma)
218 host->ops->enable_dma(host);
219 }
220
221 /* Resetting the controller clears many */
222 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800223 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800224}
225
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800226static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
227
228static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800229{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800230 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100231 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800232 else
Russell King03231f92014-04-25 12:57:12 +0100233 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800234
Russell Kingb537f942014-04-25 12:56:01 +0100235 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
236 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
237 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
238 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
239 SDHCI_INT_RESPONSE;
240
241 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
242 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800243
244 if (soft) {
245 /* force clock reconfiguration */
246 host->clock = 0;
247 sdhci_set_ios(host->mmc, &host->mmc->ios);
248 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300249}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800250
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300251static void sdhci_reinit(struct sdhci_host *host)
252{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800253 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300254 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800255}
256
257static void sdhci_activate_led(struct sdhci_host *host)
258{
259 u8 ctrl;
260
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300261 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800262 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300263 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800264}
265
266static void sdhci_deactivate_led(struct sdhci_host *host)
267{
268 u8 ctrl;
269
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300270 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800271 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300272 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273}
274
Pierre Ossmanf9134312008-12-21 17:01:48 +0100275#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100276static void sdhci_led_control(struct led_classdev *led,
277 enum led_brightness brightness)
278{
279 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
280 unsigned long flags;
281
282 spin_lock_irqsave(&host->lock, flags);
283
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300284 if (host->runtime_suspended)
285 goto out;
286
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100287 if (brightness == LED_OFF)
288 sdhci_deactivate_led(host);
289 else
290 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300291out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100292 spin_unlock_irqrestore(&host->lock, flags);
293}
294#endif
295
Pierre Ossmand129bce2006-03-24 03:18:17 -0800296/*****************************************************************************\
297 * *
298 * Core functions *
299 * *
300\*****************************************************************************/
301
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100302static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800303{
Pierre Ossman76591502008-07-21 00:32:11 +0200304 unsigned long flags;
305 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700306 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200307 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800308
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100309 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800310
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100311 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200312 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800313
Pierre Ossman76591502008-07-21 00:32:11 +0200314 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800315
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100316 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300317 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800318
Pierre Ossman76591502008-07-21 00:32:11 +0200319 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800320
Pierre Ossman76591502008-07-21 00:32:11 +0200321 blksize -= len;
322 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200323
Pierre Ossman76591502008-07-21 00:32:11 +0200324 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800325
Pierre Ossman76591502008-07-21 00:32:11 +0200326 while (len) {
327 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300328 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200329 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800330 }
Pierre Ossman76591502008-07-21 00:32:11 +0200331
332 *buf = scratch & 0xFF;
333
334 buf++;
335 scratch >>= 8;
336 chunk--;
337 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800338 }
339 }
Pierre Ossman76591502008-07-21 00:32:11 +0200340
341 sg_miter_stop(&host->sg_miter);
342
343 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100344}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800345
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100346static void sdhci_write_block_pio(struct sdhci_host *host)
347{
Pierre Ossman76591502008-07-21 00:32:11 +0200348 unsigned long flags;
349 size_t blksize, len, chunk;
350 u32 scratch;
351 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100352
353 DBG("PIO writing\n");
354
355 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200356 chunk = 0;
357 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358
Pierre Ossman76591502008-07-21 00:32:11 +0200359 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100360
361 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300362 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100363
Pierre Ossman76591502008-07-21 00:32:11 +0200364 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200365
Pierre Ossman76591502008-07-21 00:32:11 +0200366 blksize -= len;
367 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100368
Pierre Ossman76591502008-07-21 00:32:11 +0200369 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100370
Pierre Ossman76591502008-07-21 00:32:11 +0200371 while (len) {
372 scratch |= (u32)*buf << (chunk * 8);
373
374 buf++;
375 chunk++;
376 len--;
377
378 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300379 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200380 chunk = 0;
381 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100382 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100383 }
384 }
Pierre Ossman76591502008-07-21 00:32:11 +0200385
386 sg_miter_stop(&host->sg_miter);
387
388 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100389}
390
391static void sdhci_transfer_pio(struct sdhci_host *host)
392{
393 u32 mask;
394
395 BUG_ON(!host->data);
396
Pierre Ossman76591502008-07-21 00:32:11 +0200397 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100398 return;
399
400 if (host->data->flags & MMC_DATA_READ)
401 mask = SDHCI_DATA_AVAILABLE;
402 else
403 mask = SDHCI_SPACE_AVAILABLE;
404
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200405 /*
406 * Some controllers (JMicron JMB38x) mess up the buffer bits
407 * for transfers < 4 bytes. As long as it is just one block,
408 * we can ignore the bits.
409 */
410 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
411 (host->data->blocks == 1))
412 mask = ~0;
413
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300414 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300415 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
416 udelay(100);
417
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100418 if (host->data->flags & MMC_DATA_READ)
419 sdhci_read_block_pio(host);
420 else
421 sdhci_write_block_pio(host);
422
Pierre Ossman76591502008-07-21 00:32:11 +0200423 host->blocks--;
424 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100425 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100426 }
427
428 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800429}
430
Pierre Ossman2134a922008-06-28 18:28:51 +0200431static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
432{
433 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800434 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200435}
436
437static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
438{
Cong Wang482fce92011-11-27 13:27:00 +0800439 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200440 local_irq_restore(*flags);
441}
442
Adrian Huntere57a5f62014-11-04 12:42:46 +0200443static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
444 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800445{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200446 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800447
Adrian Huntere57a5f62014-11-04 12:42:46 +0200448 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200449 dma_desc->cmd = cpu_to_le16(cmd);
450 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200451 dma_desc->addr_lo = cpu_to_le32((u32)addr);
452
453 if (host->flags & SDHCI_USE_64_BIT_DMA)
454 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800455}
456
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200457static void sdhci_adma_mark_end(void *desc)
458{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200459 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200460
Adrian Huntere57a5f62014-11-04 12:42:46 +0200461 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200462 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200463}
464
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200465static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200466 struct mmc_data *data)
467{
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200468 void *desc;
469 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200470 dma_addr_t addr;
471 dma_addr_t align_addr;
472 int len, offset;
473
474 struct scatterlist *sg;
475 int i;
476 char *buffer;
477 unsigned long flags;
478
479 /*
480 * The spec does not specify endianness of descriptor table.
481 * We currently guess that it is LE.
482 */
483
Haibo Chend31911b2015-08-25 10:02:11 +0800484 host->sg_count = sdhci_pre_dma_transfer(host, data);
Haibo Chen348487c2014-12-09 17:04:05 +0800485 if (host->sg_count < 0)
Russell Kingedd63fc2016-01-26 13:39:50 +0000486 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200487
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200488 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200489 align = host->align_buffer;
490
491 align_addr = host->align_addr;
492
493 for_each_sg(data->sg, sg, host->sg_count, i) {
494 addr = sg_dma_address(sg);
495 len = sg_dma_len(sg);
496
497 /*
498 * The SDHCI specification states that ADMA
499 * addresses must be 32-bit aligned. If they
500 * aren't, then we use a bounce buffer for
501 * the (up to three) bytes that screw up the
502 * alignment.
503 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200504 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
505 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200506 if (offset) {
507 if (data->flags & MMC_DATA_WRITE) {
508 buffer = sdhci_kmap_atomic(sg, &flags);
509 memcpy(align, buffer, offset);
510 sdhci_kunmap_atomic(buffer, &flags);
511 }
512
Ben Dooks118cd172010-03-05 13:43:26 -0800513 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200514 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200515 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200516
517 BUG_ON(offset > 65536);
518
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200519 align += SDHCI_ADMA2_ALIGN;
520 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200521
Adrian Hunter76fe3792014-11-04 12:42:42 +0200522 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200523
524 addr += offset;
525 len -= offset;
526 }
527
Pierre Ossman2134a922008-06-28 18:28:51 +0200528 BUG_ON(len > 65536);
529
Adrian Hunter347ea322015-11-26 14:00:48 +0200530 if (len) {
531 /* tran, valid */
532 sdhci_adma_write_desc(host, desc, addr, len,
533 ADMA2_TRAN_VALID);
534 desc += host->desc_sz;
535 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200536
537 /*
538 * If this triggers then we have a calculation bug
539 * somewhere. :/
540 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200541 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200542 }
543
Thomas Abraham70764a92010-05-26 14:42:04 -0700544 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
545 /*
546 * Mark the last descriptor as the terminating descriptor
547 */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200548 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200549 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200550 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700551 }
552 } else {
553 /*
554 * Add a terminating entry.
555 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200556
Thomas Abraham70764a92010-05-26 14:42:04 -0700557 /* nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200558 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700559 }
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200560 return 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200561}
562
563static void sdhci_adma_table_post(struct sdhci_host *host,
564 struct mmc_data *data)
565{
566 int direction;
567
568 struct scatterlist *sg;
569 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200570 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200571 char *buffer;
572 unsigned long flags;
Russell Kingde0b65a2014-04-25 12:58:29 +0100573 bool has_unaligned;
Pierre Ossman2134a922008-06-28 18:28:51 +0200574
575 if (data->flags & MMC_DATA_READ)
576 direction = DMA_FROM_DEVICE;
577 else
578 direction = DMA_TO_DEVICE;
579
Russell Kingde0b65a2014-04-25 12:58:29 +0100580 /* Do a quick scan of the SG list for any unaligned mappings */
581 has_unaligned = false;
582 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200583 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell Kingde0b65a2014-04-25 12:58:29 +0100584 has_unaligned = true;
585 break;
586 }
587
588 if (has_unaligned && data->flags & MMC_DATA_READ) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200589 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
590 data->sg_len, direction);
591
592 align = host->align_buffer;
593
594 for_each_sg(data->sg, sg, host->sg_count, i) {
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200595 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
596 size = SDHCI_ADMA2_ALIGN -
597 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
Pierre Ossman2134a922008-06-28 18:28:51 +0200598
599 buffer = sdhci_kmap_atomic(sg, &flags);
600 memcpy(buffer, align, size);
601 sdhci_kunmap_atomic(buffer, &flags);
602
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200603 align += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200604 }
605 }
606 }
607
Haibo Chend31911b2015-08-25 10:02:11 +0800608 if (data->host_cookie == COOKIE_MAPPED) {
Haibo Chen348487c2014-12-09 17:04:05 +0800609 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
610 data->sg_len, direction);
Haibo Chend31911b2015-08-25 10:02:11 +0800611 data->host_cookie = COOKIE_UNMAPPED;
612 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200613}
614
Andrei Warkentina3c77782011-04-11 16:13:42 -0500615static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800616{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700617 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500618 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700619 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800620
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200621 /*
622 * If the host controller provides us with an incorrect timeout
623 * value, just skip the check and use 0xE. The hardware may take
624 * longer to time out, but that's much better than having a too-short
625 * timeout value.
626 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200627 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200628 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200629
Andrei Warkentina3c77782011-04-11 16:13:42 -0500630 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100631 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500632 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800633
Andrei Warkentina3c77782011-04-11 16:13:42 -0500634 /* timeout in us */
635 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100636 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300637 else {
638 target_timeout = data->timeout_ns / 1000;
639 if (host->clock)
640 target_timeout += data->timeout_clks / host->clock;
641 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700642
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700643 /*
644 * Figure out needed cycles.
645 * We do this in steps in order to fit inside a 32 bit int.
646 * The first step is the minimum timeout, which will have a
647 * minimum resolution of 6 bits:
648 * (1) 2^13*1000 > 2^22,
649 * (2) host->timeout_clk < 2^16
650 * =>
651 * (1) / (2) > 2^6
652 */
653 count = 0;
654 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
655 while (current_timeout < target_timeout) {
656 count++;
657 current_timeout <<= 1;
658 if (count >= 0xF)
659 break;
660 }
661
662 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400663 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
664 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700665 count = 0xE;
666 }
667
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200668 return count;
669}
670
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300671static void sdhci_set_transfer_irqs(struct sdhci_host *host)
672{
673 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
674 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
675
676 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100677 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300678 else
Russell Kingb537f942014-04-25 12:56:01 +0100679 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
680
681 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
682 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300683}
684
Aisheng Dongb45e6682014-08-27 15:26:29 +0800685static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200686{
687 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800688
689 if (host->ops->set_timeout) {
690 host->ops->set_timeout(host, cmd);
691 } else {
692 count = sdhci_calc_timeout(host, cmd);
693 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
694 }
695}
696
697static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
698{
Pierre Ossman2134a922008-06-28 18:28:51 +0200699 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500700 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200701 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200702
703 WARN_ON(host->data);
704
Aisheng Dongb45e6682014-08-27 15:26:29 +0800705 if (data || (cmd->flags & MMC_RSP_BUSY))
706 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500707
708 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200709 return;
710
711 /* Sanity checks */
712 BUG_ON(data->blksz * data->blocks > 524288);
713 BUG_ON(data->blksz > host->mmc->max_blk_size);
714 BUG_ON(data->blocks > 65535);
715
716 host->data = data;
717 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400718 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200719
Richard Röjforsa13abc72009-09-22 16:45:30 -0700720 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100721 host->flags |= SDHCI_REQ_USE_DMA;
722
Pierre Ossman2134a922008-06-28 18:28:51 +0200723 /*
724 * FIXME: This doesn't account for merging when mapping the
725 * scatterlist.
726 */
727 if (host->flags & SDHCI_REQ_USE_DMA) {
728 int broken, i;
729 struct scatterlist *sg;
730
731 broken = 0;
732 if (host->flags & SDHCI_USE_ADMA) {
733 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
734 broken = 1;
735 } else {
736 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
737 broken = 1;
738 }
739
740 if (unlikely(broken)) {
741 for_each_sg(data->sg, sg, data->sg_len, i) {
742 if (sg->length & 0x3) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100743 DBG("Reverting to PIO because of transfer size (%d)\n",
Pierre Ossman2134a922008-06-28 18:28:51 +0200744 sg->length);
745 host->flags &= ~SDHCI_REQ_USE_DMA;
746 break;
747 }
748 }
749 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100750 }
751
752 /*
753 * The assumption here being that alignment is the same after
754 * translation to device address space.
755 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200756 if (host->flags & SDHCI_REQ_USE_DMA) {
757 int broken, i;
758 struct scatterlist *sg;
759
760 broken = 0;
761 if (host->flags & SDHCI_USE_ADMA) {
762 /*
763 * As we use 3 byte chunks to work around
764 * alignment problems, we need to check this
765 * quirk.
766 */
767 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
768 broken = 1;
769 } else {
770 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
771 broken = 1;
772 }
773
774 if (unlikely(broken)) {
775 for_each_sg(data->sg, sg, data->sg_len, i) {
776 if (sg->offset & 0x3) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100777 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200778 host->flags &= ~SDHCI_REQ_USE_DMA;
779 break;
780 }
781 }
782 }
783 }
784
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200785 if (host->flags & SDHCI_REQ_USE_DMA) {
786 if (host->flags & SDHCI_USE_ADMA) {
787 ret = sdhci_adma_table_pre(host, data);
788 if (ret) {
789 /*
790 * This only happens when someone fed
791 * us an invalid request.
792 */
793 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200794 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200795 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300796 sdhci_writel(host, host->adma_addr,
797 SDHCI_ADMA_ADDRESS);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200798 if (host->flags & SDHCI_USE_64_BIT_DMA)
799 sdhci_writel(host,
800 (u64)host->adma_addr >> 32,
801 SDHCI_ADMA_ADDRESS_HI);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200802 }
803 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300804 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200805
Haibo Chend31911b2015-08-25 10:02:11 +0800806 sg_cnt = sdhci_pre_dma_transfer(host, data);
Jiri Slaby62a7f362015-06-12 11:45:02 +0200807 if (sg_cnt <= 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200808 /*
809 * This only happens when someone fed
810 * us an invalid request.
811 */
812 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200813 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200814 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200815 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300816 sdhci_writel(host, sg_dma_address(data->sg),
817 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200818 }
819 }
820 }
821
Pierre Ossman2134a922008-06-28 18:28:51 +0200822 /*
823 * Always adjust the DMA selection as some controllers
824 * (e.g. JMicron) can't do PIO properly when the selection
825 * is ADMA.
826 */
827 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300828 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200829 ctrl &= ~SDHCI_CTRL_DMA_MASK;
830 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200831 (host->flags & SDHCI_USE_ADMA)) {
832 if (host->flags & SDHCI_USE_64_BIT_DMA)
833 ctrl |= SDHCI_CTRL_ADMA64;
834 else
835 ctrl |= SDHCI_CTRL_ADMA32;
836 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200837 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200838 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300839 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100840 }
841
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200842 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200843 int flags;
844
845 flags = SG_MITER_ATOMIC;
846 if (host->data->flags & MMC_DATA_READ)
847 flags |= SG_MITER_TO_SG;
848 else
849 flags |= SG_MITER_FROM_SG;
850 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200851 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800852 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700853
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300854 sdhci_set_transfer_irqs(host);
855
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400856 /* Set the DMA boundary value and block size */
857 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
858 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300859 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700860}
861
862static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500863 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700864{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800865 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500866 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700867
Dong Aisheng2b558c12013-10-30 22:09:48 +0800868 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800869 if (host->quirks2 &
870 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
871 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
872 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800873 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800874 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
875 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800876 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800877 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700878 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800879 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700880
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200881 WARN_ON(!host->data);
882
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800883 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
884 mode = SDHCI_TRNS_BLK_CNT_EN;
885
Andrei Warkentine89d4562011-05-23 15:06:37 -0500886 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800887 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500888 /*
889 * If we are sending CMD23, CMD12 never gets sent
890 * on successful completion (so no Auto-CMD12).
891 */
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800892 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
893 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500894 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500895 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
896 mode |= SDHCI_TRNS_AUTO_CMD23;
897 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
898 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700899 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500900
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700901 if (data->flags & MMC_DATA_READ)
902 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100903 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700904 mode |= SDHCI_TRNS_DMA;
905
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300906 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800907}
908
909static void sdhci_finish_data(struct sdhci_host *host)
910{
911 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800912
913 BUG_ON(!host->data);
914
915 data = host->data;
916 host->data = NULL;
917
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100918 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200919 if (host->flags & SDHCI_USE_ADMA)
920 sdhci_adma_table_post(host, data);
921 else {
Haibo Chend31911b2015-08-25 10:02:11 +0800922 if (data->host_cookie == COOKIE_MAPPED) {
Haibo Chen348487c2014-12-09 17:04:05 +0800923 dma_unmap_sg(mmc_dev(host->mmc),
924 data->sg, data->sg_len,
925 (data->flags & MMC_DATA_READ) ?
Pierre Ossman2134a922008-06-28 18:28:51 +0200926 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Haibo Chend31911b2015-08-25 10:02:11 +0800927 data->host_cookie = COOKIE_UNMAPPED;
928 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200929 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800930 }
931
932 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200933 * The specification states that the block count register must
934 * be updated, but it does not specify at what point in the
935 * data flow. That makes the register entirely useless to read
936 * back so we have to assume that nothing made it to the card
937 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800938 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200939 if (data->error)
940 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800941 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200942 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800943
Andrei Warkentine89d4562011-05-23 15:06:37 -0500944 /*
945 * Need to send CMD12 if -
946 * a) open-ended multiblock transfer (no CMD23)
947 * b) error in multiblock transfer
948 */
949 if (data->stop &&
950 (data->error ||
951 !host->mrq->sbc)) {
952
Pierre Ossmand129bce2006-03-24 03:18:17 -0800953 /*
954 * The controller needs a reset of internal state machines
955 * upon error conditions.
956 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200957 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100958 sdhci_do_reset(host, SDHCI_RESET_CMD);
959 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800960 }
961
962 sdhci_send_command(host, data->stop);
963 } else
964 tasklet_schedule(&host->finish_tasklet);
965}
966
Dong Aishengc0e551292013-09-13 19:11:31 +0800967void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800968{
969 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700970 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700971 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800972
973 WARN_ON(host->cmd);
974
Russell King96776202016-01-26 13:39:34 +0000975 /* Initially, a command has no error */
976 cmd->error = 0;
977
Pierre Ossmand129bce2006-03-24 03:18:17 -0800978 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700979 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700980
981 mask = SDHCI_CMD_INHIBIT;
982 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
983 mask |= SDHCI_DATA_INHIBIT;
984
985 /* We shouldn't wait for data inihibit for stop commands, even
986 though they might use busy signaling */
987 if (host->mrq->data && (cmd == host->mrq->data->stop))
988 mask &= ~SDHCI_DATA_INHIBIT;
989
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300990 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700991 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100992 pr_err("%s: Controller never released inhibit bit(s).\n",
993 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200995 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996 tasklet_schedule(&host->finish_tasklet);
997 return;
998 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700999 timeout--;
1000 mdelay(1);
1001 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001002
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001003 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001004 if (!cmd->data && cmd->busy_timeout > 9000)
1005 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001006 else
1007 timeout += 10 * HZ;
1008 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009
1010 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001011 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012
Andrei Warkentina3c77782011-04-11 16:13:42 -05001013 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001015 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001016
Andrei Warkentine89d4562011-05-23 15:06:37 -05001017 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001018
Pierre Ossmand129bce2006-03-24 03:18:17 -08001019 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301020 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001021 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001022 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001023 tasklet_schedule(&host->finish_tasklet);
1024 return;
1025 }
1026
1027 if (!(cmd->flags & MMC_RSP_PRESENT))
1028 flags = SDHCI_CMD_RESP_NONE;
1029 else if (cmd->flags & MMC_RSP_136)
1030 flags = SDHCI_CMD_RESP_LONG;
1031 else if (cmd->flags & MMC_RSP_BUSY)
1032 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1033 else
1034 flags = SDHCI_CMD_RESP_SHORT;
1035
1036 if (cmd->flags & MMC_RSP_CRC)
1037 flags |= SDHCI_CMD_CRC;
1038 if (cmd->flags & MMC_RSP_OPCODE)
1039 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301040
1041 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301042 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1043 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001044 flags |= SDHCI_CMD_DATA;
1045
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001046 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047}
Dong Aishengc0e551292013-09-13 19:11:31 +08001048EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001049
1050static void sdhci_finish_command(struct sdhci_host *host)
1051{
1052 int i;
1053
1054 BUG_ON(host->cmd == NULL);
1055
1056 if (host->cmd->flags & MMC_RSP_PRESENT) {
1057 if (host->cmd->flags & MMC_RSP_136) {
1058 /* CRC is stripped so we need to do some shifting. */
1059 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001060 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001061 SDHCI_RESPONSE + (3-i)*4) << 8;
1062 if (i != 3)
1063 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001064 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001065 SDHCI_RESPONSE + (3-i)*4-1);
1066 }
1067 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001068 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001069 }
1070 }
1071
Andrei Warkentine89d4562011-05-23 15:06:37 -05001072 /* Finished CMD23, now send actual command. */
1073 if (host->cmd == host->mrq->sbc) {
1074 host->cmd = NULL;
1075 sdhci_send_command(host, host->mrq->cmd);
1076 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001077
Andrei Warkentine89d4562011-05-23 15:06:37 -05001078 /* Processed actual command. */
1079 if (host->data && host->data_early)
1080 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001081
Andrei Warkentine89d4562011-05-23 15:06:37 -05001082 if (!host->cmd->data)
1083 tasklet_schedule(&host->finish_tasklet);
1084
1085 host->cmd = NULL;
1086 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001087}
1088
Kevin Liu52983382013-01-31 11:31:37 +08001089static u16 sdhci_get_preset_value(struct sdhci_host *host)
1090{
Russell Kingd975f122014-04-25 12:59:31 +01001091 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001092
Russell Kingd975f122014-04-25 12:59:31 +01001093 switch (host->timing) {
1094 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001095 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1096 break;
Russell Kingd975f122014-04-25 12:59:31 +01001097 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001098 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1099 break;
Russell Kingd975f122014-04-25 12:59:31 +01001100 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001101 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1102 break;
Russell Kingd975f122014-04-25 12:59:31 +01001103 case MMC_TIMING_UHS_SDR104:
1104 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001105 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1106 break;
Russell Kingd975f122014-04-25 12:59:31 +01001107 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001108 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001109 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1110 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001111 case MMC_TIMING_MMC_HS400:
1112 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1113 break;
Kevin Liu52983382013-01-31 11:31:37 +08001114 default:
1115 pr_warn("%s: Invalid UHS-I mode selected\n",
1116 mmc_hostname(host->mmc));
1117 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1118 break;
1119 }
1120 return preset;
1121}
1122
Russell King17710592014-04-25 12:58:55 +01001123void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001124{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301125 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001126 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301127 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001128 unsigned long timeout;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001129 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001130
Russell King1650d0c2014-04-25 12:58:50 +01001131 host->mmc->actual_clock = 0;
1132
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001133 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
ludovic.desroches@atmel.comaf951762015-09-17 10:16:19 +02001134 if (host->quirks2 & SDHCI_QUIRK2_NEED_DELAY_AFTER_INT_CLK_RST)
1135 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001136
1137 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001138 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001139
Zhangfei Gao85105c52010-08-06 07:10:01 +08001140 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001141 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001142 u16 pre_val;
1143
1144 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1145 pre_val = sdhci_get_preset_value(host);
1146 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1147 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1148 if (host->clk_mul &&
1149 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1150 clk = SDHCI_PROG_CLOCK_MODE;
1151 real_div = div + 1;
1152 clk_mul = host->clk_mul;
1153 } else {
1154 real_div = max_t(int, 1, div << 1);
1155 }
1156 goto clock_set;
1157 }
1158
Arindam Nathc3ed3872011-05-05 12:19:06 +05301159 /*
1160 * Check if the Host Controller supports Programmable Clock
1161 * Mode.
1162 */
1163 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001164 for (div = 1; div <= 1024; div++) {
1165 if ((host->max_clk * host->clk_mul / div)
1166 <= clock)
1167 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001168 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001169 if ((host->max_clk * host->clk_mul / div) <= clock) {
1170 /*
1171 * Set Programmable Clock Mode in the Clock
1172 * Control register.
1173 */
1174 clk = SDHCI_PROG_CLOCK_MODE;
1175 real_div = div;
1176 clk_mul = host->clk_mul;
1177 div--;
1178 } else {
1179 /*
1180 * Divisor can be too small to reach clock
1181 * speed requirement. Then use the base clock.
1182 */
1183 switch_base_clk = true;
1184 }
1185 }
1186
1187 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301188 /* Version 3.00 divisors must be a multiple of 2. */
1189 if (host->max_clk <= clock)
1190 div = 1;
1191 else {
1192 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1193 div += 2) {
1194 if ((host->max_clk / div) <= clock)
1195 break;
1196 }
1197 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001198 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301199 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301200 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1201 && !div && host->max_clk <= 25000000)
1202 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001203 }
1204 } else {
1205 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001206 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001207 if ((host->max_clk / div) <= clock)
1208 break;
1209 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001210 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301211 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001212 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001213
Kevin Liu52983382013-01-31 11:31:37 +08001214clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001215 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001216 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301217 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001218 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1219 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001220 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001221 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001222
Chris Ball27f6cb12009-09-22 16:45:31 -07001223 /* Wait max 20 ms */
1224 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001225 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001226 & SDHCI_CLOCK_INT_STABLE)) {
1227 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001228 pr_err("%s: Internal clock never stabilised.\n",
1229 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001230 sdhci_dumpregs(host);
1231 return;
1232 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001233 timeout--;
1234 mdelay(1);
1235 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001236
1237 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001238 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001239}
Russell King17710592014-04-25 12:58:55 +01001240EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001241
Russell King24fbb3c2014-04-25 13:00:06 +01001242static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1243 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001244{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001245 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001246 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001247
Russell King24fbb3c2014-04-25 13:00:06 +01001248 if (mode != MMC_POWER_OFF) {
1249 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001250 case MMC_VDD_165_195:
1251 pwr = SDHCI_POWER_180;
1252 break;
1253 case MMC_VDD_29_30:
1254 case MMC_VDD_30_31:
1255 pwr = SDHCI_POWER_300;
1256 break;
1257 case MMC_VDD_32_33:
1258 case MMC_VDD_33_34:
1259 pwr = SDHCI_POWER_330;
1260 break;
1261 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001262 WARN(1, "%s: Invalid vdd %#x\n",
1263 mmc_hostname(host->mmc), vdd);
1264 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001265 }
1266 }
1267
1268 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001269 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001270
Pierre Ossmanae628902009-05-03 20:45:03 +02001271 host->pwr = pwr;
1272
1273 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001274 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001275 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1276 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001277 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001278 } else {
1279 /*
1280 * Spec says that we should clear the power reg before setting
1281 * a new value. Some controllers don't seem to like this though.
1282 */
1283 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1284 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001285
Russell Kinge921a8b2014-04-25 13:00:01 +01001286 /*
1287 * At least the Marvell CaFe chip gets confused if we set the
1288 * voltage and set turn on power at the same time, so set the
1289 * voltage first.
1290 */
1291 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1292 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001293
Russell Kinge921a8b2014-04-25 13:00:01 +01001294 pwr |= SDHCI_POWER_ON;
1295
Pierre Ossmanae628902009-05-03 20:45:03 +02001296 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1297
Russell Kinge921a8b2014-04-25 13:00:01 +01001298 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1299 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001300
Russell Kinge921a8b2014-04-25 13:00:01 +01001301 /*
1302 * Some controllers need an extra 10ms delay of 10ms before
1303 * they can apply clock after applying power
1304 */
1305 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1306 mdelay(10);
1307 }
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001308
1309 if (!IS_ERR(mmc->supply.vmmc)) {
1310 spin_unlock_irq(&host->lock);
1311 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1312 spin_lock_irq(&host->lock);
1313 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001314}
1315
Pierre Ossmand129bce2006-03-24 03:18:17 -08001316/*****************************************************************************\
1317 * *
1318 * MMC callbacks *
1319 * *
1320\*****************************************************************************/
1321
1322static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1323{
1324 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001325 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001326 unsigned long flags;
1327
1328 host = mmc_priv(mmc);
1329
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001330 sdhci_runtime_pm_get(host);
1331
Scott Branden04e079cf2015-03-10 11:35:10 -07001332 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001333 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001334
Pierre Ossmand129bce2006-03-24 03:18:17 -08001335 spin_lock_irqsave(&host->lock, flags);
1336
1337 WARN_ON(host->mrq != NULL);
1338
Pierre Ossmanf9134312008-12-21 17:01:48 +01001339#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001340 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001341#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001342
1343 /*
1344 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1345 * requests if Auto-CMD12 is enabled.
1346 */
1347 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001348 if (mrq->stop) {
1349 mrq->data->stop = NULL;
1350 mrq->stop = NULL;
1351 }
1352 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001353
1354 host->mrq = mrq;
1355
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001356 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001357 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001358 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301359 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001360 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001361 sdhci_send_command(host, mrq->sbc);
1362 else
1363 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301364 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001365
Pierre Ossman5f25a662006-10-04 02:15:39 -07001366 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001367 spin_unlock_irqrestore(&host->lock, flags);
1368}
1369
Russell King2317f562014-04-25 12:57:07 +01001370void sdhci_set_bus_width(struct sdhci_host *host, int width)
1371{
1372 u8 ctrl;
1373
1374 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1375 if (width == MMC_BUS_WIDTH_8) {
1376 ctrl &= ~SDHCI_CTRL_4BITBUS;
1377 if (host->version >= SDHCI_SPEC_300)
1378 ctrl |= SDHCI_CTRL_8BITBUS;
1379 } else {
1380 if (host->version >= SDHCI_SPEC_300)
1381 ctrl &= ~SDHCI_CTRL_8BITBUS;
1382 if (width == MMC_BUS_WIDTH_4)
1383 ctrl |= SDHCI_CTRL_4BITBUS;
1384 else
1385 ctrl &= ~SDHCI_CTRL_4BITBUS;
1386 }
1387 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1388}
1389EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1390
Russell King96d7b782014-04-25 12:59:26 +01001391void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1392{
1393 u16 ctrl_2;
1394
1395 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1396 /* Select Bus Speed Mode for host */
1397 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1398 if ((timing == MMC_TIMING_MMC_HS200) ||
1399 (timing == MMC_TIMING_UHS_SDR104))
1400 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1401 else if (timing == MMC_TIMING_UHS_SDR12)
1402 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1403 else if (timing == MMC_TIMING_UHS_SDR25)
1404 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1405 else if (timing == MMC_TIMING_UHS_SDR50)
1406 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1407 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1408 (timing == MMC_TIMING_MMC_DDR52))
1409 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001410 else if (timing == MMC_TIMING_MMC_HS400)
1411 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001412 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1413}
1414EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1415
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001416static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001417{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001418 unsigned long flags;
1419 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001420 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001421
Pierre Ossmand129bce2006-03-24 03:18:17 -08001422 spin_lock_irqsave(&host->lock, flags);
1423
Adrian Hunterceb61432011-12-27 15:48:41 +02001424 if (host->flags & SDHCI_DEVICE_DEAD) {
1425 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001426 if (!IS_ERR(mmc->supply.vmmc) &&
1427 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001428 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001429 return;
1430 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001431
Pierre Ossmand129bce2006-03-24 03:18:17 -08001432 /*
1433 * Reset the chip on each power off.
1434 * Should clear out any weird states.
1435 */
1436 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001437 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001438 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001439 }
1440
Kevin Liu52983382013-01-31 11:31:37 +08001441 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001442 (ios->power_mode == MMC_POWER_UP) &&
1443 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001444 sdhci_enable_preset_value(host, false);
1445
Russell King373073e2014-04-25 12:58:45 +01001446 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001447 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001448 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001449
1450 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1451 host->clock) {
1452 host->timeout_clk = host->mmc->actual_clock ?
1453 host->mmc->actual_clock / 1000 :
1454 host->clock / 1000;
1455 host->mmc->max_busy_timeout =
1456 host->ops->get_max_timeout_count ?
1457 host->ops->get_max_timeout_count(host) :
1458 1 << 27;
1459 host->mmc->max_busy_timeout /= host->timeout_clk;
1460 }
Russell King373073e2014-04-25 12:58:45 +01001461 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001462
Russell King24fbb3c2014-04-25 13:00:06 +01001463 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001464
Philip Rakity643a81f2010-09-23 08:24:32 -07001465 if (host->ops->platform_send_init_74_clocks)
1466 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1467
Russell King2317f562014-04-25 12:57:07 +01001468 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001469
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001470 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001471
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001472 if ((ios->timing == MMC_TIMING_SD_HS ||
1473 ios->timing == MMC_TIMING_MMC_HS)
1474 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001475 ctrl |= SDHCI_CTRL_HISPD;
1476 else
1477 ctrl &= ~SDHCI_CTRL_HISPD;
1478
Arindam Nathd6d50a12011-05-05 12:18:59 +05301479 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301480 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301481
1482 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001483 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1484 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001485 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301486 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301487 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1488 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001489 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301490 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301491
Russell Kingda91a8f2014-04-25 13:00:12 +01001492 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301493 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301494 /*
1495 * We only need to set Driver Strength if the
1496 * preset value enable is not set.
1497 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001498 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301499 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1500 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1501 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001502 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1503 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301504 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1505 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001506 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1507 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1508 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001509 pr_warn("%s: invalid driver type, default to driver type B\n",
1510 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001511 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1512 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301513
1514 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301515 } else {
1516 /*
1517 * According to SDHC Spec v3.00, if the Preset Value
1518 * Enable in the Host Control 2 register is set, we
1519 * need to reset SD Clock Enable before changing High
1520 * Speed Enable to avoid generating clock gliches.
1521 */
Arindam Nath758535c2011-05-05 12:19:00 +05301522
1523 /* Reset SD Clock Enable */
1524 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1525 clk &= ~SDHCI_CLOCK_CARD_EN;
1526 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1527
1528 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1529
1530 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001531 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301532 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301533
Arindam Nath49c468f2011-05-05 12:19:01 +05301534 /* Reset SD Clock Enable */
1535 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1536 clk &= ~SDHCI_CLOCK_CARD_EN;
1537 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1538
Russell King96d7b782014-04-25 12:59:26 +01001539 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001540 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301541
Kevin Liu52983382013-01-31 11:31:37 +08001542 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1543 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1544 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1545 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1546 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001547 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1548 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001549 u16 preset;
1550
1551 sdhci_enable_preset_value(host, true);
1552 preset = sdhci_get_preset_value(host);
1553 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1554 >> SDHCI_PRESET_DRV_SHIFT;
1555 }
1556
Arindam Nath49c468f2011-05-05 12:19:01 +05301557 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001558 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301559 } else
1560 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301561
Leandro Dorileob8352262007-07-25 23:47:04 +02001562 /*
1563 * Some (ENE) controllers go apeshit on some ios operation,
1564 * signalling timeout and CRC errors even on CMD0. Resetting
1565 * it on each ios seems to solve the problem.
1566 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301567 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001568 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001569
Pierre Ossman5f25a662006-10-04 02:15:39 -07001570 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001571 spin_unlock_irqrestore(&host->lock, flags);
1572}
1573
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001574static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1575{
1576 struct sdhci_host *host = mmc_priv(mmc);
1577
1578 sdhci_runtime_pm_get(host);
1579 sdhci_do_set_ios(host, ios);
1580 sdhci_runtime_pm_put(host);
1581}
1582
Kevin Liu94144a42013-02-28 17:35:53 +08001583static int sdhci_do_get_cd(struct sdhci_host *host)
1584{
1585 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1586
1587 if (host->flags & SDHCI_DEVICE_DEAD)
1588 return 0;
1589
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001590 /* If nonremovable, assume that the card is always present. */
1591 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
Kevin Liu94144a42013-02-28 17:35:53 +08001592 return 1;
1593
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001594 /*
1595 * Try slot gpio detect, if defined it take precedence
1596 * over build in controller functionality
1597 */
Kevin Liu94144a42013-02-28 17:35:53 +08001598 if (!IS_ERR_VALUE(gpio_cd))
1599 return !!gpio_cd;
1600
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001601 /* If polling, assume that the card is always present. */
1602 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1603 return 1;
1604
Kevin Liu94144a42013-02-28 17:35:53 +08001605 /* Host native card detect */
1606 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1607}
1608
1609static int sdhci_get_cd(struct mmc_host *mmc)
1610{
1611 struct sdhci_host *host = mmc_priv(mmc);
1612 int ret;
1613
1614 sdhci_runtime_pm_get(host);
1615 ret = sdhci_do_get_cd(host);
1616 sdhci_runtime_pm_put(host);
1617 return ret;
1618}
1619
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001620static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001621{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001622 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001623 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001624
Pierre Ossmand129bce2006-03-24 03:18:17 -08001625 spin_lock_irqsave(&host->lock, flags);
1626
Pierre Ossman1e728592008-04-16 19:13:13 +02001627 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001628 is_readonly = 0;
1629 else if (host->ops->get_ro)
1630 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001631 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001632 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1633 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001634
1635 spin_unlock_irqrestore(&host->lock, flags);
1636
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001637 /* This quirk needs to be replaced by a callback-function later */
1638 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1639 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001640}
1641
Takashi Iwai82b0e232011-04-21 20:26:38 +02001642#define SAMPLE_COUNT 5
1643
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001644static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001645{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001646 int i, ro_count;
1647
Takashi Iwai82b0e232011-04-21 20:26:38 +02001648 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001649 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001650
1651 ro_count = 0;
1652 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001653 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001654 if (++ro_count > SAMPLE_COUNT / 2)
1655 return 1;
1656 }
1657 msleep(30);
1658 }
1659 return 0;
1660}
1661
Adrian Hunter20758b62011-08-29 16:42:12 +03001662static void sdhci_hw_reset(struct mmc_host *mmc)
1663{
1664 struct sdhci_host *host = mmc_priv(mmc);
1665
1666 if (host->ops && host->ops->hw_reset)
1667 host->ops->hw_reset(host);
1668}
1669
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001670static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001671{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001672 struct sdhci_host *host = mmc_priv(mmc);
1673 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001674
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001675 sdhci_runtime_pm_get(host);
1676 ret = sdhci_do_get_ro(host);
1677 sdhci_runtime_pm_put(host);
1678 return ret;
1679}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001680
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001681static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1682{
Russell Kingbe138552014-04-25 12:55:56 +01001683 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001684 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001685 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001686 else
Russell Kingb537f942014-04-25 12:56:01 +01001687 host->ier &= ~SDHCI_INT_CARD_INT;
1688
1689 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1690 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001691 mmiowb();
1692 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001693}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001694
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001695static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1696{
1697 struct sdhci_host *host = mmc_priv(mmc);
1698 unsigned long flags;
1699
Russell Kingef104332014-04-25 12:55:41 +01001700 sdhci_runtime_pm_get(host);
1701
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001702 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001703 if (enable)
1704 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1705 else
1706 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1707
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001708 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001709 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001710
1711 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001712}
1713
Philip Rakity6231f3d2012-07-23 15:56:23 -07001714static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001715 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001716{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001717 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001718 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001719 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001720
1721 /*
1722 * Signal Voltage Switching is only applicable for Host Controllers
1723 * v3.00 and above.
1724 */
1725 if (host->version < SDHCI_SPEC_300)
1726 return 0;
1727
Philip Rakity6231f3d2012-07-23 15:56:23 -07001728 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001729
Fabio Estevam21f59982013-02-14 10:35:03 -02001730 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001731 case MMC_SIGNAL_VOLTAGE_330:
1732 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1733 ctrl &= ~SDHCI_CTRL_VDD_180;
1734 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1735
Tim Kryger3a48edc2014-06-13 10:13:56 -07001736 if (!IS_ERR(mmc->supply.vqmmc)) {
1737 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1738 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001739 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001740 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1741 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001742 return -EIO;
1743 }
1744 }
1745 /* Wait for 5ms */
1746 usleep_range(5000, 5500);
1747
1748 /* 3.3V regulator output should be stable within 5 ms */
1749 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1750 if (!(ctrl & SDHCI_CTRL_VDD_180))
1751 return 0;
1752
Joe Perches66061102014-09-12 14:56:56 -07001753 pr_warn("%s: 3.3V regulator output did not became stable\n",
1754 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001755
1756 return -EAGAIN;
1757 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001758 if (!IS_ERR(mmc->supply.vqmmc)) {
1759 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001760 1700000, 1950000);
1761 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001762 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1763 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001764 return -EIO;
1765 }
1766 }
1767
1768 /*
1769 * Enable 1.8V Signal Enable in the Host Control2
1770 * register
1771 */
1772 ctrl |= SDHCI_CTRL_VDD_180;
1773 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1774
Vincent Yang9d967a62015-01-20 16:05:15 +08001775 /* Some controller need to do more when switching */
1776 if (host->ops->voltage_switch)
1777 host->ops->voltage_switch(host);
1778
Kevin Liu20b92a32012-12-17 19:29:26 +08001779 /* 1.8V regulator output should be stable within 5 ms */
1780 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1781 if (ctrl & SDHCI_CTRL_VDD_180)
1782 return 0;
1783
Joe Perches66061102014-09-12 14:56:56 -07001784 pr_warn("%s: 1.8V regulator output did not became stable\n",
1785 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001786
1787 return -EAGAIN;
1788 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001789 if (!IS_ERR(mmc->supply.vqmmc)) {
1790 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1791 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001792 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001793 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1794 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001795 return -EIO;
1796 }
1797 }
1798 return 0;
1799 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301800 /* No signal voltage switch required */
1801 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001802 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301803}
1804
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001805static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001806 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001807{
1808 struct sdhci_host *host = mmc_priv(mmc);
1809 int err;
1810
1811 if (host->version < SDHCI_SPEC_300)
1812 return 0;
1813 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001814 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001815 sdhci_runtime_pm_put(host);
1816 return err;
1817}
1818
Kevin Liu20b92a32012-12-17 19:29:26 +08001819static int sdhci_card_busy(struct mmc_host *mmc)
1820{
1821 struct sdhci_host *host = mmc_priv(mmc);
1822 u32 present_state;
1823
1824 sdhci_runtime_pm_get(host);
1825 /* Check whether DAT[3:0] is 0000 */
1826 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1827 sdhci_runtime_pm_put(host);
1828
1829 return !(present_state & SDHCI_DATA_LVL_MASK);
1830}
1831
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001832static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1833{
1834 struct sdhci_host *host = mmc_priv(mmc);
1835 unsigned long flags;
1836
1837 spin_lock_irqsave(&host->lock, flags);
1838 host->flags |= SDHCI_HS400_TUNING;
1839 spin_unlock_irqrestore(&host->lock, flags);
1840
1841 return 0;
1842}
1843
Girish K S069c9f12012-01-06 09:56:39 +05301844static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301845{
Russell King4b6f37d2014-04-25 12:59:36 +01001846 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301847 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301848 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301849 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001850 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001851 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001852 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301853
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001854 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001855 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301856
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001857 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1858 host->flags &= ~SDHCI_HS400_TUNING;
1859
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001860 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1861 tuning_count = host->tuning_count;
1862
Arindam Nathb513ea22011-05-05 12:19:04 +05301863 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001864 * The Host Controller needs tuning in case of SDR104 and DDR50
1865 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1866 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301867 * If the Host Controller supports the HS200 mode then the
1868 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301869 */
Russell King4b6f37d2014-04-25 12:59:36 +01001870 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001871 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001872 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001873 err = -EINVAL;
1874 goto out_unlock;
1875
Russell King4b6f37d2014-04-25 12:59:36 +01001876 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001877 /*
1878 * Periodic re-tuning for HS400 is not expected to be needed, so
1879 * disable it here.
1880 */
1881 if (hs400_tuning)
1882 tuning_count = 0;
1883 break;
1884
Russell King4b6f37d2014-04-25 12:59:36 +01001885 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001886 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001887 break;
Girish K S069c9f12012-01-06 09:56:39 +05301888
Russell King4b6f37d2014-04-25 12:59:36 +01001889 case MMC_TIMING_UHS_SDR50:
1890 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1891 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1892 break;
1893 /* FALLTHROUGH */
1894
1895 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001896 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301897 }
1898
Dong Aisheng45251812013-09-13 19:11:30 +08001899 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001900 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001901 err = host->ops->platform_execute_tuning(host, opcode);
1902 sdhci_runtime_pm_put(host);
1903 return err;
1904 }
1905
Russell King4b6f37d2014-04-25 12:59:36 +01001906 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1907 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001908 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1909 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301910 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1911
1912 /*
1913 * As per the Host Controller spec v3.00, tuning command
1914 * generates Buffer Read Ready interrupt, so enable that.
1915 *
1916 * Note: The spec clearly says that when tuning sequence
1917 * is being performed, the controller does not generate
1918 * interrupts other than Buffer Read Ready interrupt. But
1919 * to make sure we don't hit a controller bug, we _only_
1920 * enable Buffer Read Ready interrupt here.
1921 */
Russell Kingb537f942014-04-25 12:56:01 +01001922 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1923 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301924
1925 /*
1926 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1927 * of loops reaches 40 times or a timeout of 150ms occurs.
1928 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301929 do {
1930 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001931 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301932
Girish K S069c9f12012-01-06 09:56:39 +05301933 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301934 cmd.arg = 0;
1935 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1936 cmd.retries = 0;
1937 cmd.data = NULL;
1938 cmd.error = 0;
1939
Al Cooper7ce45e92014-05-09 11:34:07 -04001940 if (tuning_loop_counter-- == 0)
1941 break;
1942
Arindam Nathb513ea22011-05-05 12:19:04 +05301943 mrq.cmd = &cmd;
1944 host->mrq = &mrq;
1945
1946 /*
1947 * In response to CMD19, the card sends 64 bytes of tuning
1948 * block to the Host Controller. So we set the block size
1949 * to 64 here.
1950 */
Girish K S069c9f12012-01-06 09:56:39 +05301951 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1952 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1953 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1954 SDHCI_BLOCK_SIZE);
1955 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1956 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1957 SDHCI_BLOCK_SIZE);
1958 } else {
1959 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1960 SDHCI_BLOCK_SIZE);
1961 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301962
1963 /*
1964 * The tuning block is sent by the card to the host controller.
1965 * So we set the TRNS_READ bit in the Transfer Mode register.
1966 * This also takes care of setting DMA Enable and Multi Block
1967 * Select in the same register to 0.
1968 */
1969 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1970
1971 sdhci_send_command(host, &cmd);
1972
1973 host->cmd = NULL;
1974 host->mrq = NULL;
1975
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001976 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301977 /* Wait for Buffer Read Ready interrupt */
1978 wait_event_interruptible_timeout(host->buf_ready_int,
1979 (host->tuning_done == 1),
1980 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001981 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301982
1983 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001984 pr_info(DRIVER_NAME ": Timeout waiting for Buffer Read Ready interrupt during tuning procedure, falling back to fixed sampling clock\n");
Arindam Nathb513ea22011-05-05 12:19:04 +05301985 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1986 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1987 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1988 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1989
1990 err = -EIO;
1991 goto out;
1992 }
1993
1994 host->tuning_done = 0;
1995
1996 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07001997
1998 /* eMMC spec does not require a delay between tuning cycles */
1999 if (opcode == MMC_SEND_TUNING_BLOCK)
2000 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302001 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2002
2003 /*
2004 * The Host Driver has exhausted the maximum number of loops allowed,
2005 * so use fixed sampling frequency.
2006 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002007 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302008 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2009 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002010 }
2011 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002012 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002013 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302014 }
2015
2016out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002017 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002018 /*
2019 * In case tuning fails, host controllers which support
2020 * re-tuning can try tuning again at a later time, when the
2021 * re-tuning timer expires. So for these controllers, we
2022 * return 0. Since there might be other controllers who do not
2023 * have this capability, we return error for them.
2024 */
2025 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302026 }
2027
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002028 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302029
Russell Kingb537f942014-04-25 12:56:01 +01002030 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2031 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002032out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002033 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002034 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302035
2036 return err;
2037}
2038
Adrian Huntercb849642015-02-06 14:12:59 +02002039static int sdhci_select_drive_strength(struct mmc_card *card,
2040 unsigned int max_dtr, int host_drv,
2041 int card_drv, int *drv_type)
2042{
2043 struct sdhci_host *host = mmc_priv(card->host);
2044
2045 if (!host->ops->select_drive_strength)
2046 return 0;
2047
2048 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2049 card_drv, drv_type);
2050}
Kevin Liu52983382013-01-31 11:31:37 +08002051
2052static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302053{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302054 /* Host Controller v3.00 defines preset value registers */
2055 if (host->version < SDHCI_SPEC_300)
2056 return;
2057
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302058 /*
2059 * We only enable or disable Preset Value if they are not already
2060 * enabled or disabled respectively. Otherwise, we bail out.
2061 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002062 if (host->preset_enabled != enable) {
2063 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2064
2065 if (enable)
2066 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2067 else
2068 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2069
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302070 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002071
2072 if (enable)
2073 host->flags |= SDHCI_PV_ENABLED;
2074 else
2075 host->flags &= ~SDHCI_PV_ENABLED;
2076
2077 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302078 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002079}
2080
Haibo Chen348487c2014-12-09 17:04:05 +08002081static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2082 int err)
2083{
2084 struct sdhci_host *host = mmc_priv(mmc);
2085 struct mmc_data *data = mrq->data;
2086
2087 if (host->flags & SDHCI_REQ_USE_DMA) {
Haibo Chend31911b2015-08-25 10:02:11 +08002088 if (data->host_cookie == COOKIE_GIVEN ||
2089 data->host_cookie == COOKIE_MAPPED)
Haibo Chen348487c2014-12-09 17:04:05 +08002090 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2091 data->flags & MMC_DATA_WRITE ?
2092 DMA_TO_DEVICE : DMA_FROM_DEVICE);
Haibo Chend31911b2015-08-25 10:02:11 +08002093 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002094 }
2095}
2096
2097static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Haibo Chend31911b2015-08-25 10:02:11 +08002098 struct mmc_data *data)
Haibo Chen348487c2014-12-09 17:04:05 +08002099{
2100 int sg_count;
2101
Haibo Chend31911b2015-08-25 10:02:11 +08002102 if (data->host_cookie == COOKIE_MAPPED) {
2103 data->host_cookie = COOKIE_GIVEN;
2104 return data->sg_count;
Haibo Chen348487c2014-12-09 17:04:05 +08002105 }
2106
Haibo Chend31911b2015-08-25 10:02:11 +08002107 WARN_ON(data->host_cookie == COOKIE_GIVEN);
Haibo Chen348487c2014-12-09 17:04:05 +08002108
Haibo Chend31911b2015-08-25 10:02:11 +08002109 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2110 data->flags & MMC_DATA_WRITE ?
2111 DMA_TO_DEVICE : DMA_FROM_DEVICE);
Haibo Chen348487c2014-12-09 17:04:05 +08002112
2113 if (sg_count == 0)
Haibo Chend31911b2015-08-25 10:02:11 +08002114 return -ENOSPC;
Haibo Chen348487c2014-12-09 17:04:05 +08002115
Haibo Chend31911b2015-08-25 10:02:11 +08002116 data->sg_count = sg_count;
2117 data->host_cookie = COOKIE_MAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002118
2119 return sg_count;
2120}
2121
2122static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2123 bool is_first_req)
2124{
2125 struct sdhci_host *host = mmc_priv(mmc);
2126
Haibo Chend31911b2015-08-25 10:02:11 +08002127 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002128
2129 if (host->flags & SDHCI_REQ_USE_DMA)
Haibo Chend31911b2015-08-25 10:02:11 +08002130 sdhci_pre_dma_transfer(host, mrq->data);
Haibo Chen348487c2014-12-09 17:04:05 +08002131}
2132
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002133static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002134{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002135 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002136 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002137 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002138
Christian Daudt722e1282013-06-20 14:26:36 -07002139 /* First check if client has provided their own card event */
2140 if (host->ops->card_event)
2141 host->ops->card_event(host);
2142
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002143 present = sdhci_do_get_cd(host);
2144
Pierre Ossmand129bce2006-03-24 03:18:17 -08002145 spin_lock_irqsave(&host->lock, flags);
2146
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002147 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002148 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302149 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002150 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302151 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002152 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002153
Russell King03231f92014-04-25 12:57:12 +01002154 sdhci_do_reset(host, SDHCI_RESET_CMD);
2155 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002156
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002157 host->mrq->cmd->error = -ENOMEDIUM;
2158 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002159 }
2160
2161 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002162}
2163
2164static const struct mmc_host_ops sdhci_ops = {
2165 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002166 .post_req = sdhci_post_req,
2167 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002168 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002169 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002170 .get_ro = sdhci_get_ro,
2171 .hw_reset = sdhci_hw_reset,
2172 .enable_sdio_irq = sdhci_enable_sdio_irq,
2173 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002174 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002175 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002176 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002177 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002178 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002179};
2180
2181/*****************************************************************************\
2182 * *
2183 * Tasklets *
2184 * *
2185\*****************************************************************************/
2186
Pierre Ossmand129bce2006-03-24 03:18:17 -08002187static void sdhci_tasklet_finish(unsigned long param)
2188{
2189 struct sdhci_host *host;
2190 unsigned long flags;
2191 struct mmc_request *mrq;
2192
2193 host = (struct sdhci_host*)param;
2194
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002195 spin_lock_irqsave(&host->lock, flags);
2196
Chris Ball0c9c99a2011-04-27 17:35:31 -04002197 /*
2198 * If this tasklet gets rescheduled while running, it will
2199 * be run again afterwards but without any active request.
2200 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002201 if (!host->mrq) {
2202 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002203 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002204 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002205
2206 del_timer(&host->timer);
2207
2208 mrq = host->mrq;
2209
Pierre Ossmand129bce2006-03-24 03:18:17 -08002210 /*
Russell King054cedf2016-01-26 13:40:42 +00002211 * Always unmap the data buffers if they were mapped by
2212 * sdhci_prepare_data() whenever we finish with a request.
2213 * This avoids leaking DMA mappings on error.
2214 */
2215 if (host->flags & SDHCI_REQ_USE_DMA) {
2216 struct mmc_data *data = mrq->data;
2217
2218 if (data && data->host_cookie == COOKIE_MAPPED) {
2219 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2220 (data->flags & MMC_DATA_READ) ?
2221 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2222 data->host_cookie = COOKIE_UNMAPPED;
2223 }
2224 }
2225
2226 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002227 * The controller needs a reset of internal state machines
2228 * upon error conditions.
2229 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002230 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002231 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002232 (mrq->sbc && mrq->sbc->error) ||
2233 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2234 (mrq->data->stop && mrq->data->stop->error))) ||
2235 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002236
2237 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002238 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002239 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002240 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002241
2242 /* Spec says we should do both at the same time, but Ricoh
2243 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002244 sdhci_do_reset(host, SDHCI_RESET_CMD);
2245 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002246 }
2247
2248 host->mrq = NULL;
2249 host->cmd = NULL;
2250 host->data = NULL;
2251
Pierre Ossmanf9134312008-12-21 17:01:48 +01002252#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002253 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002254#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002255
Pierre Ossman5f25a662006-10-04 02:15:39 -07002256 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002257 spin_unlock_irqrestore(&host->lock, flags);
2258
2259 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002260 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002261}
2262
2263static void sdhci_timeout_timer(unsigned long data)
2264{
2265 struct sdhci_host *host;
2266 unsigned long flags;
2267
2268 host = (struct sdhci_host*)data;
2269
2270 spin_lock_irqsave(&host->lock, flags);
2271
2272 if (host->mrq) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002273 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2274 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002275 sdhci_dumpregs(host);
2276
2277 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002278 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002279 sdhci_finish_data(host);
2280 } else {
2281 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002282 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002283 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002284 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002285
2286 tasklet_schedule(&host->finish_tasklet);
2287 }
2288 }
2289
Pierre Ossman5f25a662006-10-04 02:15:39 -07002290 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002291 spin_unlock_irqrestore(&host->lock, flags);
2292}
2293
2294/*****************************************************************************\
2295 * *
2296 * Interrupt handling *
2297 * *
2298\*****************************************************************************/
2299
Adrian Hunter61541392014-09-24 10:27:27 +03002300static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002301{
2302 BUG_ON(intmask == 0);
2303
2304 if (!host->cmd) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002305 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2306 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002307 sdhci_dumpregs(host);
2308 return;
2309 }
2310
Russell Kingec014cb2016-01-26 13:39:39 +00002311 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2312 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2313 if (intmask & SDHCI_INT_TIMEOUT)
2314 host->cmd->error = -ETIMEDOUT;
2315 else
2316 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002317
Russell King71fcbda2016-01-26 13:39:45 +00002318 /*
2319 * If this command initiates a data phase and a response
2320 * CRC error is signalled, the card can start transferring
2321 * data - the card may have received the command without
2322 * error. We must not terminate the mmc_request early.
2323 *
2324 * If the card did not receive the command or returned an
2325 * error which prevented it sending data, the data phase
2326 * will time out.
2327 */
2328 if (host->cmd->data &&
2329 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2330 SDHCI_INT_CRC) {
2331 host->cmd = NULL;
2332 return;
2333 }
2334
Pierre Ossmand129bce2006-03-24 03:18:17 -08002335 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002336 return;
2337 }
2338
2339 /*
2340 * The host can send and interrupt when the busy state has
2341 * ended, allowing us to wait without wasting CPU cycles.
2342 * Unfortunately this is overloaded on the "data complete"
2343 * interrupt, so we need to take some care when handling
2344 * it.
2345 *
2346 * Note: The 1.0 specification is a bit ambiguous about this
2347 * feature so there might be some problems with older
2348 * controllers.
2349 */
2350 if (host->cmd->flags & MMC_RSP_BUSY) {
2351 if (host->cmd->data)
Marek Vasut2e4456f2015-11-18 10:47:02 +01002352 DBG("Cannot wait for busy signal when also doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002353 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2354 && !host->busy_handle) {
2355 /* Mark that command complete before busy is ended */
2356 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002357 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002358 }
Ben Dooksf9454052009-02-20 20:33:08 +03002359
2360 /* The controller does not support the end-of-busy IRQ,
2361 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002362 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2363 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2364 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002365 }
2366
2367 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002368 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002369}
2370
George G. Davis0957c332010-02-18 12:32:12 -05002371#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002372static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002373{
2374 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002375 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002376
2377 sdhci_dumpregs(host);
2378
2379 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002380 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002381
Adrian Huntere57a5f62014-11-04 12:42:46 +02002382 if (host->flags & SDHCI_USE_64_BIT_DMA)
2383 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2384 name, desc, le32_to_cpu(dma_desc->addr_hi),
2385 le32_to_cpu(dma_desc->addr_lo),
2386 le16_to_cpu(dma_desc->len),
2387 le16_to_cpu(dma_desc->cmd));
2388 else
2389 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2390 name, desc, le32_to_cpu(dma_desc->addr_lo),
2391 le16_to_cpu(dma_desc->len),
2392 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002393
Adrian Hunter76fe3792014-11-04 12:42:42 +02002394 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002395
Adrian Hunter05452302014-11-04 12:42:45 +02002396 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002397 break;
2398 }
2399}
2400#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002401static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002402#endif
2403
Pierre Ossmand129bce2006-03-24 03:18:17 -08002404static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2405{
Girish K S069c9f12012-01-06 09:56:39 +05302406 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002407 BUG_ON(intmask == 0);
2408
Arindam Nathb513ea22011-05-05 12:19:04 +05302409 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2410 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302411 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2412 if (command == MMC_SEND_TUNING_BLOCK ||
2413 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302414 host->tuning_done = 1;
2415 wake_up(&host->buf_ready_int);
2416 return;
2417 }
2418 }
2419
Pierre Ossmand129bce2006-03-24 03:18:17 -08002420 if (!host->data) {
2421 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002422 * The "data complete" interrupt is also used to
2423 * indicate that a busy state has ended. See comment
2424 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002425 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002426 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002427 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2428 host->cmd->error = -ETIMEDOUT;
2429 tasklet_schedule(&host->finish_tasklet);
2430 return;
2431 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002432 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002433 /*
2434 * Some cards handle busy-end interrupt
2435 * before the command completed, so make
2436 * sure we do things in the proper order.
2437 */
2438 if (host->busy_handle)
2439 sdhci_finish_command(host);
2440 else
2441 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002442 return;
2443 }
2444 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002445
Marek Vasut2e4456f2015-11-18 10:47:02 +01002446 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2447 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002448 sdhci_dumpregs(host);
2449
2450 return;
2451 }
2452
2453 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002454 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002455 else if (intmask & SDHCI_INT_DATA_END_BIT)
2456 host->data->error = -EILSEQ;
2457 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2458 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2459 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002460 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002461 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302462 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002463 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002464 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002465 if (host->ops->adma_workaround)
2466 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002467 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002468
Pierre Ossman17b04292007-07-22 22:18:46 +02002469 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002470 sdhci_finish_data(host);
2471 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002472 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002473 sdhci_transfer_pio(host);
2474
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002475 /*
2476 * We currently don't do anything fancy with DMA
2477 * boundaries, but as we can't disable the feature
2478 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002479 *
2480 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2481 * should return a valid address to continue from, but as
2482 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002483 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002484 if (intmask & SDHCI_INT_DMA_END) {
2485 u32 dmastart, dmanow;
2486 dmastart = sg_dma_address(host->data->sg);
2487 dmanow = dmastart + host->data->bytes_xfered;
2488 /*
2489 * Force update to the next DMA block boundary.
2490 */
2491 dmanow = (dmanow &
2492 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2493 SDHCI_DEFAULT_BOUNDARY_SIZE;
2494 host->data->bytes_xfered = dmanow - dmastart;
2495 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2496 " next 0x%08x\n",
2497 mmc_hostname(host->mmc), dmastart,
2498 host->data->bytes_xfered, dmanow);
2499 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2500 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002501
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002502 if (intmask & SDHCI_INT_DATA_END) {
2503 if (host->cmd) {
2504 /*
2505 * Data managed to finish before the
2506 * command completed. Make sure we do
2507 * things in the proper order.
2508 */
2509 host->data_early = 1;
2510 } else {
2511 sdhci_finish_data(host);
2512 }
2513 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002514 }
2515}
2516
David Howells7d12e782006-10-05 14:55:46 +01002517static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002518{
Russell King781e9892014-04-25 12:55:46 +01002519 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002520 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002521 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002522 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002523
2524 spin_lock(&host->lock);
2525
Russell Kingbe138552014-04-25 12:55:56 +01002526 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002527 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002528 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002529 }
2530
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002531 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002532 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002533 result = IRQ_NONE;
2534 goto out;
2535 }
2536
Russell King41005002014-04-25 12:55:36 +01002537 do {
2538 /* Clear selected interrupts. */
2539 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2540 SDHCI_INT_BUS_POWER);
2541 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002542
Russell King41005002014-04-25 12:55:36 +01002543 DBG("*** %s got interrupt: 0x%08x\n",
2544 mmc_hostname(host->mmc), intmask);
2545
2546 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2547 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2548 SDHCI_CARD_PRESENT;
2549
2550 /*
2551 * There is a observation on i.mx esdhc. INSERT
2552 * bit will be immediately set again when it gets
2553 * cleared, if a card is inserted. We have to mask
2554 * the irq to prevent interrupt storm which will
2555 * freeze the system. And the REMOVE gets the
2556 * same situation.
2557 *
2558 * More testing are needed here to ensure it works
2559 * for other platforms though.
2560 */
Russell Kingb537f942014-04-25 12:56:01 +01002561 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2562 SDHCI_INT_CARD_REMOVE);
2563 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2564 SDHCI_INT_CARD_INSERT;
2565 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2566 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002567
2568 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2569 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002570
2571 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2572 SDHCI_INT_CARD_REMOVE);
2573 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002574 }
2575
2576 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002577 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2578 &intmask);
Russell King41005002014-04-25 12:55:36 +01002579
2580 if (intmask & SDHCI_INT_DATA_MASK)
2581 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2582
2583 if (intmask & SDHCI_INT_BUS_POWER)
2584 pr_err("%s: Card is consuming too much power!\n",
2585 mmc_hostname(host->mmc));
2586
Russell King781e9892014-04-25 12:55:46 +01002587 if (intmask & SDHCI_INT_CARD_INT) {
2588 sdhci_enable_sdio_irq_nolock(host, false);
2589 host->thread_isr |= SDHCI_INT_CARD_INT;
2590 result = IRQ_WAKE_THREAD;
2591 }
Russell King41005002014-04-25 12:55:36 +01002592
2593 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2594 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2595 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2596 SDHCI_INT_CARD_INT);
2597
2598 if (intmask) {
2599 unexpected |= intmask;
2600 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2601 }
2602
Russell King781e9892014-04-25 12:55:46 +01002603 if (result == IRQ_NONE)
2604 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002605
2606 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002607 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002608out:
2609 spin_unlock(&host->lock);
2610
Alexander Stein6379b232012-03-14 09:52:10 +01002611 if (unexpected) {
2612 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2613 mmc_hostname(host->mmc), unexpected);
2614 sdhci_dumpregs(host);
2615 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002616
Pierre Ossmand129bce2006-03-24 03:18:17 -08002617 return result;
2618}
2619
Russell King781e9892014-04-25 12:55:46 +01002620static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2621{
2622 struct sdhci_host *host = dev_id;
2623 unsigned long flags;
2624 u32 isr;
2625
2626 spin_lock_irqsave(&host->lock, flags);
2627 isr = host->thread_isr;
2628 host->thread_isr = 0;
2629 spin_unlock_irqrestore(&host->lock, flags);
2630
Russell King3560db82014-04-25 12:55:51 +01002631 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2632 sdhci_card_event(host->mmc);
2633 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2634 }
2635
Russell King781e9892014-04-25 12:55:46 +01002636 if (isr & SDHCI_INT_CARD_INT) {
2637 sdio_run_irqs(host->mmc);
2638
2639 spin_lock_irqsave(&host->lock, flags);
2640 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2641 sdhci_enable_sdio_irq_nolock(host, true);
2642 spin_unlock_irqrestore(&host->lock, flags);
2643 }
2644
2645 return isr ? IRQ_HANDLED : IRQ_NONE;
2646}
2647
Pierre Ossmand129bce2006-03-24 03:18:17 -08002648/*****************************************************************************\
2649 * *
2650 * Suspend/resume *
2651 * *
2652\*****************************************************************************/
2653
2654#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002655void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2656{
2657 u8 val;
2658 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2659 | SDHCI_WAKE_ON_INT;
2660
2661 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2662 val |= mask ;
2663 /* Avoid fake wake up */
2664 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2665 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2666 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2667}
2668EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2669
Fabio Estevam0b10f472014-08-30 14:53:13 -03002670static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002671{
2672 u8 val;
2673 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2674 | SDHCI_WAKE_ON_INT;
2675
2676 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2677 val &= ~mask;
2678 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2679}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002680
Manuel Lauss29495aa2011-11-03 11:09:45 +01002681int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002682{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002683 sdhci_disable_card_detection(host);
2684
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002685 mmc_retune_timer_stop(host->mmc);
2686 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302687
Kevin Liuad080d72013-01-05 17:21:33 +08002688 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002689 host->ier = 0;
2690 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2691 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002692 free_irq(host->irq, host);
2693 } else {
2694 sdhci_enable_irq_wakeups(host);
2695 enable_irq_wake(host->irq);
2696 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002697 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002698}
2699
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002700EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002701
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002702int sdhci_resume_host(struct sdhci_host *host)
2703{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002704 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002705
Richard Röjforsa13abc72009-09-22 16:45:30 -07002706 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002707 if (host->ops->enable_dma)
2708 host->ops->enable_dma(host);
2709 }
2710
Adrian Hunter6308d292012-02-07 14:48:54 +02002711 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2712 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2713 /* Card keeps power but host controller does not */
2714 sdhci_init(host, 0);
2715 host->pwr = 0;
2716 host->clock = 0;
2717 sdhci_do_set_ios(host, &host->mmc->ios);
2718 } else {
2719 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2720 mmiowb();
2721 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002722
Haibo Chen14a7b41642015-09-15 18:32:58 +08002723 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2724 ret = request_threaded_irq(host->irq, sdhci_irq,
2725 sdhci_thread_irq, IRQF_SHARED,
2726 mmc_hostname(host->mmc), host);
2727 if (ret)
2728 return ret;
2729 } else {
2730 sdhci_disable_irq_wakeups(host);
2731 disable_irq_wake(host->irq);
2732 }
2733
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002734 sdhci_enable_card_detection(host);
2735
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002736 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002737}
2738
2739EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002740
2741static int sdhci_runtime_pm_get(struct sdhci_host *host)
2742{
2743 return pm_runtime_get_sync(host->mmc->parent);
2744}
2745
2746static int sdhci_runtime_pm_put(struct sdhci_host *host)
2747{
2748 pm_runtime_mark_last_busy(host->mmc->parent);
2749 return pm_runtime_put_autosuspend(host->mmc->parent);
2750}
2751
Adrian Hunterf0710a52013-05-06 12:17:32 +03002752static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2753{
Adrian Hunter5c671c42015-11-26 14:00:50 +02002754 if (host->bus_on)
Adrian Hunterf0710a52013-05-06 12:17:32 +03002755 return;
2756 host->bus_on = true;
2757 pm_runtime_get_noresume(host->mmc->parent);
2758}
2759
2760static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2761{
Adrian Hunter5c671c42015-11-26 14:00:50 +02002762 if (!host->bus_on)
Adrian Hunterf0710a52013-05-06 12:17:32 +03002763 return;
2764 host->bus_on = false;
2765 pm_runtime_put_noidle(host->mmc->parent);
2766}
2767
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002768int sdhci_runtime_suspend_host(struct sdhci_host *host)
2769{
2770 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002771
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002772 mmc_retune_timer_stop(host->mmc);
2773 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002774
2775 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002776 host->ier &= SDHCI_INT_CARD_INT;
2777 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2778 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002779 spin_unlock_irqrestore(&host->lock, flags);
2780
Russell King781e9892014-04-25 12:55:46 +01002781 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002782
2783 spin_lock_irqsave(&host->lock, flags);
2784 host->runtime_suspended = true;
2785 spin_unlock_irqrestore(&host->lock, flags);
2786
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002787 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002788}
2789EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2790
2791int sdhci_runtime_resume_host(struct sdhci_host *host)
2792{
2793 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002794 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002795
2796 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2797 if (host->ops->enable_dma)
2798 host->ops->enable_dma(host);
2799 }
2800
2801 sdhci_init(host, 0);
2802
2803 /* Force clock and power re-program */
2804 host->pwr = 0;
2805 host->clock = 0;
Jisheng Zhang3396e732015-01-29 17:42:12 +08002806 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002807 sdhci_do_set_ios(host, &host->mmc->ios);
2808
Kevin Liu52983382013-01-31 11:31:37 +08002809 if ((host_flags & SDHCI_PV_ENABLED) &&
2810 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2811 spin_lock_irqsave(&host->lock, flags);
2812 sdhci_enable_preset_value(host, true);
2813 spin_unlock_irqrestore(&host->lock, flags);
2814 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002815
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002816 spin_lock_irqsave(&host->lock, flags);
2817
2818 host->runtime_suspended = false;
2819
2820 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002821 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002822 sdhci_enable_sdio_irq_nolock(host, true);
2823
2824 /* Enable Card Detection */
2825 sdhci_enable_card_detection(host);
2826
2827 spin_unlock_irqrestore(&host->lock, flags);
2828
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002829 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002830}
2831EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2832
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002833#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002834
Pierre Ossmand129bce2006-03-24 03:18:17 -08002835/*****************************************************************************\
2836 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002837 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002838 * *
2839\*****************************************************************************/
2840
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002841struct sdhci_host *sdhci_alloc_host(struct device *dev,
2842 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002843{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002844 struct mmc_host *mmc;
2845 struct sdhci_host *host;
2846
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002847 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002848
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002849 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002850 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002851 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002852
2853 host = mmc_priv(mmc);
2854 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002855 host->mmc_host_ops = sdhci_ops;
2856 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002857
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002858 return host;
2859}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002860
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002861EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002862
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002863int sdhci_add_host(struct sdhci_host *host)
2864{
2865 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002866 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302867 u32 max_current_caps;
2868 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002869 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08002870 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002871 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002872
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002873 WARN_ON(host == NULL);
2874 if (host == NULL)
2875 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002876
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002877 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002878
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002879 if (debug_quirks)
2880 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002881 if (debug_quirks2)
2882 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002883
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002884 override_timeout_clk = host->timeout_clk;
2885
Russell King03231f92014-04-25 12:57:12 +01002886 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002887
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002888 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002889 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2890 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002891 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002892 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
2893 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002894 }
2895
Arindam Nathf2119df2011-05-05 12:18:57 +05302896 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002897 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002898
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002899 if (host->version >= SDHCI_SPEC_300)
2900 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2901 host->caps1 :
2902 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302903
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002904 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002905 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302906 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002907 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002908 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002909 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002910
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002911 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002912 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002913 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002914 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002915 }
2916
Arindam Nathf2119df2011-05-05 12:18:57 +05302917 if ((host->version >= SDHCI_SPEC_200) &&
2918 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002919 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002920
2921 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2922 (host->flags & SDHCI_USE_ADMA)) {
2923 DBG("Disabling ADMA as it is marked broken\n");
2924 host->flags &= ~SDHCI_USE_ADMA;
2925 }
2926
Adrian Huntere57a5f62014-11-04 12:42:46 +02002927 /*
2928 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2929 * and *must* do 64-bit DMA. A driver has the opportunity to change
2930 * that during the first call to ->enable_dma(). Similarly
2931 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2932 * implement.
2933 */
Al Cooper5eaa7472016-02-10 15:25:39 -05002934 if (caps[0] & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02002935 host->flags |= SDHCI_USE_64_BIT_DMA;
2936
Richard Röjforsa13abc72009-09-22 16:45:30 -07002937 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002938 if (host->ops->enable_dma) {
2939 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07002940 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002941 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002942 host->flags &=
2943 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002944 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002945 }
2946 }
2947
Adrian Huntere57a5f62014-11-04 12:42:46 +02002948 /* SDMA does not support 64-bit DMA */
2949 if (host->flags & SDHCI_USE_64_BIT_DMA)
2950 host->flags &= ~SDHCI_USE_SDMA;
2951
Pierre Ossman2134a922008-06-28 18:28:51 +02002952 if (host->flags & SDHCI_USE_ADMA) {
2953 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002954 * The DMA descriptor table size is calculated as the maximum
2955 * number of segments times 2, to allow for an alignment
2956 * descriptor for each segment, plus 1 for a nop end descriptor,
2957 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002958 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02002959 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2960 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2961 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002962 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002963 } else {
2964 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2965 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002966 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002967 }
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002968 host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +02002969 host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002970 &host->adma_addr,
2971 GFP_KERNEL);
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002972 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kingedd63fc2016-01-26 13:39:50 +00002973 host->align_buffer = dma_alloc_coherent(mmc_dev(mmc),
2974 host->align_buffer_sz,
2975 &host->align_addr,
2976 GFP_KERNEL);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002977 if (!host->adma_table || !host->align_buffer) {
Peng Fan7ac02032015-06-22 11:41:23 +08002978 if (host->adma_table)
2979 dma_free_coherent(mmc_dev(mmc),
2980 host->adma_table_sz,
2981 host->adma_table,
2982 host->adma_addr);
Russell Kingedd63fc2016-01-26 13:39:50 +00002983 if (host->align_buffer)
2984 dma_free_coherent(mmc_dev(mmc),
2985 host->align_buffer_sz,
2986 host->align_buffer,
2987 host->align_addr);
Joe Perches66061102014-09-12 14:56:56 -07002988 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002989 mmc_hostname(mmc));
2990 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002991 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01002992 host->align_buffer = NULL;
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002993 } else if (host->adma_addr & (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07002994 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2995 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002996 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002997 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002998 host->adma_table, host->adma_addr);
Russell Kingedd63fc2016-01-26 13:39:50 +00002999 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz,
3000 host->align_buffer, host->align_addr);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003001 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01003002 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003003 }
Russell Kingedd63fc2016-01-26 13:39:50 +00003004
3005 /* dma_alloc_coherent returns page aligned and sized buffers */
3006 BUG_ON(host->align_addr & SDHCI_ADMA2_MASK);
Pierre Ossman2134a922008-06-28 18:28:51 +02003007 }
3008
Pierre Ossman76591502008-07-21 00:32:11 +02003009 /*
3010 * If we use DMA, then it's up to the caller to set the DMA
3011 * mask, but PIO does not need the hw shim so we set a new
3012 * mask here in that case.
3013 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003014 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003015 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003016 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003017 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003018
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003019 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05303020 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003021 >> SDHCI_CLOCK_BASE_SHIFT;
3022 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303023 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003024 >> SDHCI_CLOCK_BASE_SHIFT;
3025
Pierre Ossmand129bce2006-03-24 03:18:17 -08003026 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003027 if (host->max_clk == 0 || host->quirks &
3028 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003029 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003030 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3031 mmc_hostname(mmc));
Ben Dooks4240ff02009-03-17 00:13:57 +03003032 return -ENODEV;
3033 }
3034 host->max_clk = host->ops->get_max_clock(host);
3035 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003036
3037 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303038 * In case of Host Controller v3.00, find out whether clock
3039 * multiplier is supported.
3040 */
3041 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3042 SDHCI_CLOCK_MUL_SHIFT;
3043
3044 /*
3045 * In case the value in Clock Multiplier is 0, then programmable
3046 * clock mode is not supported, otherwise the actual clock
3047 * multiplier is one more than the value of Clock Multiplier
3048 * in the Capabilities Register.
3049 */
3050 if (host->clk_mul)
3051 host->clk_mul += 1;
3052
3053 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003054 * Set host parameters.
3055 */
Dong Aisheng59241752015-07-22 20:53:07 +08003056 max_clk = host->max_clk;
3057
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003058 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003059 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303060 else if (host->version >= SDHCI_SPEC_300) {
3061 if (host->clk_mul) {
3062 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003063 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303064 } else
3065 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3066 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003067 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003068
Dong Aisheng59241752015-07-22 20:53:07 +08003069 if (!mmc->f_max || (mmc->f_max && (mmc->f_max > max_clk)))
3070 mmc->f_max = max_clk;
3071
Aisheng Dong28aab052014-08-27 15:26:31 +08003072 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3073 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3074 SDHCI_TIMEOUT_CLK_SHIFT;
3075 if (host->timeout_clk == 0) {
3076 if (host->ops->get_timeout_clock) {
3077 host->timeout_clk =
3078 host->ops->get_timeout_clock(host);
3079 } else {
3080 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3081 mmc_hostname(mmc));
3082 return -ENODEV;
3083 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003084 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003085
Aisheng Dong28aab052014-08-27 15:26:31 +08003086 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3087 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003088
Aisheng Dong28aab052014-08-27 15:26:31 +08003089 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003090 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003091 mmc->max_busy_timeout /= host->timeout_clk;
3092 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003093
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003094 if (override_timeout_clk)
3095 host->timeout_clk = override_timeout_clk;
3096
Andrei Warkentine89d4562011-05-23 15:06:37 -05003097 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003098 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003099
3100 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3101 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003102
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003103 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003104 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003105 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003106 !(host->flags & SDHCI_USE_SDMA)) &&
3107 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003108 host->flags |= SDHCI_AUTO_CMD23;
3109 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3110 } else {
3111 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3112 }
3113
Philip Rakity15ec4462010-11-19 16:48:39 -05003114 /*
3115 * A controller may support 8-bit width, but the board itself
3116 * might not have the pins brought out. Boards that support
3117 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3118 * their platform code before calling sdhci_add_host(), and we
3119 * won't assume 8-bit width for hosts without that CAP.
3120 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003121 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003122 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003123
Jerry Huang63ef5d82012-10-25 13:47:19 +08003124 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3125 mmc->caps &= ~MMC_CAP_CMD23;
3126
Arindam Nathf2119df2011-05-05 12:18:57 +05303127 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003128 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003129
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003130 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Ivan T. Ivanovc31d22e2015-07-06 15:16:20 +03003131 !(mmc->caps & MMC_CAP_NONREMOVABLE) &&
3132 IS_ERR_VALUE(mmc_gpio_get_cd(host->mmc)))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003133 mmc->caps |= MMC_CAP_NEEDS_POLL;
3134
Tim Kryger3a48edc2014-06-13 10:13:56 -07003135 /* If there are external regulators, get them */
3136 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3137 return -EPROBE_DEFER;
3138
Philip Rakity6231f3d2012-07-23 15:56:23 -07003139 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003140 if (!IS_ERR(mmc->supply.vqmmc)) {
3141 ret = regulator_enable(mmc->supply.vqmmc);
3142 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3143 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003144 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3145 SDHCI_SUPPORT_SDR50 |
3146 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003147 if (ret) {
3148 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3149 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003150 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003151 }
Kevin Liu8363c372012-11-17 17:55:51 -05003152 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003153
Daniel Drake6a661802012-11-25 13:01:19 -05003154 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3155 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3156 SDHCI_SUPPORT_DDR50);
3157
Al Cooper4188bba2012-03-16 15:54:17 -04003158 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3159 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3160 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303161 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3162
3163 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003164 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303165 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003166 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3167 * field can be promoted to support HS200.
3168 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003169 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003170 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003171 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303172 mmc->caps |= MMC_CAP_UHS_SDR50;
3173
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003174 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3175 (caps[1] & SDHCI_SUPPORT_HS400))
3176 mmc->caps2 |= MMC_CAP2_HS400;
3177
Adrian Hunter549c0b12014-11-06 15:19:05 +02003178 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3179 (IS_ERR(mmc->supply.vqmmc) ||
3180 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3181 1300000)))
3182 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3183
Micky Ching9107ebb2014-02-21 18:40:35 +08003184 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3185 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303186 mmc->caps |= MMC_CAP_UHS_DDR50;
3187
Girish K S069c9f12012-01-06 09:56:39 +05303188 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303189 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3190 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3191
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003192 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303193 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003194 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303195
Arindam Nathd6d50a12011-05-05 12:18:59 +05303196 /* Driver Type(s) (A, C, D) supported by the host */
3197 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3198 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3199 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3200 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3201 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3202 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3203
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303204 /* Initial value for re-tuning timer count */
3205 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3206 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3207
3208 /*
3209 * In case Re-tuning Timer is not disabled, the actual value of
3210 * re-tuning timer will be 2 ^ (n - 1).
3211 */
3212 if (host->tuning_count)
3213 host->tuning_count = 1 << (host->tuning_count - 1);
3214
3215 /* Re-tuning mode supported by the Host Controller */
3216 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3217 SDHCI_RETUNING_MODE_SHIFT;
3218
Takashi Iwai8f230f42010-12-08 10:04:30 +01003219 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003220
Arindam Nathf2119df2011-05-05 12:18:57 +05303221 /*
3222 * According to SD Host Controller spec v3.00, if the Host System
3223 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3224 * the value is meaningful only if Voltage Support in the Capabilities
3225 * register is set. The actual current value is 4 times the register
3226 * value.
3227 */
3228 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003229 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003230 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003231 if (curr > 0) {
3232
3233 /* convert to SDHCI_MAX_CURRENT format */
3234 curr = curr/1000; /* convert to mA */
3235 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3236
3237 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3238 max_current_caps =
3239 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3240 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3241 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3242 }
3243 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303244
3245 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003246 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303247
Aaron Lu55c46652012-07-04 13:31:48 +08003248 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303249 SDHCI_MAX_CURRENT_330_MASK) >>
3250 SDHCI_MAX_CURRENT_330_SHIFT) *
3251 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303252 }
3253 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003254 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303255
Aaron Lu55c46652012-07-04 13:31:48 +08003256 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303257 SDHCI_MAX_CURRENT_300_MASK) >>
3258 SDHCI_MAX_CURRENT_300_SHIFT) *
3259 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303260 }
3261 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003262 ocr_avail |= MMC_VDD_165_195;
3263
Aaron Lu55c46652012-07-04 13:31:48 +08003264 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303265 SDHCI_MAX_CURRENT_180_MASK) >>
3266 SDHCI_MAX_CURRENT_180_SHIFT) *
3267 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303268 }
3269
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003270 /* If OCR set by host, use it instead. */
3271 if (host->ocr_mask)
3272 ocr_avail = host->ocr_mask;
3273
3274 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003275 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003276 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003277
Takashi Iwai8f230f42010-12-08 10:04:30 +01003278 mmc->ocr_avail = ocr_avail;
3279 mmc->ocr_avail_sdio = ocr_avail;
3280 if (host->ocr_avail_sdio)
3281 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3282 mmc->ocr_avail_sd = ocr_avail;
3283 if (host->ocr_avail_sd)
3284 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3285 else /* normal SD controllers don't support 1.8V */
3286 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3287 mmc->ocr_avail_mmc = ocr_avail;
3288 if (host->ocr_avail_mmc)
3289 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003290
3291 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003292 pr_err("%s: Hardware doesn't report any support voltages.\n",
3293 mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003294 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003295 }
3296
Pierre Ossmand129bce2006-03-24 03:18:17 -08003297 spin_lock_init(&host->lock);
3298
3299 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003300 * Maximum number of segments. Depends on if the hardware
3301 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003302 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003303 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003304 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003305 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003306 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003307 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003308 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003309
3310 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003311 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3312 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3313 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003314 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003315 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003316
3317 /*
3318 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003319 * of bytes. When doing hardware scatter/gather, each entry cannot
3320 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003321 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003322 if (host->flags & SDHCI_USE_ADMA) {
3323 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3324 mmc->max_seg_size = 65535;
3325 else
3326 mmc->max_seg_size = 65536;
3327 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003328 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003329 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003330
3331 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003332 * Maximum block size. This varies from controller to controller and
3333 * is specified in the capabilities register.
3334 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003335 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3336 mmc->max_blk_size = 2;
3337 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303338 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003339 SDHCI_MAX_BLOCK_SHIFT;
3340 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003341 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3342 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003343 mmc->max_blk_size = 0;
3344 }
3345 }
3346
3347 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003348
3349 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003350 * Maximum block count.
3351 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003352 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003353
3354 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003355 * Init tasklets.
3356 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003357 tasklet_init(&host->finish_tasklet,
3358 sdhci_tasklet_finish, (unsigned long)host);
3359
Al Viroe4cad1b2006-10-10 22:47:07 +01003360 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003361
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003362 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303363
Shawn Guo2af502c2013-07-05 14:38:55 +08003364 sdhci_init(host, 0);
3365
Russell King781e9892014-04-25 12:55:46 +01003366 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3367 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003368 if (ret) {
3369 pr_err("%s: Failed to request IRQ %d: %d\n",
3370 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003371 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003372 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003373
Pierre Ossmand129bce2006-03-24 03:18:17 -08003374#ifdef CONFIG_MMC_DEBUG
3375 sdhci_dumpregs(host);
3376#endif
3377
Pierre Ossmanf9134312008-12-21 17:01:48 +01003378#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003379 snprintf(host->led_name, sizeof(host->led_name),
3380 "%s::", mmc_hostname(mmc));
3381 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003382 host->led.brightness = LED_OFF;
3383 host->led.default_trigger = mmc_hostname(mmc);
3384 host->led.brightness_set = sdhci_led_control;
3385
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003386 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003387 if (ret) {
3388 pr_err("%s: Failed to register LED device: %d\n",
3389 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003390 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003391 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003392#endif
3393
Pierre Ossman5f25a662006-10-04 02:15:39 -07003394 mmiowb();
3395
Pierre Ossmand129bce2006-03-24 03:18:17 -08003396 mmc_add_host(mmc);
3397
Girish K Sa3c76eb2011-10-11 11:44:09 +05303398 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003399 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003400 (host->flags & SDHCI_USE_ADMA) ?
3401 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003402 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003403
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003404 sdhci_enable_card_detection(host);
3405
Pierre Ossmand129bce2006-03-24 03:18:17 -08003406 return 0;
3407
Pierre Ossmanf9134312008-12-21 17:01:48 +01003408#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003409reset:
Russell King03231f92014-04-25 12:57:12 +01003410 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003411 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3412 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003413 free_irq(host->irq, host);
3414#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003415untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003416 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003417
3418 return ret;
3419}
3420
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003421EXPORT_SYMBOL_GPL(sdhci_add_host);
3422
Pierre Ossman1e728592008-04-16 19:13:13 +02003423void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003424{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003425 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003426 unsigned long flags;
3427
3428 if (dead) {
3429 spin_lock_irqsave(&host->lock, flags);
3430
3431 host->flags |= SDHCI_DEVICE_DEAD;
3432
3433 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303434 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003435 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003436
3437 host->mrq->cmd->error = -ENOMEDIUM;
3438 tasklet_schedule(&host->finish_tasklet);
3439 }
3440
3441 spin_unlock_irqrestore(&host->lock, flags);
3442 }
3443
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003444 sdhci_disable_card_detection(host);
3445
Markus Mayer4e743f12014-07-03 13:27:42 -07003446 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003447
Pierre Ossmanf9134312008-12-21 17:01:48 +01003448#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003449 led_classdev_unregister(&host->led);
3450#endif
3451
Pierre Ossman1e728592008-04-16 19:13:13 +02003452 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003453 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003454
Russell Kingb537f942014-04-25 12:56:01 +01003455 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3456 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003457 free_irq(host->irq, host);
3458
3459 del_timer_sync(&host->timer);
3460
Pierre Ossmand129bce2006-03-24 03:18:17 -08003461 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003462
Tim Kryger3a48edc2014-06-13 10:13:56 -07003463 if (!IS_ERR(mmc->supply.vqmmc))
3464 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003465
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003466 if (host->adma_table)
Adrian Hunter76fe3792014-11-04 12:42:42 +02003467 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003468 host->adma_table, host->adma_addr);
Russell Kingedd63fc2016-01-26 13:39:50 +00003469 if (host->align_buffer)
3470 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz,
3471 host->align_buffer, host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003472
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003473 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003474 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003475}
3476
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003477EXPORT_SYMBOL_GPL(sdhci_remove_host);
3478
3479void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003480{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003481 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003482}
3483
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003484EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003485
3486/*****************************************************************************\
3487 * *
3488 * Driver init/exit *
3489 * *
3490\*****************************************************************************/
3491
3492static int __init sdhci_drv_init(void)
3493{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303494 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003495 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303496 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003497
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003498 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003499}
3500
3501static void __exit sdhci_drv_exit(void)
3502{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003503}
3504
3505module_init(sdhci_drv_init);
3506module_exit(sdhci_drv_exit);
3507
Pierre Ossmandf673b22006-06-30 02:22:31 -07003508module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003509module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003510
Pierre Ossman32710e82009-04-08 20:14:54 +02003511MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003512MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003513MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003514
Pierre Ossmandf673b22006-06-30 02:22:31 -07003515MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003516MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");