blob: 5c8b192c4c3ec9d455695e9ee1720cbbe5c1aba9 [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 Lu473b095a2012-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
Russell Kingd1e49f72014-04-25 12:58:34 +010047#define ADMA_SIZE ((128 * 2 + 1) * 4)
48
Pierre Ossmandf673b22006-06-30 02:22:31 -070049static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030050static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070051
Pierre Ossmand129bce2006-03-24 03:18:17 -080052static void sdhci_finish_data(struct sdhci_host *);
53
Pierre Ossmand129bce2006-03-24 03:18:17 -080054static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053055static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053056static void sdhci_tuning_timer(unsigned long data);
Kevin Liu52983382013-01-31 11:31:37 +080057static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080058
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030059#ifdef CONFIG_PM_RUNTIME
60static int sdhci_runtime_pm_get(struct sdhci_host *host);
61static int sdhci_runtime_pm_put(struct sdhci_host *host);
Adrian Hunterf0710a52013-05-06 12:17:32 +030062static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
63static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030064#else
65static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
66{
67 return 0;
68}
69static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
70{
71 return 0;
72}
Adrian Hunterf0710a52013-05-06 12:17:32 +030073static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
74{
75}
76static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
77{
78}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030079#endif
80
Pierre Ossmand129bce2006-03-24 03:18:17 -080081static void sdhci_dumpregs(struct sdhci_host *host)
82{
Girish K Sa3c76eb2011-10-11 11:44:09 +053083 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070084 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080085
Girish K Sa3c76eb2011-10-11 11:44:09 +053086 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030087 sdhci_readl(host, SDHCI_DMA_ADDRESS),
88 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053089 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030090 sdhci_readw(host, SDHCI_BLOCK_SIZE),
91 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053092 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030093 sdhci_readl(host, SDHCI_ARGUMENT),
94 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053095 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030096 sdhci_readl(host, SDHCI_PRESENT_STATE),
97 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053098 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030099 sdhci_readb(host, SDHCI_POWER_CONTROL),
100 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530101 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300102 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
103 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530104 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300105 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
106 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530107 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300108 sdhci_readl(host, SDHCI_INT_ENABLE),
109 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530110 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300111 sdhci_readw(host, SDHCI_ACMD12_ERR),
112 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530113 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300114 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500115 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530116 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500117 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300118 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530119 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530120 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800121
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100122 if (host->flags & SDHCI_USE_ADMA)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530123 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100124 readl(host->ioaddr + SDHCI_ADMA_ERROR),
125 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
126
Girish K Sa3c76eb2011-10-11 11:44:09 +0530127 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800128}
129
130/*****************************************************************************\
131 * *
132 * Low level functions *
133 * *
134\*****************************************************************************/
135
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300136static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
137{
Russell King5b4f1f62014-04-25 12:57:02 +0100138 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300139
Adrian Hunterc79396c2011-12-27 15:48:42 +0200140 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100141 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300142 return;
143
Russell King5b4f1f62014-04-25 12:57:02 +0100144 if (enable) {
145 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
146 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800147
Russell King5b4f1f62014-04-25 12:57:02 +0100148 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
149 SDHCI_INT_CARD_INSERT;
150 } else {
151 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
152 }
Russell Kingb537f942014-04-25 12:56:01 +0100153
154 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
155 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300156}
157
158static void sdhci_enable_card_detection(struct sdhci_host *host)
159{
160 sdhci_set_card_detection(host, true);
161}
162
163static void sdhci_disable_card_detection(struct sdhci_host *host)
164{
165 sdhci_set_card_detection(host, false);
166}
167
Russell King03231f92014-04-25 12:57:12 +0100168void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800169{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700170 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800171
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300172 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800173
Adrian Hunterf0710a52013-05-06 12:17:32 +0300174 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800175 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300176 /* Reset-all turns off SD Bus Power */
177 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
178 sdhci_runtime_pm_bus_off(host);
179 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800180
Pierre Ossmane16514d82006-06-30 02:22:24 -0700181 /* Wait max 100 ms */
182 timeout = 100;
183
184 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300185 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700186 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530187 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700188 mmc_hostname(host->mmc), (int)mask);
189 sdhci_dumpregs(host);
190 return;
191 }
192 timeout--;
193 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800194 }
Russell King03231f92014-04-25 12:57:12 +0100195}
196EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300197
Russell King03231f92014-04-25 12:57:12 +0100198static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
199{
200 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
201 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
202 SDHCI_CARD_PRESENT))
203 return;
204 }
205
206 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800207
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800208 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
209 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
210 host->ops->enable_dma(host);
211 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800212}
213
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800214static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
215
216static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800217{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800218 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100219 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800220 else
Russell King03231f92014-04-25 12:57:12 +0100221 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222
Russell Kingb537f942014-04-25 12:56:01 +0100223 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
224 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
225 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
226 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
227 SDHCI_INT_RESPONSE;
228
229 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
230 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800231
232 if (soft) {
233 /* force clock reconfiguration */
234 host->clock = 0;
235 sdhci_set_ios(host->mmc, &host->mmc->ios);
236 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300237}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800238
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300239static void sdhci_reinit(struct sdhci_host *host)
240{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800241 sdhci_init(host, 0);
Aaron Lub67c6b42012-06-29 16:17:31 +0800242 /*
243 * Retuning stuffs are affected by different cards inserted and only
244 * applicable to UHS-I cards. So reset these fields to their initial
245 * value when card is removed.
246 */
Aaron Lu973905f2012-07-04 13:29:09 +0800247 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
248 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
249
Aaron Lub67c6b42012-06-29 16:17:31 +0800250 del_timer_sync(&host->tuning_timer);
251 host->flags &= ~SDHCI_NEEDS_RETUNING;
252 host->mmc->max_blk_count =
253 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
254 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300255 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800256}
257
258static void sdhci_activate_led(struct sdhci_host *host)
259{
260 u8 ctrl;
261
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300262 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800263 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300264 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800265}
266
267static void sdhci_deactivate_led(struct sdhci_host *host)
268{
269 u8 ctrl;
270
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300271 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800272 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300273 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800274}
275
Pierre Ossmanf9134312008-12-21 17:01:48 +0100276#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100277static void sdhci_led_control(struct led_classdev *led,
278 enum led_brightness brightness)
279{
280 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
281 unsigned long flags;
282
283 spin_lock_irqsave(&host->lock, flags);
284
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300285 if (host->runtime_suspended)
286 goto out;
287
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100288 if (brightness == LED_OFF)
289 sdhci_deactivate_led(host);
290 else
291 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300292out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100293 spin_unlock_irqrestore(&host->lock, flags);
294}
295#endif
296
Pierre Ossmand129bce2006-03-24 03:18:17 -0800297/*****************************************************************************\
298 * *
299 * Core functions *
300 * *
301\*****************************************************************************/
302
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100303static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800304{
Pierre Ossman76591502008-07-21 00:32:11 +0200305 unsigned long flags;
306 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700307 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200308 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800309
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100310 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800311
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100312 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200313 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800314
Pierre Ossman76591502008-07-21 00:32:11 +0200315 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800316
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100317 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200318 if (!sg_miter_next(&host->sg_miter))
319 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800320
Pierre Ossman76591502008-07-21 00:32:11 +0200321 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800322
Pierre Ossman76591502008-07-21 00:32:11 +0200323 blksize -= len;
324 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200325
Pierre Ossman76591502008-07-21 00:32:11 +0200326 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800327
Pierre Ossman76591502008-07-21 00:32:11 +0200328 while (len) {
329 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300330 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200331 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800332 }
Pierre Ossman76591502008-07-21 00:32:11 +0200333
334 *buf = scratch & 0xFF;
335
336 buf++;
337 scratch >>= 8;
338 chunk--;
339 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800340 }
341 }
Pierre Ossman76591502008-07-21 00:32:11 +0200342
343 sg_miter_stop(&host->sg_miter);
344
345 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100346}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800347
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100348static void sdhci_write_block_pio(struct sdhci_host *host)
349{
Pierre Ossman76591502008-07-21 00:32:11 +0200350 unsigned long flags;
351 size_t blksize, len, chunk;
352 u32 scratch;
353 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100354
355 DBG("PIO writing\n");
356
357 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200358 chunk = 0;
359 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100360
Pierre Ossman76591502008-07-21 00:32:11 +0200361 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100362
363 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200364 if (!sg_miter_next(&host->sg_miter))
365 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100366
Pierre Ossman76591502008-07-21 00:32:11 +0200367 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200368
Pierre Ossman76591502008-07-21 00:32:11 +0200369 blksize -= len;
370 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100371
Pierre Ossman76591502008-07-21 00:32:11 +0200372 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100373
Pierre Ossman76591502008-07-21 00:32:11 +0200374 while (len) {
375 scratch |= (u32)*buf << (chunk * 8);
376
377 buf++;
378 chunk++;
379 len--;
380
381 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300382 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200383 chunk = 0;
384 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100385 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100386 }
387 }
Pierre Ossman76591502008-07-21 00:32:11 +0200388
389 sg_miter_stop(&host->sg_miter);
390
391 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100392}
393
394static void sdhci_transfer_pio(struct sdhci_host *host)
395{
396 u32 mask;
397
398 BUG_ON(!host->data);
399
Pierre Ossman76591502008-07-21 00:32:11 +0200400 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100401 return;
402
403 if (host->data->flags & MMC_DATA_READ)
404 mask = SDHCI_DATA_AVAILABLE;
405 else
406 mask = SDHCI_SPACE_AVAILABLE;
407
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200408 /*
409 * Some controllers (JMicron JMB38x) mess up the buffer bits
410 * for transfers < 4 bytes. As long as it is just one block,
411 * we can ignore the bits.
412 */
413 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
414 (host->data->blocks == 1))
415 mask = ~0;
416
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300417 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300418 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
419 udelay(100);
420
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100421 if (host->data->flags & MMC_DATA_READ)
422 sdhci_read_block_pio(host);
423 else
424 sdhci_write_block_pio(host);
425
Pierre Ossman76591502008-07-21 00:32:11 +0200426 host->blocks--;
427 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100428 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100429 }
430
431 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800432}
433
Pierre Ossman2134a922008-06-28 18:28:51 +0200434static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
435{
436 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800437 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200438}
439
440static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
441{
Cong Wang482fce92011-11-27 13:27:00 +0800442 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200443 local_irq_restore(*flags);
444}
445
Ben Dooks118cd172010-03-05 13:43:26 -0800446static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
447{
Ben Dooks9e506f32010-03-05 13:43:29 -0800448 __le32 *dataddr = (__le32 __force *)(desc + 4);
449 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800450
Ben Dooks9e506f32010-03-05 13:43:29 -0800451 /* SDHCI specification says ADMA descriptors should be 4 byte
452 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800453
Ben Dooks9e506f32010-03-05 13:43:29 -0800454 cmdlen[0] = cpu_to_le16(cmd);
455 cmdlen[1] = cpu_to_le16(len);
456
457 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800458}
459
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200460static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200461 struct mmc_data *data)
462{
463 int direction;
464
465 u8 *desc;
466 u8 *align;
467 dma_addr_t addr;
468 dma_addr_t align_addr;
469 int len, offset;
470
471 struct scatterlist *sg;
472 int i;
473 char *buffer;
474 unsigned long flags;
475
476 /*
477 * The spec does not specify endianness of descriptor table.
478 * We currently guess that it is LE.
479 */
480
481 if (data->flags & MMC_DATA_READ)
482 direction = DMA_FROM_DEVICE;
483 else
484 direction = DMA_TO_DEVICE;
485
Pierre Ossman2134a922008-06-28 18:28:51 +0200486 host->align_addr = dma_map_single(mmc_dev(host->mmc),
487 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700488 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200489 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200490 BUG_ON(host->align_addr & 0x3);
491
492 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
493 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200494 if (host->sg_count == 0)
495 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200496
497 desc = host->adma_desc;
498 align = host->align_buffer;
499
500 align_addr = host->align_addr;
501
502 for_each_sg(data->sg, sg, host->sg_count, i) {
503 addr = sg_dma_address(sg);
504 len = sg_dma_len(sg);
505
506 /*
507 * The SDHCI specification states that ADMA
508 * addresses must be 32-bit aligned. If they
509 * aren't, then we use a bounce buffer for
510 * the (up to three) bytes that screw up the
511 * alignment.
512 */
513 offset = (4 - (addr & 0x3)) & 0x3;
514 if (offset) {
515 if (data->flags & MMC_DATA_WRITE) {
516 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200517 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200518 memcpy(align, buffer, offset);
519 sdhci_kunmap_atomic(buffer, &flags);
520 }
521
Ben Dooks118cd172010-03-05 13:43:26 -0800522 /* tran, valid */
523 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200524
525 BUG_ON(offset > 65536);
526
Pierre Ossman2134a922008-06-28 18:28:51 +0200527 align += 4;
528 align_addr += 4;
529
530 desc += 8;
531
532 addr += offset;
533 len -= offset;
534 }
535
Pierre Ossman2134a922008-06-28 18:28:51 +0200536 BUG_ON(len > 65536);
537
Ben Dooks118cd172010-03-05 13:43:26 -0800538 /* tran, valid */
539 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200540 desc += 8;
541
542 /*
543 * If this triggers then we have a calculation bug
544 * somewhere. :/
545 */
Russell Kingd1e49f72014-04-25 12:58:34 +0100546 WARN_ON((desc - host->adma_desc) > ADMA_SIZE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200547 }
548
Thomas Abraham70764a92010-05-26 14:42:04 -0700549 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
550 /*
551 * Mark the last descriptor as the terminating descriptor
552 */
553 if (desc != host->adma_desc) {
554 desc -= 8;
555 desc[0] |= 0x2; /* end */
556 }
557 } else {
558 /*
559 * Add a terminating entry.
560 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200561
Thomas Abraham70764a92010-05-26 14:42:04 -0700562 /* nop, end, valid */
563 sdhci_set_adma_desc(desc, 0, 0, 0x3);
564 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200565
566 /*
567 * Resync align buffer as we might have changed it.
568 */
569 if (data->flags & MMC_DATA_WRITE) {
570 dma_sync_single_for_device(mmc_dev(host->mmc),
571 host->align_addr, 128 * 4, direction);
572 }
573
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200574 return 0;
575
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200576unmap_align:
577 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
578 128 * 4, direction);
579fail:
580 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200581}
582
583static void sdhci_adma_table_post(struct sdhci_host *host,
584 struct mmc_data *data)
585{
586 int direction;
587
588 struct scatterlist *sg;
589 int i, size;
590 u8 *align;
591 char *buffer;
592 unsigned long flags;
Russell Kingde0b65a2014-04-25 12:58:29 +0100593 bool has_unaligned;
Pierre Ossman2134a922008-06-28 18:28:51 +0200594
595 if (data->flags & MMC_DATA_READ)
596 direction = DMA_FROM_DEVICE;
597 else
598 direction = DMA_TO_DEVICE;
599
Pierre Ossman2134a922008-06-28 18:28:51 +0200600 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
601 128 * 4, direction);
602
Russell Kingde0b65a2014-04-25 12:58:29 +0100603 /* Do a quick scan of the SG list for any unaligned mappings */
604 has_unaligned = false;
605 for_each_sg(data->sg, sg, host->sg_count, i)
606 if (sg_dma_address(sg) & 3) {
607 has_unaligned = true;
608 break;
609 }
610
611 if (has_unaligned && data->flags & MMC_DATA_READ) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200612 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
613 data->sg_len, direction);
614
615 align = host->align_buffer;
616
617 for_each_sg(data->sg, sg, host->sg_count, i) {
618 if (sg_dma_address(sg) & 0x3) {
619 size = 4 - (sg_dma_address(sg) & 0x3);
620
621 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200622 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200623 memcpy(buffer, align, size);
624 sdhci_kunmap_atomic(buffer, &flags);
625
626 align += 4;
627 }
628 }
629 }
630
631 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
632 data->sg_len, direction);
633}
634
Andrei Warkentina3c77782011-04-11 16:13:42 -0500635static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800636{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700637 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500638 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700639 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800640
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200641 /*
642 * If the host controller provides us with an incorrect timeout
643 * value, just skip the check and use 0xE. The hardware may take
644 * longer to time out, but that's much better than having a too-short
645 * timeout value.
646 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200647 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200648 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200649
Andrei Warkentina3c77782011-04-11 16:13:42 -0500650 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100651 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500652 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800653
Andrei Warkentina3c77782011-04-11 16:13:42 -0500654 /* timeout in us */
655 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100656 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300657 else {
658 target_timeout = data->timeout_ns / 1000;
659 if (host->clock)
660 target_timeout += data->timeout_clks / host->clock;
661 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700662
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700663 /*
664 * Figure out needed cycles.
665 * We do this in steps in order to fit inside a 32 bit int.
666 * The first step is the minimum timeout, which will have a
667 * minimum resolution of 6 bits:
668 * (1) 2^13*1000 > 2^22,
669 * (2) host->timeout_clk < 2^16
670 * =>
671 * (1) / (2) > 2^6
672 */
673 count = 0;
674 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
675 while (current_timeout < target_timeout) {
676 count++;
677 current_timeout <<= 1;
678 if (count >= 0xF)
679 break;
680 }
681
682 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400683 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
684 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700685 count = 0xE;
686 }
687
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200688 return count;
689}
690
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300691static void sdhci_set_transfer_irqs(struct sdhci_host *host)
692{
693 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
694 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
695
696 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100697 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300698 else
Russell Kingb537f942014-04-25 12:56:01 +0100699 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
700
701 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
702 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300703}
704
Andrei Warkentina3c77782011-04-11 16:13:42 -0500705static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200706{
707 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200708 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500709 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200710 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200711
712 WARN_ON(host->data);
713
Andrei Warkentina3c77782011-04-11 16:13:42 -0500714 if (data || (cmd->flags & MMC_RSP_BUSY)) {
715 count = sdhci_calc_timeout(host, cmd);
716 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
717 }
718
719 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200720 return;
721
722 /* Sanity checks */
723 BUG_ON(data->blksz * data->blocks > 524288);
724 BUG_ON(data->blksz > host->mmc->max_blk_size);
725 BUG_ON(data->blocks > 65535);
726
727 host->data = data;
728 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400729 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200730
Richard Röjforsa13abc72009-09-22 16:45:30 -0700731 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100732 host->flags |= SDHCI_REQ_USE_DMA;
733
Pierre Ossman2134a922008-06-28 18:28:51 +0200734 /*
735 * FIXME: This doesn't account for merging when mapping the
736 * scatterlist.
737 */
738 if (host->flags & SDHCI_REQ_USE_DMA) {
739 int broken, i;
740 struct scatterlist *sg;
741
742 broken = 0;
743 if (host->flags & SDHCI_USE_ADMA) {
744 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
745 broken = 1;
746 } else {
747 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
748 broken = 1;
749 }
750
751 if (unlikely(broken)) {
752 for_each_sg(data->sg, sg, data->sg_len, i) {
753 if (sg->length & 0x3) {
754 DBG("Reverting to PIO because of "
755 "transfer size (%d)\n",
756 sg->length);
757 host->flags &= ~SDHCI_REQ_USE_DMA;
758 break;
759 }
760 }
761 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100762 }
763
764 /*
765 * The assumption here being that alignment is the same after
766 * translation to device address space.
767 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200768 if (host->flags & SDHCI_REQ_USE_DMA) {
769 int broken, i;
770 struct scatterlist *sg;
771
772 broken = 0;
773 if (host->flags & SDHCI_USE_ADMA) {
774 /*
775 * As we use 3 byte chunks to work around
776 * alignment problems, we need to check this
777 * quirk.
778 */
779 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
780 broken = 1;
781 } else {
782 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
783 broken = 1;
784 }
785
786 if (unlikely(broken)) {
787 for_each_sg(data->sg, sg, data->sg_len, i) {
788 if (sg->offset & 0x3) {
789 DBG("Reverting to PIO because of "
790 "bad alignment\n");
791 host->flags &= ~SDHCI_REQ_USE_DMA;
792 break;
793 }
794 }
795 }
796 }
797
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200798 if (host->flags & SDHCI_REQ_USE_DMA) {
799 if (host->flags & SDHCI_USE_ADMA) {
800 ret = sdhci_adma_table_pre(host, data);
801 if (ret) {
802 /*
803 * This only happens when someone fed
804 * us an invalid request.
805 */
806 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200807 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200808 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300809 sdhci_writel(host, host->adma_addr,
810 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200811 }
812 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300813 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200814
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300815 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200816 data->sg, data->sg_len,
817 (data->flags & MMC_DATA_READ) ?
818 DMA_FROM_DEVICE :
819 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300820 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200821 /*
822 * This only happens when someone fed
823 * us an invalid request.
824 */
825 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200826 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200827 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200828 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300829 sdhci_writel(host, sg_dma_address(data->sg),
830 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200831 }
832 }
833 }
834
Pierre Ossman2134a922008-06-28 18:28:51 +0200835 /*
836 * Always adjust the DMA selection as some controllers
837 * (e.g. JMicron) can't do PIO properly when the selection
838 * is ADMA.
839 */
840 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300841 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200842 ctrl &= ~SDHCI_CTRL_DMA_MASK;
843 if ((host->flags & SDHCI_REQ_USE_DMA) &&
844 (host->flags & SDHCI_USE_ADMA))
845 ctrl |= SDHCI_CTRL_ADMA32;
846 else
847 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300848 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100849 }
850
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200851 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200852 int flags;
853
854 flags = SG_MITER_ATOMIC;
855 if (host->data->flags & MMC_DATA_READ)
856 flags |= SG_MITER_TO_SG;
857 else
858 flags |= SG_MITER_FROM_SG;
859 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200860 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800861 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700862
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300863 sdhci_set_transfer_irqs(host);
864
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400865 /* Set the DMA boundary value and block size */
866 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
867 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300868 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700869}
870
871static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500872 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700873{
874 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500875 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700876
Dong Aisheng2b558c12013-10-30 22:09:48 +0800877 if (data == NULL) {
878 /* clear Auto CMD settings for no data CMDs */
879 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
880 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
881 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700882 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800883 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700884
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200885 WARN_ON(!host->data);
886
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700887 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500888 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
889 mode |= SDHCI_TRNS_MULTI;
890 /*
891 * If we are sending CMD23, CMD12 never gets sent
892 * on successful completion (so no Auto-CMD12).
893 */
894 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
895 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500896 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
897 mode |= SDHCI_TRNS_AUTO_CMD23;
898 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
899 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700900 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500901
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700902 if (data->flags & MMC_DATA_READ)
903 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100904 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700905 mode |= SDHCI_TRNS_DMA;
906
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300907 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800908}
909
910static void sdhci_finish_data(struct sdhci_host *host)
911{
912 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800913
914 BUG_ON(!host->data);
915
916 data = host->data;
917 host->data = NULL;
918
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100919 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200920 if (host->flags & SDHCI_USE_ADMA)
921 sdhci_adma_table_post(host, data);
922 else {
923 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
924 data->sg_len, (data->flags & MMC_DATA_READ) ?
925 DMA_FROM_DEVICE : DMA_TO_DEVICE);
926 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800927 }
928
929 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200930 * The specification states that the block count register must
931 * be updated, but it does not specify at what point in the
932 * data flow. That makes the register entirely useless to read
933 * back so we have to assume that nothing made it to the card
934 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800935 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200936 if (data->error)
937 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800938 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200939 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800940
Andrei Warkentine89d4562011-05-23 15:06:37 -0500941 /*
942 * Need to send CMD12 if -
943 * a) open-ended multiblock transfer (no CMD23)
944 * b) error in multiblock transfer
945 */
946 if (data->stop &&
947 (data->error ||
948 !host->mrq->sbc)) {
949
Pierre Ossmand129bce2006-03-24 03:18:17 -0800950 /*
951 * The controller needs a reset of internal state machines
952 * upon error conditions.
953 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200954 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100955 sdhci_do_reset(host, SDHCI_RESET_CMD);
956 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800957 }
958
959 sdhci_send_command(host, data->stop);
960 } else
961 tasklet_schedule(&host->finish_tasklet);
962}
963
Dong Aishengc0e551292013-09-13 19:11:31 +0800964void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800965{
966 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700967 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700968 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800969
970 WARN_ON(host->cmd);
971
Pierre Ossmand129bce2006-03-24 03:18:17 -0800972 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700973 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700974
975 mask = SDHCI_CMD_INHIBIT;
976 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
977 mask |= SDHCI_DATA_INHIBIT;
978
979 /* We shouldn't wait for data inihibit for stop commands, even
980 though they might use busy signaling */
981 if (host->mrq->data && (cmd == host->mrq->data->stop))
982 mask &= ~SDHCI_DATA_INHIBIT;
983
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300984 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700985 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530986 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100987 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800988 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200989 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800990 tasklet_schedule(&host->finish_tasklet);
991 return;
992 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700993 timeout--;
994 mdelay(1);
995 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996
Adrian Hunter3e1a6892013-11-14 10:16:20 +0200997 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100998 if (!cmd->data && cmd->busy_timeout > 9000)
999 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001000 else
1001 timeout += 10 * HZ;
1002 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001003
1004 host->cmd = cmd;
1005
Andrei Warkentina3c77782011-04-11 16:13:42 -05001006 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001007
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001008 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009
Andrei Warkentine89d4562011-05-23 15:06:37 -05001010 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001011
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301013 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001015 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001016 tasklet_schedule(&host->finish_tasklet);
1017 return;
1018 }
1019
1020 if (!(cmd->flags & MMC_RSP_PRESENT))
1021 flags = SDHCI_CMD_RESP_NONE;
1022 else if (cmd->flags & MMC_RSP_136)
1023 flags = SDHCI_CMD_RESP_LONG;
1024 else if (cmd->flags & MMC_RSP_BUSY)
1025 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1026 else
1027 flags = SDHCI_CMD_RESP_SHORT;
1028
1029 if (cmd->flags & MMC_RSP_CRC)
1030 flags |= SDHCI_CMD_CRC;
1031 if (cmd->flags & MMC_RSP_OPCODE)
1032 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301033
1034 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301035 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1036 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001037 flags |= SDHCI_CMD_DATA;
1038
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001039 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001040}
Dong Aishengc0e551292013-09-13 19:11:31 +08001041EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001042
1043static void sdhci_finish_command(struct sdhci_host *host)
1044{
1045 int i;
1046
1047 BUG_ON(host->cmd == NULL);
1048
1049 if (host->cmd->flags & MMC_RSP_PRESENT) {
1050 if (host->cmd->flags & MMC_RSP_136) {
1051 /* CRC is stripped so we need to do some shifting. */
1052 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001053 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001054 SDHCI_RESPONSE + (3-i)*4) << 8;
1055 if (i != 3)
1056 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001057 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001058 SDHCI_RESPONSE + (3-i)*4-1);
1059 }
1060 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001061 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001062 }
1063 }
1064
Pierre Ossman17b04292007-07-22 22:18:46 +02001065 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001066
Andrei Warkentine89d4562011-05-23 15:06:37 -05001067 /* Finished CMD23, now send actual command. */
1068 if (host->cmd == host->mrq->sbc) {
1069 host->cmd = NULL;
1070 sdhci_send_command(host, host->mrq->cmd);
1071 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001072
Andrei Warkentine89d4562011-05-23 15:06:37 -05001073 /* Processed actual command. */
1074 if (host->data && host->data_early)
1075 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001076
Andrei Warkentine89d4562011-05-23 15:06:37 -05001077 if (!host->cmd->data)
1078 tasklet_schedule(&host->finish_tasklet);
1079
1080 host->cmd = NULL;
1081 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001082}
1083
Kevin Liu52983382013-01-31 11:31:37 +08001084static u16 sdhci_get_preset_value(struct sdhci_host *host)
1085{
Russell Kingd975f122014-04-25 12:59:31 +01001086 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001087
Russell Kingd975f122014-04-25 12:59:31 +01001088 switch (host->timing) {
1089 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001090 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1091 break;
Russell Kingd975f122014-04-25 12:59:31 +01001092 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001093 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1094 break;
Russell Kingd975f122014-04-25 12:59:31 +01001095 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001096 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1097 break;
Russell Kingd975f122014-04-25 12:59:31 +01001098 case MMC_TIMING_UHS_SDR104:
1099 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001100 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1101 break;
Russell Kingd975f122014-04-25 12:59:31 +01001102 case MMC_TIMING_UHS_DDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001103 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1104 break;
1105 default:
1106 pr_warn("%s: Invalid UHS-I mode selected\n",
1107 mmc_hostname(host->mmc));
1108 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1109 break;
1110 }
1111 return preset;
1112}
1113
Russell King17710592014-04-25 12:58:55 +01001114void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001115{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301116 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001117 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301118 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001119 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001120
Russell King1650d0c2014-04-25 12:58:50 +01001121 host->mmc->actual_clock = 0;
1122
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001123 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001124
1125 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001126 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001127
Zhangfei Gao85105c52010-08-06 07:10:01 +08001128 if (host->version >= SDHCI_SPEC_300) {
Kevin Liu52983382013-01-31 11:31:37 +08001129 if (sdhci_readw(host, SDHCI_HOST_CONTROL2) &
1130 SDHCI_CTRL_PRESET_VAL_ENABLE) {
1131 u16 pre_val;
1132
1133 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1134 pre_val = sdhci_get_preset_value(host);
1135 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1136 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1137 if (host->clk_mul &&
1138 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1139 clk = SDHCI_PROG_CLOCK_MODE;
1140 real_div = div + 1;
1141 clk_mul = host->clk_mul;
1142 } else {
1143 real_div = max_t(int, 1, div << 1);
1144 }
1145 goto clock_set;
1146 }
1147
Arindam Nathc3ed3872011-05-05 12:19:06 +05301148 /*
1149 * Check if the Host Controller supports Programmable Clock
1150 * Mode.
1151 */
1152 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001153 for (div = 1; div <= 1024; div++) {
1154 if ((host->max_clk * host->clk_mul / div)
1155 <= clock)
1156 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001157 }
Kevin Liu52983382013-01-31 11:31:37 +08001158 /*
1159 * Set Programmable Clock Mode in the Clock
1160 * Control register.
1161 */
1162 clk = SDHCI_PROG_CLOCK_MODE;
1163 real_div = div;
1164 clk_mul = host->clk_mul;
1165 div--;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301166 } else {
1167 /* Version 3.00 divisors must be a multiple of 2. */
1168 if (host->max_clk <= clock)
1169 div = 1;
1170 else {
1171 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1172 div += 2) {
1173 if ((host->max_clk / div) <= clock)
1174 break;
1175 }
1176 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001177 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301178 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001179 }
1180 } else {
1181 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001182 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001183 if ((host->max_clk / div) <= clock)
1184 break;
1185 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001186 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301187 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001188 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001189
Kevin Liu52983382013-01-31 11:31:37 +08001190clock_set:
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001191 if (real_div)
1192 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1193
Arindam Nathc3ed3872011-05-05 12:19:06 +05301194 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001195 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1196 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001197 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001198 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001199
Chris Ball27f6cb12009-09-22 16:45:31 -07001200 /* Wait max 20 ms */
1201 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001202 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001203 & SDHCI_CLOCK_INT_STABLE)) {
1204 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301205 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001206 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001207 sdhci_dumpregs(host);
1208 return;
1209 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001210 timeout--;
1211 mdelay(1);
1212 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001213
1214 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001215 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001216}
Russell King17710592014-04-25 12:58:55 +01001217EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001218
Adrian Hunterceb61432011-12-27 15:48:41 +02001219static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001220{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001221 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001222
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001223 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001224 switch (1 << power) {
1225 case MMC_VDD_165_195:
1226 pwr = SDHCI_POWER_180;
1227 break;
1228 case MMC_VDD_29_30:
1229 case MMC_VDD_30_31:
1230 pwr = SDHCI_POWER_300;
1231 break;
1232 case MMC_VDD_32_33:
1233 case MMC_VDD_33_34:
1234 pwr = SDHCI_POWER_330;
1235 break;
1236 default:
1237 BUG();
1238 }
1239 }
1240
1241 if (host->pwr == pwr)
Adrian Hunterceb61432011-12-27 15:48:41 +02001242 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001243
Pierre Ossmanae628902009-05-03 20:45:03 +02001244 host->pwr = pwr;
1245
1246 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001247 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001248 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1249 sdhci_runtime_pm_bus_off(host);
Adrian Hunterceb61432011-12-27 15:48:41 +02001250 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001251 }
1252
1253 /*
1254 * Spec says that we should clear the power reg before setting
1255 * a new value. Some controllers don't seem to like this though.
1256 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001257 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001258 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001259
Andres Salomone08c1692008-07-04 10:00:03 -07001260 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001261 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001262 * and set turn on power at the same time, so set the voltage first.
1263 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001264 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001265 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1266
1267 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001268
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001269 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001270
Adrian Hunterf0710a52013-05-06 12:17:32 +03001271 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1272 sdhci_runtime_pm_bus_on(host);
1273
Harald Welte557b0692009-06-18 16:53:38 +02001274 /*
1275 * Some controllers need an extra 10ms delay of 10ms before they
1276 * can apply clock after applying power
1277 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001278 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001279 mdelay(10);
Adrian Hunterceb61432011-12-27 15:48:41 +02001280
1281 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001282}
1283
Pierre Ossmand129bce2006-03-24 03:18:17 -08001284/*****************************************************************************\
1285 * *
1286 * MMC callbacks *
1287 * *
1288\*****************************************************************************/
1289
1290static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1291{
1292 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001293 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001294 unsigned long flags;
Aaron Lu473b095a2012-07-03 17:27:49 +08001295 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001296
1297 host = mmc_priv(mmc);
1298
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001299 sdhci_runtime_pm_get(host);
1300
Pierre Ossmand129bce2006-03-24 03:18:17 -08001301 spin_lock_irqsave(&host->lock, flags);
1302
1303 WARN_ON(host->mrq != NULL);
1304
Pierre Ossmanf9134312008-12-21 17:01:48 +01001305#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001306 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001307#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001308
1309 /*
1310 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1311 * requests if Auto-CMD12 is enabled.
1312 */
1313 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001314 if (mrq->stop) {
1315 mrq->data->stop = NULL;
1316 mrq->stop = NULL;
1317 }
1318 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001319
1320 host->mrq = mrq;
1321
Shawn Guo505a8682012-12-11 15:23:42 +08001322 /*
1323 * Firstly check card presence from cd-gpio. The return could
1324 * be one of the following possibilities:
1325 * negative: cd-gpio is not available
1326 * zero: cd-gpio is used, and card is removed
1327 * one: cd-gpio is used, and card is present
1328 */
1329 present = mmc_gpio_get_cd(host->mmc);
1330 if (present < 0) {
1331 /* If polling, assume that the card is always present. */
1332 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1333 present = 1;
1334 else
1335 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1336 SDHCI_CARD_PRESENT;
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +08001337 }
1338
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001339 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001340 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001341 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301342 } else {
1343 u32 present_state;
1344
1345 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1346 /*
1347 * Check if the re-tuning timer has already expired and there
1348 * is no on-going data transfer. If so, we need to execute
1349 * tuning procedure before sending command.
1350 */
1351 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1352 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
Chris Ball14efd952012-11-05 14:29:49 -05001353 if (mmc->card) {
1354 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1355 tuning_opcode =
1356 mmc->card->type == MMC_TYPE_MMC ?
1357 MMC_SEND_TUNING_BLOCK_HS200 :
1358 MMC_SEND_TUNING_BLOCK;
Chuansheng Liu63c21182013-11-05 14:52:45 +08001359
1360 /* Here we need to set the host->mrq to NULL,
1361 * in case the pending finish_tasklet
1362 * finishes it incorrectly.
1363 */
1364 host->mrq = NULL;
1365
Chris Ball14efd952012-11-05 14:29:49 -05001366 spin_unlock_irqrestore(&host->lock, flags);
1367 sdhci_execute_tuning(mmc, tuning_opcode);
1368 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301369
Chris Ball14efd952012-11-05 14:29:49 -05001370 /* Restore original mmc_request structure */
1371 host->mrq = mrq;
1372 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301373 }
1374
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001375 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001376 sdhci_send_command(host, mrq->sbc);
1377 else
1378 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301379 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001380
Pierre Ossman5f25a662006-10-04 02:15:39 -07001381 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001382 spin_unlock_irqrestore(&host->lock, flags);
1383}
1384
Russell King2317f562014-04-25 12:57:07 +01001385void sdhci_set_bus_width(struct sdhci_host *host, int width)
1386{
1387 u8 ctrl;
1388
1389 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1390 if (width == MMC_BUS_WIDTH_8) {
1391 ctrl &= ~SDHCI_CTRL_4BITBUS;
1392 if (host->version >= SDHCI_SPEC_300)
1393 ctrl |= SDHCI_CTRL_8BITBUS;
1394 } else {
1395 if (host->version >= SDHCI_SPEC_300)
1396 ctrl &= ~SDHCI_CTRL_8BITBUS;
1397 if (width == MMC_BUS_WIDTH_4)
1398 ctrl |= SDHCI_CTRL_4BITBUS;
1399 else
1400 ctrl &= ~SDHCI_CTRL_4BITBUS;
1401 }
1402 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1403}
1404EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1405
Russell King96d7b782014-04-25 12:59:26 +01001406void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1407{
1408 u16 ctrl_2;
1409
1410 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1411 /* Select Bus Speed Mode for host */
1412 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1413 if ((timing == MMC_TIMING_MMC_HS200) ||
1414 (timing == MMC_TIMING_UHS_SDR104))
1415 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1416 else if (timing == MMC_TIMING_UHS_SDR12)
1417 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1418 else if (timing == MMC_TIMING_UHS_SDR25)
1419 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1420 else if (timing == MMC_TIMING_UHS_SDR50)
1421 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1422 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1423 (timing == MMC_TIMING_MMC_DDR52))
1424 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1425 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1426}
1427EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1428
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001429static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001430{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001431 unsigned long flags;
Adrian Hunterceb61432011-12-27 15:48:41 +02001432 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001433 u8 ctrl;
1434
Pierre Ossmand129bce2006-03-24 03:18:17 -08001435 spin_lock_irqsave(&host->lock, flags);
1436
Adrian Hunterceb61432011-12-27 15:48:41 +02001437 if (host->flags & SDHCI_DEVICE_DEAD) {
1438 spin_unlock_irqrestore(&host->lock, flags);
1439 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1440 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1441 return;
1442 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001443
Pierre Ossmand129bce2006-03-24 03:18:17 -08001444 /*
1445 * Reset the chip on each power off.
1446 * Should clear out any weird states.
1447 */
1448 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001449 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001450 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001451 }
1452
Kevin Liu52983382013-01-31 11:31:37 +08001453 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001454 (ios->power_mode == MMC_POWER_UP) &&
1455 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001456 sdhci_enable_preset_value(host, false);
1457
Russell King373073e2014-04-25 12:58:45 +01001458 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001459 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001460 host->clock = ios->clock;
1461 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001462
1463 if (ios->power_mode == MMC_POWER_OFF)
Adrian Hunterceb61432011-12-27 15:48:41 +02001464 vdd_bit = sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001465 else
Adrian Hunterceb61432011-12-27 15:48:41 +02001466 vdd_bit = sdhci_set_power(host, ios->vdd);
1467
1468 if (host->vmmc && vdd_bit != -1) {
1469 spin_unlock_irqrestore(&host->lock, flags);
1470 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1471 spin_lock_irqsave(&host->lock, flags);
1472 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001473
Philip Rakity643a81f2010-09-23 08:24:32 -07001474 if (host->ops->platform_send_init_74_clocks)
1475 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1476
Russell King2317f562014-04-25 12:57:07 +01001477 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001478
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001479 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001480
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001481 if ((ios->timing == MMC_TIMING_SD_HS ||
1482 ios->timing == MMC_TIMING_MMC_HS)
1483 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001484 ctrl |= SDHCI_CTRL_HISPD;
1485 else
1486 ctrl &= ~SDHCI_CTRL_HISPD;
1487
Arindam Nathd6d50a12011-05-05 12:18:59 +05301488 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301489 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301490
1491 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301492 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001493 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301494 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301495 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1496 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001497 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301498 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301499
1500 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1501 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301502 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301503 /*
1504 * We only need to set Driver Strength if the
1505 * preset value enable is not set.
1506 */
1507 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1508 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1509 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1510 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1511 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1512
1513 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301514 } else {
1515 /*
1516 * According to SDHC Spec v3.00, if the Preset Value
1517 * Enable in the Host Control 2 register is set, we
1518 * need to reset SD Clock Enable before changing High
1519 * Speed Enable to avoid generating clock gliches.
1520 */
Arindam Nath758535c2011-05-05 12:19:00 +05301521
1522 /* Reset SD Clock Enable */
1523 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1524 clk &= ~SDHCI_CLOCK_CARD_EN;
1525 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1526
1527 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1528
1529 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001530 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301531 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301532
Arindam Nath49c468f2011-05-05 12:19:01 +05301533
1534 /* Reset SD Clock Enable */
1535 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1536 clk &= ~SDHCI_CLOCK_CARD_EN;
1537 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1538
Russell King96d7b782014-04-25 12:59:26 +01001539 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001540 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301541
Kevin Liu52983382013-01-31 11:31:37 +08001542 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1543 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1544 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1545 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1546 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1547 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1548 u16 preset;
1549
1550 sdhci_enable_preset_value(host, true);
1551 preset = sdhci_get_preset_value(host);
1552 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1553 >> SDHCI_PRESET_DRV_SHIFT;
1554 }
1555
Arindam Nath49c468f2011-05-05 12:19:01 +05301556 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001557 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301558 } else
1559 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301560
Leandro Dorileob8352262007-07-25 23:47:04 +02001561 /*
1562 * Some (ENE) controllers go apeshit on some ios operation,
1563 * signalling timeout and CRC errors even on CMD0. Resetting
1564 * it on each ios seems to solve the problem.
1565 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001566 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001567 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001568
Pierre Ossman5f25a662006-10-04 02:15:39 -07001569 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001570 spin_unlock_irqrestore(&host->lock, flags);
1571}
1572
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001573static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1574{
1575 struct sdhci_host *host = mmc_priv(mmc);
1576
1577 sdhci_runtime_pm_get(host);
1578 sdhci_do_set_ios(host, ios);
1579 sdhci_runtime_pm_put(host);
1580}
1581
Kevin Liu94144a42013-02-28 17:35:53 +08001582static int sdhci_do_get_cd(struct sdhci_host *host)
1583{
1584 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1585
1586 if (host->flags & SDHCI_DEVICE_DEAD)
1587 return 0;
1588
1589 /* If polling/nonremovable, assume that the card is always present. */
1590 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1591 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1592 return 1;
1593
1594 /* Try slot gpio detect */
1595 if (!IS_ERR_VALUE(gpio_cd))
1596 return !!gpio_cd;
1597
1598 /* Host native card detect */
1599 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1600}
1601
1602static int sdhci_get_cd(struct mmc_host *mmc)
1603{
1604 struct sdhci_host *host = mmc_priv(mmc);
1605 int ret;
1606
1607 sdhci_runtime_pm_get(host);
1608 ret = sdhci_do_get_cd(host);
1609 sdhci_runtime_pm_put(host);
1610 return ret;
1611}
1612
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001613static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001614{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001615 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001616 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001617
Pierre Ossmand129bce2006-03-24 03:18:17 -08001618 spin_lock_irqsave(&host->lock, flags);
1619
Pierre Ossman1e728592008-04-16 19:13:13 +02001620 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001621 is_readonly = 0;
1622 else if (host->ops->get_ro)
1623 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001624 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001625 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1626 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001627
1628 spin_unlock_irqrestore(&host->lock, flags);
1629
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001630 /* This quirk needs to be replaced by a callback-function later */
1631 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1632 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001633}
1634
Takashi Iwai82b0e232011-04-21 20:26:38 +02001635#define SAMPLE_COUNT 5
1636
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001637static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001638{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001639 int i, ro_count;
1640
Takashi Iwai82b0e232011-04-21 20:26:38 +02001641 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001642 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001643
1644 ro_count = 0;
1645 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001646 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001647 if (++ro_count > SAMPLE_COUNT / 2)
1648 return 1;
1649 }
1650 msleep(30);
1651 }
1652 return 0;
1653}
1654
Adrian Hunter20758b62011-08-29 16:42:12 +03001655static void sdhci_hw_reset(struct mmc_host *mmc)
1656{
1657 struct sdhci_host *host = mmc_priv(mmc);
1658
1659 if (host->ops && host->ops->hw_reset)
1660 host->ops->hw_reset(host);
1661}
1662
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001663static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001664{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001665 struct sdhci_host *host = mmc_priv(mmc);
1666 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001667
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001668 sdhci_runtime_pm_get(host);
1669 ret = sdhci_do_get_ro(host);
1670 sdhci_runtime_pm_put(host);
1671 return ret;
1672}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001673
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001674static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1675{
Russell Kingbe138552014-04-25 12:55:56 +01001676 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001677 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001678 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001679 else
Russell Kingb537f942014-04-25 12:56:01 +01001680 host->ier &= ~SDHCI_INT_CARD_INT;
1681
1682 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1683 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001684 mmiowb();
1685 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001686}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001687
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001688static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1689{
1690 struct sdhci_host *host = mmc_priv(mmc);
1691 unsigned long flags;
1692
Russell Kingef104332014-04-25 12:55:41 +01001693 sdhci_runtime_pm_get(host);
1694
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001695 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001696 if (enable)
1697 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1698 else
1699 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1700
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001701 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001702 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001703
1704 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001705}
1706
Philip Rakity6231f3d2012-07-23 15:56:23 -07001707static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001708 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001709{
1710 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001711 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001712
1713 /*
1714 * Signal Voltage Switching is only applicable for Host Controllers
1715 * v3.00 and above.
1716 */
1717 if (host->version < SDHCI_SPEC_300)
1718 return 0;
1719
Philip Rakity6231f3d2012-07-23 15:56:23 -07001720 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001721
Fabio Estevam21f59982013-02-14 10:35:03 -02001722 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001723 case MMC_SIGNAL_VOLTAGE_330:
1724 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1725 ctrl &= ~SDHCI_CTRL_VDD_180;
1726 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1727
1728 if (host->vqmmc) {
1729 ret = regulator_set_voltage(host->vqmmc, 2700000, 3600000);
1730 if (ret) {
1731 pr_warning("%s: Switching to 3.3V signalling voltage "
1732 " failed\n", mmc_hostname(host->mmc));
1733 return -EIO;
1734 }
1735 }
1736 /* Wait for 5ms */
1737 usleep_range(5000, 5500);
1738
1739 /* 3.3V regulator output should be stable within 5 ms */
1740 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1741 if (!(ctrl & SDHCI_CTRL_VDD_180))
1742 return 0;
1743
1744 pr_warning("%s: 3.3V regulator output did not became stable\n",
1745 mmc_hostname(host->mmc));
1746
1747 return -EAGAIN;
1748 case MMC_SIGNAL_VOLTAGE_180:
1749 if (host->vqmmc) {
1750 ret = regulator_set_voltage(host->vqmmc,
1751 1700000, 1950000);
1752 if (ret) {
1753 pr_warning("%s: Switching to 1.8V signalling voltage "
1754 " failed\n", mmc_hostname(host->mmc));
1755 return -EIO;
1756 }
1757 }
1758
1759 /*
1760 * Enable 1.8V Signal Enable in the Host Control2
1761 * register
1762 */
1763 ctrl |= SDHCI_CTRL_VDD_180;
1764 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1765
1766 /* Wait for 5ms */
1767 usleep_range(5000, 5500);
1768
1769 /* 1.8V regulator output should be stable within 5 ms */
1770 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1771 if (ctrl & SDHCI_CTRL_VDD_180)
1772 return 0;
1773
1774 pr_warning("%s: 1.8V regulator output did not became stable\n",
1775 mmc_hostname(host->mmc));
1776
1777 return -EAGAIN;
1778 case MMC_SIGNAL_VOLTAGE_120:
1779 if (host->vqmmc) {
1780 ret = regulator_set_voltage(host->vqmmc, 1100000, 1300000);
1781 if (ret) {
1782 pr_warning("%s: Switching to 1.2V signalling voltage "
1783 " failed\n", mmc_hostname(host->mmc));
1784 return -EIO;
1785 }
1786 }
1787 return 0;
1788 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301789 /* No signal voltage switch required */
1790 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001791 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301792}
1793
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001794static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001795 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001796{
1797 struct sdhci_host *host = mmc_priv(mmc);
1798 int err;
1799
1800 if (host->version < SDHCI_SPEC_300)
1801 return 0;
1802 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001803 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001804 sdhci_runtime_pm_put(host);
1805 return err;
1806}
1807
Kevin Liu20b92a32012-12-17 19:29:26 +08001808static int sdhci_card_busy(struct mmc_host *mmc)
1809{
1810 struct sdhci_host *host = mmc_priv(mmc);
1811 u32 present_state;
1812
1813 sdhci_runtime_pm_get(host);
1814 /* Check whether DAT[3:0] is 0000 */
1815 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1816 sdhci_runtime_pm_put(host);
1817
1818 return !(present_state & SDHCI_DATA_LVL_MASK);
1819}
1820
Girish K S069c9f12012-01-06 09:56:39 +05301821static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301822{
Russell King4b6f37d2014-04-25 12:59:36 +01001823 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301824 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301825 int tuning_loop_counter = MAX_TUNING_LOOP;
1826 unsigned long timeout;
1827 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001828 unsigned long flags;
Arindam Nathb513ea22011-05-05 12:19:04 +05301829
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001830 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001831 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301832
Arindam Nathb513ea22011-05-05 12:19:04 +05301833 /*
Girish K S069c9f12012-01-06 09:56:39 +05301834 * The Host Controller needs tuning only in case of SDR104 mode
1835 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301836 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301837 * If the Host Controller supports the HS200 mode then the
1838 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301839 */
Russell King4b6f37d2014-04-25 12:59:36 +01001840 switch (host->timing) {
1841 case MMC_TIMING_MMC_HS200:
1842 case MMC_TIMING_UHS_SDR104:
1843 break;
Girish K S069c9f12012-01-06 09:56:39 +05301844
Russell King4b6f37d2014-04-25 12:59:36 +01001845 case MMC_TIMING_UHS_SDR50:
1846 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1847 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1848 break;
1849 /* FALLTHROUGH */
1850
1851 default:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001852 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001853 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301854 return 0;
1855 }
1856
Dong Aisheng45251812013-09-13 19:11:30 +08001857 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001858 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001859 err = host->ops->platform_execute_tuning(host, opcode);
1860 sdhci_runtime_pm_put(host);
1861 return err;
1862 }
1863
Russell King4b6f37d2014-04-25 12:59:36 +01001864 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1865 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Arindam Nathb513ea22011-05-05 12:19:04 +05301866 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1867
1868 /*
1869 * As per the Host Controller spec v3.00, tuning command
1870 * generates Buffer Read Ready interrupt, so enable that.
1871 *
1872 * Note: The spec clearly says that when tuning sequence
1873 * is being performed, the controller does not generate
1874 * interrupts other than Buffer Read Ready interrupt. But
1875 * to make sure we don't hit a controller bug, we _only_
1876 * enable Buffer Read Ready interrupt here.
1877 */
Russell Kingb537f942014-04-25 12:56:01 +01001878 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1879 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301880
1881 /*
1882 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1883 * of loops reaches 40 times or a timeout of 150ms occurs.
1884 */
1885 timeout = 150;
1886 do {
1887 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001888 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301889
1890 if (!tuning_loop_counter && !timeout)
1891 break;
1892
Girish K S069c9f12012-01-06 09:56:39 +05301893 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301894 cmd.arg = 0;
1895 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1896 cmd.retries = 0;
1897 cmd.data = NULL;
1898 cmd.error = 0;
1899
1900 mrq.cmd = &cmd;
1901 host->mrq = &mrq;
1902
1903 /*
1904 * In response to CMD19, the card sends 64 bytes of tuning
1905 * block to the Host Controller. So we set the block size
1906 * to 64 here.
1907 */
Girish K S069c9f12012-01-06 09:56:39 +05301908 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1909 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1910 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1911 SDHCI_BLOCK_SIZE);
1912 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1913 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1914 SDHCI_BLOCK_SIZE);
1915 } else {
1916 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1917 SDHCI_BLOCK_SIZE);
1918 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301919
1920 /*
1921 * The tuning block is sent by the card to the host controller.
1922 * So we set the TRNS_READ bit in the Transfer Mode register.
1923 * This also takes care of setting DMA Enable and Multi Block
1924 * Select in the same register to 0.
1925 */
1926 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1927
1928 sdhci_send_command(host, &cmd);
1929
1930 host->cmd = NULL;
1931 host->mrq = NULL;
1932
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001933 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301934 /* Wait for Buffer Read Ready interrupt */
1935 wait_event_interruptible_timeout(host->buf_ready_int,
1936 (host->tuning_done == 1),
1937 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001938 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301939
1940 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301941 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301942 "Buffer Read Ready interrupt during tuning "
1943 "procedure, falling back to fixed sampling "
1944 "clock\n");
1945 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1946 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1947 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1948 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1949
1950 err = -EIO;
1951 goto out;
1952 }
1953
1954 host->tuning_done = 0;
1955
1956 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1957 tuning_loop_counter--;
1958 timeout--;
Nick Sanders197160d2014-05-06 18:52:38 -07001959
1960 /* eMMC spec does not require a delay between tuning cycles */
1961 if (opcode == MMC_SEND_TUNING_BLOCK)
1962 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05301963 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1964
1965 /*
1966 * The Host Driver has exhausted the maximum number of loops allowed,
1967 * so use fixed sampling frequency.
1968 */
1969 if (!tuning_loop_counter || !timeout) {
1970 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1971 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Dong Aisheng114f2bf2013-10-18 19:48:45 +08001972 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05301973 } else {
1974 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301975 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301976 " failed, falling back to fixed sampling"
1977 " clock\n");
1978 err = -EIO;
1979 }
1980 }
1981
1982out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301983 /*
1984 * If this is the very first time we are here, we start the retuning
1985 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1986 * flag won't be set, we check this condition before actually starting
1987 * the timer.
1988 */
1989 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1990 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
Aaron Lu973905f2012-07-04 13:29:09 +08001991 host->flags |= SDHCI_USING_RETUNING_TIMER;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301992 mod_timer(&host->tuning_timer, jiffies +
1993 host->tuning_count * HZ);
1994 /* Tuning mode 1 limits the maximum data length to 4MB */
1995 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
Arend van Spriel2bc02482014-01-04 13:51:26 +01001996 } else if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301997 host->flags &= ~SDHCI_NEEDS_RETUNING;
1998 /* Reload the new initial value for timer */
Arend van Spriel2bc02482014-01-04 13:51:26 +01001999 mod_timer(&host->tuning_timer, jiffies +
2000 host->tuning_count * HZ);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302001 }
2002
2003 /*
2004 * In case tuning fails, host controllers which support re-tuning can
2005 * try tuning again at a later time, when the re-tuning timer expires.
2006 * So for these controllers, we return 0. Since there might be other
2007 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08002008 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2009 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302010 */
Aaron Lu973905f2012-07-04 13:29:09 +08002011 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302012 err = 0;
2013
Russell Kingb537f942014-04-25 12:56:01 +01002014 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2015 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002016 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002017 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302018
2019 return err;
2020}
2021
Kevin Liu52983382013-01-31 11:31:37 +08002022
2023static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302024{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302025 u16 ctrl;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302026
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302027 /* Host Controller v3.00 defines preset value registers */
2028 if (host->version < SDHCI_SPEC_300)
2029 return;
2030
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302031 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2032
2033 /*
2034 * We only enable or disable Preset Value if they are not already
2035 * enabled or disabled respectively. Otherwise, we bail out.
2036 */
2037 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2038 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2039 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002040 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302041 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2042 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2043 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002044 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302045 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002046}
2047
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002048static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002049{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002050 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002051 unsigned long flags;
2052
Christian Daudt722e1282013-06-20 14:26:36 -07002053 /* First check if client has provided their own card event */
2054 if (host->ops->card_event)
2055 host->ops->card_event(host);
2056
Pierre Ossmand129bce2006-03-24 03:18:17 -08002057 spin_lock_irqsave(&host->lock, flags);
2058
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002059 /* Check host->mrq first in case we are runtime suspended */
Shawn Guo9668d762013-06-09 19:49:24 +08002060 if (host->mrq && !sdhci_do_get_cd(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302061 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002062 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302063 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002064 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002065
Russell King03231f92014-04-25 12:57:12 +01002066 sdhci_do_reset(host, SDHCI_RESET_CMD);
2067 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002068
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002069 host->mrq->cmd->error = -ENOMEDIUM;
2070 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002071 }
2072
2073 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002074}
2075
2076static const struct mmc_host_ops sdhci_ops = {
2077 .request = sdhci_request,
2078 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002079 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002080 .get_ro = sdhci_get_ro,
2081 .hw_reset = sdhci_hw_reset,
2082 .enable_sdio_irq = sdhci_enable_sdio_irq,
2083 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2084 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002085 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002086 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002087};
2088
2089/*****************************************************************************\
2090 * *
2091 * Tasklets *
2092 * *
2093\*****************************************************************************/
2094
Pierre Ossmand129bce2006-03-24 03:18:17 -08002095static void sdhci_tasklet_finish(unsigned long param)
2096{
2097 struct sdhci_host *host;
2098 unsigned long flags;
2099 struct mmc_request *mrq;
2100
2101 host = (struct sdhci_host*)param;
2102
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002103 spin_lock_irqsave(&host->lock, flags);
2104
Chris Ball0c9c99a2011-04-27 17:35:31 -04002105 /*
2106 * If this tasklet gets rescheduled while running, it will
2107 * be run again afterwards but without any active request.
2108 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002109 if (!host->mrq) {
2110 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002111 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002112 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002113
2114 del_timer(&host->timer);
2115
2116 mrq = host->mrq;
2117
Pierre Ossmand129bce2006-03-24 03:18:17 -08002118 /*
2119 * The controller needs a reset of internal state machines
2120 * upon error conditions.
2121 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002122 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002123 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002124 (mrq->data && (mrq->data->error ||
2125 (mrq->data->stop && mrq->data->stop->error))) ||
2126 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002127
2128 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002129 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002130 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002131 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002132
2133 /* Spec says we should do both at the same time, but Ricoh
2134 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002135 sdhci_do_reset(host, SDHCI_RESET_CMD);
2136 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002137 }
2138
2139 host->mrq = NULL;
2140 host->cmd = NULL;
2141 host->data = NULL;
2142
Pierre Ossmanf9134312008-12-21 17:01:48 +01002143#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002144 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002145#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002146
Pierre Ossman5f25a662006-10-04 02:15:39 -07002147 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002148 spin_unlock_irqrestore(&host->lock, flags);
2149
2150 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002151 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002152}
2153
2154static void sdhci_timeout_timer(unsigned long data)
2155{
2156 struct sdhci_host *host;
2157 unsigned long flags;
2158
2159 host = (struct sdhci_host*)data;
2160
2161 spin_lock_irqsave(&host->lock, flags);
2162
2163 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302164 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002165 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002166 sdhci_dumpregs(host);
2167
2168 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002169 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002170 sdhci_finish_data(host);
2171 } else {
2172 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002173 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002174 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002175 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002176
2177 tasklet_schedule(&host->finish_tasklet);
2178 }
2179 }
2180
Pierre Ossman5f25a662006-10-04 02:15:39 -07002181 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002182 spin_unlock_irqrestore(&host->lock, flags);
2183}
2184
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302185static void sdhci_tuning_timer(unsigned long data)
2186{
2187 struct sdhci_host *host;
2188 unsigned long flags;
2189
2190 host = (struct sdhci_host *)data;
2191
2192 spin_lock_irqsave(&host->lock, flags);
2193
2194 host->flags |= SDHCI_NEEDS_RETUNING;
2195
2196 spin_unlock_irqrestore(&host->lock, flags);
2197}
2198
Pierre Ossmand129bce2006-03-24 03:18:17 -08002199/*****************************************************************************\
2200 * *
2201 * Interrupt handling *
2202 * *
2203\*****************************************************************************/
2204
2205static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2206{
2207 BUG_ON(intmask == 0);
2208
2209 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302210 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002211 "though no command operation was in progress.\n",
2212 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002213 sdhci_dumpregs(host);
2214 return;
2215 }
2216
Pierre Ossman43b58b32007-07-25 23:15:27 +02002217 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002218 host->cmd->error = -ETIMEDOUT;
2219 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2220 SDHCI_INT_INDEX))
2221 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002222
Pierre Ossmane8095172008-07-25 01:09:08 +02002223 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002224 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002225 return;
2226 }
2227
2228 /*
2229 * The host can send and interrupt when the busy state has
2230 * ended, allowing us to wait without wasting CPU cycles.
2231 * Unfortunately this is overloaded on the "data complete"
2232 * interrupt, so we need to take some care when handling
2233 * it.
2234 *
2235 * Note: The 1.0 specification is a bit ambiguous about this
2236 * feature so there might be some problems with older
2237 * controllers.
2238 */
2239 if (host->cmd->flags & MMC_RSP_BUSY) {
2240 if (host->cmd->data)
2241 DBG("Cannot wait for busy signal when also "
2242 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002243 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002244 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002245
2246 /* The controller does not support the end-of-busy IRQ,
2247 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002248 }
2249
2250 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002251 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002252}
2253
George G. Davis0957c332010-02-18 12:32:12 -05002254#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002255static void sdhci_show_adma_error(struct sdhci_host *host)
2256{
2257 const char *name = mmc_hostname(host->mmc);
2258 u8 *desc = host->adma_desc;
2259 __le32 *dma;
2260 __le16 *len;
2261 u8 attr;
2262
2263 sdhci_dumpregs(host);
2264
2265 while (true) {
2266 dma = (__le32 *)(desc + 4);
2267 len = (__le16 *)(desc + 2);
2268 attr = *desc;
2269
2270 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2271 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2272
2273 desc += 8;
2274
2275 if (attr & 2)
2276 break;
2277 }
2278}
2279#else
2280static void sdhci_show_adma_error(struct sdhci_host *host) { }
2281#endif
2282
Pierre Ossmand129bce2006-03-24 03:18:17 -08002283static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2284{
Girish K S069c9f12012-01-06 09:56:39 +05302285 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002286 BUG_ON(intmask == 0);
2287
Arindam Nathb513ea22011-05-05 12:19:04 +05302288 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2289 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302290 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2291 if (command == MMC_SEND_TUNING_BLOCK ||
2292 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302293 host->tuning_done = 1;
2294 wake_up(&host->buf_ready_int);
2295 return;
2296 }
2297 }
2298
Pierre Ossmand129bce2006-03-24 03:18:17 -08002299 if (!host->data) {
2300 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002301 * The "data complete" interrupt is also used to
2302 * indicate that a busy state has ended. See comment
2303 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002304 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002305 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2306 if (intmask & SDHCI_INT_DATA_END) {
2307 sdhci_finish_command(host);
2308 return;
2309 }
2310 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002311
Girish K Sa3c76eb2011-10-11 11:44:09 +05302312 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002313 "though no data operation was in progress.\n",
2314 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002315 sdhci_dumpregs(host);
2316
2317 return;
2318 }
2319
2320 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002321 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002322 else if (intmask & SDHCI_INT_DATA_END_BIT)
2323 host->data->error = -EILSEQ;
2324 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2325 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2326 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002327 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002328 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302329 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002330 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002331 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002332 if (host->ops->adma_workaround)
2333 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002334 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002335
Pierre Ossman17b04292007-07-22 22:18:46 +02002336 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002337 sdhci_finish_data(host);
2338 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002339 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002340 sdhci_transfer_pio(host);
2341
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002342 /*
2343 * We currently don't do anything fancy with DMA
2344 * boundaries, but as we can't disable the feature
2345 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002346 *
2347 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2348 * should return a valid address to continue from, but as
2349 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002350 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002351 if (intmask & SDHCI_INT_DMA_END) {
2352 u32 dmastart, dmanow;
2353 dmastart = sg_dma_address(host->data->sg);
2354 dmanow = dmastart + host->data->bytes_xfered;
2355 /*
2356 * Force update to the next DMA block boundary.
2357 */
2358 dmanow = (dmanow &
2359 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2360 SDHCI_DEFAULT_BOUNDARY_SIZE;
2361 host->data->bytes_xfered = dmanow - dmastart;
2362 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2363 " next 0x%08x\n",
2364 mmc_hostname(host->mmc), dmastart,
2365 host->data->bytes_xfered, dmanow);
2366 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2367 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002368
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002369 if (intmask & SDHCI_INT_DATA_END) {
2370 if (host->cmd) {
2371 /*
2372 * Data managed to finish before the
2373 * command completed. Make sure we do
2374 * things in the proper order.
2375 */
2376 host->data_early = 1;
2377 } else {
2378 sdhci_finish_data(host);
2379 }
2380 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002381 }
2382}
2383
David Howells7d12e782006-10-05 14:55:46 +01002384static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002385{
Russell King781e9892014-04-25 12:55:46 +01002386 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002387 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002388 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002389 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002390
2391 spin_lock(&host->lock);
2392
Russell Kingbe138552014-04-25 12:55:56 +01002393 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002394 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002395 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002396 }
2397
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002398 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002399 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002400 result = IRQ_NONE;
2401 goto out;
2402 }
2403
Russell King41005002014-04-25 12:55:36 +01002404 do {
2405 /* Clear selected interrupts. */
2406 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2407 SDHCI_INT_BUS_POWER);
2408 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002409
Russell King41005002014-04-25 12:55:36 +01002410 DBG("*** %s got interrupt: 0x%08x\n",
2411 mmc_hostname(host->mmc), intmask);
2412
2413 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2414 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2415 SDHCI_CARD_PRESENT;
2416
2417 /*
2418 * There is a observation on i.mx esdhc. INSERT
2419 * bit will be immediately set again when it gets
2420 * cleared, if a card is inserted. We have to mask
2421 * the irq to prevent interrupt storm which will
2422 * freeze the system. And the REMOVE gets the
2423 * same situation.
2424 *
2425 * More testing are needed here to ensure it works
2426 * for other platforms though.
2427 */
Russell Kingb537f942014-04-25 12:56:01 +01002428 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2429 SDHCI_INT_CARD_REMOVE);
2430 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2431 SDHCI_INT_CARD_INSERT;
2432 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2433 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002434
2435 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2436 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002437
2438 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2439 SDHCI_INT_CARD_REMOVE);
2440 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002441 }
2442
2443 if (intmask & SDHCI_INT_CMD_MASK)
2444 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
2445
2446 if (intmask & SDHCI_INT_DATA_MASK)
2447 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2448
2449 if (intmask & SDHCI_INT_BUS_POWER)
2450 pr_err("%s: Card is consuming too much power!\n",
2451 mmc_hostname(host->mmc));
2452
Russell King781e9892014-04-25 12:55:46 +01002453 if (intmask & SDHCI_INT_CARD_INT) {
2454 sdhci_enable_sdio_irq_nolock(host, false);
2455 host->thread_isr |= SDHCI_INT_CARD_INT;
2456 result = IRQ_WAKE_THREAD;
2457 }
Russell King41005002014-04-25 12:55:36 +01002458
2459 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2460 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2461 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2462 SDHCI_INT_CARD_INT);
2463
2464 if (intmask) {
2465 unexpected |= intmask;
2466 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2467 }
2468
Russell King781e9892014-04-25 12:55:46 +01002469 if (result == IRQ_NONE)
2470 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002471
2472 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002473 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002474out:
2475 spin_unlock(&host->lock);
2476
Alexander Stein6379b232012-03-14 09:52:10 +01002477 if (unexpected) {
2478 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2479 mmc_hostname(host->mmc), unexpected);
2480 sdhci_dumpregs(host);
2481 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002482
Pierre Ossmand129bce2006-03-24 03:18:17 -08002483 return result;
2484}
2485
Russell King781e9892014-04-25 12:55:46 +01002486static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2487{
2488 struct sdhci_host *host = dev_id;
2489 unsigned long flags;
2490 u32 isr;
2491
2492 spin_lock_irqsave(&host->lock, flags);
2493 isr = host->thread_isr;
2494 host->thread_isr = 0;
2495 spin_unlock_irqrestore(&host->lock, flags);
2496
Russell King3560db82014-04-25 12:55:51 +01002497 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2498 sdhci_card_event(host->mmc);
2499 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2500 }
2501
Russell King781e9892014-04-25 12:55:46 +01002502 if (isr & SDHCI_INT_CARD_INT) {
2503 sdio_run_irqs(host->mmc);
2504
2505 spin_lock_irqsave(&host->lock, flags);
2506 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2507 sdhci_enable_sdio_irq_nolock(host, true);
2508 spin_unlock_irqrestore(&host->lock, flags);
2509 }
2510
2511 return isr ? IRQ_HANDLED : IRQ_NONE;
2512}
2513
Pierre Ossmand129bce2006-03-24 03:18:17 -08002514/*****************************************************************************\
2515 * *
2516 * Suspend/resume *
2517 * *
2518\*****************************************************************************/
2519
2520#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002521void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2522{
2523 u8 val;
2524 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2525 | SDHCI_WAKE_ON_INT;
2526
2527 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2528 val |= mask ;
2529 /* Avoid fake wake up */
2530 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2531 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2532 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2533}
2534EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2535
2536void sdhci_disable_irq_wakeups(struct sdhci_host *host)
2537{
2538 u8 val;
2539 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2540 | SDHCI_WAKE_ON_INT;
2541
2542 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2543 val &= ~mask;
2544 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2545}
2546EXPORT_SYMBOL_GPL(sdhci_disable_irq_wakeups);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002547
Manuel Lauss29495aa2011-11-03 11:09:45 +01002548int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002549{
Chris Balla1b13b42012-02-06 00:43:59 -05002550 if (host->ops->platform_suspend)
2551 host->ops->platform_suspend(host);
2552
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002553 sdhci_disable_card_detection(host);
2554
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302555 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002556 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002557 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302558 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302559 }
2560
Kevin Liuad080d72013-01-05 17:21:33 +08002561 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002562 host->ier = 0;
2563 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2564 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002565 free_irq(host->irq, host);
2566 } else {
2567 sdhci_enable_irq_wakeups(host);
2568 enable_irq_wake(host->irq);
2569 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002570 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002571}
2572
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002573EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002574
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002575int sdhci_resume_host(struct sdhci_host *host)
2576{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002577 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002578
Richard Röjforsa13abc72009-09-22 16:45:30 -07002579 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002580 if (host->ops->enable_dma)
2581 host->ops->enable_dma(host);
2582 }
2583
Kevin Liuad080d72013-01-05 17:21:33 +08002584 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell King781e9892014-04-25 12:55:46 +01002585 ret = request_threaded_irq(host->irq, sdhci_irq,
2586 sdhci_thread_irq, IRQF_SHARED,
2587 mmc_hostname(host->mmc), host);
Kevin Liuad080d72013-01-05 17:21:33 +08002588 if (ret)
2589 return ret;
2590 } else {
2591 sdhci_disable_irq_wakeups(host);
2592 disable_irq_wake(host->irq);
2593 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002594
Adrian Hunter6308d292012-02-07 14:48:54 +02002595 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2596 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2597 /* Card keeps power but host controller does not */
2598 sdhci_init(host, 0);
2599 host->pwr = 0;
2600 host->clock = 0;
2601 sdhci_do_set_ios(host, &host->mmc->ios);
2602 } else {
2603 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2604 mmiowb();
2605 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002606
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002607 sdhci_enable_card_detection(host);
2608
Chris Balla1b13b42012-02-06 00:43:59 -05002609 if (host->ops->platform_resume)
2610 host->ops->platform_resume(host);
2611
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302612 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002613 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302614 host->flags |= SDHCI_NEEDS_RETUNING;
2615
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002616 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002617}
2618
2619EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002620#endif /* CONFIG_PM */
2621
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002622#ifdef CONFIG_PM_RUNTIME
2623
2624static int sdhci_runtime_pm_get(struct sdhci_host *host)
2625{
2626 return pm_runtime_get_sync(host->mmc->parent);
2627}
2628
2629static int sdhci_runtime_pm_put(struct sdhci_host *host)
2630{
2631 pm_runtime_mark_last_busy(host->mmc->parent);
2632 return pm_runtime_put_autosuspend(host->mmc->parent);
2633}
2634
Adrian Hunterf0710a52013-05-06 12:17:32 +03002635static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2636{
2637 if (host->runtime_suspended || host->bus_on)
2638 return;
2639 host->bus_on = true;
2640 pm_runtime_get_noresume(host->mmc->parent);
2641}
2642
2643static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2644{
2645 if (host->runtime_suspended || !host->bus_on)
2646 return;
2647 host->bus_on = false;
2648 pm_runtime_put_noidle(host->mmc->parent);
2649}
2650
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002651int sdhci_runtime_suspend_host(struct sdhci_host *host)
2652{
2653 unsigned long flags;
2654 int ret = 0;
2655
2656 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002657 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002658 del_timer_sync(&host->tuning_timer);
2659 host->flags &= ~SDHCI_NEEDS_RETUNING;
2660 }
2661
2662 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002663 host->ier &= SDHCI_INT_CARD_INT;
2664 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2665 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002666 spin_unlock_irqrestore(&host->lock, flags);
2667
Russell King781e9892014-04-25 12:55:46 +01002668 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002669
2670 spin_lock_irqsave(&host->lock, flags);
2671 host->runtime_suspended = true;
2672 spin_unlock_irqrestore(&host->lock, flags);
2673
2674 return ret;
2675}
2676EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2677
2678int sdhci_runtime_resume_host(struct sdhci_host *host)
2679{
2680 unsigned long flags;
2681 int ret = 0, host_flags = host->flags;
2682
2683 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2684 if (host->ops->enable_dma)
2685 host->ops->enable_dma(host);
2686 }
2687
2688 sdhci_init(host, 0);
2689
2690 /* Force clock and power re-program */
2691 host->pwr = 0;
2692 host->clock = 0;
2693 sdhci_do_set_ios(host, &host->mmc->ios);
2694
2695 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002696 if ((host_flags & SDHCI_PV_ENABLED) &&
2697 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2698 spin_lock_irqsave(&host->lock, flags);
2699 sdhci_enable_preset_value(host, true);
2700 spin_unlock_irqrestore(&host->lock, flags);
2701 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002702
2703 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002704 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002705 host->flags |= SDHCI_NEEDS_RETUNING;
2706
2707 spin_lock_irqsave(&host->lock, flags);
2708
2709 host->runtime_suspended = false;
2710
2711 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002712 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002713 sdhci_enable_sdio_irq_nolock(host, true);
2714
2715 /* Enable Card Detection */
2716 sdhci_enable_card_detection(host);
2717
2718 spin_unlock_irqrestore(&host->lock, flags);
2719
2720 return ret;
2721}
2722EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2723
2724#endif
2725
Pierre Ossmand129bce2006-03-24 03:18:17 -08002726/*****************************************************************************\
2727 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002728 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002729 * *
2730\*****************************************************************************/
2731
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002732struct sdhci_host *sdhci_alloc_host(struct device *dev,
2733 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002734{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002735 struct mmc_host *mmc;
2736 struct sdhci_host *host;
2737
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002738 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002739
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002740 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002741 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002742 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002743
2744 host = mmc_priv(mmc);
2745 host->mmc = mmc;
2746
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002747 return host;
2748}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002749
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002750EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002751
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002752int sdhci_add_host(struct sdhci_host *host)
2753{
2754 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002755 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302756 u32 max_current_caps;
2757 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002758 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002759
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002760 WARN_ON(host == NULL);
2761 if (host == NULL)
2762 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002763
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002764 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002765
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002766 if (debug_quirks)
2767 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002768 if (debug_quirks2)
2769 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002770
Russell King03231f92014-04-25 12:57:12 +01002771 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002772
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002773 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002774 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2775 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002776 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302777 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002778 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002779 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002780 }
2781
Arindam Nathf2119df2011-05-05 12:18:57 +05302782 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002783 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002784
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002785 if (host->version >= SDHCI_SPEC_300)
2786 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2787 host->caps1 :
2788 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302789
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002790 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002791 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302792 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002793 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002794 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002795 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002796
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002797 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002798 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002799 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002800 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002801 }
2802
Arindam Nathf2119df2011-05-05 12:18:57 +05302803 if ((host->version >= SDHCI_SPEC_200) &&
2804 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002805 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002806
2807 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2808 (host->flags & SDHCI_USE_ADMA)) {
2809 DBG("Disabling ADMA as it is marked broken\n");
2810 host->flags &= ~SDHCI_USE_ADMA;
2811 }
2812
Richard Röjforsa13abc72009-09-22 16:45:30 -07002813 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002814 if (host->ops->enable_dma) {
2815 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302816 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002817 "available. Falling back to PIO.\n",
2818 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002819 host->flags &=
2820 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002821 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002822 }
2823 }
2824
Pierre Ossman2134a922008-06-28 18:28:51 +02002825 if (host->flags & SDHCI_USE_ADMA) {
2826 /*
2827 * We need to allocate descriptors for all sg entries
2828 * (128) and potentially one alignment transfer for
2829 * each of those entries.
2830 */
Russell Kingd1e49f72014-04-25 12:58:34 +01002831 host->adma_desc = dma_alloc_coherent(mmc_dev(host->mmc),
2832 ADMA_SIZE, &host->adma_addr,
2833 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02002834 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2835 if (!host->adma_desc || !host->align_buffer) {
Russell Kingd1e49f72014-04-25 12:58:34 +01002836 dma_free_coherent(mmc_dev(host->mmc), ADMA_SIZE,
2837 host->adma_desc, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02002838 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302839 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002840 "buffers. Falling back to standard DMA.\n",
2841 mmc_hostname(mmc));
2842 host->flags &= ~SDHCI_USE_ADMA;
Russell Kingd1e49f72014-04-25 12:58:34 +01002843 host->adma_desc = NULL;
2844 host->align_buffer = NULL;
2845 } else if (host->adma_addr & 3) {
2846 pr_warning("%s: unable to allocate aligned ADMA descriptor\n",
2847 mmc_hostname(mmc));
2848 host->flags &= ~SDHCI_USE_ADMA;
2849 dma_free_coherent(mmc_dev(host->mmc), ADMA_SIZE,
2850 host->adma_desc, host->adma_addr);
2851 kfree(host->align_buffer);
2852 host->adma_desc = NULL;
2853 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02002854 }
2855 }
2856
Pierre Ossman76591502008-07-21 00:32:11 +02002857 /*
2858 * If we use DMA, then it's up to the caller to set the DMA
2859 * mask, but PIO does not need the hw shim so we set a new
2860 * mask here in that case.
2861 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002862 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002863 host->dma_mask = DMA_BIT_MASK(64);
2864 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2865 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002866
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002867 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302868 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002869 >> SDHCI_CLOCK_BASE_SHIFT;
2870 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302871 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002872 >> SDHCI_CLOCK_BASE_SHIFT;
2873
Pierre Ossmand129bce2006-03-24 03:18:17 -08002874 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002875 if (host->max_clk == 0 || host->quirks &
2876 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002877 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302878 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002879 "frequency.\n", mmc_hostname(mmc));
2880 return -ENODEV;
2881 }
2882 host->max_clk = host->ops->get_max_clock(host);
2883 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002884
2885 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302886 * In case of Host Controller v3.00, find out whether clock
2887 * multiplier is supported.
2888 */
2889 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2890 SDHCI_CLOCK_MUL_SHIFT;
2891
2892 /*
2893 * In case the value in Clock Multiplier is 0, then programmable
2894 * clock mode is not supported, otherwise the actual clock
2895 * multiplier is one more than the value of Clock Multiplier
2896 * in the Capabilities Register.
2897 */
2898 if (host->clk_mul)
2899 host->clk_mul += 1;
2900
2901 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002902 * Set host parameters.
2903 */
2904 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302905 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002906 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002907 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302908 else if (host->version >= SDHCI_SPEC_300) {
2909 if (host->clk_mul) {
2910 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2911 mmc->f_max = host->max_clk * host->clk_mul;
2912 } else
2913 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2914 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002915 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002916
Andy Shevchenko272308c2011-08-03 18:36:00 +03002917 host->timeout_clk =
2918 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2919 if (host->timeout_clk == 0) {
2920 if (host->ops->get_timeout_clock) {
2921 host->timeout_clk = host->ops->get_timeout_clock(host);
2922 } else if (!(host->quirks &
2923 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302924 pr_err("%s: Hardware doesn't specify timeout clock "
Andy Shevchenko272308c2011-08-03 18:36:00 +03002925 "frequency.\n", mmc_hostname(mmc));
2926 return -ENODEV;
2927 }
2928 }
2929 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2930 host->timeout_clk *= 1000;
2931
2932 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002933 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002934
Ulf Hansson68eb80e2013-12-18 09:57:38 +01002935 mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002936
Andrei Warkentine89d4562011-05-23 15:06:37 -05002937 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01002938 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05002939
2940 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2941 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002942
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002943 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002944 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002945 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002946 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002947 host->flags |= SDHCI_AUTO_CMD23;
2948 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2949 } else {
2950 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2951 }
2952
Philip Rakity15ec4462010-11-19 16:48:39 -05002953 /*
2954 * A controller may support 8-bit width, but the board itself
2955 * might not have the pins brought out. Boards that support
2956 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2957 * their platform code before calling sdhci_add_host(), and we
2958 * won't assume 8-bit width for hosts without that CAP.
2959 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002960 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002961 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002962
Jerry Huang63ef5d82012-10-25 13:47:19 +08002963 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
2964 mmc->caps &= ~MMC_CAP_CMD23;
2965
Arindam Nathf2119df2011-05-05 12:18:57 +05302966 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002967 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002968
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002969 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Daniel Drakeeb6d5ae2012-07-05 22:06:13 +01002970 !(host->mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002971 mmc->caps |= MMC_CAP_NEEDS_POLL;
2972
Philip Rakity6231f3d2012-07-23 15:56:23 -07002973 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Mark Brown462849a2013-07-29 21:52:55 +01002974 host->vqmmc = regulator_get_optional(mmc_dev(mmc), "vqmmc");
Kevin Liu657d5982012-10-17 19:04:44 +08002975 if (IS_ERR_OR_NULL(host->vqmmc)) {
2976 if (PTR_ERR(host->vqmmc) < 0) {
2977 pr_info("%s: no vqmmc regulator found\n",
2978 mmc_hostname(mmc));
2979 host->vqmmc = NULL;
2980 }
Kevin Liu8363c372012-11-17 17:55:51 -05002981 } else {
Chris Balla3361ab2013-03-11 17:51:53 -04002982 ret = regulator_enable(host->vqmmc);
Kevin Liucec2e212012-11-20 08:24:32 -05002983 if (!regulator_is_supported_voltage(host->vqmmc, 1700000,
2984 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05002985 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
2986 SDHCI_SUPPORT_SDR50 |
2987 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04002988 if (ret) {
2989 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
2990 mmc_hostname(mmc), ret);
2991 host->vqmmc = NULL;
2992 }
Kevin Liu8363c372012-11-17 17:55:51 -05002993 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07002994
Daniel Drake6a661802012-11-25 13:01:19 -05002995 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
2996 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2997 SDHCI_SUPPORT_DDR50);
2998
Al Cooper4188bba2012-03-16 15:54:17 -04002999 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3000 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3001 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303002 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3003
3004 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003005 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303006 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003007 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3008 * field can be promoted to support HS200.
3009 */
David Cohen13868bf2013-10-29 10:58:26 -07003010 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
3011 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003012 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303013 mmc->caps |= MMC_CAP_UHS_SDR50;
3014
Micky Ching9107ebb2014-02-21 18:40:35 +08003015 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3016 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303017 mmc->caps |= MMC_CAP_UHS_DDR50;
3018
Girish K S069c9f12012-01-06 09:56:39 +05303019 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303020 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3021 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3022
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003023 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303024 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003025 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303026
Arindam Nathd6d50a12011-05-05 12:18:59 +05303027 /* Driver Type(s) (A, C, D) supported by the host */
3028 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3029 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3030 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3031 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3032 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3033 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3034
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303035 /* Initial value for re-tuning timer count */
3036 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3037 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3038
3039 /*
3040 * In case Re-tuning Timer is not disabled, the actual value of
3041 * re-tuning timer will be 2 ^ (n - 1).
3042 */
3043 if (host->tuning_count)
3044 host->tuning_count = 1 << (host->tuning_count - 1);
3045
3046 /* Re-tuning mode supported by the Host Controller */
3047 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3048 SDHCI_RETUNING_MODE_SHIFT;
3049
Takashi Iwai8f230f42010-12-08 10:04:30 +01003050 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003051
Mark Brown462849a2013-07-29 21:52:55 +01003052 host->vmmc = regulator_get_optional(mmc_dev(mmc), "vmmc");
Kevin Liu657d5982012-10-17 19:04:44 +08003053 if (IS_ERR_OR_NULL(host->vmmc)) {
3054 if (PTR_ERR(host->vmmc) < 0) {
3055 pr_info("%s: no vmmc regulator found\n",
3056 mmc_hostname(mmc));
3057 host->vmmc = NULL;
3058 }
Kevin Liu8363c372012-11-17 17:55:51 -05003059 }
Philip Rakitybad37e12012-05-27 18:36:44 -07003060
Philip Rakity68737042012-06-08 12:26:13 -07003061#ifdef CONFIG_REGULATOR
Marek Szyprowskia4f8f252013-02-12 09:01:36 +01003062 /*
3063 * Voltage range check makes sense only if regulator reports
3064 * any voltage value.
3065 */
3066 if (host->vmmc && regulator_get_voltage(host->vmmc) > 0) {
Kevin Liucec2e212012-11-20 08:24:32 -05003067 ret = regulator_is_supported_voltage(host->vmmc, 2700000,
3068 3600000);
Philip Rakity68737042012-06-08 12:26:13 -07003069 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_330)))
3070 caps[0] &= ~SDHCI_CAN_VDD_330;
Philip Rakity68737042012-06-08 12:26:13 -07003071 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_300)))
3072 caps[0] &= ~SDHCI_CAN_VDD_300;
Kevin Liucec2e212012-11-20 08:24:32 -05003073 ret = regulator_is_supported_voltage(host->vmmc, 1700000,
3074 1950000);
Philip Rakity68737042012-06-08 12:26:13 -07003075 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_180)))
3076 caps[0] &= ~SDHCI_CAN_VDD_180;
3077 }
3078#endif /* CONFIG_REGULATOR */
3079
Arindam Nathf2119df2011-05-05 12:18:57 +05303080 /*
3081 * According to SD Host Controller spec v3.00, if the Host System
3082 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3083 * the value is meaningful only if Voltage Support in the Capabilities
3084 * register is set. The actual current value is 4 times the register
3085 * value.
3086 */
3087 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Philip Rakitybad37e12012-05-27 18:36:44 -07003088 if (!max_current_caps && host->vmmc) {
3089 u32 curr = regulator_get_current_limit(host->vmmc);
3090 if (curr > 0) {
3091
3092 /* convert to SDHCI_MAX_CURRENT format */
3093 curr = curr/1000; /* convert to mA */
3094 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3095
3096 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3097 max_current_caps =
3098 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3099 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3100 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3101 }
3102 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303103
3104 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003105 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303106
Aaron Lu55c46652012-07-04 13:31:48 +08003107 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303108 SDHCI_MAX_CURRENT_330_MASK) >>
3109 SDHCI_MAX_CURRENT_330_SHIFT) *
3110 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303111 }
3112 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003113 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303114
Aaron Lu55c46652012-07-04 13:31:48 +08003115 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303116 SDHCI_MAX_CURRENT_300_MASK) >>
3117 SDHCI_MAX_CURRENT_300_SHIFT) *
3118 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303119 }
3120 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003121 ocr_avail |= MMC_VDD_165_195;
3122
Aaron Lu55c46652012-07-04 13:31:48 +08003123 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303124 SDHCI_MAX_CURRENT_180_MASK) >>
3125 SDHCI_MAX_CURRENT_180_SHIFT) *
3126 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303127 }
3128
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003129 if (host->ocr_mask)
3130 ocr_avail = host->ocr_mask;
3131
Takashi Iwai8f230f42010-12-08 10:04:30 +01003132 mmc->ocr_avail = ocr_avail;
3133 mmc->ocr_avail_sdio = ocr_avail;
3134 if (host->ocr_avail_sdio)
3135 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3136 mmc->ocr_avail_sd = ocr_avail;
3137 if (host->ocr_avail_sd)
3138 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3139 else /* normal SD controllers don't support 1.8V */
3140 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3141 mmc->ocr_avail_mmc = ocr_avail;
3142 if (host->ocr_avail_mmc)
3143 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003144
3145 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303146 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003147 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003148 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003149 }
3150
Pierre Ossmand129bce2006-03-24 03:18:17 -08003151 spin_lock_init(&host->lock);
3152
3153 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003154 * Maximum number of segments. Depends on if the hardware
3155 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003156 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003157 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003158 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003159 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003160 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003161 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04003162 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003163
3164 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003165 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01003166 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08003167 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003168 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003169
3170 /*
3171 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003172 * of bytes. When doing hardware scatter/gather, each entry cannot
3173 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003174 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003175 if (host->flags & SDHCI_USE_ADMA) {
3176 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3177 mmc->max_seg_size = 65535;
3178 else
3179 mmc->max_seg_size = 65536;
3180 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003181 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003182 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003183
3184 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003185 * Maximum block size. This varies from controller to controller and
3186 * is specified in the capabilities register.
3187 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003188 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3189 mmc->max_blk_size = 2;
3190 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303191 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003192 SDHCI_MAX_BLOCK_SHIFT;
3193 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303194 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003195 "assuming 512 bytes\n", mmc_hostname(mmc));
3196 mmc->max_blk_size = 0;
3197 }
3198 }
3199
3200 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003201
3202 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003203 * Maximum block count.
3204 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003205 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003206
3207 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003208 * Init tasklets.
3209 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003210 tasklet_init(&host->finish_tasklet,
3211 sdhci_tasklet_finish, (unsigned long)host);
3212
Al Viroe4cad1b2006-10-10 22:47:07 +01003213 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003214
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303215 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303216 init_waitqueue_head(&host->buf_ready_int);
3217
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303218 /* Initialize re-tuning timer */
3219 init_timer(&host->tuning_timer);
3220 host->tuning_timer.data = (unsigned long)host;
3221 host->tuning_timer.function = sdhci_tuning_timer;
3222 }
3223
Shawn Guo2af502c2013-07-05 14:38:55 +08003224 sdhci_init(host, 0);
3225
Russell King781e9892014-04-25 12:55:46 +01003226 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3227 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003228 if (ret) {
3229 pr_err("%s: Failed to request IRQ %d: %d\n",
3230 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003231 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003232 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003233
Pierre Ossmand129bce2006-03-24 03:18:17 -08003234#ifdef CONFIG_MMC_DEBUG
3235 sdhci_dumpregs(host);
3236#endif
3237
Pierre Ossmanf9134312008-12-21 17:01:48 +01003238#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003239 snprintf(host->led_name, sizeof(host->led_name),
3240 "%s::", mmc_hostname(mmc));
3241 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003242 host->led.brightness = LED_OFF;
3243 host->led.default_trigger = mmc_hostname(mmc);
3244 host->led.brightness_set = sdhci_led_control;
3245
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003246 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003247 if (ret) {
3248 pr_err("%s: Failed to register LED device: %d\n",
3249 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003250 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003251 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003252#endif
3253
Pierre Ossman5f25a662006-10-04 02:15:39 -07003254 mmiowb();
3255
Pierre Ossmand129bce2006-03-24 03:18:17 -08003256 mmc_add_host(mmc);
3257
Girish K Sa3c76eb2011-10-11 11:44:09 +05303258 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003259 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003260 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3261 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003262
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003263 sdhci_enable_card_detection(host);
3264
Pierre Ossmand129bce2006-03-24 03:18:17 -08003265 return 0;
3266
Pierre Ossmanf9134312008-12-21 17:01:48 +01003267#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003268reset:
Russell King03231f92014-04-25 12:57:12 +01003269 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003270 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3271 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003272 free_irq(host->irq, host);
3273#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003274untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003275 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003276
3277 return ret;
3278}
3279
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003280EXPORT_SYMBOL_GPL(sdhci_add_host);
3281
Pierre Ossman1e728592008-04-16 19:13:13 +02003282void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003283{
Pierre Ossman1e728592008-04-16 19:13:13 +02003284 unsigned long flags;
3285
3286 if (dead) {
3287 spin_lock_irqsave(&host->lock, flags);
3288
3289 host->flags |= SDHCI_DEVICE_DEAD;
3290
3291 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303292 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003293 " transfer!\n", mmc_hostname(host->mmc));
3294
3295 host->mrq->cmd->error = -ENOMEDIUM;
3296 tasklet_schedule(&host->finish_tasklet);
3297 }
3298
3299 spin_unlock_irqrestore(&host->lock, flags);
3300 }
3301
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003302 sdhci_disable_card_detection(host);
3303
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003304 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003305
Pierre Ossmanf9134312008-12-21 17:01:48 +01003306#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003307 led_classdev_unregister(&host->led);
3308#endif
3309
Pierre Ossman1e728592008-04-16 19:13:13 +02003310 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003311 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003312
Russell Kingb537f942014-04-25 12:56:01 +01003313 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3314 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003315 free_irq(host->irq, host);
3316
3317 del_timer_sync(&host->timer);
3318
Pierre Ossmand129bce2006-03-24 03:18:17 -08003319 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003320
Philip Rakity77dcb3f2012-07-23 17:25:18 -07003321 if (host->vmmc) {
3322 regulator_disable(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003323 regulator_put(host->vmmc);
Philip Rakity77dcb3f2012-07-23 17:25:18 -07003324 }
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003325
Philip Rakity6231f3d2012-07-23 15:56:23 -07003326 if (host->vqmmc) {
3327 regulator_disable(host->vqmmc);
3328 regulator_put(host->vqmmc);
3329 }
3330
Russell Kingd1e49f72014-04-25 12:58:34 +01003331 if (host->adma_desc)
3332 dma_free_coherent(mmc_dev(host->mmc), ADMA_SIZE,
3333 host->adma_desc, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003334 kfree(host->align_buffer);
3335
3336 host->adma_desc = NULL;
3337 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003338}
3339
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003340EXPORT_SYMBOL_GPL(sdhci_remove_host);
3341
3342void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003343{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003344 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003345}
3346
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003347EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003348
3349/*****************************************************************************\
3350 * *
3351 * Driver init/exit *
3352 * *
3353\*****************************************************************************/
3354
3355static int __init sdhci_drv_init(void)
3356{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303357 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003358 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303359 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003360
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003361 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003362}
3363
3364static void __exit sdhci_drv_exit(void)
3365{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003366}
3367
3368module_init(sdhci_drv_init);
3369module_exit(sdhci_drv_exit);
3370
Pierre Ossmandf673b22006-06-30 02:22:31 -07003371module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003372module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003373
Pierre Ossman32710e82009-04-08 20:14:54 +02003374MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003375MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003376MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003377
Pierre Ossmandf673b22006-06-30 02:22:31 -07003378MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003379MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");