blob: e40414ab7ef8dfb19fc07eff3ab3d217b9f587ba [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>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080031#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080032
Pierre Ossmand129bce2006-03-24 03:18:17 -080033#include "sdhci.h"
34
35#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080036
Pierre Ossmand129bce2006-03-24 03:18:17 -080037#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010038 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080039
Pierre Ossmanf9134312008-12-21 17:01:48 +010040#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
41 defined(CONFIG_MMC_SDHCI_MODULE))
42#define SDHCI_USE_LEDS_CLASS
43#endif
44
Arindam Nathb513ea22011-05-05 12:19:04 +053045#define MAX_TUNING_LOOP 40
46
Adrian Hunterc09df942014-11-04 12:42:35 +020047/*
48 * The ADMA2 descriptor table size is calculated as the maximum number of
49 * segments (128), times 2 to allow for an alignment descriptor for each
50 * segment, plus 1 for a nop end descriptor, all multipled by the 32-bit
51 * descriptor size (8).
52 */
53#define ADMA_SIZE ((128 * 2 + 1) * 8)
Russell Kingd1e49f72014-04-25 12:58:34 +010054
Pierre Ossmandf673b22006-06-30 02:22:31 -070055static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030056static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070057
Pierre Ossmand129bce2006-03-24 03:18:17 -080058static void sdhci_finish_data(struct sdhci_host *);
59
Pierre Ossmand129bce2006-03-24 03:18:17 -080060static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053061static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053062static void sdhci_tuning_timer(unsigned long data);
Kevin Liu52983382013-01-31 11:31:37 +080063static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080064
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030065#ifdef CONFIG_PM_RUNTIME
66static int sdhci_runtime_pm_get(struct sdhci_host *host);
67static int sdhci_runtime_pm_put(struct sdhci_host *host);
Adrian Hunterf0710a52013-05-06 12:17:32 +030068static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
69static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030070#else
71static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
72{
73 return 0;
74}
75static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
76{
77 return 0;
78}
Adrian Hunterf0710a52013-05-06 12:17:32 +030079static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
80{
81}
82static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
83{
84}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030085#endif
86
Pierre Ossmand129bce2006-03-24 03:18:17 -080087static void sdhci_dumpregs(struct sdhci_host *host)
88{
Girish K Sa3c76eb2011-10-11 11:44:09 +053089 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070090 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080091
Girish K Sa3c76eb2011-10-11 11:44:09 +053092 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030093 sdhci_readl(host, SDHCI_DMA_ADDRESS),
94 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053095 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030096 sdhci_readw(host, SDHCI_BLOCK_SIZE),
97 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053098 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030099 sdhci_readl(host, SDHCI_ARGUMENT),
100 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530101 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300102 sdhci_readl(host, SDHCI_PRESENT_STATE),
103 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530104 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300105 sdhci_readb(host, SDHCI_POWER_CONTROL),
106 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530107 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300108 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
109 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530110 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300111 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
112 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530113 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300114 sdhci_readl(host, SDHCI_INT_ENABLE),
115 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530116 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300117 sdhci_readw(host, SDHCI_ACMD12_ERR),
118 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530119 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300120 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500121 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530122 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500123 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300124 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530125 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530126 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800127
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100128 if (host->flags & SDHCI_USE_ADMA)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530129 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100130 readl(host->ioaddr + SDHCI_ADMA_ERROR),
131 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
132
Girish K Sa3c76eb2011-10-11 11:44:09 +0530133 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800134}
135
136/*****************************************************************************\
137 * *
138 * Low level functions *
139 * *
140\*****************************************************************************/
141
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300142static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
143{
Russell King5b4f1f62014-04-25 12:57:02 +0100144 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300145
Adrian Hunterc79396c2011-12-27 15:48:42 +0200146 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100147 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300148 return;
149
Russell King5b4f1f62014-04-25 12:57:02 +0100150 if (enable) {
151 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
152 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800153
Russell King5b4f1f62014-04-25 12:57:02 +0100154 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
155 SDHCI_INT_CARD_INSERT;
156 } else {
157 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
158 }
Russell Kingb537f942014-04-25 12:56:01 +0100159
160 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
161 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300162}
163
164static void sdhci_enable_card_detection(struct sdhci_host *host)
165{
166 sdhci_set_card_detection(host, true);
167}
168
169static void sdhci_disable_card_detection(struct sdhci_host *host)
170{
171 sdhci_set_card_detection(host, false);
172}
173
Russell King03231f92014-04-25 12:57:12 +0100174void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800175{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700176 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800177
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300178 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800179
Adrian Hunterf0710a52013-05-06 12:17:32 +0300180 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800181 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300182 /* Reset-all turns off SD Bus Power */
183 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
184 sdhci_runtime_pm_bus_off(host);
185 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800186
Pierre Ossmane16514d82006-06-30 02:22:24 -0700187 /* Wait max 100 ms */
188 timeout = 100;
189
190 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300191 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700192 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530193 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700194 mmc_hostname(host->mmc), (int)mask);
195 sdhci_dumpregs(host);
196 return;
197 }
198 timeout--;
199 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800200 }
Russell King03231f92014-04-25 12:57:12 +0100201}
202EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300203
Russell King03231f92014-04-25 12:57:12 +0100204static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
205{
206 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
207 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
208 SDHCI_CARD_PRESENT))
209 return;
210 }
211
212 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800213
Russell Kingda91a8f2014-04-25 13:00:12 +0100214 if (mask & SDHCI_RESET_ALL) {
215 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
216 if (host->ops->enable_dma)
217 host->ops->enable_dma(host);
218 }
219
220 /* Resetting the controller clears many */
221 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800222 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800223}
224
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800225static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
226
227static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800228{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800229 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100230 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800231 else
Russell King03231f92014-04-25 12:57:12 +0100232 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800233
Russell Kingb537f942014-04-25 12:56:01 +0100234 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
235 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
236 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
237 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
238 SDHCI_INT_RESPONSE;
239
240 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
241 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800242
243 if (soft) {
244 /* force clock reconfiguration */
245 host->clock = 0;
246 sdhci_set_ios(host->mmc, &host->mmc->ios);
247 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300248}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800249
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300250static void sdhci_reinit(struct sdhci_host *host)
251{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800252 sdhci_init(host, 0);
Aaron Lub67c6b42012-06-29 16:17:31 +0800253 /*
254 * Retuning stuffs are affected by different cards inserted and only
255 * applicable to UHS-I cards. So reset these fields to their initial
256 * value when card is removed.
257 */
Aaron Lu973905f2012-07-04 13:29:09 +0800258 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
259 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
260
Aaron Lub67c6b42012-06-29 16:17:31 +0800261 del_timer_sync(&host->tuning_timer);
262 host->flags &= ~SDHCI_NEEDS_RETUNING;
263 host->mmc->max_blk_count =
264 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
265 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300266 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800267}
268
269static void sdhci_activate_led(struct sdhci_host *host)
270{
271 u8 ctrl;
272
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300273 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800274 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300275 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800276}
277
278static void sdhci_deactivate_led(struct sdhci_host *host)
279{
280 u8 ctrl;
281
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300282 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800283 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300284 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800285}
286
Pierre Ossmanf9134312008-12-21 17:01:48 +0100287#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100288static void sdhci_led_control(struct led_classdev *led,
289 enum led_brightness brightness)
290{
291 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
292 unsigned long flags;
293
294 spin_lock_irqsave(&host->lock, flags);
295
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300296 if (host->runtime_suspended)
297 goto out;
298
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100299 if (brightness == LED_OFF)
300 sdhci_deactivate_led(host);
301 else
302 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300303out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100304 spin_unlock_irqrestore(&host->lock, flags);
305}
306#endif
307
Pierre Ossmand129bce2006-03-24 03:18:17 -0800308/*****************************************************************************\
309 * *
310 * Core functions *
311 * *
312\*****************************************************************************/
313
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100314static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800315{
Pierre Ossman76591502008-07-21 00:32:11 +0200316 unsigned long flags;
317 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700318 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200319 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800320
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100321 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800322
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100323 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200324 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800325
Pierre Ossman76591502008-07-21 00:32:11 +0200326 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800327
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100328 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200329 if (!sg_miter_next(&host->sg_miter))
330 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800331
Pierre Ossman76591502008-07-21 00:32:11 +0200332 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800333
Pierre Ossman76591502008-07-21 00:32:11 +0200334 blksize -= len;
335 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200336
Pierre Ossman76591502008-07-21 00:32:11 +0200337 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800338
Pierre Ossman76591502008-07-21 00:32:11 +0200339 while (len) {
340 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300341 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200342 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800343 }
Pierre Ossman76591502008-07-21 00:32:11 +0200344
345 *buf = scratch & 0xFF;
346
347 buf++;
348 scratch >>= 8;
349 chunk--;
350 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800351 }
352 }
Pierre Ossman76591502008-07-21 00:32:11 +0200353
354 sg_miter_stop(&host->sg_miter);
355
356 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100357}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800358
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100359static void sdhci_write_block_pio(struct sdhci_host *host)
360{
Pierre Ossman76591502008-07-21 00:32:11 +0200361 unsigned long flags;
362 size_t blksize, len, chunk;
363 u32 scratch;
364 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100365
366 DBG("PIO writing\n");
367
368 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200369 chunk = 0;
370 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100371
Pierre Ossman76591502008-07-21 00:32:11 +0200372 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100373
374 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200375 if (!sg_miter_next(&host->sg_miter))
376 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100377
Pierre Ossman76591502008-07-21 00:32:11 +0200378 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200379
Pierre Ossman76591502008-07-21 00:32:11 +0200380 blksize -= len;
381 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100382
Pierre Ossman76591502008-07-21 00:32:11 +0200383 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100384
Pierre Ossman76591502008-07-21 00:32:11 +0200385 while (len) {
386 scratch |= (u32)*buf << (chunk * 8);
387
388 buf++;
389 chunk++;
390 len--;
391
392 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300393 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200394 chunk = 0;
395 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100396 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100397 }
398 }
Pierre Ossman76591502008-07-21 00:32:11 +0200399
400 sg_miter_stop(&host->sg_miter);
401
402 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100403}
404
405static void sdhci_transfer_pio(struct sdhci_host *host)
406{
407 u32 mask;
408
409 BUG_ON(!host->data);
410
Pierre Ossman76591502008-07-21 00:32:11 +0200411 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100412 return;
413
414 if (host->data->flags & MMC_DATA_READ)
415 mask = SDHCI_DATA_AVAILABLE;
416 else
417 mask = SDHCI_SPACE_AVAILABLE;
418
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200419 /*
420 * Some controllers (JMicron JMB38x) mess up the buffer bits
421 * for transfers < 4 bytes. As long as it is just one block,
422 * we can ignore the bits.
423 */
424 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
425 (host->data->blocks == 1))
426 mask = ~0;
427
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300428 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300429 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
430 udelay(100);
431
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100432 if (host->data->flags & MMC_DATA_READ)
433 sdhci_read_block_pio(host);
434 else
435 sdhci_write_block_pio(host);
436
Pierre Ossman76591502008-07-21 00:32:11 +0200437 host->blocks--;
438 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100439 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100440 }
441
442 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800443}
444
Pierre Ossman2134a922008-06-28 18:28:51 +0200445static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
446{
447 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800448 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200449}
450
451static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
452{
Cong Wang482fce92011-11-27 13:27:00 +0800453 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200454 local_irq_restore(*flags);
455}
456
Adrian Hunter08621b12014-11-04 12:42:38 +0200457static void sdhci_adma_write_desc(u8 *desc, u32 addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800458{
Ben Dooks9e506f32010-03-05 13:43:29 -0800459 __le32 *dataddr = (__le32 __force *)(desc + 4);
460 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800461
Ben Dooks9e506f32010-03-05 13:43:29 -0800462 /* SDHCI specification says ADMA descriptors should be 4 byte
463 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800464
Ben Dooks9e506f32010-03-05 13:43:29 -0800465 cmdlen[0] = cpu_to_le16(cmd);
466 cmdlen[1] = cpu_to_le16(len);
467
468 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800469}
470
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200471static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200472 struct mmc_data *data)
473{
474 int direction;
475
476 u8 *desc;
477 u8 *align;
478 dma_addr_t addr;
479 dma_addr_t align_addr;
480 int len, offset;
481
482 struct scatterlist *sg;
483 int i;
484 char *buffer;
485 unsigned long flags;
486
487 /*
488 * The spec does not specify endianness of descriptor table.
489 * We currently guess that it is LE.
490 */
491
492 if (data->flags & MMC_DATA_READ)
493 direction = DMA_FROM_DEVICE;
494 else
495 direction = DMA_TO_DEVICE;
496
Pierre Ossman2134a922008-06-28 18:28:51 +0200497 host->align_addr = dma_map_single(mmc_dev(host->mmc),
498 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700499 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200500 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200501 BUG_ON(host->align_addr & 0x3);
502
503 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
504 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200505 if (host->sg_count == 0)
506 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200507
508 desc = host->adma_desc;
509 align = host->align_buffer;
510
511 align_addr = host->align_addr;
512
513 for_each_sg(data->sg, sg, host->sg_count, i) {
514 addr = sg_dma_address(sg);
515 len = sg_dma_len(sg);
516
517 /*
518 * The SDHCI specification states that ADMA
519 * addresses must be 32-bit aligned. If they
520 * aren't, then we use a bounce buffer for
521 * the (up to three) bytes that screw up the
522 * alignment.
523 */
524 offset = (4 - (addr & 0x3)) & 0x3;
525 if (offset) {
526 if (data->flags & MMC_DATA_WRITE) {
527 buffer = sdhci_kmap_atomic(sg, &flags);
Adrian Hunter8be78c62014-11-04 12:42:36 +0200528 WARN_ON(((long)buffer & (PAGE_SIZE - 1)) >
529 (PAGE_SIZE - offset));
Pierre Ossman2134a922008-06-28 18:28:51 +0200530 memcpy(align, buffer, offset);
531 sdhci_kunmap_atomic(buffer, &flags);
532 }
533
Ben Dooks118cd172010-03-05 13:43:26 -0800534 /* tran, valid */
Adrian Hunter08621b12014-11-04 12:42:38 +0200535 sdhci_adma_write_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200536
537 BUG_ON(offset > 65536);
538
Pierre Ossman2134a922008-06-28 18:28:51 +0200539 align += 4;
540 align_addr += 4;
541
542 desc += 8;
543
544 addr += offset;
545 len -= offset;
546 }
547
Pierre Ossman2134a922008-06-28 18:28:51 +0200548 BUG_ON(len > 65536);
549
Ben Dooks118cd172010-03-05 13:43:26 -0800550 /* tran, valid */
Adrian Hunter08621b12014-11-04 12:42:38 +0200551 sdhci_adma_write_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200552 desc += 8;
553
554 /*
555 * If this triggers then we have a calculation bug
556 * somewhere. :/
557 */
Adrian Hunterb521cf32014-11-04 12:42:37 +0200558 WARN_ON((desc - host->adma_desc) >= ADMA_SIZE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200559 }
560
Thomas Abraham70764a92010-05-26 14:42:04 -0700561 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
562 /*
563 * Mark the last descriptor as the terminating descriptor
564 */
565 if (desc != host->adma_desc) {
566 desc -= 8;
567 desc[0] |= 0x2; /* end */
568 }
569 } else {
570 /*
571 * Add a terminating entry.
572 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200573
Thomas Abraham70764a92010-05-26 14:42:04 -0700574 /* nop, end, valid */
Adrian Hunter08621b12014-11-04 12:42:38 +0200575 sdhci_adma_write_desc(desc, 0, 0, 0x3);
Thomas Abraham70764a92010-05-26 14:42:04 -0700576 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200577
578 /*
579 * Resync align buffer as we might have changed it.
580 */
581 if (data->flags & MMC_DATA_WRITE) {
582 dma_sync_single_for_device(mmc_dev(host->mmc),
583 host->align_addr, 128 * 4, direction);
584 }
585
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200586 return 0;
587
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200588unmap_align:
589 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
590 128 * 4, direction);
591fail:
592 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200593}
594
595static void sdhci_adma_table_post(struct sdhci_host *host,
596 struct mmc_data *data)
597{
598 int direction;
599
600 struct scatterlist *sg;
601 int i, size;
602 u8 *align;
603 char *buffer;
604 unsigned long flags;
Russell Kingde0b65a2014-04-25 12:58:29 +0100605 bool has_unaligned;
Pierre Ossman2134a922008-06-28 18:28:51 +0200606
607 if (data->flags & MMC_DATA_READ)
608 direction = DMA_FROM_DEVICE;
609 else
610 direction = DMA_TO_DEVICE;
611
Pierre Ossman2134a922008-06-28 18:28:51 +0200612 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
613 128 * 4, direction);
614
Russell Kingde0b65a2014-04-25 12:58:29 +0100615 /* Do a quick scan of the SG list for any unaligned mappings */
616 has_unaligned = false;
617 for_each_sg(data->sg, sg, host->sg_count, i)
618 if (sg_dma_address(sg) & 3) {
619 has_unaligned = true;
620 break;
621 }
622
623 if (has_unaligned && data->flags & MMC_DATA_READ) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200624 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
625 data->sg_len, direction);
626
627 align = host->align_buffer;
628
629 for_each_sg(data->sg, sg, host->sg_count, i) {
630 if (sg_dma_address(sg) & 0x3) {
631 size = 4 - (sg_dma_address(sg) & 0x3);
632
633 buffer = sdhci_kmap_atomic(sg, &flags);
Adrian Hunter8be78c62014-11-04 12:42:36 +0200634 WARN_ON(((long)buffer & (PAGE_SIZE - 1)) >
635 (PAGE_SIZE - size));
Pierre Ossman2134a922008-06-28 18:28:51 +0200636 memcpy(buffer, align, size);
637 sdhci_kunmap_atomic(buffer, &flags);
638
639 align += 4;
640 }
641 }
642 }
643
644 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
645 data->sg_len, direction);
646}
647
Andrei Warkentina3c77782011-04-11 16:13:42 -0500648static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800649{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700650 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500651 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700652 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800653
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200654 /*
655 * If the host controller provides us with an incorrect timeout
656 * value, just skip the check and use 0xE. The hardware may take
657 * longer to time out, but that's much better than having a too-short
658 * timeout value.
659 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200660 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200661 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200662
Andrei Warkentina3c77782011-04-11 16:13:42 -0500663 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100664 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500665 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800666
Andrei Warkentina3c77782011-04-11 16:13:42 -0500667 /* timeout in us */
668 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100669 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300670 else {
671 target_timeout = data->timeout_ns / 1000;
672 if (host->clock)
673 target_timeout += data->timeout_clks / host->clock;
674 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700675
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700676 /*
677 * Figure out needed cycles.
678 * We do this in steps in order to fit inside a 32 bit int.
679 * The first step is the minimum timeout, which will have a
680 * minimum resolution of 6 bits:
681 * (1) 2^13*1000 > 2^22,
682 * (2) host->timeout_clk < 2^16
683 * =>
684 * (1) / (2) > 2^6
685 */
686 count = 0;
687 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
688 while (current_timeout < target_timeout) {
689 count++;
690 current_timeout <<= 1;
691 if (count >= 0xF)
692 break;
693 }
694
695 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400696 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
697 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700698 count = 0xE;
699 }
700
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200701 return count;
702}
703
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300704static void sdhci_set_transfer_irqs(struct sdhci_host *host)
705{
706 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
707 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
708
709 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100710 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300711 else
Russell Kingb537f942014-04-25 12:56:01 +0100712 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
713
714 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
715 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300716}
717
Aisheng Dongb45e6682014-08-27 15:26:29 +0800718static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200719{
720 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800721
722 if (host->ops->set_timeout) {
723 host->ops->set_timeout(host, cmd);
724 } else {
725 count = sdhci_calc_timeout(host, cmd);
726 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
727 }
728}
729
730static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
731{
Pierre Ossman2134a922008-06-28 18:28:51 +0200732 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500733 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200734 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200735
736 WARN_ON(host->data);
737
Aisheng Dongb45e6682014-08-27 15:26:29 +0800738 if (data || (cmd->flags & MMC_RSP_BUSY))
739 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500740
741 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200742 return;
743
744 /* Sanity checks */
745 BUG_ON(data->blksz * data->blocks > 524288);
746 BUG_ON(data->blksz > host->mmc->max_blk_size);
747 BUG_ON(data->blocks > 65535);
748
749 host->data = data;
750 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400751 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200752
Richard Röjforsa13abc72009-09-22 16:45:30 -0700753 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100754 host->flags |= SDHCI_REQ_USE_DMA;
755
Pierre Ossman2134a922008-06-28 18:28:51 +0200756 /*
757 * FIXME: This doesn't account for merging when mapping the
758 * scatterlist.
759 */
760 if (host->flags & SDHCI_REQ_USE_DMA) {
761 int broken, i;
762 struct scatterlist *sg;
763
764 broken = 0;
765 if (host->flags & SDHCI_USE_ADMA) {
766 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
767 broken = 1;
768 } else {
769 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
770 broken = 1;
771 }
772
773 if (unlikely(broken)) {
774 for_each_sg(data->sg, sg, data->sg_len, i) {
775 if (sg->length & 0x3) {
776 DBG("Reverting to PIO because of "
777 "transfer size (%d)\n",
778 sg->length);
779 host->flags &= ~SDHCI_REQ_USE_DMA;
780 break;
781 }
782 }
783 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100784 }
785
786 /*
787 * The assumption here being that alignment is the same after
788 * translation to device address space.
789 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200790 if (host->flags & SDHCI_REQ_USE_DMA) {
791 int broken, i;
792 struct scatterlist *sg;
793
794 broken = 0;
795 if (host->flags & SDHCI_USE_ADMA) {
796 /*
797 * As we use 3 byte chunks to work around
798 * alignment problems, we need to check this
799 * quirk.
800 */
801 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
802 broken = 1;
803 } else {
804 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
805 broken = 1;
806 }
807
808 if (unlikely(broken)) {
809 for_each_sg(data->sg, sg, data->sg_len, i) {
810 if (sg->offset & 0x3) {
811 DBG("Reverting to PIO because of "
812 "bad alignment\n");
813 host->flags &= ~SDHCI_REQ_USE_DMA;
814 break;
815 }
816 }
817 }
818 }
819
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200820 if (host->flags & SDHCI_REQ_USE_DMA) {
821 if (host->flags & SDHCI_USE_ADMA) {
822 ret = sdhci_adma_table_pre(host, data);
823 if (ret) {
824 /*
825 * This only happens when someone fed
826 * us an invalid request.
827 */
828 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200829 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200830 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300831 sdhci_writel(host, host->adma_addr,
832 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200833 }
834 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300835 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200836
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300837 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200838 data->sg, data->sg_len,
839 (data->flags & MMC_DATA_READ) ?
840 DMA_FROM_DEVICE :
841 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300842 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200843 /*
844 * This only happens when someone fed
845 * us an invalid request.
846 */
847 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200848 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200849 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200850 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300851 sdhci_writel(host, sg_dma_address(data->sg),
852 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200853 }
854 }
855 }
856
Pierre Ossman2134a922008-06-28 18:28:51 +0200857 /*
858 * Always adjust the DMA selection as some controllers
859 * (e.g. JMicron) can't do PIO properly when the selection
860 * is ADMA.
861 */
862 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300863 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200864 ctrl &= ~SDHCI_CTRL_DMA_MASK;
865 if ((host->flags & SDHCI_REQ_USE_DMA) &&
866 (host->flags & SDHCI_USE_ADMA))
867 ctrl |= SDHCI_CTRL_ADMA32;
868 else
869 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300870 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100871 }
872
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200873 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200874 int flags;
875
876 flags = SG_MITER_ATOMIC;
877 if (host->data->flags & MMC_DATA_READ)
878 flags |= SG_MITER_TO_SG;
879 else
880 flags |= SG_MITER_FROM_SG;
881 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200882 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800883 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700884
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300885 sdhci_set_transfer_irqs(host);
886
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400887 /* Set the DMA boundary value and block size */
888 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
889 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300890 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700891}
892
893static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500894 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700895{
896 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500897 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700898
Dong Aisheng2b558c12013-10-30 22:09:48 +0800899 if (data == NULL) {
900 /* clear Auto CMD settings for no data CMDs */
901 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
902 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
903 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700904 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800905 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700906
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200907 WARN_ON(!host->data);
908
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700909 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500910 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
911 mode |= SDHCI_TRNS_MULTI;
912 /*
913 * If we are sending CMD23, CMD12 never gets sent
914 * on successful completion (so no Auto-CMD12).
915 */
916 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
917 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500918 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
919 mode |= SDHCI_TRNS_AUTO_CMD23;
920 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
921 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700922 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500923
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700924 if (data->flags & MMC_DATA_READ)
925 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100926 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700927 mode |= SDHCI_TRNS_DMA;
928
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300929 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800930}
931
932static void sdhci_finish_data(struct sdhci_host *host)
933{
934 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800935
936 BUG_ON(!host->data);
937
938 data = host->data;
939 host->data = NULL;
940
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100941 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200942 if (host->flags & SDHCI_USE_ADMA)
943 sdhci_adma_table_post(host, data);
944 else {
945 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
946 data->sg_len, (data->flags & MMC_DATA_READ) ?
947 DMA_FROM_DEVICE : DMA_TO_DEVICE);
948 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800949 }
950
951 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200952 * The specification states that the block count register must
953 * be updated, but it does not specify at what point in the
954 * data flow. That makes the register entirely useless to read
955 * back so we have to assume that nothing made it to the card
956 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800957 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200958 if (data->error)
959 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800960 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200961 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800962
Andrei Warkentine89d4562011-05-23 15:06:37 -0500963 /*
964 * Need to send CMD12 if -
965 * a) open-ended multiblock transfer (no CMD23)
966 * b) error in multiblock transfer
967 */
968 if (data->stop &&
969 (data->error ||
970 !host->mrq->sbc)) {
971
Pierre Ossmand129bce2006-03-24 03:18:17 -0800972 /*
973 * The controller needs a reset of internal state machines
974 * upon error conditions.
975 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200976 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100977 sdhci_do_reset(host, SDHCI_RESET_CMD);
978 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800979 }
980
981 sdhci_send_command(host, data->stop);
982 } else
983 tasklet_schedule(&host->finish_tasklet);
984}
985
Dong Aishengc0e551292013-09-13 19:11:31 +0800986void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800987{
988 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700989 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700990 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800991
992 WARN_ON(host->cmd);
993
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700995 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700996
997 mask = SDHCI_CMD_INHIBIT;
998 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
999 mask |= SDHCI_DATA_INHIBIT;
1000
1001 /* We shouldn't wait for data inihibit for stop commands, even
1002 though they might use busy signaling */
1003 if (host->mrq->data && (cmd == host->mrq->data->stop))
1004 mask &= ~SDHCI_DATA_INHIBIT;
1005
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001006 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001007 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301008 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001009 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001010 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001011 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012 tasklet_schedule(&host->finish_tasklet);
1013 return;
1014 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001015 timeout--;
1016 mdelay(1);
1017 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001018
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001019 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001020 if (!cmd->data && cmd->busy_timeout > 9000)
1021 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001022 else
1023 timeout += 10 * HZ;
1024 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001025
1026 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001027 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001028
Andrei Warkentina3c77782011-04-11 16:13:42 -05001029 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001031 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001032
Andrei Warkentine89d4562011-05-23 15:06:37 -05001033 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001034
Pierre Ossmand129bce2006-03-24 03:18:17 -08001035 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301036 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001037 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001038 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001039 tasklet_schedule(&host->finish_tasklet);
1040 return;
1041 }
1042
1043 if (!(cmd->flags & MMC_RSP_PRESENT))
1044 flags = SDHCI_CMD_RESP_NONE;
1045 else if (cmd->flags & MMC_RSP_136)
1046 flags = SDHCI_CMD_RESP_LONG;
1047 else if (cmd->flags & MMC_RSP_BUSY)
1048 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1049 else
1050 flags = SDHCI_CMD_RESP_SHORT;
1051
1052 if (cmd->flags & MMC_RSP_CRC)
1053 flags |= SDHCI_CMD_CRC;
1054 if (cmd->flags & MMC_RSP_OPCODE)
1055 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301056
1057 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301058 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1059 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001060 flags |= SDHCI_CMD_DATA;
1061
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001062 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001063}
Dong Aishengc0e551292013-09-13 19:11:31 +08001064EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001065
1066static void sdhci_finish_command(struct sdhci_host *host)
1067{
1068 int i;
1069
1070 BUG_ON(host->cmd == NULL);
1071
1072 if (host->cmd->flags & MMC_RSP_PRESENT) {
1073 if (host->cmd->flags & MMC_RSP_136) {
1074 /* CRC is stripped so we need to do some shifting. */
1075 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001076 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001077 SDHCI_RESPONSE + (3-i)*4) << 8;
1078 if (i != 3)
1079 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001080 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001081 SDHCI_RESPONSE + (3-i)*4-1);
1082 }
1083 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001084 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001085 }
1086 }
1087
Pierre Ossman17b04292007-07-22 22:18:46 +02001088 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001089
Andrei Warkentine89d4562011-05-23 15:06:37 -05001090 /* Finished CMD23, now send actual command. */
1091 if (host->cmd == host->mrq->sbc) {
1092 host->cmd = NULL;
1093 sdhci_send_command(host, host->mrq->cmd);
1094 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001095
Andrei Warkentine89d4562011-05-23 15:06:37 -05001096 /* Processed actual command. */
1097 if (host->data && host->data_early)
1098 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001099
Andrei Warkentine89d4562011-05-23 15:06:37 -05001100 if (!host->cmd->data)
1101 tasklet_schedule(&host->finish_tasklet);
1102
1103 host->cmd = NULL;
1104 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001105}
1106
Kevin Liu52983382013-01-31 11:31:37 +08001107static u16 sdhci_get_preset_value(struct sdhci_host *host)
1108{
Russell Kingd975f122014-04-25 12:59:31 +01001109 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001110
Russell Kingd975f122014-04-25 12:59:31 +01001111 switch (host->timing) {
1112 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001113 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1114 break;
Russell Kingd975f122014-04-25 12:59:31 +01001115 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001116 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1117 break;
Russell Kingd975f122014-04-25 12:59:31 +01001118 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001119 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1120 break;
Russell Kingd975f122014-04-25 12:59:31 +01001121 case MMC_TIMING_UHS_SDR104:
1122 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001123 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1124 break;
Russell Kingd975f122014-04-25 12:59:31 +01001125 case MMC_TIMING_UHS_DDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001126 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1127 break;
1128 default:
1129 pr_warn("%s: Invalid UHS-I mode selected\n",
1130 mmc_hostname(host->mmc));
1131 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1132 break;
1133 }
1134 return preset;
1135}
1136
Russell King17710592014-04-25 12:58:55 +01001137void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001138{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301139 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001140 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301141 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001142 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001143
Russell King1650d0c2014-04-25 12:58:50 +01001144 host->mmc->actual_clock = 0;
1145
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001146 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001147
1148 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001149 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001150
Zhangfei Gao85105c52010-08-06 07:10:01 +08001151 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001152 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001153 u16 pre_val;
1154
1155 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1156 pre_val = sdhci_get_preset_value(host);
1157 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1158 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1159 if (host->clk_mul &&
1160 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1161 clk = SDHCI_PROG_CLOCK_MODE;
1162 real_div = div + 1;
1163 clk_mul = host->clk_mul;
1164 } else {
1165 real_div = max_t(int, 1, div << 1);
1166 }
1167 goto clock_set;
1168 }
1169
Arindam Nathc3ed3872011-05-05 12:19:06 +05301170 /*
1171 * Check if the Host Controller supports Programmable Clock
1172 * Mode.
1173 */
1174 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001175 for (div = 1; div <= 1024; div++) {
1176 if ((host->max_clk * host->clk_mul / div)
1177 <= clock)
1178 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001179 }
Kevin Liu52983382013-01-31 11:31:37 +08001180 /*
1181 * Set Programmable Clock Mode in the Clock
1182 * Control register.
1183 */
1184 clk = SDHCI_PROG_CLOCK_MODE;
1185 real_div = div;
1186 clk_mul = host->clk_mul;
1187 div--;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301188 } else {
1189 /* Version 3.00 divisors must be a multiple of 2. */
1190 if (host->max_clk <= clock)
1191 div = 1;
1192 else {
1193 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1194 div += 2) {
1195 if ((host->max_clk / div) <= clock)
1196 break;
1197 }
1198 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001199 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301200 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001201 }
1202 } else {
1203 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001204 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001205 if ((host->max_clk / div) <= clock)
1206 break;
1207 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001208 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301209 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001210 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001211
Kevin Liu52983382013-01-31 11:31:37 +08001212clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001213 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001214 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301215 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001216 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1217 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001218 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001219 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001220
Chris Ball27f6cb12009-09-22 16:45:31 -07001221 /* Wait max 20 ms */
1222 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001223 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001224 & SDHCI_CLOCK_INT_STABLE)) {
1225 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301226 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001227 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001228 sdhci_dumpregs(host);
1229 return;
1230 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001231 timeout--;
1232 mdelay(1);
1233 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001234
1235 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001236 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001237}
Russell King17710592014-04-25 12:58:55 +01001238EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001239
Russell King24fbb3c2014-04-25 13:00:06 +01001240static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1241 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001242{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001243 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001244 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001245
Tim Kryger52221612014-06-25 00:25:34 -07001246 if (!IS_ERR(mmc->supply.vmmc)) {
1247 spin_unlock_irq(&host->lock);
Markus Mayer4e743f12014-07-03 13:27:42 -07001248 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Tim Kryger52221612014-06-25 00:25:34 -07001249 spin_lock_irq(&host->lock);
1250 return;
1251 }
1252
Russell King24fbb3c2014-04-25 13:00:06 +01001253 if (mode != MMC_POWER_OFF) {
1254 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001255 case MMC_VDD_165_195:
1256 pwr = SDHCI_POWER_180;
1257 break;
1258 case MMC_VDD_29_30:
1259 case MMC_VDD_30_31:
1260 pwr = SDHCI_POWER_300;
1261 break;
1262 case MMC_VDD_32_33:
1263 case MMC_VDD_33_34:
1264 pwr = SDHCI_POWER_330;
1265 break;
1266 default:
1267 BUG();
1268 }
1269 }
1270
1271 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001272 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001273
Pierre Ossmanae628902009-05-03 20:45:03 +02001274 host->pwr = pwr;
1275
1276 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001277 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001278 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1279 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001280 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001281 } else {
1282 /*
1283 * Spec says that we should clear the power reg before setting
1284 * a new value. Some controllers don't seem to like this though.
1285 */
1286 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1287 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001288
Russell Kinge921a8b2014-04-25 13:00:01 +01001289 /*
1290 * At least the Marvell CaFe chip gets confused if we set the
1291 * voltage and set turn on power at the same time, so set the
1292 * voltage first.
1293 */
1294 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1295 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001296
Russell Kinge921a8b2014-04-25 13:00:01 +01001297 pwr |= SDHCI_POWER_ON;
1298
Pierre Ossmanae628902009-05-03 20:45:03 +02001299 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1300
Russell Kinge921a8b2014-04-25 13:00:01 +01001301 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1302 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001303
Russell Kinge921a8b2014-04-25 13:00:01 +01001304 /*
1305 * Some controllers need an extra 10ms delay of 10ms before
1306 * they can apply clock after applying power
1307 */
1308 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1309 mdelay(10);
1310 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001311}
1312
Pierre Ossmand129bce2006-03-24 03:18:17 -08001313/*****************************************************************************\
1314 * *
1315 * MMC callbacks *
1316 * *
1317\*****************************************************************************/
1318
1319static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1320{
1321 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001322 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001323 unsigned long flags;
Aaron Lu473b0952012-07-03 17:27:49 +08001324 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001325
1326 host = mmc_priv(mmc);
1327
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001328 sdhci_runtime_pm_get(host);
1329
Pierre Ossmand129bce2006-03-24 03:18:17 -08001330 spin_lock_irqsave(&host->lock, flags);
1331
1332 WARN_ON(host->mrq != NULL);
1333
Pierre Ossmanf9134312008-12-21 17:01:48 +01001334#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001335 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001336#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001337
1338 /*
1339 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1340 * requests if Auto-CMD12 is enabled.
1341 */
1342 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001343 if (mrq->stop) {
1344 mrq->data->stop = NULL;
1345 mrq->stop = NULL;
1346 }
1347 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001348
1349 host->mrq = mrq;
1350
Shawn Guo505a8682012-12-11 15:23:42 +08001351 /*
1352 * Firstly check card presence from cd-gpio. The return could
1353 * be one of the following possibilities:
1354 * negative: cd-gpio is not available
1355 * zero: cd-gpio is used, and card is removed
1356 * one: cd-gpio is used, and card is present
1357 */
1358 present = mmc_gpio_get_cd(host->mmc);
1359 if (present < 0) {
1360 /* If polling, assume that the card is always present. */
1361 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1362 present = 1;
1363 else
1364 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1365 SDHCI_CARD_PRESENT;
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +08001366 }
1367
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001368 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001369 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001370 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301371 } else {
1372 u32 present_state;
1373
1374 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1375 /*
1376 * Check if the re-tuning timer has already expired and there
Yi Sun7756a96d2014-09-09 02:13:59 +00001377 * is no on-going data transfer and DAT0 is not busy. If so,
1378 * we need to execute tuning procedure before sending command.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301379 */
1380 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
Yi Sun7756a96d2014-09-09 02:13:59 +00001381 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ)) &&
1382 (present_state & SDHCI_DATA_0_LVL_MASK)) {
Chris Ball14efd952012-11-05 14:29:49 -05001383 if (mmc->card) {
1384 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1385 tuning_opcode =
1386 mmc->card->type == MMC_TYPE_MMC ?
1387 MMC_SEND_TUNING_BLOCK_HS200 :
1388 MMC_SEND_TUNING_BLOCK;
Chuansheng Liu63c21182013-11-05 14:52:45 +08001389
1390 /* Here we need to set the host->mrq to NULL,
1391 * in case the pending finish_tasklet
1392 * finishes it incorrectly.
1393 */
1394 host->mrq = NULL;
1395
Chris Ball14efd952012-11-05 14:29:49 -05001396 spin_unlock_irqrestore(&host->lock, flags);
1397 sdhci_execute_tuning(mmc, tuning_opcode);
1398 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301399
Chris Ball14efd952012-11-05 14:29:49 -05001400 /* Restore original mmc_request structure */
1401 host->mrq = mrq;
1402 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301403 }
1404
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001405 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001406 sdhci_send_command(host, mrq->sbc);
1407 else
1408 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301409 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001410
Pierre Ossman5f25a662006-10-04 02:15:39 -07001411 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001412 spin_unlock_irqrestore(&host->lock, flags);
1413}
1414
Russell King2317f562014-04-25 12:57:07 +01001415void sdhci_set_bus_width(struct sdhci_host *host, int width)
1416{
1417 u8 ctrl;
1418
1419 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1420 if (width == MMC_BUS_WIDTH_8) {
1421 ctrl &= ~SDHCI_CTRL_4BITBUS;
1422 if (host->version >= SDHCI_SPEC_300)
1423 ctrl |= SDHCI_CTRL_8BITBUS;
1424 } else {
1425 if (host->version >= SDHCI_SPEC_300)
1426 ctrl &= ~SDHCI_CTRL_8BITBUS;
1427 if (width == MMC_BUS_WIDTH_4)
1428 ctrl |= SDHCI_CTRL_4BITBUS;
1429 else
1430 ctrl &= ~SDHCI_CTRL_4BITBUS;
1431 }
1432 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1433}
1434EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1435
Russell King96d7b782014-04-25 12:59:26 +01001436void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1437{
1438 u16 ctrl_2;
1439
1440 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1441 /* Select Bus Speed Mode for host */
1442 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1443 if ((timing == MMC_TIMING_MMC_HS200) ||
1444 (timing == MMC_TIMING_UHS_SDR104))
1445 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1446 else if (timing == MMC_TIMING_UHS_SDR12)
1447 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1448 else if (timing == MMC_TIMING_UHS_SDR25)
1449 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1450 else if (timing == MMC_TIMING_UHS_SDR50)
1451 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1452 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1453 (timing == MMC_TIMING_MMC_DDR52))
1454 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1455 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1456}
1457EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1458
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001459static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001460{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001461 unsigned long flags;
1462 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001463 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001464
Pierre Ossmand129bce2006-03-24 03:18:17 -08001465 spin_lock_irqsave(&host->lock, flags);
1466
Adrian Hunterceb61432011-12-27 15:48:41 +02001467 if (host->flags & SDHCI_DEVICE_DEAD) {
1468 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001469 if (!IS_ERR(mmc->supply.vmmc) &&
1470 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001471 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001472 return;
1473 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001474
Pierre Ossmand129bce2006-03-24 03:18:17 -08001475 /*
1476 * Reset the chip on each power off.
1477 * Should clear out any weird states.
1478 */
1479 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001480 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001481 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001482 }
1483
Kevin Liu52983382013-01-31 11:31:37 +08001484 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001485 (ios->power_mode == MMC_POWER_UP) &&
1486 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001487 sdhci_enable_preset_value(host, false);
1488
Russell King373073e2014-04-25 12:58:45 +01001489 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001490 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001491 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001492
1493 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1494 host->clock) {
1495 host->timeout_clk = host->mmc->actual_clock ?
1496 host->mmc->actual_clock / 1000 :
1497 host->clock / 1000;
1498 host->mmc->max_busy_timeout =
1499 host->ops->get_max_timeout_count ?
1500 host->ops->get_max_timeout_count(host) :
1501 1 << 27;
1502 host->mmc->max_busy_timeout /= host->timeout_clk;
1503 }
Russell King373073e2014-04-25 12:58:45 +01001504 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001505
Russell King24fbb3c2014-04-25 13:00:06 +01001506 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001507
Philip Rakity643a81f2010-09-23 08:24:32 -07001508 if (host->ops->platform_send_init_74_clocks)
1509 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1510
Russell King2317f562014-04-25 12:57:07 +01001511 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001512
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001513 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001514
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001515 if ((ios->timing == MMC_TIMING_SD_HS ||
1516 ios->timing == MMC_TIMING_MMC_HS)
1517 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001518 ctrl |= SDHCI_CTRL_HISPD;
1519 else
1520 ctrl &= ~SDHCI_CTRL_HISPD;
1521
Arindam Nathd6d50a12011-05-05 12:18:59 +05301522 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301523 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301524
1525 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301526 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001527 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301528 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301529 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1530 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001531 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301532 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301533
Russell Kingda91a8f2014-04-25 13:00:12 +01001534 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301535 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301536 /*
1537 * We only need to set Driver Strength if the
1538 * preset value enable is not set.
1539 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001540 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301541 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1542 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1543 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1544 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1545 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1546
1547 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301548 } else {
1549 /*
1550 * According to SDHC Spec v3.00, if the Preset Value
1551 * Enable in the Host Control 2 register is set, we
1552 * need to reset SD Clock Enable before changing High
1553 * Speed Enable to avoid generating clock gliches.
1554 */
Arindam Nath758535c2011-05-05 12:19:00 +05301555
1556 /* Reset SD Clock Enable */
1557 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1558 clk &= ~SDHCI_CLOCK_CARD_EN;
1559 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1560
1561 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1562
1563 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001564 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301565 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301566
Arindam Nath49c468f2011-05-05 12:19:01 +05301567 /* Reset SD Clock Enable */
1568 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1569 clk &= ~SDHCI_CLOCK_CARD_EN;
1570 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1571
Russell King96d7b782014-04-25 12:59:26 +01001572 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001573 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301574
Kevin Liu52983382013-01-31 11:31:37 +08001575 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1576 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1577 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1578 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1579 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1580 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1581 u16 preset;
1582
1583 sdhci_enable_preset_value(host, true);
1584 preset = sdhci_get_preset_value(host);
1585 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1586 >> SDHCI_PRESET_DRV_SHIFT;
1587 }
1588
Arindam Nath49c468f2011-05-05 12:19:01 +05301589 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001590 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301591 } else
1592 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301593
Leandro Dorileob8352262007-07-25 23:47:04 +02001594 /*
1595 * Some (ENE) controllers go apeshit on some ios operation,
1596 * signalling timeout and CRC errors even on CMD0. Resetting
1597 * it on each ios seems to solve the problem.
1598 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001599 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001600 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001601
Pierre Ossman5f25a662006-10-04 02:15:39 -07001602 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001603 spin_unlock_irqrestore(&host->lock, flags);
1604}
1605
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001606static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1607{
1608 struct sdhci_host *host = mmc_priv(mmc);
1609
1610 sdhci_runtime_pm_get(host);
1611 sdhci_do_set_ios(host, ios);
1612 sdhci_runtime_pm_put(host);
1613}
1614
Kevin Liu94144a42013-02-28 17:35:53 +08001615static int sdhci_do_get_cd(struct sdhci_host *host)
1616{
1617 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1618
1619 if (host->flags & SDHCI_DEVICE_DEAD)
1620 return 0;
1621
1622 /* If polling/nonremovable, assume that the card is always present. */
1623 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1624 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1625 return 1;
1626
1627 /* Try slot gpio detect */
1628 if (!IS_ERR_VALUE(gpio_cd))
1629 return !!gpio_cd;
1630
1631 /* Host native card detect */
1632 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1633}
1634
1635static int sdhci_get_cd(struct mmc_host *mmc)
1636{
1637 struct sdhci_host *host = mmc_priv(mmc);
1638 int ret;
1639
1640 sdhci_runtime_pm_get(host);
1641 ret = sdhci_do_get_cd(host);
1642 sdhci_runtime_pm_put(host);
1643 return ret;
1644}
1645
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001646static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001647{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001648 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001649 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001650
Pierre Ossmand129bce2006-03-24 03:18:17 -08001651 spin_lock_irqsave(&host->lock, flags);
1652
Pierre Ossman1e728592008-04-16 19:13:13 +02001653 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001654 is_readonly = 0;
1655 else if (host->ops->get_ro)
1656 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001657 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001658 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1659 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001660
1661 spin_unlock_irqrestore(&host->lock, flags);
1662
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001663 /* This quirk needs to be replaced by a callback-function later */
1664 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1665 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001666}
1667
Takashi Iwai82b0e232011-04-21 20:26:38 +02001668#define SAMPLE_COUNT 5
1669
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001670static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001671{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001672 int i, ro_count;
1673
Takashi Iwai82b0e232011-04-21 20:26:38 +02001674 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001675 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001676
1677 ro_count = 0;
1678 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001679 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001680 if (++ro_count > SAMPLE_COUNT / 2)
1681 return 1;
1682 }
1683 msleep(30);
1684 }
1685 return 0;
1686}
1687
Adrian Hunter20758b62011-08-29 16:42:12 +03001688static void sdhci_hw_reset(struct mmc_host *mmc)
1689{
1690 struct sdhci_host *host = mmc_priv(mmc);
1691
1692 if (host->ops && host->ops->hw_reset)
1693 host->ops->hw_reset(host);
1694}
1695
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001696static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001697{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001698 struct sdhci_host *host = mmc_priv(mmc);
1699 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001700
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001701 sdhci_runtime_pm_get(host);
1702 ret = sdhci_do_get_ro(host);
1703 sdhci_runtime_pm_put(host);
1704 return ret;
1705}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001706
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001707static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1708{
Russell Kingbe138552014-04-25 12:55:56 +01001709 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001710 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001711 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001712 else
Russell Kingb537f942014-04-25 12:56:01 +01001713 host->ier &= ~SDHCI_INT_CARD_INT;
1714
1715 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1716 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001717 mmiowb();
1718 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001719}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001720
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001721static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1722{
1723 struct sdhci_host *host = mmc_priv(mmc);
1724 unsigned long flags;
1725
Russell Kingef104332014-04-25 12:55:41 +01001726 sdhci_runtime_pm_get(host);
1727
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001728 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001729 if (enable)
1730 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1731 else
1732 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1733
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001734 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001735 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001736
1737 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001738}
1739
Philip Rakity6231f3d2012-07-23 15:56:23 -07001740static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001741 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001742{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001743 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001744 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001745 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001746
1747 /*
1748 * Signal Voltage Switching is only applicable for Host Controllers
1749 * v3.00 and above.
1750 */
1751 if (host->version < SDHCI_SPEC_300)
1752 return 0;
1753
Philip Rakity6231f3d2012-07-23 15:56:23 -07001754 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001755
Fabio Estevam21f59982013-02-14 10:35:03 -02001756 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001757 case MMC_SIGNAL_VOLTAGE_330:
1758 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1759 ctrl &= ~SDHCI_CTRL_VDD_180;
1760 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1761
Tim Kryger3a48edc2014-06-13 10:13:56 -07001762 if (!IS_ERR(mmc->supply.vqmmc)) {
1763 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1764 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001765 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001766 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1767 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001768 return -EIO;
1769 }
1770 }
1771 /* Wait for 5ms */
1772 usleep_range(5000, 5500);
1773
1774 /* 3.3V regulator output should be stable within 5 ms */
1775 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1776 if (!(ctrl & SDHCI_CTRL_VDD_180))
1777 return 0;
1778
Joe Perches66061102014-09-12 14:56:56 -07001779 pr_warn("%s: 3.3V regulator output did not became stable\n",
1780 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001781
1782 return -EAGAIN;
1783 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001784 if (!IS_ERR(mmc->supply.vqmmc)) {
1785 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001786 1700000, 1950000);
1787 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001788 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1789 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001790 return -EIO;
1791 }
1792 }
1793
1794 /*
1795 * Enable 1.8V Signal Enable in the Host Control2
1796 * register
1797 */
1798 ctrl |= SDHCI_CTRL_VDD_180;
1799 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1800
Kevin Liu20b92a32012-12-17 19:29:26 +08001801 /* 1.8V regulator output should be stable within 5 ms */
1802 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1803 if (ctrl & SDHCI_CTRL_VDD_180)
1804 return 0;
1805
Joe Perches66061102014-09-12 14:56:56 -07001806 pr_warn("%s: 1.8V regulator output did not became stable\n",
1807 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001808
1809 return -EAGAIN;
1810 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001811 if (!IS_ERR(mmc->supply.vqmmc)) {
1812 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1813 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001814 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001815 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1816 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001817 return -EIO;
1818 }
1819 }
1820 return 0;
1821 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301822 /* No signal voltage switch required */
1823 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001824 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301825}
1826
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001827static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001828 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001829{
1830 struct sdhci_host *host = mmc_priv(mmc);
1831 int err;
1832
1833 if (host->version < SDHCI_SPEC_300)
1834 return 0;
1835 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001836 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001837 sdhci_runtime_pm_put(host);
1838 return err;
1839}
1840
Kevin Liu20b92a32012-12-17 19:29:26 +08001841static int sdhci_card_busy(struct mmc_host *mmc)
1842{
1843 struct sdhci_host *host = mmc_priv(mmc);
1844 u32 present_state;
1845
1846 sdhci_runtime_pm_get(host);
1847 /* Check whether DAT[3:0] is 0000 */
1848 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1849 sdhci_runtime_pm_put(host);
1850
1851 return !(present_state & SDHCI_DATA_LVL_MASK);
1852}
1853
Girish K S069c9f12012-01-06 09:56:39 +05301854static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301855{
Russell King4b6f37d2014-04-25 12:59:36 +01001856 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301857 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301858 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301859 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001860 unsigned long flags;
Arindam Nathb513ea22011-05-05 12:19:04 +05301861
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001862 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001863 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301864
Arindam Nathb513ea22011-05-05 12:19:04 +05301865 /*
Girish K S069c9f12012-01-06 09:56:39 +05301866 * The Host Controller needs tuning only in case of SDR104 mode
1867 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301868 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301869 * If the Host Controller supports the HS200 mode then the
1870 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301871 */
Russell King4b6f37d2014-04-25 12:59:36 +01001872 switch (host->timing) {
1873 case MMC_TIMING_MMC_HS200:
1874 case MMC_TIMING_UHS_SDR104:
1875 break;
Girish K S069c9f12012-01-06 09:56:39 +05301876
Russell King4b6f37d2014-04-25 12:59:36 +01001877 case MMC_TIMING_UHS_SDR50:
1878 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1879 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1880 break;
1881 /* FALLTHROUGH */
1882
1883 default:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001884 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001885 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301886 return 0;
1887 }
1888
Dong Aisheng45251812013-09-13 19:11:30 +08001889 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001890 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001891 err = host->ops->platform_execute_tuning(host, opcode);
1892 sdhci_runtime_pm_put(host);
1893 return err;
1894 }
1895
Russell King4b6f37d2014-04-25 12:59:36 +01001896 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1897 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Arindam Nathb513ea22011-05-05 12:19:04 +05301898 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1899
1900 /*
1901 * As per the Host Controller spec v3.00, tuning command
1902 * generates Buffer Read Ready interrupt, so enable that.
1903 *
1904 * Note: The spec clearly says that when tuning sequence
1905 * is being performed, the controller does not generate
1906 * interrupts other than Buffer Read Ready interrupt. But
1907 * to make sure we don't hit a controller bug, we _only_
1908 * enable Buffer Read Ready interrupt here.
1909 */
Russell Kingb537f942014-04-25 12:56:01 +01001910 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1911 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301912
1913 /*
1914 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1915 * of loops reaches 40 times or a timeout of 150ms occurs.
1916 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301917 do {
1918 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001919 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301920
Girish K S069c9f12012-01-06 09:56:39 +05301921 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301922 cmd.arg = 0;
1923 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1924 cmd.retries = 0;
1925 cmd.data = NULL;
1926 cmd.error = 0;
1927
Al Cooper7ce45e92014-05-09 11:34:07 -04001928 if (tuning_loop_counter-- == 0)
1929 break;
1930
Arindam Nathb513ea22011-05-05 12:19:04 +05301931 mrq.cmd = &cmd;
1932 host->mrq = &mrq;
1933
1934 /*
1935 * In response to CMD19, the card sends 64 bytes of tuning
1936 * block to the Host Controller. So we set the block size
1937 * to 64 here.
1938 */
Girish K S069c9f12012-01-06 09:56:39 +05301939 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1940 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1941 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1942 SDHCI_BLOCK_SIZE);
1943 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1944 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1945 SDHCI_BLOCK_SIZE);
1946 } else {
1947 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1948 SDHCI_BLOCK_SIZE);
1949 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301950
1951 /*
1952 * The tuning block is sent by the card to the host controller.
1953 * So we set the TRNS_READ bit in the Transfer Mode register.
1954 * This also takes care of setting DMA Enable and Multi Block
1955 * Select in the same register to 0.
1956 */
1957 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1958
1959 sdhci_send_command(host, &cmd);
1960
1961 host->cmd = NULL;
1962 host->mrq = NULL;
1963
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001964 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301965 /* Wait for Buffer Read Ready interrupt */
1966 wait_event_interruptible_timeout(host->buf_ready_int,
1967 (host->tuning_done == 1),
1968 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001969 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301970
1971 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301972 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301973 "Buffer Read Ready interrupt during tuning "
1974 "procedure, falling back to fixed sampling "
1975 "clock\n");
1976 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1977 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1978 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1979 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1980
1981 err = -EIO;
1982 goto out;
1983 }
1984
1985 host->tuning_done = 0;
1986
1987 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07001988
1989 /* eMMC spec does not require a delay between tuning cycles */
1990 if (opcode == MMC_SEND_TUNING_BLOCK)
1991 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05301992 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1993
1994 /*
1995 * The Host Driver has exhausted the maximum number of loops allowed,
1996 * so use fixed sampling frequency.
1997 */
Al Cooper7ce45e92014-05-09 11:34:07 -04001998 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05301999 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2000 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002001 }
2002 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
2003 pr_info(DRIVER_NAME ": Tuning procedure"
2004 " failed, falling back to fixed sampling"
2005 " clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002006 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302007 }
2008
2009out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302010 /*
2011 * If this is the very first time we are here, we start the retuning
2012 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2013 * flag won't be set, we check this condition before actually starting
2014 * the timer.
2015 */
2016 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2017 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
Aaron Lu973905f2012-07-04 13:29:09 +08002018 host->flags |= SDHCI_USING_RETUNING_TIMER;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302019 mod_timer(&host->tuning_timer, jiffies +
2020 host->tuning_count * HZ);
2021 /* Tuning mode 1 limits the maximum data length to 4MB */
2022 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
Arend van Spriel2bc02482014-01-04 13:51:26 +01002023 } else if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302024 host->flags &= ~SDHCI_NEEDS_RETUNING;
2025 /* Reload the new initial value for timer */
Arend van Spriel2bc02482014-01-04 13:51:26 +01002026 mod_timer(&host->tuning_timer, jiffies +
2027 host->tuning_count * HZ);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302028 }
2029
2030 /*
2031 * In case tuning fails, host controllers which support re-tuning can
2032 * try tuning again at a later time, when the re-tuning timer expires.
2033 * So for these controllers, we return 0. Since there might be other
2034 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08002035 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2036 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302037 */
Aaron Lu973905f2012-07-04 13:29:09 +08002038 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302039 err = 0;
2040
Russell Kingb537f942014-04-25 12:56:01 +01002041 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2042 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002043 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002044 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302045
2046 return err;
2047}
2048
Kevin Liu52983382013-01-31 11:31:37 +08002049
2050static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302051{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302052 /* Host Controller v3.00 defines preset value registers */
2053 if (host->version < SDHCI_SPEC_300)
2054 return;
2055
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302056 /*
2057 * We only enable or disable Preset Value if they are not already
2058 * enabled or disabled respectively. Otherwise, we bail out.
2059 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002060 if (host->preset_enabled != enable) {
2061 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2062
2063 if (enable)
2064 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2065 else
2066 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2067
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302068 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002069
2070 if (enable)
2071 host->flags |= SDHCI_PV_ENABLED;
2072 else
2073 host->flags &= ~SDHCI_PV_ENABLED;
2074
2075 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302076 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002077}
2078
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002079static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002080{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002081 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002082 unsigned long flags;
2083
Christian Daudt722e1282013-06-20 14:26:36 -07002084 /* First check if client has provided their own card event */
2085 if (host->ops->card_event)
2086 host->ops->card_event(host);
2087
Pierre Ossmand129bce2006-03-24 03:18:17 -08002088 spin_lock_irqsave(&host->lock, flags);
2089
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002090 /* Check host->mrq first in case we are runtime suspended */
Shawn Guo9668d762013-06-09 19:49:24 +08002091 if (host->mrq && !sdhci_do_get_cd(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302092 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002093 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302094 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002095 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002096
Russell King03231f92014-04-25 12:57:12 +01002097 sdhci_do_reset(host, SDHCI_RESET_CMD);
2098 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002099
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002100 host->mrq->cmd->error = -ENOMEDIUM;
2101 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002102 }
2103
2104 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002105}
2106
2107static const struct mmc_host_ops sdhci_ops = {
2108 .request = sdhci_request,
2109 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002110 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002111 .get_ro = sdhci_get_ro,
2112 .hw_reset = sdhci_hw_reset,
2113 .enable_sdio_irq = sdhci_enable_sdio_irq,
2114 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2115 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002116 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002117 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002118};
2119
2120/*****************************************************************************\
2121 * *
2122 * Tasklets *
2123 * *
2124\*****************************************************************************/
2125
Pierre Ossmand129bce2006-03-24 03:18:17 -08002126static void sdhci_tasklet_finish(unsigned long param)
2127{
2128 struct sdhci_host *host;
2129 unsigned long flags;
2130 struct mmc_request *mrq;
2131
2132 host = (struct sdhci_host*)param;
2133
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002134 spin_lock_irqsave(&host->lock, flags);
2135
Chris Ball0c9c99a2011-04-27 17:35:31 -04002136 /*
2137 * If this tasklet gets rescheduled while running, it will
2138 * be run again afterwards but without any active request.
2139 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002140 if (!host->mrq) {
2141 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002142 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002143 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002144
2145 del_timer(&host->timer);
2146
2147 mrq = host->mrq;
2148
Pierre Ossmand129bce2006-03-24 03:18:17 -08002149 /*
2150 * The controller needs a reset of internal state machines
2151 * upon error conditions.
2152 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002153 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002154 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002155 (mrq->sbc && mrq->sbc->error) ||
2156 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2157 (mrq->data->stop && mrq->data->stop->error))) ||
2158 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002159
2160 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002161 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002162 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002163 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002164
2165 /* Spec says we should do both at the same time, but Ricoh
2166 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002167 sdhci_do_reset(host, SDHCI_RESET_CMD);
2168 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002169 }
2170
2171 host->mrq = NULL;
2172 host->cmd = NULL;
2173 host->data = NULL;
2174
Pierre Ossmanf9134312008-12-21 17:01:48 +01002175#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002176 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002177#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002178
Pierre Ossman5f25a662006-10-04 02:15:39 -07002179 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002180 spin_unlock_irqrestore(&host->lock, flags);
2181
2182 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002183 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002184}
2185
2186static void sdhci_timeout_timer(unsigned long data)
2187{
2188 struct sdhci_host *host;
2189 unsigned long flags;
2190
2191 host = (struct sdhci_host*)data;
2192
2193 spin_lock_irqsave(&host->lock, flags);
2194
2195 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302196 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002197 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002198 sdhci_dumpregs(host);
2199
2200 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002201 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002202 sdhci_finish_data(host);
2203 } else {
2204 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002205 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002206 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002207 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002208
2209 tasklet_schedule(&host->finish_tasklet);
2210 }
2211 }
2212
Pierre Ossman5f25a662006-10-04 02:15:39 -07002213 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002214 spin_unlock_irqrestore(&host->lock, flags);
2215}
2216
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302217static void sdhci_tuning_timer(unsigned long data)
2218{
2219 struct sdhci_host *host;
2220 unsigned long flags;
2221
2222 host = (struct sdhci_host *)data;
2223
2224 spin_lock_irqsave(&host->lock, flags);
2225
2226 host->flags |= SDHCI_NEEDS_RETUNING;
2227
2228 spin_unlock_irqrestore(&host->lock, flags);
2229}
2230
Pierre Ossmand129bce2006-03-24 03:18:17 -08002231/*****************************************************************************\
2232 * *
2233 * Interrupt handling *
2234 * *
2235\*****************************************************************************/
2236
Adrian Hunter61541392014-09-24 10:27:27 +03002237static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002238{
2239 BUG_ON(intmask == 0);
2240
2241 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302242 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002243 "though no command operation was in progress.\n",
2244 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002245 sdhci_dumpregs(host);
2246 return;
2247 }
2248
Pierre Ossman43b58b32007-07-25 23:15:27 +02002249 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002250 host->cmd->error = -ETIMEDOUT;
2251 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2252 SDHCI_INT_INDEX))
2253 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002254
Pierre Ossmane8095172008-07-25 01:09:08 +02002255 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002256 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002257 return;
2258 }
2259
2260 /*
2261 * The host can send and interrupt when the busy state has
2262 * ended, allowing us to wait without wasting CPU cycles.
2263 * Unfortunately this is overloaded on the "data complete"
2264 * interrupt, so we need to take some care when handling
2265 * it.
2266 *
2267 * Note: The 1.0 specification is a bit ambiguous about this
2268 * feature so there might be some problems with older
2269 * controllers.
2270 */
2271 if (host->cmd->flags & MMC_RSP_BUSY) {
2272 if (host->cmd->data)
2273 DBG("Cannot wait for busy signal when also "
2274 "doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002275 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2276 && !host->busy_handle) {
2277 /* Mark that command complete before busy is ended */
2278 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002279 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002280 }
Ben Dooksf9454052009-02-20 20:33:08 +03002281
2282 /* The controller does not support the end-of-busy IRQ,
2283 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002284 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2285 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2286 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002287 }
2288
2289 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002290 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002291}
2292
George G. Davis0957c332010-02-18 12:32:12 -05002293#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002294static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002295{
2296 const char *name = mmc_hostname(host->mmc);
2297 u8 *desc = host->adma_desc;
2298 __le32 *dma;
2299 __le16 *len;
2300 u8 attr;
2301
2302 sdhci_dumpregs(host);
2303
2304 while (true) {
2305 dma = (__le32 *)(desc + 4);
2306 len = (__le16 *)(desc + 2);
2307 attr = *desc;
2308
2309 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2310 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2311
2312 desc += 8;
2313
2314 if (attr & 2)
2315 break;
2316 }
2317}
2318#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002319static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002320#endif
2321
Pierre Ossmand129bce2006-03-24 03:18:17 -08002322static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2323{
Girish K S069c9f12012-01-06 09:56:39 +05302324 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002325 BUG_ON(intmask == 0);
2326
Arindam Nathb513ea22011-05-05 12:19:04 +05302327 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2328 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302329 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2330 if (command == MMC_SEND_TUNING_BLOCK ||
2331 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302332 host->tuning_done = 1;
2333 wake_up(&host->buf_ready_int);
2334 return;
2335 }
2336 }
2337
Pierre Ossmand129bce2006-03-24 03:18:17 -08002338 if (!host->data) {
2339 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002340 * The "data complete" interrupt is also used to
2341 * indicate that a busy state has ended. See comment
2342 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002343 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002344 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002345 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2346 host->cmd->error = -ETIMEDOUT;
2347 tasklet_schedule(&host->finish_tasklet);
2348 return;
2349 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002350 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002351 /*
2352 * Some cards handle busy-end interrupt
2353 * before the command completed, so make
2354 * sure we do things in the proper order.
2355 */
2356 if (host->busy_handle)
2357 sdhci_finish_command(host);
2358 else
2359 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002360 return;
2361 }
2362 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002363
Girish K Sa3c76eb2011-10-11 11:44:09 +05302364 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002365 "though no data operation was in progress.\n",
2366 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002367 sdhci_dumpregs(host);
2368
2369 return;
2370 }
2371
2372 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002373 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002374 else if (intmask & SDHCI_INT_DATA_END_BIT)
2375 host->data->error = -EILSEQ;
2376 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2377 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2378 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002379 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002380 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302381 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002382 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002383 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002384 if (host->ops->adma_workaround)
2385 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002386 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002387
Pierre Ossman17b04292007-07-22 22:18:46 +02002388 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002389 sdhci_finish_data(host);
2390 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002391 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002392 sdhci_transfer_pio(host);
2393
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002394 /*
2395 * We currently don't do anything fancy with DMA
2396 * boundaries, but as we can't disable the feature
2397 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002398 *
2399 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2400 * should return a valid address to continue from, but as
2401 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002402 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002403 if (intmask & SDHCI_INT_DMA_END) {
2404 u32 dmastart, dmanow;
2405 dmastart = sg_dma_address(host->data->sg);
2406 dmanow = dmastart + host->data->bytes_xfered;
2407 /*
2408 * Force update to the next DMA block boundary.
2409 */
2410 dmanow = (dmanow &
2411 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2412 SDHCI_DEFAULT_BOUNDARY_SIZE;
2413 host->data->bytes_xfered = dmanow - dmastart;
2414 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2415 " next 0x%08x\n",
2416 mmc_hostname(host->mmc), dmastart,
2417 host->data->bytes_xfered, dmanow);
2418 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2419 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002420
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002421 if (intmask & SDHCI_INT_DATA_END) {
2422 if (host->cmd) {
2423 /*
2424 * Data managed to finish before the
2425 * command completed. Make sure we do
2426 * things in the proper order.
2427 */
2428 host->data_early = 1;
2429 } else {
2430 sdhci_finish_data(host);
2431 }
2432 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002433 }
2434}
2435
David Howells7d12e782006-10-05 14:55:46 +01002436static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002437{
Russell King781e9892014-04-25 12:55:46 +01002438 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002439 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002440 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002441 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002442
2443 spin_lock(&host->lock);
2444
Russell Kingbe138552014-04-25 12:55:56 +01002445 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002446 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002447 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002448 }
2449
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002450 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002451 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002452 result = IRQ_NONE;
2453 goto out;
2454 }
2455
Russell King41005002014-04-25 12:55:36 +01002456 do {
2457 /* Clear selected interrupts. */
2458 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2459 SDHCI_INT_BUS_POWER);
2460 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002461
Russell King41005002014-04-25 12:55:36 +01002462 DBG("*** %s got interrupt: 0x%08x\n",
2463 mmc_hostname(host->mmc), intmask);
2464
2465 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2466 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2467 SDHCI_CARD_PRESENT;
2468
2469 /*
2470 * There is a observation on i.mx esdhc. INSERT
2471 * bit will be immediately set again when it gets
2472 * cleared, if a card is inserted. We have to mask
2473 * the irq to prevent interrupt storm which will
2474 * freeze the system. And the REMOVE gets the
2475 * same situation.
2476 *
2477 * More testing are needed here to ensure it works
2478 * for other platforms though.
2479 */
Russell Kingb537f942014-04-25 12:56:01 +01002480 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2481 SDHCI_INT_CARD_REMOVE);
2482 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2483 SDHCI_INT_CARD_INSERT;
2484 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2485 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002486
2487 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2488 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002489
2490 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2491 SDHCI_INT_CARD_REMOVE);
2492 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002493 }
2494
2495 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002496 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2497 &intmask);
Russell King41005002014-04-25 12:55:36 +01002498
2499 if (intmask & SDHCI_INT_DATA_MASK)
2500 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2501
2502 if (intmask & SDHCI_INT_BUS_POWER)
2503 pr_err("%s: Card is consuming too much power!\n",
2504 mmc_hostname(host->mmc));
2505
Russell King781e9892014-04-25 12:55:46 +01002506 if (intmask & SDHCI_INT_CARD_INT) {
2507 sdhci_enable_sdio_irq_nolock(host, false);
2508 host->thread_isr |= SDHCI_INT_CARD_INT;
2509 result = IRQ_WAKE_THREAD;
2510 }
Russell King41005002014-04-25 12:55:36 +01002511
2512 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2513 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2514 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2515 SDHCI_INT_CARD_INT);
2516
2517 if (intmask) {
2518 unexpected |= intmask;
2519 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2520 }
2521
Russell King781e9892014-04-25 12:55:46 +01002522 if (result == IRQ_NONE)
2523 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002524
2525 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002526 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002527out:
2528 spin_unlock(&host->lock);
2529
Alexander Stein6379b232012-03-14 09:52:10 +01002530 if (unexpected) {
2531 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2532 mmc_hostname(host->mmc), unexpected);
2533 sdhci_dumpregs(host);
2534 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002535
Pierre Ossmand129bce2006-03-24 03:18:17 -08002536 return result;
2537}
2538
Russell King781e9892014-04-25 12:55:46 +01002539static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2540{
2541 struct sdhci_host *host = dev_id;
2542 unsigned long flags;
2543 u32 isr;
2544
2545 spin_lock_irqsave(&host->lock, flags);
2546 isr = host->thread_isr;
2547 host->thread_isr = 0;
2548 spin_unlock_irqrestore(&host->lock, flags);
2549
Russell King3560db82014-04-25 12:55:51 +01002550 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2551 sdhci_card_event(host->mmc);
2552 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2553 }
2554
Russell King781e9892014-04-25 12:55:46 +01002555 if (isr & SDHCI_INT_CARD_INT) {
2556 sdio_run_irqs(host->mmc);
2557
2558 spin_lock_irqsave(&host->lock, flags);
2559 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2560 sdhci_enable_sdio_irq_nolock(host, true);
2561 spin_unlock_irqrestore(&host->lock, flags);
2562 }
2563
2564 return isr ? IRQ_HANDLED : IRQ_NONE;
2565}
2566
Pierre Ossmand129bce2006-03-24 03:18:17 -08002567/*****************************************************************************\
2568 * *
2569 * Suspend/resume *
2570 * *
2571\*****************************************************************************/
2572
2573#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002574void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2575{
2576 u8 val;
2577 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2578 | SDHCI_WAKE_ON_INT;
2579
2580 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2581 val |= mask ;
2582 /* Avoid fake wake up */
2583 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2584 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2585 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2586}
2587EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2588
Fabio Estevam0b10f472014-08-30 14:53:13 -03002589static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002590{
2591 u8 val;
2592 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2593 | SDHCI_WAKE_ON_INT;
2594
2595 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2596 val &= ~mask;
2597 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2598}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002599
Manuel Lauss29495aa2011-11-03 11:09:45 +01002600int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002601{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002602 sdhci_disable_card_detection(host);
2603
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302604 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002605 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002606 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302607 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302608 }
2609
Kevin Liuad080d72013-01-05 17:21:33 +08002610 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002611 host->ier = 0;
2612 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2613 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002614 free_irq(host->irq, host);
2615 } else {
2616 sdhci_enable_irq_wakeups(host);
2617 enable_irq_wake(host->irq);
2618 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002619 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002620}
2621
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002622EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002623
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002624int sdhci_resume_host(struct sdhci_host *host)
2625{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002626 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002627
Richard Röjforsa13abc72009-09-22 16:45:30 -07002628 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002629 if (host->ops->enable_dma)
2630 host->ops->enable_dma(host);
2631 }
2632
Kevin Liuad080d72013-01-05 17:21:33 +08002633 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell King781e9892014-04-25 12:55:46 +01002634 ret = request_threaded_irq(host->irq, sdhci_irq,
2635 sdhci_thread_irq, IRQF_SHARED,
2636 mmc_hostname(host->mmc), host);
Kevin Liuad080d72013-01-05 17:21:33 +08002637 if (ret)
2638 return ret;
2639 } else {
2640 sdhci_disable_irq_wakeups(host);
2641 disable_irq_wake(host->irq);
2642 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002643
Adrian Hunter6308d292012-02-07 14:48:54 +02002644 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2645 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2646 /* Card keeps power but host controller does not */
2647 sdhci_init(host, 0);
2648 host->pwr = 0;
2649 host->clock = 0;
2650 sdhci_do_set_ios(host, &host->mmc->ios);
2651 } else {
2652 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2653 mmiowb();
2654 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002655
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002656 sdhci_enable_card_detection(host);
2657
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302658 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002659 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302660 host->flags |= SDHCI_NEEDS_RETUNING;
2661
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002662 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002663}
2664
2665EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002666#endif /* CONFIG_PM */
2667
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002668#ifdef CONFIG_PM_RUNTIME
2669
2670static int sdhci_runtime_pm_get(struct sdhci_host *host)
2671{
2672 return pm_runtime_get_sync(host->mmc->parent);
2673}
2674
2675static int sdhci_runtime_pm_put(struct sdhci_host *host)
2676{
2677 pm_runtime_mark_last_busy(host->mmc->parent);
2678 return pm_runtime_put_autosuspend(host->mmc->parent);
2679}
2680
Adrian Hunterf0710a52013-05-06 12:17:32 +03002681static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2682{
2683 if (host->runtime_suspended || host->bus_on)
2684 return;
2685 host->bus_on = true;
2686 pm_runtime_get_noresume(host->mmc->parent);
2687}
2688
2689static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2690{
2691 if (host->runtime_suspended || !host->bus_on)
2692 return;
2693 host->bus_on = false;
2694 pm_runtime_put_noidle(host->mmc->parent);
2695}
2696
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002697int sdhci_runtime_suspend_host(struct sdhci_host *host)
2698{
2699 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002700
2701 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002702 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002703 del_timer_sync(&host->tuning_timer);
2704 host->flags &= ~SDHCI_NEEDS_RETUNING;
2705 }
2706
2707 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002708 host->ier &= SDHCI_INT_CARD_INT;
2709 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2710 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002711 spin_unlock_irqrestore(&host->lock, flags);
2712
Russell King781e9892014-04-25 12:55:46 +01002713 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002714
2715 spin_lock_irqsave(&host->lock, flags);
2716 host->runtime_suspended = true;
2717 spin_unlock_irqrestore(&host->lock, flags);
2718
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002719 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002720}
2721EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2722
2723int sdhci_runtime_resume_host(struct sdhci_host *host)
2724{
2725 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002726 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002727
2728 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2729 if (host->ops->enable_dma)
2730 host->ops->enable_dma(host);
2731 }
2732
2733 sdhci_init(host, 0);
2734
2735 /* Force clock and power re-program */
2736 host->pwr = 0;
2737 host->clock = 0;
2738 sdhci_do_set_ios(host, &host->mmc->ios);
2739
2740 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002741 if ((host_flags & SDHCI_PV_ENABLED) &&
2742 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2743 spin_lock_irqsave(&host->lock, flags);
2744 sdhci_enable_preset_value(host, true);
2745 spin_unlock_irqrestore(&host->lock, flags);
2746 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002747
2748 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002749 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002750 host->flags |= SDHCI_NEEDS_RETUNING;
2751
2752 spin_lock_irqsave(&host->lock, flags);
2753
2754 host->runtime_suspended = false;
2755
2756 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002757 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002758 sdhci_enable_sdio_irq_nolock(host, true);
2759
2760 /* Enable Card Detection */
2761 sdhci_enable_card_detection(host);
2762
2763 spin_unlock_irqrestore(&host->lock, flags);
2764
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002765 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002766}
2767EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2768
2769#endif
2770
Pierre Ossmand129bce2006-03-24 03:18:17 -08002771/*****************************************************************************\
2772 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002773 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002774 * *
2775\*****************************************************************************/
2776
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002777struct sdhci_host *sdhci_alloc_host(struct device *dev,
2778 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002779{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002780 struct mmc_host *mmc;
2781 struct sdhci_host *host;
2782
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002783 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002784
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002785 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002786 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002787 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002788
2789 host = mmc_priv(mmc);
2790 host->mmc = mmc;
2791
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002792 return host;
2793}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002794
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002795EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002796
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002797int sdhci_add_host(struct sdhci_host *host)
2798{
2799 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002800 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302801 u32 max_current_caps;
2802 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002803 unsigned int override_timeout_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002804 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002805
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002806 WARN_ON(host == NULL);
2807 if (host == NULL)
2808 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002809
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002810 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002811
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002812 if (debug_quirks)
2813 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002814 if (debug_quirks2)
2815 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002816
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002817 override_timeout_clk = host->timeout_clk;
2818
Russell King03231f92014-04-25 12:57:12 +01002819 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002820
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002821 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002822 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2823 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002824 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302825 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002826 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002827 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002828 }
2829
Arindam Nathf2119df2011-05-05 12:18:57 +05302830 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002831 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002832
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002833 if (host->version >= SDHCI_SPEC_300)
2834 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2835 host->caps1 :
2836 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302837
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002838 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002839 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302840 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002841 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002842 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002843 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002844
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002845 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002846 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002847 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002848 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002849 }
2850
Arindam Nathf2119df2011-05-05 12:18:57 +05302851 if ((host->version >= SDHCI_SPEC_200) &&
2852 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002853 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002854
2855 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2856 (host->flags & SDHCI_USE_ADMA)) {
2857 DBG("Disabling ADMA as it is marked broken\n");
2858 host->flags &= ~SDHCI_USE_ADMA;
2859 }
2860
Richard Röjforsa13abc72009-09-22 16:45:30 -07002861 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002862 if (host->ops->enable_dma) {
2863 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07002864 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002865 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002866 host->flags &=
2867 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002868 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002869 }
2870 }
2871
Pierre Ossman2134a922008-06-28 18:28:51 +02002872 if (host->flags & SDHCI_USE_ADMA) {
2873 /*
2874 * We need to allocate descriptors for all sg entries
2875 * (128) and potentially one alignment transfer for
2876 * each of those entries.
2877 */
Markus Mayer4e743f12014-07-03 13:27:42 -07002878 host->adma_desc = dma_alloc_coherent(mmc_dev(mmc),
Russell Kingd1e49f72014-04-25 12:58:34 +01002879 ADMA_SIZE, &host->adma_addr,
2880 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02002881 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2882 if (!host->adma_desc || !host->align_buffer) {
Markus Mayer4e743f12014-07-03 13:27:42 -07002883 dma_free_coherent(mmc_dev(mmc), ADMA_SIZE,
Russell Kingd1e49f72014-04-25 12:58:34 +01002884 host->adma_desc, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02002885 kfree(host->align_buffer);
Joe Perches66061102014-09-12 14:56:56 -07002886 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002887 mmc_hostname(mmc));
2888 host->flags &= ~SDHCI_USE_ADMA;
Russell Kingd1e49f72014-04-25 12:58:34 +01002889 host->adma_desc = NULL;
2890 host->align_buffer = NULL;
2891 } else if (host->adma_addr & 3) {
Joe Perches66061102014-09-12 14:56:56 -07002892 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2893 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002894 host->flags &= ~SDHCI_USE_ADMA;
Markus Mayer4e743f12014-07-03 13:27:42 -07002895 dma_free_coherent(mmc_dev(mmc), ADMA_SIZE,
Russell Kingd1e49f72014-04-25 12:58:34 +01002896 host->adma_desc, host->adma_addr);
2897 kfree(host->align_buffer);
2898 host->adma_desc = NULL;
2899 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02002900 }
2901 }
2902
Pierre Ossman76591502008-07-21 00:32:11 +02002903 /*
2904 * If we use DMA, then it's up to the caller to set the DMA
2905 * mask, but PIO does not need the hw shim so we set a new
2906 * mask here in that case.
2907 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002908 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002909 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07002910 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02002911 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002912
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002913 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302914 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002915 >> SDHCI_CLOCK_BASE_SHIFT;
2916 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302917 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002918 >> SDHCI_CLOCK_BASE_SHIFT;
2919
Pierre Ossmand129bce2006-03-24 03:18:17 -08002920 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002921 if (host->max_clk == 0 || host->quirks &
2922 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002923 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302924 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002925 "frequency.\n", mmc_hostname(mmc));
2926 return -ENODEV;
2927 }
2928 host->max_clk = host->ops->get_max_clock(host);
2929 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002930
2931 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302932 * In case of Host Controller v3.00, find out whether clock
2933 * multiplier is supported.
2934 */
2935 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2936 SDHCI_CLOCK_MUL_SHIFT;
2937
2938 /*
2939 * In case the value in Clock Multiplier is 0, then programmable
2940 * clock mode is not supported, otherwise the actual clock
2941 * multiplier is one more than the value of Clock Multiplier
2942 * in the Capabilities Register.
2943 */
2944 if (host->clk_mul)
2945 host->clk_mul += 1;
2946
2947 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002948 * Set host parameters.
2949 */
2950 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302951 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002952 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002953 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302954 else if (host->version >= SDHCI_SPEC_300) {
2955 if (host->clk_mul) {
2956 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2957 mmc->f_max = host->max_clk * host->clk_mul;
2958 } else
2959 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2960 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002961 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002962
Aisheng Dong28aab052014-08-27 15:26:31 +08002963 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2964 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
2965 SDHCI_TIMEOUT_CLK_SHIFT;
2966 if (host->timeout_clk == 0) {
2967 if (host->ops->get_timeout_clock) {
2968 host->timeout_clk =
2969 host->ops->get_timeout_clock(host);
2970 } else {
2971 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
2972 mmc_hostname(mmc));
2973 return -ENODEV;
2974 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03002975 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03002976
Aisheng Dong28aab052014-08-27 15:26:31 +08002977 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2978 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002979
Aisheng Dong28aab052014-08-27 15:26:31 +08002980 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08002981 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08002982 mmc->max_busy_timeout /= host->timeout_clk;
2983 }
Adrian Hunter58d12462011-06-28 17:16:03 +03002984
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002985 if (override_timeout_clk)
2986 host->timeout_clk = override_timeout_clk;
2987
Andrei Warkentine89d4562011-05-23 15:06:37 -05002988 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01002989 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05002990
2991 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2992 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002993
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002994 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002995 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002996 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002997 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002998 host->flags |= SDHCI_AUTO_CMD23;
2999 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3000 } else {
3001 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3002 }
3003
Philip Rakity15ec4462010-11-19 16:48:39 -05003004 /*
3005 * A controller may support 8-bit width, but the board itself
3006 * might not have the pins brought out. Boards that support
3007 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3008 * their platform code before calling sdhci_add_host(), and we
3009 * won't assume 8-bit width for hosts without that CAP.
3010 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003011 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003012 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003013
Jerry Huang63ef5d82012-10-25 13:47:19 +08003014 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3015 mmc->caps &= ~MMC_CAP_CMD23;
3016
Arindam Nathf2119df2011-05-05 12:18:57 +05303017 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003018 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003019
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003020 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Markus Mayer4e743f12014-07-03 13:27:42 -07003021 !(mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003022 mmc->caps |= MMC_CAP_NEEDS_POLL;
3023
Tim Kryger3a48edc2014-06-13 10:13:56 -07003024 /* If there are external regulators, get them */
3025 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3026 return -EPROBE_DEFER;
3027
Philip Rakity6231f3d2012-07-23 15:56:23 -07003028 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003029 if (!IS_ERR(mmc->supply.vqmmc)) {
3030 ret = regulator_enable(mmc->supply.vqmmc);
3031 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3032 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003033 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3034 SDHCI_SUPPORT_SDR50 |
3035 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003036 if (ret) {
3037 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3038 mmc_hostname(mmc), ret);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003039 mmc->supply.vqmmc = NULL;
Chris Balla3361ab2013-03-11 17:51:53 -04003040 }
Kevin Liu8363c372012-11-17 17:55:51 -05003041 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003042
Daniel Drake6a661802012-11-25 13:01:19 -05003043 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3044 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3045 SDHCI_SUPPORT_DDR50);
3046
Al Cooper4188bba2012-03-16 15:54:17 -04003047 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3048 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3049 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303050 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3051
3052 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003053 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303054 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003055 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3056 * field can be promoted to support HS200.
3057 */
Chuanxiao.Dongadc82852014-08-19 11:02:41 +08003058 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200)) {
David Cohen13868bf2013-10-29 10:58:26 -07003059 mmc->caps2 |= MMC_CAP2_HS200;
Chuanxiao.Dongadc82852014-08-19 11:02:41 +08003060 if (IS_ERR(mmc->supply.vqmmc) ||
3061 !regulator_is_supported_voltage
3062 (mmc->supply.vqmmc, 1100000, 1300000))
3063 mmc->caps2 &= ~MMC_CAP2_HS200_1_2V_SDR;
3064 }
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003065 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303066 mmc->caps |= MMC_CAP_UHS_SDR50;
3067
Micky Ching9107ebb2014-02-21 18:40:35 +08003068 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3069 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303070 mmc->caps |= MMC_CAP_UHS_DDR50;
3071
Girish K S069c9f12012-01-06 09:56:39 +05303072 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303073 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3074 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3075
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003076 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303077 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003078 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303079
Arindam Nathd6d50a12011-05-05 12:18:59 +05303080 /* Driver Type(s) (A, C, D) supported by the host */
3081 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3082 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3083 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3084 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3085 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3086 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3087
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303088 /* Initial value for re-tuning timer count */
3089 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3090 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3091
3092 /*
3093 * In case Re-tuning Timer is not disabled, the actual value of
3094 * re-tuning timer will be 2 ^ (n - 1).
3095 */
3096 if (host->tuning_count)
3097 host->tuning_count = 1 << (host->tuning_count - 1);
3098
3099 /* Re-tuning mode supported by the Host Controller */
3100 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3101 SDHCI_RETUNING_MODE_SHIFT;
3102
Takashi Iwai8f230f42010-12-08 10:04:30 +01003103 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003104
Arindam Nathf2119df2011-05-05 12:18:57 +05303105 /*
3106 * According to SD Host Controller spec v3.00, if the Host System
3107 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3108 * the value is meaningful only if Voltage Support in the Capabilities
3109 * register is set. The actual current value is 4 times the register
3110 * value.
3111 */
3112 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003113 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003114 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003115 if (curr > 0) {
3116
3117 /* convert to SDHCI_MAX_CURRENT format */
3118 curr = curr/1000; /* convert to mA */
3119 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3120
3121 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3122 max_current_caps =
3123 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3124 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3125 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3126 }
3127 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303128
3129 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003130 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303131
Aaron Lu55c46652012-07-04 13:31:48 +08003132 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303133 SDHCI_MAX_CURRENT_330_MASK) >>
3134 SDHCI_MAX_CURRENT_330_SHIFT) *
3135 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303136 }
3137 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003138 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303139
Aaron Lu55c46652012-07-04 13:31:48 +08003140 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303141 SDHCI_MAX_CURRENT_300_MASK) >>
3142 SDHCI_MAX_CURRENT_300_SHIFT) *
3143 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303144 }
3145 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003146 ocr_avail |= MMC_VDD_165_195;
3147
Aaron Lu55c46652012-07-04 13:31:48 +08003148 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303149 SDHCI_MAX_CURRENT_180_MASK) >>
3150 SDHCI_MAX_CURRENT_180_SHIFT) *
3151 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303152 }
3153
Tim Kryger52221612014-06-25 00:25:34 -07003154 /* If OCR set by external regulators, use it instead */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003155 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003156 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003157
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003158 if (host->ocr_mask)
Tim Kryger3a48edc2014-06-13 10:13:56 -07003159 ocr_avail &= host->ocr_mask;
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003160
Takashi Iwai8f230f42010-12-08 10:04:30 +01003161 mmc->ocr_avail = ocr_avail;
3162 mmc->ocr_avail_sdio = ocr_avail;
3163 if (host->ocr_avail_sdio)
3164 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3165 mmc->ocr_avail_sd = ocr_avail;
3166 if (host->ocr_avail_sd)
3167 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3168 else /* normal SD controllers don't support 1.8V */
3169 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3170 mmc->ocr_avail_mmc = ocr_avail;
3171 if (host->ocr_avail_mmc)
3172 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003173
3174 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303175 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003176 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003177 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003178 }
3179
Pierre Ossmand129bce2006-03-24 03:18:17 -08003180 spin_lock_init(&host->lock);
3181
3182 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003183 * Maximum number of segments. Depends on if the hardware
3184 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003185 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003186 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003187 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003188 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003189 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003190 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04003191 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003192
3193 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003194 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01003195 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08003196 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003197 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003198
3199 /*
3200 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003201 * of bytes. When doing hardware scatter/gather, each entry cannot
3202 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003203 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003204 if (host->flags & SDHCI_USE_ADMA) {
3205 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3206 mmc->max_seg_size = 65535;
3207 else
3208 mmc->max_seg_size = 65536;
3209 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003210 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003211 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003212
3213 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003214 * Maximum block size. This varies from controller to controller and
3215 * is specified in the capabilities register.
3216 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003217 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3218 mmc->max_blk_size = 2;
3219 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303220 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003221 SDHCI_MAX_BLOCK_SHIFT;
3222 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003223 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3224 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003225 mmc->max_blk_size = 0;
3226 }
3227 }
3228
3229 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003230
3231 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003232 * Maximum block count.
3233 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003234 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003235
3236 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003237 * Init tasklets.
3238 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003239 tasklet_init(&host->finish_tasklet,
3240 sdhci_tasklet_finish, (unsigned long)host);
3241
Al Viroe4cad1b2006-10-10 22:47:07 +01003242 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003243
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303244 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303245 init_waitqueue_head(&host->buf_ready_int);
3246
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303247 /* Initialize re-tuning timer */
3248 init_timer(&host->tuning_timer);
3249 host->tuning_timer.data = (unsigned long)host;
3250 host->tuning_timer.function = sdhci_tuning_timer;
3251 }
3252
Shawn Guo2af502c2013-07-05 14:38:55 +08003253 sdhci_init(host, 0);
3254
Russell King781e9892014-04-25 12:55:46 +01003255 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3256 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003257 if (ret) {
3258 pr_err("%s: Failed to request IRQ %d: %d\n",
3259 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003260 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003261 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003262
Pierre Ossmand129bce2006-03-24 03:18:17 -08003263#ifdef CONFIG_MMC_DEBUG
3264 sdhci_dumpregs(host);
3265#endif
3266
Pierre Ossmanf9134312008-12-21 17:01:48 +01003267#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003268 snprintf(host->led_name, sizeof(host->led_name),
3269 "%s::", mmc_hostname(mmc));
3270 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003271 host->led.brightness = LED_OFF;
3272 host->led.default_trigger = mmc_hostname(mmc);
3273 host->led.brightness_set = sdhci_led_control;
3274
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003275 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003276 if (ret) {
3277 pr_err("%s: Failed to register LED device: %d\n",
3278 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003279 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003280 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003281#endif
3282
Pierre Ossman5f25a662006-10-04 02:15:39 -07003283 mmiowb();
3284
Pierre Ossmand129bce2006-03-24 03:18:17 -08003285 mmc_add_host(mmc);
3286
Girish K Sa3c76eb2011-10-11 11:44:09 +05303287 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003288 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003289 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3290 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003291
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003292 sdhci_enable_card_detection(host);
3293
Pierre Ossmand129bce2006-03-24 03:18:17 -08003294 return 0;
3295
Pierre Ossmanf9134312008-12-21 17:01:48 +01003296#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003297reset:
Russell King03231f92014-04-25 12:57:12 +01003298 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003299 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3300 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003301 free_irq(host->irq, host);
3302#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003303untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003304 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003305
3306 return ret;
3307}
3308
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003309EXPORT_SYMBOL_GPL(sdhci_add_host);
3310
Pierre Ossman1e728592008-04-16 19:13:13 +02003311void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003312{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003313 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003314 unsigned long flags;
3315
3316 if (dead) {
3317 spin_lock_irqsave(&host->lock, flags);
3318
3319 host->flags |= SDHCI_DEVICE_DEAD;
3320
3321 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303322 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003323 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003324
3325 host->mrq->cmd->error = -ENOMEDIUM;
3326 tasklet_schedule(&host->finish_tasklet);
3327 }
3328
3329 spin_unlock_irqrestore(&host->lock, flags);
3330 }
3331
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003332 sdhci_disable_card_detection(host);
3333
Markus Mayer4e743f12014-07-03 13:27:42 -07003334 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003335
Pierre Ossmanf9134312008-12-21 17:01:48 +01003336#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003337 led_classdev_unregister(&host->led);
3338#endif
3339
Pierre Ossman1e728592008-04-16 19:13:13 +02003340 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003341 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003342
Russell Kingb537f942014-04-25 12:56:01 +01003343 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3344 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003345 free_irq(host->irq, host);
3346
3347 del_timer_sync(&host->timer);
3348
Pierre Ossmand129bce2006-03-24 03:18:17 -08003349 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003350
Tim Kryger3a48edc2014-06-13 10:13:56 -07003351 if (!IS_ERR(mmc->supply.vqmmc))
3352 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003353
Russell Kingd1e49f72014-04-25 12:58:34 +01003354 if (host->adma_desc)
Markus Mayer4e743f12014-07-03 13:27:42 -07003355 dma_free_coherent(mmc_dev(mmc), ADMA_SIZE,
Russell Kingd1e49f72014-04-25 12:58:34 +01003356 host->adma_desc, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003357 kfree(host->align_buffer);
3358
3359 host->adma_desc = NULL;
3360 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003361}
3362
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003363EXPORT_SYMBOL_GPL(sdhci_remove_host);
3364
3365void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003366{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003367 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003368}
3369
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003370EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003371
3372/*****************************************************************************\
3373 * *
3374 * Driver init/exit *
3375 * *
3376\*****************************************************************************/
3377
3378static int __init sdhci_drv_init(void)
3379{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303380 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003381 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303382 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003383
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003384 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003385}
3386
3387static void __exit sdhci_drv_exit(void)
3388{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003389}
3390
3391module_init(sdhci_drv_init);
3392module_exit(sdhci_drv_exit);
3393
Pierre Ossmandf673b22006-06-30 02:22:31 -07003394module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003395module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003396
Pierre Ossman32710e82009-04-08 20:14:54 +02003397MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003398MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003399MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003400
Pierre Ossmandf673b22006-06-30 02:22:31 -07003401MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003402MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");