blob: 8cd20ef0a9cb4f69a85905934c5b2f23b5e2086a [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
Pierre Ossmandf673b22006-06-30 02:22:31 -070047static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030048static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070049
Pierre Ossmand129bce2006-03-24 03:18:17 -080050static void sdhci_finish_data(struct sdhci_host *);
51
Pierre Ossmand129bce2006-03-24 03:18:17 -080052static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053053static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053054static void sdhci_tuning_timer(unsigned long data);
Kevin Liu52983382013-01-31 11:31:37 +080055static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080056
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030057#ifdef CONFIG_PM_RUNTIME
58static int sdhci_runtime_pm_get(struct sdhci_host *host);
59static int sdhci_runtime_pm_put(struct sdhci_host *host);
Adrian Hunterf0710a52013-05-06 12:17:32 +030060static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
61static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030062#else
63static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
64{
65 return 0;
66}
67static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
68{
69 return 0;
70}
Adrian Hunterf0710a52013-05-06 12:17:32 +030071static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
72{
73}
74static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
75{
76}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030077#endif
78
Pierre Ossmand129bce2006-03-24 03:18:17 -080079static void sdhci_dumpregs(struct sdhci_host *host)
80{
Girish K Sa3c76eb2011-10-11 11:44:09 +053081 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070082 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080083
Girish K Sa3c76eb2011-10-11 11:44:09 +053084 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030085 sdhci_readl(host, SDHCI_DMA_ADDRESS),
86 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053087 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030088 sdhci_readw(host, SDHCI_BLOCK_SIZE),
89 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053090 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030091 sdhci_readl(host, SDHCI_ARGUMENT),
92 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053093 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030094 sdhci_readl(host, SDHCI_PRESENT_STATE),
95 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053096 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030097 sdhci_readb(host, SDHCI_POWER_CONTROL),
98 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053099 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300100 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
101 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530102 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300103 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
104 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530105 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readl(host, SDHCI_INT_ENABLE),
107 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530108 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300109 sdhci_readw(host, SDHCI_ACMD12_ERR),
110 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530111 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300112 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500113 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530114 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500115 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300116 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530117 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530118 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800119
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100120 if (host->flags & SDHCI_USE_ADMA)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530121 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100122 readl(host->ioaddr + SDHCI_ADMA_ERROR),
123 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
124
Girish K Sa3c76eb2011-10-11 11:44:09 +0530125 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800126}
127
128/*****************************************************************************\
129 * *
130 * Low level functions *
131 * *
132\*****************************************************************************/
133
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300134static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
135{
Russell King5b4f1f62014-04-25 12:57:02 +0100136 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300137
Adrian Hunterc79396c2011-12-27 15:48:42 +0200138 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100139 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300140 return;
141
Russell King5b4f1f62014-04-25 12:57:02 +0100142 if (enable) {
143 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
144 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800145
Russell King5b4f1f62014-04-25 12:57:02 +0100146 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
147 SDHCI_INT_CARD_INSERT;
148 } else {
149 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
150 }
Russell Kingb537f942014-04-25 12:56:01 +0100151
152 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
153 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300154}
155
156static void sdhci_enable_card_detection(struct sdhci_host *host)
157{
158 sdhci_set_card_detection(host, true);
159}
160
161static void sdhci_disable_card_detection(struct sdhci_host *host)
162{
163 sdhci_set_card_detection(host, false);
164}
165
Pierre Ossmand129bce2006-03-24 03:18:17 -0800166static void sdhci_reset(struct sdhci_host *host, u8 mask)
167{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700168 unsigned long timeout;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100169 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300170 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700171 SDHCI_CARD_PRESENT))
172 return;
173 }
174
Philip Rakity393c1a32011-01-21 11:26:40 -0800175 if (host->ops->platform_reset_enter)
176 host->ops->platform_reset_enter(host, mask);
177
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 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300201
Philip Rakity393c1a32011-01-21 11:26:40 -0800202 if (host->ops->platform_reset_exit)
203 host->ops->platform_reset_exit(host, mask);
204
Russell Kingb537f942014-04-25 12:56:01 +0100205 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET) {
206 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
207 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
208 }
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800209
210 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
211 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
212 host->ops->enable_dma(host);
213 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800214}
215
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800216static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
217
218static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800219{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800220 if (soft)
221 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
222 else
223 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800224
Russell Kingb537f942014-04-25 12:56:01 +0100225 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
226 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
227 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
228 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
229 SDHCI_INT_RESPONSE;
230
231 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
232 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800233
234 if (soft) {
235 /* force clock reconfiguration */
236 host->clock = 0;
237 sdhci_set_ios(host->mmc, &host->mmc->ios);
238 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300239}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800240
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300241static void sdhci_reinit(struct sdhci_host *host)
242{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800243 sdhci_init(host, 0);
Aaron Lub67c6b42012-06-29 16:17:31 +0800244 /*
245 * Retuning stuffs are affected by different cards inserted and only
246 * applicable to UHS-I cards. So reset these fields to their initial
247 * value when card is removed.
248 */
Aaron Lu973905f2012-07-04 13:29:09 +0800249 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
250 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
251
Aaron Lub67c6b42012-06-29 16:17:31 +0800252 del_timer_sync(&host->tuning_timer);
253 host->flags &= ~SDHCI_NEEDS_RETUNING;
254 host->mmc->max_blk_count =
255 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
256 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300257 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800258}
259
260static void sdhci_activate_led(struct sdhci_host *host)
261{
262 u8 ctrl;
263
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300264 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800265 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300266 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800267}
268
269static void sdhci_deactivate_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
Pierre Ossmanf9134312008-12-21 17:01:48 +0100278#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100279static void sdhci_led_control(struct led_classdev *led,
280 enum led_brightness brightness)
281{
282 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
283 unsigned long flags;
284
285 spin_lock_irqsave(&host->lock, flags);
286
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300287 if (host->runtime_suspended)
288 goto out;
289
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100290 if (brightness == LED_OFF)
291 sdhci_deactivate_led(host);
292 else
293 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300294out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100295 spin_unlock_irqrestore(&host->lock, flags);
296}
297#endif
298
Pierre Ossmand129bce2006-03-24 03:18:17 -0800299/*****************************************************************************\
300 * *
301 * Core functions *
302 * *
303\*****************************************************************************/
304
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100305static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800306{
Pierre Ossman76591502008-07-21 00:32:11 +0200307 unsigned long flags;
308 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700309 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200310 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800311
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100312 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800313
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100314 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200315 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800316
Pierre Ossman76591502008-07-21 00:32:11 +0200317 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800318
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100319 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200320 if (!sg_miter_next(&host->sg_miter))
321 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800322
Pierre Ossman76591502008-07-21 00:32:11 +0200323 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800324
Pierre Ossman76591502008-07-21 00:32:11 +0200325 blksize -= len;
326 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200327
Pierre Ossman76591502008-07-21 00:32:11 +0200328 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800329
Pierre Ossman76591502008-07-21 00:32:11 +0200330 while (len) {
331 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300332 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200333 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800334 }
Pierre Ossman76591502008-07-21 00:32:11 +0200335
336 *buf = scratch & 0xFF;
337
338 buf++;
339 scratch >>= 8;
340 chunk--;
341 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800342 }
343 }
Pierre Ossman76591502008-07-21 00:32:11 +0200344
345 sg_miter_stop(&host->sg_miter);
346
347 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100348}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800349
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100350static void sdhci_write_block_pio(struct sdhci_host *host)
351{
Pierre Ossman76591502008-07-21 00:32:11 +0200352 unsigned long flags;
353 size_t blksize, len, chunk;
354 u32 scratch;
355 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100356
357 DBG("PIO writing\n");
358
359 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200360 chunk = 0;
361 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100362
Pierre Ossman76591502008-07-21 00:32:11 +0200363 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100364
365 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200366 if (!sg_miter_next(&host->sg_miter))
367 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100368
Pierre Ossman76591502008-07-21 00:32:11 +0200369 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200370
Pierre Ossman76591502008-07-21 00:32:11 +0200371 blksize -= len;
372 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100373
Pierre Ossman76591502008-07-21 00:32:11 +0200374 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100375
Pierre Ossman76591502008-07-21 00:32:11 +0200376 while (len) {
377 scratch |= (u32)*buf << (chunk * 8);
378
379 buf++;
380 chunk++;
381 len--;
382
383 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300384 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200385 chunk = 0;
386 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100387 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100388 }
389 }
Pierre Ossman76591502008-07-21 00:32:11 +0200390
391 sg_miter_stop(&host->sg_miter);
392
393 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100394}
395
396static void sdhci_transfer_pio(struct sdhci_host *host)
397{
398 u32 mask;
399
400 BUG_ON(!host->data);
401
Pierre Ossman76591502008-07-21 00:32:11 +0200402 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100403 return;
404
405 if (host->data->flags & MMC_DATA_READ)
406 mask = SDHCI_DATA_AVAILABLE;
407 else
408 mask = SDHCI_SPACE_AVAILABLE;
409
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200410 /*
411 * Some controllers (JMicron JMB38x) mess up the buffer bits
412 * for transfers < 4 bytes. As long as it is just one block,
413 * we can ignore the bits.
414 */
415 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
416 (host->data->blocks == 1))
417 mask = ~0;
418
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300419 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300420 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
421 udelay(100);
422
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100423 if (host->data->flags & MMC_DATA_READ)
424 sdhci_read_block_pio(host);
425 else
426 sdhci_write_block_pio(host);
427
Pierre Ossman76591502008-07-21 00:32:11 +0200428 host->blocks--;
429 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100430 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100431 }
432
433 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800434}
435
Pierre Ossman2134a922008-06-28 18:28:51 +0200436static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
437{
438 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800439 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200440}
441
442static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
443{
Cong Wang482fce92011-11-27 13:27:00 +0800444 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200445 local_irq_restore(*flags);
446}
447
Ben Dooks118cd172010-03-05 13:43:26 -0800448static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
449{
Ben Dooks9e506f32010-03-05 13:43:29 -0800450 __le32 *dataddr = (__le32 __force *)(desc + 4);
451 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800452
Ben Dooks9e506f32010-03-05 13:43:29 -0800453 /* SDHCI specification says ADMA descriptors should be 4 byte
454 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800455
Ben Dooks9e506f32010-03-05 13:43:29 -0800456 cmdlen[0] = cpu_to_le16(cmd);
457 cmdlen[1] = cpu_to_le16(len);
458
459 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800460}
461
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200462static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200463 struct mmc_data *data)
464{
465 int direction;
466
467 u8 *desc;
468 u8 *align;
469 dma_addr_t addr;
470 dma_addr_t align_addr;
471 int len, offset;
472
473 struct scatterlist *sg;
474 int i;
475 char *buffer;
476 unsigned long flags;
477
478 /*
479 * The spec does not specify endianness of descriptor table.
480 * We currently guess that it is LE.
481 */
482
483 if (data->flags & MMC_DATA_READ)
484 direction = DMA_FROM_DEVICE;
485 else
486 direction = DMA_TO_DEVICE;
487
488 /*
489 * The ADMA descriptor table is mapped further down as we
490 * need to fill it with data first.
491 */
492
493 host->align_addr = dma_map_single(mmc_dev(host->mmc),
494 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700495 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200496 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200497 BUG_ON(host->align_addr & 0x3);
498
499 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
500 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200501 if (host->sg_count == 0)
502 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200503
504 desc = host->adma_desc;
505 align = host->align_buffer;
506
507 align_addr = host->align_addr;
508
509 for_each_sg(data->sg, sg, host->sg_count, i) {
510 addr = sg_dma_address(sg);
511 len = sg_dma_len(sg);
512
513 /*
514 * The SDHCI specification states that ADMA
515 * addresses must be 32-bit aligned. If they
516 * aren't, then we use a bounce buffer for
517 * the (up to three) bytes that screw up the
518 * alignment.
519 */
520 offset = (4 - (addr & 0x3)) & 0x3;
521 if (offset) {
522 if (data->flags & MMC_DATA_WRITE) {
523 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200524 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200525 memcpy(align, buffer, offset);
526 sdhci_kunmap_atomic(buffer, &flags);
527 }
528
Ben Dooks118cd172010-03-05 13:43:26 -0800529 /* tran, valid */
530 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200531
532 BUG_ON(offset > 65536);
533
Pierre Ossman2134a922008-06-28 18:28:51 +0200534 align += 4;
535 align_addr += 4;
536
537 desc += 8;
538
539 addr += offset;
540 len -= offset;
541 }
542
Pierre Ossman2134a922008-06-28 18:28:51 +0200543 BUG_ON(len > 65536);
544
Ben Dooks118cd172010-03-05 13:43:26 -0800545 /* tran, valid */
546 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200547 desc += 8;
548
549 /*
550 * If this triggers then we have a calculation bug
551 * somewhere. :/
552 */
553 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
554 }
555
Thomas Abraham70764a92010-05-26 14:42:04 -0700556 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
557 /*
558 * Mark the last descriptor as the terminating descriptor
559 */
560 if (desc != host->adma_desc) {
561 desc -= 8;
562 desc[0] |= 0x2; /* end */
563 }
564 } else {
565 /*
566 * Add a terminating entry.
567 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200568
Thomas Abraham70764a92010-05-26 14:42:04 -0700569 /* nop, end, valid */
570 sdhci_set_adma_desc(desc, 0, 0, 0x3);
571 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200572
573 /*
574 * Resync align buffer as we might have changed it.
575 */
576 if (data->flags & MMC_DATA_WRITE) {
577 dma_sync_single_for_device(mmc_dev(host->mmc),
578 host->align_addr, 128 * 4, direction);
579 }
580
581 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
582 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200583 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200584 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200585 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200586
587 return 0;
588
589unmap_entries:
590 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
591 data->sg_len, direction);
592unmap_align:
593 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
594 128 * 4, direction);
595fail:
596 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200597}
598
599static void sdhci_adma_table_post(struct sdhci_host *host,
600 struct mmc_data *data)
601{
602 int direction;
603
604 struct scatterlist *sg;
605 int i, size;
606 u8 *align;
607 char *buffer;
608 unsigned long flags;
609
610 if (data->flags & MMC_DATA_READ)
611 direction = DMA_FROM_DEVICE;
612 else
613 direction = DMA_TO_DEVICE;
614
615 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
616 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
617
618 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
619 128 * 4, direction);
620
621 if (data->flags & MMC_DATA_READ) {
622 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
623 data->sg_len, direction);
624
625 align = host->align_buffer;
626
627 for_each_sg(data->sg, sg, host->sg_count, i) {
628 if (sg_dma_address(sg) & 0x3) {
629 size = 4 - (sg_dma_address(sg) & 0x3);
630
631 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200632 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200633 memcpy(buffer, align, size);
634 sdhci_kunmap_atomic(buffer, &flags);
635
636 align += 4;
637 }
638 }
639 }
640
641 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
642 data->sg_len, direction);
643}
644
Andrei Warkentina3c77782011-04-11 16:13:42 -0500645static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800646{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700647 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500648 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700649 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800650
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200651 /*
652 * If the host controller provides us with an incorrect timeout
653 * value, just skip the check and use 0xE. The hardware may take
654 * longer to time out, but that's much better than having a too-short
655 * timeout value.
656 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200657 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200658 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200659
Andrei Warkentina3c77782011-04-11 16:13:42 -0500660 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100661 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500662 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800663
Andrei Warkentina3c77782011-04-11 16:13:42 -0500664 /* timeout in us */
665 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100666 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300667 else {
668 target_timeout = data->timeout_ns / 1000;
669 if (host->clock)
670 target_timeout += data->timeout_clks / host->clock;
671 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700672
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700673 /*
674 * Figure out needed cycles.
675 * We do this in steps in order to fit inside a 32 bit int.
676 * The first step is the minimum timeout, which will have a
677 * minimum resolution of 6 bits:
678 * (1) 2^13*1000 > 2^22,
679 * (2) host->timeout_clk < 2^16
680 * =>
681 * (1) / (2) > 2^6
682 */
683 count = 0;
684 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
685 while (current_timeout < target_timeout) {
686 count++;
687 current_timeout <<= 1;
688 if (count >= 0xF)
689 break;
690 }
691
692 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400693 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
694 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700695 count = 0xE;
696 }
697
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200698 return count;
699}
700
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300701static void sdhci_set_transfer_irqs(struct sdhci_host *host)
702{
703 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
704 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
705
706 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100707 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300708 else
Russell Kingb537f942014-04-25 12:56:01 +0100709 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
710
711 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
712 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300713}
714
Andrei Warkentina3c77782011-04-11 16:13:42 -0500715static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200716{
717 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200718 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500719 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200720 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200721
722 WARN_ON(host->data);
723
Andrei Warkentina3c77782011-04-11 16:13:42 -0500724 if (data || (cmd->flags & MMC_RSP_BUSY)) {
725 count = sdhci_calc_timeout(host, cmd);
726 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
727 }
728
729 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200730 return;
731
732 /* Sanity checks */
733 BUG_ON(data->blksz * data->blocks > 524288);
734 BUG_ON(data->blksz > host->mmc->max_blk_size);
735 BUG_ON(data->blocks > 65535);
736
737 host->data = data;
738 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400739 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200740
Richard Röjforsa13abc72009-09-22 16:45:30 -0700741 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100742 host->flags |= SDHCI_REQ_USE_DMA;
743
Pierre Ossman2134a922008-06-28 18:28:51 +0200744 /*
745 * FIXME: This doesn't account for merging when mapping the
746 * scatterlist.
747 */
748 if (host->flags & SDHCI_REQ_USE_DMA) {
749 int broken, i;
750 struct scatterlist *sg;
751
752 broken = 0;
753 if (host->flags & SDHCI_USE_ADMA) {
754 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
755 broken = 1;
756 } else {
757 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
758 broken = 1;
759 }
760
761 if (unlikely(broken)) {
762 for_each_sg(data->sg, sg, data->sg_len, i) {
763 if (sg->length & 0x3) {
764 DBG("Reverting to PIO because of "
765 "transfer size (%d)\n",
766 sg->length);
767 host->flags &= ~SDHCI_REQ_USE_DMA;
768 break;
769 }
770 }
771 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100772 }
773
774 /*
775 * The assumption here being that alignment is the same after
776 * translation to device address space.
777 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200778 if (host->flags & SDHCI_REQ_USE_DMA) {
779 int broken, i;
780 struct scatterlist *sg;
781
782 broken = 0;
783 if (host->flags & SDHCI_USE_ADMA) {
784 /*
785 * As we use 3 byte chunks to work around
786 * alignment problems, we need to check this
787 * quirk.
788 */
789 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
790 broken = 1;
791 } else {
792 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
793 broken = 1;
794 }
795
796 if (unlikely(broken)) {
797 for_each_sg(data->sg, sg, data->sg_len, i) {
798 if (sg->offset & 0x3) {
799 DBG("Reverting to PIO because of "
800 "bad alignment\n");
801 host->flags &= ~SDHCI_REQ_USE_DMA;
802 break;
803 }
804 }
805 }
806 }
807
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200808 if (host->flags & SDHCI_REQ_USE_DMA) {
809 if (host->flags & SDHCI_USE_ADMA) {
810 ret = sdhci_adma_table_pre(host, data);
811 if (ret) {
812 /*
813 * This only happens when someone fed
814 * us an invalid request.
815 */
816 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200817 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200818 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300819 sdhci_writel(host, host->adma_addr,
820 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200821 }
822 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300823 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200824
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300825 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200826 data->sg, data->sg_len,
827 (data->flags & MMC_DATA_READ) ?
828 DMA_FROM_DEVICE :
829 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300830 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200831 /*
832 * This only happens when someone fed
833 * us an invalid request.
834 */
835 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200836 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200837 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200838 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300839 sdhci_writel(host, sg_dma_address(data->sg),
840 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200841 }
842 }
843 }
844
Pierre Ossman2134a922008-06-28 18:28:51 +0200845 /*
846 * Always adjust the DMA selection as some controllers
847 * (e.g. JMicron) can't do PIO properly when the selection
848 * is ADMA.
849 */
850 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300851 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200852 ctrl &= ~SDHCI_CTRL_DMA_MASK;
853 if ((host->flags & SDHCI_REQ_USE_DMA) &&
854 (host->flags & SDHCI_USE_ADMA))
855 ctrl |= SDHCI_CTRL_ADMA32;
856 else
857 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300858 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100859 }
860
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200861 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200862 int flags;
863
864 flags = SG_MITER_ATOMIC;
865 if (host->data->flags & MMC_DATA_READ)
866 flags |= SG_MITER_TO_SG;
867 else
868 flags |= SG_MITER_FROM_SG;
869 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200870 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800871 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700872
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300873 sdhci_set_transfer_irqs(host);
874
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400875 /* Set the DMA boundary value and block size */
876 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
877 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300878 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700879}
880
881static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500882 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700883{
884 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500885 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700886
Dong Aisheng2b558c12013-10-30 22:09:48 +0800887 if (data == NULL) {
888 /* clear Auto CMD settings for no data CMDs */
889 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
890 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
891 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700892 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800893 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700894
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200895 WARN_ON(!host->data);
896
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700897 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500898 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
899 mode |= SDHCI_TRNS_MULTI;
900 /*
901 * If we are sending CMD23, CMD12 never gets sent
902 * on successful completion (so no Auto-CMD12).
903 */
904 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
905 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500906 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
907 mode |= SDHCI_TRNS_AUTO_CMD23;
908 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
909 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700910 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500911
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700912 if (data->flags & MMC_DATA_READ)
913 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100914 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700915 mode |= SDHCI_TRNS_DMA;
916
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300917 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800918}
919
920static void sdhci_finish_data(struct sdhci_host *host)
921{
922 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800923
924 BUG_ON(!host->data);
925
926 data = host->data;
927 host->data = NULL;
928
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100929 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200930 if (host->flags & SDHCI_USE_ADMA)
931 sdhci_adma_table_post(host, data);
932 else {
933 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
934 data->sg_len, (data->flags & MMC_DATA_READ) ?
935 DMA_FROM_DEVICE : DMA_TO_DEVICE);
936 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800937 }
938
939 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200940 * The specification states that the block count register must
941 * be updated, but it does not specify at what point in the
942 * data flow. That makes the register entirely useless to read
943 * back so we have to assume that nothing made it to the card
944 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800945 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200946 if (data->error)
947 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800948 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200949 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800950
Andrei Warkentine89d4562011-05-23 15:06:37 -0500951 /*
952 * Need to send CMD12 if -
953 * a) open-ended multiblock transfer (no CMD23)
954 * b) error in multiblock transfer
955 */
956 if (data->stop &&
957 (data->error ||
958 !host->mrq->sbc)) {
959
Pierre Ossmand129bce2006-03-24 03:18:17 -0800960 /*
961 * The controller needs a reset of internal state machines
962 * upon error conditions.
963 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200964 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800965 sdhci_reset(host, SDHCI_RESET_CMD);
966 sdhci_reset(host, SDHCI_RESET_DATA);
967 }
968
969 sdhci_send_command(host, data->stop);
970 } else
971 tasklet_schedule(&host->finish_tasklet);
972}
973
Dong Aishengc0e551292013-09-13 19:11:31 +0800974void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800975{
976 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700977 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700978 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800979
980 WARN_ON(host->cmd);
981
Pierre Ossmand129bce2006-03-24 03:18:17 -0800982 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700983 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700984
985 mask = SDHCI_CMD_INHIBIT;
986 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
987 mask |= SDHCI_DATA_INHIBIT;
988
989 /* We shouldn't wait for data inihibit for stop commands, even
990 though they might use busy signaling */
991 if (host->mrq->data && (cmd == host->mrq->data->stop))
992 mask &= ~SDHCI_DATA_INHIBIT;
993
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300994 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700995 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530996 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100997 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200999 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001000 tasklet_schedule(&host->finish_tasklet);
1001 return;
1002 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001003 timeout--;
1004 mdelay(1);
1005 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001006
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001007 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001008 if (!cmd->data && cmd->busy_timeout > 9000)
1009 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001010 else
1011 timeout += 10 * HZ;
1012 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001013
1014 host->cmd = cmd;
1015
Andrei Warkentina3c77782011-04-11 16:13:42 -05001016 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001017
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001018 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001019
Andrei Warkentine89d4562011-05-23 15:06:37 -05001020 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001021
Pierre Ossmand129bce2006-03-24 03:18:17 -08001022 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301023 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001024 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001025 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001026 tasklet_schedule(&host->finish_tasklet);
1027 return;
1028 }
1029
1030 if (!(cmd->flags & MMC_RSP_PRESENT))
1031 flags = SDHCI_CMD_RESP_NONE;
1032 else if (cmd->flags & MMC_RSP_136)
1033 flags = SDHCI_CMD_RESP_LONG;
1034 else if (cmd->flags & MMC_RSP_BUSY)
1035 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1036 else
1037 flags = SDHCI_CMD_RESP_SHORT;
1038
1039 if (cmd->flags & MMC_RSP_CRC)
1040 flags |= SDHCI_CMD_CRC;
1041 if (cmd->flags & MMC_RSP_OPCODE)
1042 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301043
1044 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301045 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1046 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047 flags |= SDHCI_CMD_DATA;
1048
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001049 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001050}
Dong Aishengc0e551292013-09-13 19:11:31 +08001051EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001052
1053static void sdhci_finish_command(struct sdhci_host *host)
1054{
1055 int i;
1056
1057 BUG_ON(host->cmd == NULL);
1058
1059 if (host->cmd->flags & MMC_RSP_PRESENT) {
1060 if (host->cmd->flags & MMC_RSP_136) {
1061 /* CRC is stripped so we need to do some shifting. */
1062 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001063 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001064 SDHCI_RESPONSE + (3-i)*4) << 8;
1065 if (i != 3)
1066 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001067 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001068 SDHCI_RESPONSE + (3-i)*4-1);
1069 }
1070 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001071 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072 }
1073 }
1074
Pierre Ossman17b04292007-07-22 22:18:46 +02001075 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001076
Andrei Warkentine89d4562011-05-23 15:06:37 -05001077 /* Finished CMD23, now send actual command. */
1078 if (host->cmd == host->mrq->sbc) {
1079 host->cmd = NULL;
1080 sdhci_send_command(host, host->mrq->cmd);
1081 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001082
Andrei Warkentine89d4562011-05-23 15:06:37 -05001083 /* Processed actual command. */
1084 if (host->data && host->data_early)
1085 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001086
Andrei Warkentine89d4562011-05-23 15:06:37 -05001087 if (!host->cmd->data)
1088 tasklet_schedule(&host->finish_tasklet);
1089
1090 host->cmd = NULL;
1091 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001092}
1093
Kevin Liu52983382013-01-31 11:31:37 +08001094static u16 sdhci_get_preset_value(struct sdhci_host *host)
1095{
1096 u16 ctrl, preset = 0;
1097
1098 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1099
1100 switch (ctrl & SDHCI_CTRL_UHS_MASK) {
1101 case SDHCI_CTRL_UHS_SDR12:
1102 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1103 break;
1104 case SDHCI_CTRL_UHS_SDR25:
1105 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1106 break;
1107 case SDHCI_CTRL_UHS_SDR50:
1108 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1109 break;
1110 case SDHCI_CTRL_UHS_SDR104:
1111 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1112 break;
1113 case SDHCI_CTRL_UHS_DDR50:
1114 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1115 break;
1116 default:
1117 pr_warn("%s: Invalid UHS-I mode selected\n",
1118 mmc_hostname(host->mmc));
1119 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1120 break;
1121 }
1122 return preset;
1123}
1124
Pierre Ossmand129bce2006-03-24 03:18:17 -08001125static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1126{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301127 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001128 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301129 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001130 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001131
Todd Poynor30832ab2011-12-27 15:48:46 +02001132 if (clock && clock == host->clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001133 return;
1134
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001135 host->mmc->actual_clock = 0;
1136
Anton Vorontsov81146342009-03-17 00:13:59 +03001137 if (host->ops->set_clock) {
1138 host->ops->set_clock(host, clock);
1139 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1140 return;
1141 }
1142
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001143 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001144
1145 if (clock == 0)
1146 goto out;
1147
Zhangfei Gao85105c52010-08-06 07:10:01 +08001148 if (host->version >= SDHCI_SPEC_300) {
Kevin Liu52983382013-01-31 11:31:37 +08001149 if (sdhci_readw(host, SDHCI_HOST_CONTROL2) &
1150 SDHCI_CTRL_PRESET_VAL_ENABLE) {
1151 u16 pre_val;
1152
1153 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1154 pre_val = sdhci_get_preset_value(host);
1155 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1156 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1157 if (host->clk_mul &&
1158 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1159 clk = SDHCI_PROG_CLOCK_MODE;
1160 real_div = div + 1;
1161 clk_mul = host->clk_mul;
1162 } else {
1163 real_div = max_t(int, 1, div << 1);
1164 }
1165 goto clock_set;
1166 }
1167
Arindam Nathc3ed3872011-05-05 12:19:06 +05301168 /*
1169 * Check if the Host Controller supports Programmable Clock
1170 * Mode.
1171 */
1172 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001173 for (div = 1; div <= 1024; div++) {
1174 if ((host->max_clk * host->clk_mul / div)
1175 <= clock)
1176 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001177 }
Kevin Liu52983382013-01-31 11:31:37 +08001178 /*
1179 * Set Programmable Clock Mode in the Clock
1180 * Control register.
1181 */
1182 clk = SDHCI_PROG_CLOCK_MODE;
1183 real_div = div;
1184 clk_mul = host->clk_mul;
1185 div--;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301186 } else {
1187 /* Version 3.00 divisors must be a multiple of 2. */
1188 if (host->max_clk <= clock)
1189 div = 1;
1190 else {
1191 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1192 div += 2) {
1193 if ((host->max_clk / div) <= clock)
1194 break;
1195 }
1196 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001197 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301198 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001199 }
1200 } else {
1201 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001202 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001203 if ((host->max_clk / div) <= clock)
1204 break;
1205 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001206 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301207 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001208 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001209
Kevin Liu52983382013-01-31 11:31:37 +08001210clock_set:
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001211 if (real_div)
1212 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1213
Arindam Nathc3ed3872011-05-05 12:19:06 +05301214 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001215 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1216 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001217 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001218 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001219
Chris Ball27f6cb12009-09-22 16:45:31 -07001220 /* Wait max 20 ms */
1221 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001222 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001223 & SDHCI_CLOCK_INT_STABLE)) {
1224 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301225 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001226 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001227 sdhci_dumpregs(host);
1228 return;
1229 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001230 timeout--;
1231 mdelay(1);
1232 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001233
1234 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001235 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001236
1237out:
1238 host->clock = clock;
1239}
1240
Andy Shevchenko8213af32013-01-07 16:31:08 +02001241static inline void sdhci_update_clock(struct sdhci_host *host)
1242{
1243 unsigned int clock;
1244
1245 clock = host->clock;
1246 host->clock = 0;
1247 sdhci_set_clock(host, clock);
1248}
1249
Adrian Hunterceb61432011-12-27 15:48:41 +02001250static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001251{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001252 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001253
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001254 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001255 switch (1 << power) {
1256 case MMC_VDD_165_195:
1257 pwr = SDHCI_POWER_180;
1258 break;
1259 case MMC_VDD_29_30:
1260 case MMC_VDD_30_31:
1261 pwr = SDHCI_POWER_300;
1262 break;
1263 case MMC_VDD_32_33:
1264 case MMC_VDD_33_34:
1265 pwr = SDHCI_POWER_330;
1266 break;
1267 default:
1268 BUG();
1269 }
1270 }
1271
1272 if (host->pwr == pwr)
Adrian Hunterceb61432011-12-27 15:48:41 +02001273 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001274
Pierre Ossmanae628902009-05-03 20:45:03 +02001275 host->pwr = pwr;
1276
1277 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001278 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001279 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1280 sdhci_runtime_pm_bus_off(host);
Adrian Hunterceb61432011-12-27 15:48:41 +02001281 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001282 }
1283
1284 /*
1285 * Spec says that we should clear the power reg before setting
1286 * a new value. Some controllers don't seem to like this though.
1287 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001288 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001289 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001290
Andres Salomone08c1692008-07-04 10:00:03 -07001291 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001292 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001293 * and set turn on power at the same time, so set the voltage first.
1294 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001295 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001296 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1297
1298 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001299
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001300 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001301
Adrian Hunterf0710a52013-05-06 12:17:32 +03001302 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1303 sdhci_runtime_pm_bus_on(host);
1304
Harald Welte557b0692009-06-18 16:53:38 +02001305 /*
1306 * Some controllers need an extra 10ms delay of 10ms before they
1307 * can apply clock after applying power
1308 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001309 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001310 mdelay(10);
Adrian Hunterceb61432011-12-27 15:48:41 +02001311
1312 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001313}
1314
Pierre Ossmand129bce2006-03-24 03:18:17 -08001315/*****************************************************************************\
1316 * *
1317 * MMC callbacks *
1318 * *
1319\*****************************************************************************/
1320
1321static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1322{
1323 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001324 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001325 unsigned long flags;
Aaron Lu473b0952012-07-03 17:27:49 +08001326 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001327
1328 host = mmc_priv(mmc);
1329
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001330 sdhci_runtime_pm_get(host);
1331
Pierre Ossmand129bce2006-03-24 03:18:17 -08001332 spin_lock_irqsave(&host->lock, flags);
1333
1334 WARN_ON(host->mrq != NULL);
1335
Pierre Ossmanf9134312008-12-21 17:01:48 +01001336#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001337 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001338#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001339
1340 /*
1341 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1342 * requests if Auto-CMD12 is enabled.
1343 */
1344 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001345 if (mrq->stop) {
1346 mrq->data->stop = NULL;
1347 mrq->stop = NULL;
1348 }
1349 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001350
1351 host->mrq = mrq;
1352
Shawn Guo505a8682012-12-11 15:23:42 +08001353 /*
1354 * Firstly check card presence from cd-gpio. The return could
1355 * be one of the following possibilities:
1356 * negative: cd-gpio is not available
1357 * zero: cd-gpio is used, and card is removed
1358 * one: cd-gpio is used, and card is present
1359 */
1360 present = mmc_gpio_get_cd(host->mmc);
1361 if (present < 0) {
1362 /* If polling, assume that the card is always present. */
1363 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1364 present = 1;
1365 else
1366 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1367 SDHCI_CARD_PRESENT;
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +08001368 }
1369
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001370 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001371 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001372 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301373 } else {
1374 u32 present_state;
1375
1376 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1377 /*
1378 * Check if the re-tuning timer has already expired and there
1379 * is no on-going data transfer. If so, we need to execute
1380 * tuning procedure before sending command.
1381 */
1382 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1383 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
Chris Ball14efd952012-11-05 14:29:49 -05001384 if (mmc->card) {
1385 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1386 tuning_opcode =
1387 mmc->card->type == MMC_TYPE_MMC ?
1388 MMC_SEND_TUNING_BLOCK_HS200 :
1389 MMC_SEND_TUNING_BLOCK;
Chuansheng Liu63c21182013-11-05 14:52:45 +08001390
1391 /* Here we need to set the host->mrq to NULL,
1392 * in case the pending finish_tasklet
1393 * finishes it incorrectly.
1394 */
1395 host->mrq = NULL;
1396
Chris Ball14efd952012-11-05 14:29:49 -05001397 spin_unlock_irqrestore(&host->lock, flags);
1398 sdhci_execute_tuning(mmc, tuning_opcode);
1399 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301400
Chris Ball14efd952012-11-05 14:29:49 -05001401 /* Restore original mmc_request structure */
1402 host->mrq = mrq;
1403 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301404 }
1405
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001406 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001407 sdhci_send_command(host, mrq->sbc);
1408 else
1409 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301410 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001411
Pierre Ossman5f25a662006-10-04 02:15:39 -07001412 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001413 spin_unlock_irqrestore(&host->lock, flags);
1414}
1415
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001416static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001417{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001418 unsigned long flags;
Adrian Hunterceb61432011-12-27 15:48:41 +02001419 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001420 u8 ctrl;
1421
Pierre Ossmand129bce2006-03-24 03:18:17 -08001422 spin_lock_irqsave(&host->lock, flags);
1423
Adrian Hunterceb61432011-12-27 15:48:41 +02001424 if (host->flags & SDHCI_DEVICE_DEAD) {
1425 spin_unlock_irqrestore(&host->lock, flags);
1426 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1427 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1428 return;
1429 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001430
Pierre Ossmand129bce2006-03-24 03:18:17 -08001431 /*
1432 * Reset the chip on each power off.
1433 * Should clear out any weird states.
1434 */
1435 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001436 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001437 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001438 }
1439
Kevin Liu52983382013-01-31 11:31:37 +08001440 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001441 (ios->power_mode == MMC_POWER_UP) &&
1442 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001443 sdhci_enable_preset_value(host, false);
1444
Pierre Ossmand129bce2006-03-24 03:18:17 -08001445 sdhci_set_clock(host, ios->clock);
1446
1447 if (ios->power_mode == MMC_POWER_OFF)
Adrian Hunterceb61432011-12-27 15:48:41 +02001448 vdd_bit = sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001449 else
Adrian Hunterceb61432011-12-27 15:48:41 +02001450 vdd_bit = sdhci_set_power(host, ios->vdd);
1451
1452 if (host->vmmc && vdd_bit != -1) {
1453 spin_unlock_irqrestore(&host->lock, flags);
1454 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1455 spin_lock_irqsave(&host->lock, flags);
1456 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001457
Philip Rakity643a81f2010-09-23 08:24:32 -07001458 if (host->ops->platform_send_init_74_clocks)
1459 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1460
Philip Rakity15ec4462010-11-19 16:48:39 -05001461 /*
1462 * If your platform has 8-bit width support but is not a v3 controller,
1463 * or if it requires special setup code, you should implement that in
Sascha Hauer7bc088d2013-01-21 19:02:27 +08001464 * platform_bus_width().
Philip Rakity15ec4462010-11-19 16:48:39 -05001465 */
Sascha Hauer7bc088d2013-01-21 19:02:27 +08001466 if (host->ops->platform_bus_width) {
1467 host->ops->platform_bus_width(host, ios->bus_width);
1468 } else {
Philip Rakity15ec4462010-11-19 16:48:39 -05001469 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1470 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1471 ctrl &= ~SDHCI_CTRL_4BITBUS;
1472 if (host->version >= SDHCI_SPEC_300)
1473 ctrl |= SDHCI_CTRL_8BITBUS;
1474 } else {
1475 if (host->version >= SDHCI_SPEC_300)
1476 ctrl &= ~SDHCI_CTRL_8BITBUS;
1477 if (ios->bus_width == MMC_BUS_WIDTH_4)
1478 ctrl |= SDHCI_CTRL_4BITBUS;
1479 else
1480 ctrl &= ~SDHCI_CTRL_4BITBUS;
1481 }
1482 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1483 }
1484
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001485 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001486
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001487 if ((ios->timing == MMC_TIMING_SD_HS ||
1488 ios->timing == MMC_TIMING_MMC_HS)
1489 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001490 ctrl |= SDHCI_CTRL_HISPD;
1491 else
1492 ctrl &= ~SDHCI_CTRL_HISPD;
1493
Arindam Nathd6d50a12011-05-05 12:18:59 +05301494 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301495 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301496
1497 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301498 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001499 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301500 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301501 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1502 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001503 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301504 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301505
1506 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1507 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301508 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301509 /*
1510 * We only need to set Driver Strength if the
1511 * preset value enable is not set.
1512 */
1513 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1514 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1515 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1516 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1517 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1518
1519 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301520 } else {
1521 /*
1522 * According to SDHC Spec v3.00, if the Preset Value
1523 * Enable in the Host Control 2 register is set, we
1524 * need to reset SD Clock Enable before changing High
1525 * Speed Enable to avoid generating clock gliches.
1526 */
Arindam Nath758535c2011-05-05 12:19:00 +05301527
1528 /* Reset SD Clock Enable */
1529 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1530 clk &= ~SDHCI_CLOCK_CARD_EN;
1531 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1532
1533 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1534
1535 /* Re-enable SD Clock */
Andy Shevchenko8213af32013-01-07 16:31:08 +02001536 sdhci_update_clock(host);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301537 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301538
Arindam Nath49c468f2011-05-05 12:19:01 +05301539
1540 /* Reset SD Clock Enable */
1541 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1542 clk &= ~SDHCI_CLOCK_CARD_EN;
1543 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1544
Philip Rakity6322cdd2011-05-13 11:17:15 +05301545 if (host->ops->set_uhs_signaling)
1546 host->ops->set_uhs_signaling(host, ios->timing);
1547 else {
1548 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1549 /* Select Bus Speed Mode for host */
1550 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Giuseppe CAVALLARO59911562013-06-13 16:41:28 +02001551 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1552 (ios->timing == MMC_TIMING_UHS_SDR104))
1553 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
Girish K S069c9f12012-01-06 09:56:39 +05301554 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301555 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1556 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1557 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1558 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1559 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001560 else if ((ios->timing == MMC_TIMING_UHS_DDR50) ||
1561 (ios->timing == MMC_TIMING_MMC_DDR52))
Philip Rakity6322cdd2011-05-13 11:17:15 +05301562 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1563 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1564 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301565
Kevin Liu52983382013-01-31 11:31:37 +08001566 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1567 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1568 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1569 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1570 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1571 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1572 u16 preset;
1573
1574 sdhci_enable_preset_value(host, true);
1575 preset = sdhci_get_preset_value(host);
1576 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1577 >> SDHCI_PRESET_DRV_SHIFT;
1578 }
1579
Arindam Nath49c468f2011-05-05 12:19:01 +05301580 /* Re-enable SD Clock */
Andy Shevchenko8213af32013-01-07 16:31:08 +02001581 sdhci_update_clock(host);
Arindam Nath758535c2011-05-05 12:19:00 +05301582 } else
1583 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301584
Leandro Dorileob8352262007-07-25 23:47:04 +02001585 /*
1586 * Some (ENE) controllers go apeshit on some ios operation,
1587 * signalling timeout and CRC errors even on CMD0. Resetting
1588 * it on each ios seems to solve the problem.
1589 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001590 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001591 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1592
Pierre Ossman5f25a662006-10-04 02:15:39 -07001593 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001594 spin_unlock_irqrestore(&host->lock, flags);
1595}
1596
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001597static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1598{
1599 struct sdhci_host *host = mmc_priv(mmc);
1600
1601 sdhci_runtime_pm_get(host);
1602 sdhci_do_set_ios(host, ios);
1603 sdhci_runtime_pm_put(host);
1604}
1605
Kevin Liu94144a42013-02-28 17:35:53 +08001606static int sdhci_do_get_cd(struct sdhci_host *host)
1607{
1608 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1609
1610 if (host->flags & SDHCI_DEVICE_DEAD)
1611 return 0;
1612
1613 /* If polling/nonremovable, assume that the card is always present. */
1614 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1615 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1616 return 1;
1617
1618 /* Try slot gpio detect */
1619 if (!IS_ERR_VALUE(gpio_cd))
1620 return !!gpio_cd;
1621
1622 /* Host native card detect */
1623 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1624}
1625
1626static int sdhci_get_cd(struct mmc_host *mmc)
1627{
1628 struct sdhci_host *host = mmc_priv(mmc);
1629 int ret;
1630
1631 sdhci_runtime_pm_get(host);
1632 ret = sdhci_do_get_cd(host);
1633 sdhci_runtime_pm_put(host);
1634 return ret;
1635}
1636
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001637static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001638{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001639 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001640 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001641
Pierre Ossmand129bce2006-03-24 03:18:17 -08001642 spin_lock_irqsave(&host->lock, flags);
1643
Pierre Ossman1e728592008-04-16 19:13:13 +02001644 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001645 is_readonly = 0;
1646 else if (host->ops->get_ro)
1647 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001648 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001649 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1650 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001651
1652 spin_unlock_irqrestore(&host->lock, flags);
1653
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001654 /* This quirk needs to be replaced by a callback-function later */
1655 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1656 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001657}
1658
Takashi Iwai82b0e232011-04-21 20:26:38 +02001659#define SAMPLE_COUNT 5
1660
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001661static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001662{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001663 int i, ro_count;
1664
Takashi Iwai82b0e232011-04-21 20:26:38 +02001665 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001666 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001667
1668 ro_count = 0;
1669 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001670 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001671 if (++ro_count > SAMPLE_COUNT / 2)
1672 return 1;
1673 }
1674 msleep(30);
1675 }
1676 return 0;
1677}
1678
Adrian Hunter20758b62011-08-29 16:42:12 +03001679static void sdhci_hw_reset(struct mmc_host *mmc)
1680{
1681 struct sdhci_host *host = mmc_priv(mmc);
1682
1683 if (host->ops && host->ops->hw_reset)
1684 host->ops->hw_reset(host);
1685}
1686
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001687static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001688{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001689 struct sdhci_host *host = mmc_priv(mmc);
1690 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001691
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001692 sdhci_runtime_pm_get(host);
1693 ret = sdhci_do_get_ro(host);
1694 sdhci_runtime_pm_put(host);
1695 return ret;
1696}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001697
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001698static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1699{
Russell Kingbe138552014-04-25 12:55:56 +01001700 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001701 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001702 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001703 else
Russell Kingb537f942014-04-25 12:56:01 +01001704 host->ier &= ~SDHCI_INT_CARD_INT;
1705
1706 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1707 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001708 mmiowb();
1709 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001710}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001711
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001712static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1713{
1714 struct sdhci_host *host = mmc_priv(mmc);
1715 unsigned long flags;
1716
Russell Kingef104332014-04-25 12:55:41 +01001717 sdhci_runtime_pm_get(host);
1718
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001719 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001720 if (enable)
1721 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1722 else
1723 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1724
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001725 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001726 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001727
1728 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001729}
1730
Philip Rakity6231f3d2012-07-23 15:56:23 -07001731static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001732 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001733{
1734 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001735 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001736
1737 /*
1738 * Signal Voltage Switching is only applicable for Host Controllers
1739 * v3.00 and above.
1740 */
1741 if (host->version < SDHCI_SPEC_300)
1742 return 0;
1743
Philip Rakity6231f3d2012-07-23 15:56:23 -07001744 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001745
Fabio Estevam21f59982013-02-14 10:35:03 -02001746 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001747 case MMC_SIGNAL_VOLTAGE_330:
1748 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1749 ctrl &= ~SDHCI_CTRL_VDD_180;
1750 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1751
1752 if (host->vqmmc) {
1753 ret = regulator_set_voltage(host->vqmmc, 2700000, 3600000);
1754 if (ret) {
1755 pr_warning("%s: Switching to 3.3V signalling voltage "
1756 " failed\n", mmc_hostname(host->mmc));
1757 return -EIO;
1758 }
1759 }
1760 /* Wait for 5ms */
1761 usleep_range(5000, 5500);
1762
1763 /* 3.3V regulator output should be stable within 5 ms */
1764 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1765 if (!(ctrl & SDHCI_CTRL_VDD_180))
1766 return 0;
1767
1768 pr_warning("%s: 3.3V regulator output did not became stable\n",
1769 mmc_hostname(host->mmc));
1770
1771 return -EAGAIN;
1772 case MMC_SIGNAL_VOLTAGE_180:
1773 if (host->vqmmc) {
1774 ret = regulator_set_voltage(host->vqmmc,
1775 1700000, 1950000);
1776 if (ret) {
1777 pr_warning("%s: Switching to 1.8V signalling voltage "
1778 " failed\n", mmc_hostname(host->mmc));
1779 return -EIO;
1780 }
1781 }
1782
1783 /*
1784 * Enable 1.8V Signal Enable in the Host Control2
1785 * register
1786 */
1787 ctrl |= SDHCI_CTRL_VDD_180;
1788 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1789
1790 /* Wait for 5ms */
1791 usleep_range(5000, 5500);
1792
1793 /* 1.8V regulator output should be stable within 5 ms */
1794 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1795 if (ctrl & SDHCI_CTRL_VDD_180)
1796 return 0;
1797
1798 pr_warning("%s: 1.8V regulator output did not became stable\n",
1799 mmc_hostname(host->mmc));
1800
1801 return -EAGAIN;
1802 case MMC_SIGNAL_VOLTAGE_120:
1803 if (host->vqmmc) {
1804 ret = regulator_set_voltage(host->vqmmc, 1100000, 1300000);
1805 if (ret) {
1806 pr_warning("%s: Switching to 1.2V signalling voltage "
1807 " failed\n", mmc_hostname(host->mmc));
1808 return -EIO;
1809 }
1810 }
1811 return 0;
1812 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301813 /* No signal voltage switch required */
1814 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001815 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301816}
1817
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001818static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001819 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001820{
1821 struct sdhci_host *host = mmc_priv(mmc);
1822 int err;
1823
1824 if (host->version < SDHCI_SPEC_300)
1825 return 0;
1826 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001827 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001828 sdhci_runtime_pm_put(host);
1829 return err;
1830}
1831
Kevin Liu20b92a32012-12-17 19:29:26 +08001832static int sdhci_card_busy(struct mmc_host *mmc)
1833{
1834 struct sdhci_host *host = mmc_priv(mmc);
1835 u32 present_state;
1836
1837 sdhci_runtime_pm_get(host);
1838 /* Check whether DAT[3:0] is 0000 */
1839 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1840 sdhci_runtime_pm_put(host);
1841
1842 return !(present_state & SDHCI_DATA_LVL_MASK);
1843}
1844
Girish K S069c9f12012-01-06 09:56:39 +05301845static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301846{
1847 struct sdhci_host *host;
1848 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301849 int tuning_loop_counter = MAX_TUNING_LOOP;
1850 unsigned long timeout;
1851 int err = 0;
Girish K S069c9f12012-01-06 09:56:39 +05301852 bool requires_tuning_nonuhs = false;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001853 unsigned long flags;
Arindam Nathb513ea22011-05-05 12:19:04 +05301854
1855 host = mmc_priv(mmc);
1856
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001857 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001858 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301859
1860 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1861
1862 /*
Girish K S069c9f12012-01-06 09:56:39 +05301863 * The Host Controller needs tuning only in case of SDR104 mode
1864 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301865 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301866 * If the Host Controller supports the HS200 mode then the
1867 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301868 */
Girish K S069c9f12012-01-06 09:56:39 +05301869 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1870 (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02001871 host->flags & SDHCI_SDR104_NEEDS_TUNING))
Girish K S069c9f12012-01-06 09:56:39 +05301872 requires_tuning_nonuhs = true;
1873
Arindam Nathb513ea22011-05-05 12:19:04 +05301874 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S069c9f12012-01-06 09:56:39 +05301875 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301876 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1877 else {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001878 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001879 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301880 return 0;
1881 }
1882
Dong Aisheng45251812013-09-13 19:11:30 +08001883 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001884 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001885 err = host->ops->platform_execute_tuning(host, opcode);
1886 sdhci_runtime_pm_put(host);
1887 return err;
1888 }
1889
Arindam Nathb513ea22011-05-05 12:19:04 +05301890 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1891
1892 /*
1893 * As per the Host Controller spec v3.00, tuning command
1894 * generates Buffer Read Ready interrupt, so enable that.
1895 *
1896 * Note: The spec clearly says that when tuning sequence
1897 * is being performed, the controller does not generate
1898 * interrupts other than Buffer Read Ready interrupt. But
1899 * to make sure we don't hit a controller bug, we _only_
1900 * enable Buffer Read Ready interrupt here.
1901 */
Russell Kingb537f942014-04-25 12:56:01 +01001902 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1903 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301904
1905 /*
1906 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1907 * of loops reaches 40 times or a timeout of 150ms occurs.
1908 */
1909 timeout = 150;
1910 do {
1911 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001912 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301913
1914 if (!tuning_loop_counter && !timeout)
1915 break;
1916
Girish K S069c9f12012-01-06 09:56:39 +05301917 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301918 cmd.arg = 0;
1919 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1920 cmd.retries = 0;
1921 cmd.data = NULL;
1922 cmd.error = 0;
1923
1924 mrq.cmd = &cmd;
1925 host->mrq = &mrq;
1926
1927 /*
1928 * In response to CMD19, the card sends 64 bytes of tuning
1929 * block to the Host Controller. So we set the block size
1930 * to 64 here.
1931 */
Girish K S069c9f12012-01-06 09:56:39 +05301932 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1933 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1934 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1935 SDHCI_BLOCK_SIZE);
1936 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1937 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1938 SDHCI_BLOCK_SIZE);
1939 } else {
1940 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1941 SDHCI_BLOCK_SIZE);
1942 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301943
1944 /*
1945 * The tuning block is sent by the card to the host controller.
1946 * So we set the TRNS_READ bit in the Transfer Mode register.
1947 * This also takes care of setting DMA Enable and Multi Block
1948 * Select in the same register to 0.
1949 */
1950 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1951
1952 sdhci_send_command(host, &cmd);
1953
1954 host->cmd = NULL;
1955 host->mrq = NULL;
1956
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001957 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301958 /* Wait for Buffer Read Ready interrupt */
1959 wait_event_interruptible_timeout(host->buf_ready_int,
1960 (host->tuning_done == 1),
1961 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001962 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301963
1964 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301965 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301966 "Buffer Read Ready interrupt during tuning "
1967 "procedure, falling back to fixed sampling "
1968 "clock\n");
1969 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1970 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1971 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1972 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1973
1974 err = -EIO;
1975 goto out;
1976 }
1977
1978 host->tuning_done = 0;
1979
1980 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1981 tuning_loop_counter--;
1982 timeout--;
Nick Sanders197160d2014-05-06 18:52:38 -07001983
1984 /* eMMC spec does not require a delay between tuning cycles */
1985 if (opcode == MMC_SEND_TUNING_BLOCK)
1986 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05301987 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1988
1989 /*
1990 * The Host Driver has exhausted the maximum number of loops allowed,
1991 * so use fixed sampling frequency.
1992 */
1993 if (!tuning_loop_counter || !timeout) {
1994 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1995 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Dong Aisheng114f2bf2013-10-18 19:48:45 +08001996 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05301997 } else {
1998 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301999 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05302000 " failed, falling back to fixed sampling"
2001 " clock\n");
2002 err = -EIO;
2003 }
2004 }
2005
2006out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302007 /*
2008 * If this is the very first time we are here, we start the retuning
2009 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2010 * flag won't be set, we check this condition before actually starting
2011 * the timer.
2012 */
2013 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2014 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
Aaron Lu973905f2012-07-04 13:29:09 +08002015 host->flags |= SDHCI_USING_RETUNING_TIMER;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302016 mod_timer(&host->tuning_timer, jiffies +
2017 host->tuning_count * HZ);
2018 /* Tuning mode 1 limits the maximum data length to 4MB */
2019 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
Arend van Spriel2bc02482014-01-04 13:51:26 +01002020 } else if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302021 host->flags &= ~SDHCI_NEEDS_RETUNING;
2022 /* Reload the new initial value for timer */
Arend van Spriel2bc02482014-01-04 13:51:26 +01002023 mod_timer(&host->tuning_timer, jiffies +
2024 host->tuning_count * HZ);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302025 }
2026
2027 /*
2028 * In case tuning fails, host controllers which support re-tuning can
2029 * try tuning again at a later time, when the re-tuning timer expires.
2030 * So for these controllers, we return 0. Since there might be other
2031 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08002032 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2033 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302034 */
Aaron Lu973905f2012-07-04 13:29:09 +08002035 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302036 err = 0;
2037
Russell Kingb537f942014-04-25 12:56:01 +01002038 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2039 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002040 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002041 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302042
2043 return err;
2044}
2045
Kevin Liu52983382013-01-31 11:31:37 +08002046
2047static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302048{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302049 u16 ctrl;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302050
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302051 /* Host Controller v3.00 defines preset value registers */
2052 if (host->version < SDHCI_SPEC_300)
2053 return;
2054
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302055 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2056
2057 /*
2058 * We only enable or disable Preset Value if they are not already
2059 * enabled or disabled respectively. Otherwise, we bail out.
2060 */
2061 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2062 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2063 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002064 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302065 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2066 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2067 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002068 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302069 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002070}
2071
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002072static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002073{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002074 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002075 unsigned long flags;
2076
Christian Daudt722e1282013-06-20 14:26:36 -07002077 /* First check if client has provided their own card event */
2078 if (host->ops->card_event)
2079 host->ops->card_event(host);
2080
Pierre Ossmand129bce2006-03-24 03:18:17 -08002081 spin_lock_irqsave(&host->lock, flags);
2082
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002083 /* Check host->mrq first in case we are runtime suspended */
Shawn Guo9668d762013-06-09 19:49:24 +08002084 if (host->mrq && !sdhci_do_get_cd(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302085 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002086 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302087 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002088 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002089
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002090 sdhci_reset(host, SDHCI_RESET_CMD);
2091 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002092
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002093 host->mrq->cmd->error = -ENOMEDIUM;
2094 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002095 }
2096
2097 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002098}
2099
2100static const struct mmc_host_ops sdhci_ops = {
2101 .request = sdhci_request,
2102 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002103 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002104 .get_ro = sdhci_get_ro,
2105 .hw_reset = sdhci_hw_reset,
2106 .enable_sdio_irq = sdhci_enable_sdio_irq,
2107 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2108 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002109 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002110 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002111};
2112
2113/*****************************************************************************\
2114 * *
2115 * Tasklets *
2116 * *
2117\*****************************************************************************/
2118
Pierre Ossmand129bce2006-03-24 03:18:17 -08002119static void sdhci_tasklet_finish(unsigned long param)
2120{
2121 struct sdhci_host *host;
2122 unsigned long flags;
2123 struct mmc_request *mrq;
2124
2125 host = (struct sdhci_host*)param;
2126
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002127 spin_lock_irqsave(&host->lock, flags);
2128
Chris Ball0c9c99a2011-04-27 17:35:31 -04002129 /*
2130 * If this tasklet gets rescheduled while running, it will
2131 * be run again afterwards but without any active request.
2132 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002133 if (!host->mrq) {
2134 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002135 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002136 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002137
2138 del_timer(&host->timer);
2139
2140 mrq = host->mrq;
2141
Pierre Ossmand129bce2006-03-24 03:18:17 -08002142 /*
2143 * The controller needs a reset of internal state machines
2144 * upon error conditions.
2145 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002146 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002147 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002148 (mrq->data && (mrq->data->error ||
2149 (mrq->data->stop && mrq->data->stop->error))) ||
2150 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002151
2152 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002153 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002154 /* This is to force an update */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002155 sdhci_update_clock(host);
Pierre Ossman645289d2006-06-30 02:22:33 -07002156
2157 /* Spec says we should do both at the same time, but Ricoh
2158 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002159 sdhci_reset(host, SDHCI_RESET_CMD);
2160 sdhci_reset(host, SDHCI_RESET_DATA);
2161 }
2162
2163 host->mrq = NULL;
2164 host->cmd = NULL;
2165 host->data = NULL;
2166
Pierre Ossmanf9134312008-12-21 17:01:48 +01002167#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002168 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002169#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002170
Pierre Ossman5f25a662006-10-04 02:15:39 -07002171 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002172 spin_unlock_irqrestore(&host->lock, flags);
2173
2174 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002175 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002176}
2177
2178static void sdhci_timeout_timer(unsigned long data)
2179{
2180 struct sdhci_host *host;
2181 unsigned long flags;
2182
2183 host = (struct sdhci_host*)data;
2184
2185 spin_lock_irqsave(&host->lock, flags);
2186
2187 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302188 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002189 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002190 sdhci_dumpregs(host);
2191
2192 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002193 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002194 sdhci_finish_data(host);
2195 } else {
2196 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002197 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002198 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002199 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002200
2201 tasklet_schedule(&host->finish_tasklet);
2202 }
2203 }
2204
Pierre Ossman5f25a662006-10-04 02:15:39 -07002205 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002206 spin_unlock_irqrestore(&host->lock, flags);
2207}
2208
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302209static void sdhci_tuning_timer(unsigned long data)
2210{
2211 struct sdhci_host *host;
2212 unsigned long flags;
2213
2214 host = (struct sdhci_host *)data;
2215
2216 spin_lock_irqsave(&host->lock, flags);
2217
2218 host->flags |= SDHCI_NEEDS_RETUNING;
2219
2220 spin_unlock_irqrestore(&host->lock, flags);
2221}
2222
Pierre Ossmand129bce2006-03-24 03:18:17 -08002223/*****************************************************************************\
2224 * *
2225 * Interrupt handling *
2226 * *
2227\*****************************************************************************/
2228
2229static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2230{
2231 BUG_ON(intmask == 0);
2232
2233 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302234 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002235 "though no command operation was in progress.\n",
2236 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002237 sdhci_dumpregs(host);
2238 return;
2239 }
2240
Pierre Ossman43b58b32007-07-25 23:15:27 +02002241 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002242 host->cmd->error = -ETIMEDOUT;
2243 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2244 SDHCI_INT_INDEX))
2245 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002246
Pierre Ossmane8095172008-07-25 01:09:08 +02002247 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002248 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002249 return;
2250 }
2251
2252 /*
2253 * The host can send and interrupt when the busy state has
2254 * ended, allowing us to wait without wasting CPU cycles.
2255 * Unfortunately this is overloaded on the "data complete"
2256 * interrupt, so we need to take some care when handling
2257 * it.
2258 *
2259 * Note: The 1.0 specification is a bit ambiguous about this
2260 * feature so there might be some problems with older
2261 * controllers.
2262 */
2263 if (host->cmd->flags & MMC_RSP_BUSY) {
2264 if (host->cmd->data)
2265 DBG("Cannot wait for busy signal when also "
2266 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002267 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002268 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002269
2270 /* The controller does not support the end-of-busy IRQ,
2271 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002272 }
2273
2274 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002275 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002276}
2277
George G. Davis0957c332010-02-18 12:32:12 -05002278#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002279static void sdhci_show_adma_error(struct sdhci_host *host)
2280{
2281 const char *name = mmc_hostname(host->mmc);
2282 u8 *desc = host->adma_desc;
2283 __le32 *dma;
2284 __le16 *len;
2285 u8 attr;
2286
2287 sdhci_dumpregs(host);
2288
2289 while (true) {
2290 dma = (__le32 *)(desc + 4);
2291 len = (__le16 *)(desc + 2);
2292 attr = *desc;
2293
2294 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2295 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2296
2297 desc += 8;
2298
2299 if (attr & 2)
2300 break;
2301 }
2302}
2303#else
2304static void sdhci_show_adma_error(struct sdhci_host *host) { }
2305#endif
2306
Pierre Ossmand129bce2006-03-24 03:18:17 -08002307static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2308{
Girish K S069c9f12012-01-06 09:56:39 +05302309 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002310 BUG_ON(intmask == 0);
2311
Arindam Nathb513ea22011-05-05 12:19:04 +05302312 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2313 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302314 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2315 if (command == MMC_SEND_TUNING_BLOCK ||
2316 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302317 host->tuning_done = 1;
2318 wake_up(&host->buf_ready_int);
2319 return;
2320 }
2321 }
2322
Pierre Ossmand129bce2006-03-24 03:18:17 -08002323 if (!host->data) {
2324 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002325 * The "data complete" interrupt is also used to
2326 * indicate that a busy state has ended. See comment
2327 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002328 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002329 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2330 if (intmask & SDHCI_INT_DATA_END) {
2331 sdhci_finish_command(host);
2332 return;
2333 }
2334 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002335
Girish K Sa3c76eb2011-10-11 11:44:09 +05302336 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002337 "though no data operation was in progress.\n",
2338 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002339 sdhci_dumpregs(host);
2340
2341 return;
2342 }
2343
2344 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002345 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002346 else if (intmask & SDHCI_INT_DATA_END_BIT)
2347 host->data->error = -EILSEQ;
2348 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2349 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2350 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002351 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002352 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302353 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002354 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002355 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002356 if (host->ops->adma_workaround)
2357 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002358 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002359
Pierre Ossman17b04292007-07-22 22:18:46 +02002360 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002361 sdhci_finish_data(host);
2362 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002363 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002364 sdhci_transfer_pio(host);
2365
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002366 /*
2367 * We currently don't do anything fancy with DMA
2368 * boundaries, but as we can't disable the feature
2369 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002370 *
2371 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2372 * should return a valid address to continue from, but as
2373 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002374 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002375 if (intmask & SDHCI_INT_DMA_END) {
2376 u32 dmastart, dmanow;
2377 dmastart = sg_dma_address(host->data->sg);
2378 dmanow = dmastart + host->data->bytes_xfered;
2379 /*
2380 * Force update to the next DMA block boundary.
2381 */
2382 dmanow = (dmanow &
2383 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2384 SDHCI_DEFAULT_BOUNDARY_SIZE;
2385 host->data->bytes_xfered = dmanow - dmastart;
2386 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2387 " next 0x%08x\n",
2388 mmc_hostname(host->mmc), dmastart,
2389 host->data->bytes_xfered, dmanow);
2390 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2391 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002392
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002393 if (intmask & SDHCI_INT_DATA_END) {
2394 if (host->cmd) {
2395 /*
2396 * Data managed to finish before the
2397 * command completed. Make sure we do
2398 * things in the proper order.
2399 */
2400 host->data_early = 1;
2401 } else {
2402 sdhci_finish_data(host);
2403 }
2404 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002405 }
2406}
2407
David Howells7d12e782006-10-05 14:55:46 +01002408static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002409{
Russell King781e9892014-04-25 12:55:46 +01002410 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002411 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002412 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002413 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002414
2415 spin_lock(&host->lock);
2416
Russell Kingbe138552014-04-25 12:55:56 +01002417 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002418 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002419 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002420 }
2421
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002422 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002423 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002424 result = IRQ_NONE;
2425 goto out;
2426 }
2427
Russell King41005002014-04-25 12:55:36 +01002428 do {
2429 /* Clear selected interrupts. */
2430 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2431 SDHCI_INT_BUS_POWER);
2432 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002433
Russell King41005002014-04-25 12:55:36 +01002434 DBG("*** %s got interrupt: 0x%08x\n",
2435 mmc_hostname(host->mmc), intmask);
2436
2437 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2438 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2439 SDHCI_CARD_PRESENT;
2440
2441 /*
2442 * There is a observation on i.mx esdhc. INSERT
2443 * bit will be immediately set again when it gets
2444 * cleared, if a card is inserted. We have to mask
2445 * the irq to prevent interrupt storm which will
2446 * freeze the system. And the REMOVE gets the
2447 * same situation.
2448 *
2449 * More testing are needed here to ensure it works
2450 * for other platforms though.
2451 */
Russell Kingb537f942014-04-25 12:56:01 +01002452 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2453 SDHCI_INT_CARD_REMOVE);
2454 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2455 SDHCI_INT_CARD_INSERT;
2456 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2457 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002458
2459 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2460 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002461
2462 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2463 SDHCI_INT_CARD_REMOVE);
2464 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002465 }
2466
2467 if (intmask & SDHCI_INT_CMD_MASK)
2468 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
2469
2470 if (intmask & SDHCI_INT_DATA_MASK)
2471 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2472
2473 if (intmask & SDHCI_INT_BUS_POWER)
2474 pr_err("%s: Card is consuming too much power!\n",
2475 mmc_hostname(host->mmc));
2476
Russell King781e9892014-04-25 12:55:46 +01002477 if (intmask & SDHCI_INT_CARD_INT) {
2478 sdhci_enable_sdio_irq_nolock(host, false);
2479 host->thread_isr |= SDHCI_INT_CARD_INT;
2480 result = IRQ_WAKE_THREAD;
2481 }
Russell King41005002014-04-25 12:55:36 +01002482
2483 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2484 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2485 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2486 SDHCI_INT_CARD_INT);
2487
2488 if (intmask) {
2489 unexpected |= intmask;
2490 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2491 }
2492
Russell King781e9892014-04-25 12:55:46 +01002493 if (result == IRQ_NONE)
2494 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002495
2496 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002497 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002498out:
2499 spin_unlock(&host->lock);
2500
Alexander Stein6379b232012-03-14 09:52:10 +01002501 if (unexpected) {
2502 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2503 mmc_hostname(host->mmc), unexpected);
2504 sdhci_dumpregs(host);
2505 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002506
Pierre Ossmand129bce2006-03-24 03:18:17 -08002507 return result;
2508}
2509
Russell King781e9892014-04-25 12:55:46 +01002510static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2511{
2512 struct sdhci_host *host = dev_id;
2513 unsigned long flags;
2514 u32 isr;
2515
2516 spin_lock_irqsave(&host->lock, flags);
2517 isr = host->thread_isr;
2518 host->thread_isr = 0;
2519 spin_unlock_irqrestore(&host->lock, flags);
2520
Russell King3560db82014-04-25 12:55:51 +01002521 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2522 sdhci_card_event(host->mmc);
2523 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2524 }
2525
Russell King781e9892014-04-25 12:55:46 +01002526 if (isr & SDHCI_INT_CARD_INT) {
2527 sdio_run_irqs(host->mmc);
2528
2529 spin_lock_irqsave(&host->lock, flags);
2530 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2531 sdhci_enable_sdio_irq_nolock(host, true);
2532 spin_unlock_irqrestore(&host->lock, flags);
2533 }
2534
2535 return isr ? IRQ_HANDLED : IRQ_NONE;
2536}
2537
Pierre Ossmand129bce2006-03-24 03:18:17 -08002538/*****************************************************************************\
2539 * *
2540 * Suspend/resume *
2541 * *
2542\*****************************************************************************/
2543
2544#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002545void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2546{
2547 u8 val;
2548 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2549 | SDHCI_WAKE_ON_INT;
2550
2551 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2552 val |= mask ;
2553 /* Avoid fake wake up */
2554 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2555 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2556 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2557}
2558EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2559
2560void sdhci_disable_irq_wakeups(struct sdhci_host *host)
2561{
2562 u8 val;
2563 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2564 | SDHCI_WAKE_ON_INT;
2565
2566 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2567 val &= ~mask;
2568 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2569}
2570EXPORT_SYMBOL_GPL(sdhci_disable_irq_wakeups);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002571
Manuel Lauss29495aa2011-11-03 11:09:45 +01002572int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002573{
Chris Balla1b13b42012-02-06 00:43:59 -05002574 if (host->ops->platform_suspend)
2575 host->ops->platform_suspend(host);
2576
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002577 sdhci_disable_card_detection(host);
2578
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302579 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002580 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002581 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302582 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302583 }
2584
Kevin Liuad080d72013-01-05 17:21:33 +08002585 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002586 host->ier = 0;
2587 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2588 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002589 free_irq(host->irq, host);
2590 } else {
2591 sdhci_enable_irq_wakeups(host);
2592 enable_irq_wake(host->irq);
2593 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002594 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002595}
2596
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002597EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002598
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002599int sdhci_resume_host(struct sdhci_host *host)
2600{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002601 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002602
Richard Röjforsa13abc72009-09-22 16:45:30 -07002603 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002604 if (host->ops->enable_dma)
2605 host->ops->enable_dma(host);
2606 }
2607
Kevin Liuad080d72013-01-05 17:21:33 +08002608 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell King781e9892014-04-25 12:55:46 +01002609 ret = request_threaded_irq(host->irq, sdhci_irq,
2610 sdhci_thread_irq, IRQF_SHARED,
2611 mmc_hostname(host->mmc), host);
Kevin Liuad080d72013-01-05 17:21:33 +08002612 if (ret)
2613 return ret;
2614 } else {
2615 sdhci_disable_irq_wakeups(host);
2616 disable_irq_wake(host->irq);
2617 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002618
Adrian Hunter6308d292012-02-07 14:48:54 +02002619 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2620 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2621 /* Card keeps power but host controller does not */
2622 sdhci_init(host, 0);
2623 host->pwr = 0;
2624 host->clock = 0;
2625 sdhci_do_set_ios(host, &host->mmc->ios);
2626 } else {
2627 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2628 mmiowb();
2629 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002630
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002631 sdhci_enable_card_detection(host);
2632
Chris Balla1b13b42012-02-06 00:43:59 -05002633 if (host->ops->platform_resume)
2634 host->ops->platform_resume(host);
2635
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302636 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002637 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302638 host->flags |= SDHCI_NEEDS_RETUNING;
2639
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002640 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002641}
2642
2643EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002644#endif /* CONFIG_PM */
2645
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002646#ifdef CONFIG_PM_RUNTIME
2647
2648static int sdhci_runtime_pm_get(struct sdhci_host *host)
2649{
2650 return pm_runtime_get_sync(host->mmc->parent);
2651}
2652
2653static int sdhci_runtime_pm_put(struct sdhci_host *host)
2654{
2655 pm_runtime_mark_last_busy(host->mmc->parent);
2656 return pm_runtime_put_autosuspend(host->mmc->parent);
2657}
2658
Adrian Hunterf0710a52013-05-06 12:17:32 +03002659static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2660{
2661 if (host->runtime_suspended || host->bus_on)
2662 return;
2663 host->bus_on = true;
2664 pm_runtime_get_noresume(host->mmc->parent);
2665}
2666
2667static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2668{
2669 if (host->runtime_suspended || !host->bus_on)
2670 return;
2671 host->bus_on = false;
2672 pm_runtime_put_noidle(host->mmc->parent);
2673}
2674
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002675int sdhci_runtime_suspend_host(struct sdhci_host *host)
2676{
2677 unsigned long flags;
2678 int ret = 0;
2679
2680 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002681 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002682 del_timer_sync(&host->tuning_timer);
2683 host->flags &= ~SDHCI_NEEDS_RETUNING;
2684 }
2685
2686 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002687 host->ier &= SDHCI_INT_CARD_INT;
2688 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2689 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002690 spin_unlock_irqrestore(&host->lock, flags);
2691
Russell King781e9892014-04-25 12:55:46 +01002692 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002693
2694 spin_lock_irqsave(&host->lock, flags);
2695 host->runtime_suspended = true;
2696 spin_unlock_irqrestore(&host->lock, flags);
2697
2698 return ret;
2699}
2700EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2701
2702int sdhci_runtime_resume_host(struct sdhci_host *host)
2703{
2704 unsigned long flags;
2705 int ret = 0, host_flags = host->flags;
2706
2707 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2708 if (host->ops->enable_dma)
2709 host->ops->enable_dma(host);
2710 }
2711
2712 sdhci_init(host, 0);
2713
2714 /* Force clock and power re-program */
2715 host->pwr = 0;
2716 host->clock = 0;
2717 sdhci_do_set_ios(host, &host->mmc->ios);
2718
2719 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002720 if ((host_flags & SDHCI_PV_ENABLED) &&
2721 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2722 spin_lock_irqsave(&host->lock, flags);
2723 sdhci_enable_preset_value(host, true);
2724 spin_unlock_irqrestore(&host->lock, flags);
2725 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002726
2727 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002728 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002729 host->flags |= SDHCI_NEEDS_RETUNING;
2730
2731 spin_lock_irqsave(&host->lock, flags);
2732
2733 host->runtime_suspended = false;
2734
2735 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002736 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002737 sdhci_enable_sdio_irq_nolock(host, true);
2738
2739 /* Enable Card Detection */
2740 sdhci_enable_card_detection(host);
2741
2742 spin_unlock_irqrestore(&host->lock, flags);
2743
2744 return ret;
2745}
2746EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2747
2748#endif
2749
Pierre Ossmand129bce2006-03-24 03:18:17 -08002750/*****************************************************************************\
2751 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002752 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002753 * *
2754\*****************************************************************************/
2755
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002756struct sdhci_host *sdhci_alloc_host(struct device *dev,
2757 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002758{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002759 struct mmc_host *mmc;
2760 struct sdhci_host *host;
2761
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002762 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002763
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002764 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002765 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002766 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002767
2768 host = mmc_priv(mmc);
2769 host->mmc = mmc;
2770
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002771 return host;
2772}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002773
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002774EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002775
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002776int sdhci_add_host(struct sdhci_host *host)
2777{
2778 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002779 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302780 u32 max_current_caps;
2781 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002782 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002783
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002784 WARN_ON(host == NULL);
2785 if (host == NULL)
2786 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002787
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002788 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002789
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002790 if (debug_quirks)
2791 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002792 if (debug_quirks2)
2793 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002794
Pierre Ossmand96649e2006-06-30 02:22:30 -07002795 sdhci_reset(host, SDHCI_RESET_ALL);
2796
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002797 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002798 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2799 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002800 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302801 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002802 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002803 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002804 }
2805
Arindam Nathf2119df2011-05-05 12:18:57 +05302806 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002807 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002808
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002809 if (host->version >= SDHCI_SPEC_300)
2810 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2811 host->caps1 :
2812 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302813
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002814 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002815 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302816 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002817 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002818 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002819 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002820
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002821 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002822 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002823 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002824 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002825 }
2826
Arindam Nathf2119df2011-05-05 12:18:57 +05302827 if ((host->version >= SDHCI_SPEC_200) &&
2828 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002829 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002830
2831 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2832 (host->flags & SDHCI_USE_ADMA)) {
2833 DBG("Disabling ADMA as it is marked broken\n");
2834 host->flags &= ~SDHCI_USE_ADMA;
2835 }
2836
Richard Röjforsa13abc72009-09-22 16:45:30 -07002837 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002838 if (host->ops->enable_dma) {
2839 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302840 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002841 "available. Falling back to PIO.\n",
2842 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002843 host->flags &=
2844 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002845 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002846 }
2847 }
2848
Pierre Ossman2134a922008-06-28 18:28:51 +02002849 if (host->flags & SDHCI_USE_ADMA) {
2850 /*
2851 * We need to allocate descriptors for all sg entries
2852 * (128) and potentially one alignment transfer for
2853 * each of those entries.
2854 */
2855 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2856 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2857 if (!host->adma_desc || !host->align_buffer) {
2858 kfree(host->adma_desc);
2859 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302860 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002861 "buffers. Falling back to standard DMA.\n",
2862 mmc_hostname(mmc));
2863 host->flags &= ~SDHCI_USE_ADMA;
2864 }
2865 }
2866
Pierre Ossman76591502008-07-21 00:32:11 +02002867 /*
2868 * If we use DMA, then it's up to the caller to set the DMA
2869 * mask, but PIO does not need the hw shim so we set a new
2870 * mask here in that case.
2871 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002872 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002873 host->dma_mask = DMA_BIT_MASK(64);
2874 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2875 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002876
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002877 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302878 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002879 >> SDHCI_CLOCK_BASE_SHIFT;
2880 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302881 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002882 >> SDHCI_CLOCK_BASE_SHIFT;
2883
Pierre Ossmand129bce2006-03-24 03:18:17 -08002884 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002885 if (host->max_clk == 0 || host->quirks &
2886 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002887 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302888 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002889 "frequency.\n", mmc_hostname(mmc));
2890 return -ENODEV;
2891 }
2892 host->max_clk = host->ops->get_max_clock(host);
2893 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002894
2895 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302896 * In case of Host Controller v3.00, find out whether clock
2897 * multiplier is supported.
2898 */
2899 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2900 SDHCI_CLOCK_MUL_SHIFT;
2901
2902 /*
2903 * In case the value in Clock Multiplier is 0, then programmable
2904 * clock mode is not supported, otherwise the actual clock
2905 * multiplier is one more than the value of Clock Multiplier
2906 * in the Capabilities Register.
2907 */
2908 if (host->clk_mul)
2909 host->clk_mul += 1;
2910
2911 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002912 * Set host parameters.
2913 */
2914 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302915 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002916 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002917 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302918 else if (host->version >= SDHCI_SPEC_300) {
2919 if (host->clk_mul) {
2920 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2921 mmc->f_max = host->max_clk * host->clk_mul;
2922 } else
2923 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2924 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002925 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002926
Andy Shevchenko272308c2011-08-03 18:36:00 +03002927 host->timeout_clk =
2928 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2929 if (host->timeout_clk == 0) {
2930 if (host->ops->get_timeout_clock) {
2931 host->timeout_clk = host->ops->get_timeout_clock(host);
2932 } else if (!(host->quirks &
2933 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302934 pr_err("%s: Hardware doesn't specify timeout clock "
Andy Shevchenko272308c2011-08-03 18:36:00 +03002935 "frequency.\n", mmc_hostname(mmc));
2936 return -ENODEV;
2937 }
2938 }
2939 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2940 host->timeout_clk *= 1000;
2941
2942 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002943 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002944
Ulf Hansson68eb80e2013-12-18 09:57:38 +01002945 mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002946
Andrei Warkentine89d4562011-05-23 15:06:37 -05002947 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01002948 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05002949
2950 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2951 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002952
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002953 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002954 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002955 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002956 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002957 host->flags |= SDHCI_AUTO_CMD23;
2958 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2959 } else {
2960 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2961 }
2962
Philip Rakity15ec4462010-11-19 16:48:39 -05002963 /*
2964 * A controller may support 8-bit width, but the board itself
2965 * might not have the pins brought out. Boards that support
2966 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2967 * their platform code before calling sdhci_add_host(), and we
2968 * won't assume 8-bit width for hosts without that CAP.
2969 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002970 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002971 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002972
Jerry Huang63ef5d82012-10-25 13:47:19 +08002973 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
2974 mmc->caps &= ~MMC_CAP_CMD23;
2975
Arindam Nathf2119df2011-05-05 12:18:57 +05302976 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002977 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002978
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002979 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Daniel Drakeeb6d5ae2012-07-05 22:06:13 +01002980 !(host->mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002981 mmc->caps |= MMC_CAP_NEEDS_POLL;
2982
Philip Rakity6231f3d2012-07-23 15:56:23 -07002983 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Mark Brown462849a2013-07-29 21:52:55 +01002984 host->vqmmc = regulator_get_optional(mmc_dev(mmc), "vqmmc");
Kevin Liu657d5982012-10-17 19:04:44 +08002985 if (IS_ERR_OR_NULL(host->vqmmc)) {
2986 if (PTR_ERR(host->vqmmc) < 0) {
2987 pr_info("%s: no vqmmc regulator found\n",
2988 mmc_hostname(mmc));
2989 host->vqmmc = NULL;
2990 }
Kevin Liu8363c372012-11-17 17:55:51 -05002991 } else {
Chris Balla3361ab2013-03-11 17:51:53 -04002992 ret = regulator_enable(host->vqmmc);
Kevin Liucec2e212012-11-20 08:24:32 -05002993 if (!regulator_is_supported_voltage(host->vqmmc, 1700000,
2994 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05002995 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
2996 SDHCI_SUPPORT_SDR50 |
2997 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04002998 if (ret) {
2999 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3000 mmc_hostname(mmc), ret);
3001 host->vqmmc = NULL;
3002 }
Kevin Liu8363c372012-11-17 17:55:51 -05003003 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003004
Daniel Drake6a661802012-11-25 13:01:19 -05003005 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3006 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3007 SDHCI_SUPPORT_DDR50);
3008
Al Cooper4188bba2012-03-16 15:54:17 -04003009 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3010 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3011 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303012 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3013
3014 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003015 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303016 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003017 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3018 * field can be promoted to support HS200.
3019 */
David Cohen13868bf2013-10-29 10:58:26 -07003020 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
3021 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003022 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303023 mmc->caps |= MMC_CAP_UHS_SDR50;
3024
Micky Ching9107ebb2014-02-21 18:40:35 +08003025 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3026 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303027 mmc->caps |= MMC_CAP_UHS_DDR50;
3028
Girish K S069c9f12012-01-06 09:56:39 +05303029 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303030 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3031 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3032
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003033 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303034 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003035 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303036
Arindam Nathd6d50a12011-05-05 12:18:59 +05303037 /* Driver Type(s) (A, C, D) supported by the host */
3038 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3039 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3040 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3041 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3042 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3043 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3044
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303045 /* Initial value for re-tuning timer count */
3046 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3047 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3048
3049 /*
3050 * In case Re-tuning Timer is not disabled, the actual value of
3051 * re-tuning timer will be 2 ^ (n - 1).
3052 */
3053 if (host->tuning_count)
3054 host->tuning_count = 1 << (host->tuning_count - 1);
3055
3056 /* Re-tuning mode supported by the Host Controller */
3057 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3058 SDHCI_RETUNING_MODE_SHIFT;
3059
Takashi Iwai8f230f42010-12-08 10:04:30 +01003060 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003061
Mark Brown462849a2013-07-29 21:52:55 +01003062 host->vmmc = regulator_get_optional(mmc_dev(mmc), "vmmc");
Kevin Liu657d5982012-10-17 19:04:44 +08003063 if (IS_ERR_OR_NULL(host->vmmc)) {
3064 if (PTR_ERR(host->vmmc) < 0) {
3065 pr_info("%s: no vmmc regulator found\n",
3066 mmc_hostname(mmc));
3067 host->vmmc = NULL;
3068 }
Kevin Liu8363c372012-11-17 17:55:51 -05003069 }
Philip Rakitybad37e12012-05-27 18:36:44 -07003070
Philip Rakity68737042012-06-08 12:26:13 -07003071#ifdef CONFIG_REGULATOR
Marek Szyprowskia4f8f252013-02-12 09:01:36 +01003072 /*
3073 * Voltage range check makes sense only if regulator reports
3074 * any voltage value.
3075 */
3076 if (host->vmmc && regulator_get_voltage(host->vmmc) > 0) {
Kevin Liucec2e212012-11-20 08:24:32 -05003077 ret = regulator_is_supported_voltage(host->vmmc, 2700000,
3078 3600000);
Philip Rakity68737042012-06-08 12:26:13 -07003079 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_330)))
3080 caps[0] &= ~SDHCI_CAN_VDD_330;
Philip Rakity68737042012-06-08 12:26:13 -07003081 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_300)))
3082 caps[0] &= ~SDHCI_CAN_VDD_300;
Kevin Liucec2e212012-11-20 08:24:32 -05003083 ret = regulator_is_supported_voltage(host->vmmc, 1700000,
3084 1950000);
Philip Rakity68737042012-06-08 12:26:13 -07003085 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_180)))
3086 caps[0] &= ~SDHCI_CAN_VDD_180;
3087 }
3088#endif /* CONFIG_REGULATOR */
3089
Arindam Nathf2119df2011-05-05 12:18:57 +05303090 /*
3091 * According to SD Host Controller spec v3.00, if the Host System
3092 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3093 * the value is meaningful only if Voltage Support in the Capabilities
3094 * register is set. The actual current value is 4 times the register
3095 * value.
3096 */
3097 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Philip Rakitybad37e12012-05-27 18:36:44 -07003098 if (!max_current_caps && host->vmmc) {
3099 u32 curr = regulator_get_current_limit(host->vmmc);
3100 if (curr > 0) {
3101
3102 /* convert to SDHCI_MAX_CURRENT format */
3103 curr = curr/1000; /* convert to mA */
3104 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3105
3106 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3107 max_current_caps =
3108 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3109 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3110 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3111 }
3112 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303113
3114 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003115 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303116
Aaron Lu55c46652012-07-04 13:31:48 +08003117 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303118 SDHCI_MAX_CURRENT_330_MASK) >>
3119 SDHCI_MAX_CURRENT_330_SHIFT) *
3120 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303121 }
3122 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003123 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303124
Aaron Lu55c46652012-07-04 13:31:48 +08003125 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303126 SDHCI_MAX_CURRENT_300_MASK) >>
3127 SDHCI_MAX_CURRENT_300_SHIFT) *
3128 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303129 }
3130 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003131 ocr_avail |= MMC_VDD_165_195;
3132
Aaron Lu55c46652012-07-04 13:31:48 +08003133 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303134 SDHCI_MAX_CURRENT_180_MASK) >>
3135 SDHCI_MAX_CURRENT_180_SHIFT) *
3136 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303137 }
3138
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003139 if (host->ocr_mask)
3140 ocr_avail = host->ocr_mask;
3141
Takashi Iwai8f230f42010-12-08 10:04:30 +01003142 mmc->ocr_avail = ocr_avail;
3143 mmc->ocr_avail_sdio = ocr_avail;
3144 if (host->ocr_avail_sdio)
3145 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3146 mmc->ocr_avail_sd = ocr_avail;
3147 if (host->ocr_avail_sd)
3148 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3149 else /* normal SD controllers don't support 1.8V */
3150 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3151 mmc->ocr_avail_mmc = ocr_avail;
3152 if (host->ocr_avail_mmc)
3153 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003154
3155 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303156 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003157 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003158 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003159 }
3160
Pierre Ossmand129bce2006-03-24 03:18:17 -08003161 spin_lock_init(&host->lock);
3162
3163 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003164 * Maximum number of segments. Depends on if the hardware
3165 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003166 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003167 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003168 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003169 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003170 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003171 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04003172 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003173
3174 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003175 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01003176 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08003177 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003178 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003179
3180 /*
3181 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003182 * of bytes. When doing hardware scatter/gather, each entry cannot
3183 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003184 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003185 if (host->flags & SDHCI_USE_ADMA) {
3186 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3187 mmc->max_seg_size = 65535;
3188 else
3189 mmc->max_seg_size = 65536;
3190 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003191 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003192 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003193
3194 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003195 * Maximum block size. This varies from controller to controller and
3196 * is specified in the capabilities register.
3197 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003198 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3199 mmc->max_blk_size = 2;
3200 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303201 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003202 SDHCI_MAX_BLOCK_SHIFT;
3203 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303204 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003205 "assuming 512 bytes\n", mmc_hostname(mmc));
3206 mmc->max_blk_size = 0;
3207 }
3208 }
3209
3210 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003211
3212 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003213 * Maximum block count.
3214 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003215 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003216
3217 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003218 * Init tasklets.
3219 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003220 tasklet_init(&host->finish_tasklet,
3221 sdhci_tasklet_finish, (unsigned long)host);
3222
Al Viroe4cad1b2006-10-10 22:47:07 +01003223 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003224
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303225 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303226 init_waitqueue_head(&host->buf_ready_int);
3227
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303228 /* Initialize re-tuning timer */
3229 init_timer(&host->tuning_timer);
3230 host->tuning_timer.data = (unsigned long)host;
3231 host->tuning_timer.function = sdhci_tuning_timer;
3232 }
3233
Shawn Guo2af502c2013-07-05 14:38:55 +08003234 sdhci_init(host, 0);
3235
Russell King781e9892014-04-25 12:55:46 +01003236 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3237 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003238 if (ret) {
3239 pr_err("%s: Failed to request IRQ %d: %d\n",
3240 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003241 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003242 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003243
Pierre Ossmand129bce2006-03-24 03:18:17 -08003244#ifdef CONFIG_MMC_DEBUG
3245 sdhci_dumpregs(host);
3246#endif
3247
Pierre Ossmanf9134312008-12-21 17:01:48 +01003248#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003249 snprintf(host->led_name, sizeof(host->led_name),
3250 "%s::", mmc_hostname(mmc));
3251 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003252 host->led.brightness = LED_OFF;
3253 host->led.default_trigger = mmc_hostname(mmc);
3254 host->led.brightness_set = sdhci_led_control;
3255
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003256 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003257 if (ret) {
3258 pr_err("%s: Failed to register LED device: %d\n",
3259 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003260 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003261 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003262#endif
3263
Pierre Ossman5f25a662006-10-04 02:15:39 -07003264 mmiowb();
3265
Pierre Ossmand129bce2006-03-24 03:18:17 -08003266 mmc_add_host(mmc);
3267
Girish K Sa3c76eb2011-10-11 11:44:09 +05303268 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003269 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003270 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3271 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003272
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003273 sdhci_enable_card_detection(host);
3274
Pierre Ossmand129bce2006-03-24 03:18:17 -08003275 return 0;
3276
Pierre Ossmanf9134312008-12-21 17:01:48 +01003277#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003278reset:
3279 sdhci_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003280 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3281 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003282 free_irq(host->irq, host);
3283#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003284untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003285 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003286
3287 return ret;
3288}
3289
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003290EXPORT_SYMBOL_GPL(sdhci_add_host);
3291
Pierre Ossman1e728592008-04-16 19:13:13 +02003292void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003293{
Pierre Ossman1e728592008-04-16 19:13:13 +02003294 unsigned long flags;
3295
3296 if (dead) {
3297 spin_lock_irqsave(&host->lock, flags);
3298
3299 host->flags |= SDHCI_DEVICE_DEAD;
3300
3301 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303302 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003303 " transfer!\n", mmc_hostname(host->mmc));
3304
3305 host->mrq->cmd->error = -ENOMEDIUM;
3306 tasklet_schedule(&host->finish_tasklet);
3307 }
3308
3309 spin_unlock_irqrestore(&host->lock, flags);
3310 }
3311
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003312 sdhci_disable_card_detection(host);
3313
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003314 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003315
Pierre Ossmanf9134312008-12-21 17:01:48 +01003316#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003317 led_classdev_unregister(&host->led);
3318#endif
3319
Pierre Ossman1e728592008-04-16 19:13:13 +02003320 if (!dead)
3321 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003322
Russell Kingb537f942014-04-25 12:56:01 +01003323 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3324 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003325 free_irq(host->irq, host);
3326
3327 del_timer_sync(&host->timer);
3328
Pierre Ossmand129bce2006-03-24 03:18:17 -08003329 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003330
Philip Rakity77dcb3f2012-07-23 17:25:18 -07003331 if (host->vmmc) {
3332 regulator_disable(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003333 regulator_put(host->vmmc);
Philip Rakity77dcb3f2012-07-23 17:25:18 -07003334 }
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003335
Philip Rakity6231f3d2012-07-23 15:56:23 -07003336 if (host->vqmmc) {
3337 regulator_disable(host->vqmmc);
3338 regulator_put(host->vqmmc);
3339 }
3340
Pierre Ossman2134a922008-06-28 18:28:51 +02003341 kfree(host->adma_desc);
3342 kfree(host->align_buffer);
3343
3344 host->adma_desc = NULL;
3345 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003346}
3347
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003348EXPORT_SYMBOL_GPL(sdhci_remove_host);
3349
3350void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003351{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003352 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003353}
3354
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003355EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003356
3357/*****************************************************************************\
3358 * *
3359 * Driver init/exit *
3360 * *
3361\*****************************************************************************/
3362
3363static int __init sdhci_drv_init(void)
3364{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303365 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003366 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303367 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003368
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003369 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003370}
3371
3372static void __exit sdhci_drv_exit(void)
3373{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003374}
3375
3376module_init(sdhci_drv_init);
3377module_exit(sdhci_drv_exit);
3378
Pierre Ossmandf673b22006-06-30 02:22:31 -07003379module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003380module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003381
Pierre Ossman32710e82009-04-08 20:14:54 +02003382MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003383MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003384MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003385
Pierre Ossmandf673b22006-06-30 02:22:31 -07003386MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003387MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");