blob: 11b62171f94ef9efac70adcd61150dc1eb213f96 [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Aaron Lu473b0952012-07-03 17:27:49 +080030#include <linux/mmc/card.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080031#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080032
Pierre Ossmand129bce2006-03-24 03:18:17 -080033#include "sdhci.h"
34
35#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080036
Pierre Ossmand129bce2006-03-24 03:18:17 -080037#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010038 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080039
Pierre Ossmanf9134312008-12-21 17:01:48 +010040#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
41 defined(CONFIG_MMC_SDHCI_MODULE))
42#define SDHCI_USE_LEDS_CLASS
43#endif
44
Arindam Nathb513ea22011-05-05 12:19:04 +053045#define MAX_TUNING_LOOP 40
46
Pierre Ossmandf673b22006-06-30 02:22:31 -070047static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030048static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070049
Pierre Ossmand129bce2006-03-24 03:18:17 -080050static void sdhci_finish_data(struct sdhci_host *);
51
Pierre Ossmand129bce2006-03-24 03:18:17 -080052static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053053static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053054static void sdhci_tuning_timer(unsigned long data);
Kevin Liu52983382013-01-31 11:31:37 +080055static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080056
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030057#ifdef CONFIG_PM_RUNTIME
58static int sdhci_runtime_pm_get(struct sdhci_host *host);
59static int sdhci_runtime_pm_put(struct sdhci_host *host);
Adrian Hunterf0710a52013-05-06 12:17:32 +030060static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
61static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030062#else
63static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
64{
65 return 0;
66}
67static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
68{
69 return 0;
70}
Adrian Hunterf0710a52013-05-06 12:17:32 +030071static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
72{
73}
74static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
75{
76}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030077#endif
78
Pierre Ossmand129bce2006-03-24 03:18:17 -080079static void sdhci_dumpregs(struct sdhci_host *host)
80{
Girish K Sa3c76eb2011-10-11 11:44:09 +053081 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070082 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080083
Girish K Sa3c76eb2011-10-11 11:44:09 +053084 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030085 sdhci_readl(host, SDHCI_DMA_ADDRESS),
86 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053087 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030088 sdhci_readw(host, SDHCI_BLOCK_SIZE),
89 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053090 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030091 sdhci_readl(host, SDHCI_ARGUMENT),
92 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053093 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030094 sdhci_readl(host, SDHCI_PRESENT_STATE),
95 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053096 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030097 sdhci_readb(host, SDHCI_POWER_CONTROL),
98 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053099 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300100 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
101 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530102 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300103 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
104 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530105 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readl(host, SDHCI_INT_ENABLE),
107 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530108 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300109 sdhci_readw(host, SDHCI_ACMD12_ERR),
110 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530111 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300112 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500113 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530114 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500115 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300116 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530117 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530118 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800119
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100120 if (host->flags & SDHCI_USE_ADMA)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530121 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100122 readl(host->ioaddr + SDHCI_ADMA_ERROR),
123 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
124
Girish K Sa3c76eb2011-10-11 11:44:09 +0530125 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800126}
127
128/*****************************************************************************\
129 * *
130 * Low level functions *
131 * *
132\*****************************************************************************/
133
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300134static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
135{
Russell King5b4f1f62014-04-25 12:57:02 +0100136 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300137
Adrian Hunterc79396c2011-12-27 15:48:42 +0200138 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100139 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300140 return;
141
Russell King5b4f1f62014-04-25 12:57:02 +0100142 if (enable) {
143 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
144 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800145
Russell King5b4f1f62014-04-25 12:57:02 +0100146 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
147 SDHCI_INT_CARD_INSERT;
148 } else {
149 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
150 }
Russell Kingb537f942014-04-25 12:56:01 +0100151
152 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
153 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300154}
155
156static void sdhci_enable_card_detection(struct sdhci_host *host)
157{
158 sdhci_set_card_detection(host, true);
159}
160
161static void sdhci_disable_card_detection(struct sdhci_host *host)
162{
163 sdhci_set_card_detection(host, false);
164}
165
Russell King03231f92014-04-25 12:57:12 +0100166void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800167{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700168 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800169
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300170 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800171
Adrian Hunterf0710a52013-05-06 12:17:32 +0300172 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800173 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300174 /* Reset-all turns off SD Bus Power */
175 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
176 sdhci_runtime_pm_bus_off(host);
177 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800178
Pierre Ossmane16514d82006-06-30 02:22:24 -0700179 /* Wait max 100 ms */
180 timeout = 100;
181
182 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300183 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700184 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530185 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700186 mmc_hostname(host->mmc), (int)mask);
187 sdhci_dumpregs(host);
188 return;
189 }
190 timeout--;
191 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800192 }
Russell King03231f92014-04-25 12:57:12 +0100193}
194EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300195
Russell King03231f92014-04-25 12:57:12 +0100196static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
197{
198 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
199 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
200 SDHCI_CARD_PRESENT))
201 return;
202 }
203
204 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800205
Russell Kingda91a8f2014-04-25 13:00:12 +0100206 if (mask & SDHCI_RESET_ALL) {
207 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
208 if (host->ops->enable_dma)
209 host->ops->enable_dma(host);
210 }
211
212 /* Resetting the controller clears many */
213 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800214 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800215}
216
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800217static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
218
219static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800220{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800221 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100222 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800223 else
Russell King03231f92014-04-25 12:57:12 +0100224 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800225
Russell Kingb537f942014-04-25 12:56:01 +0100226 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
227 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
228 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
229 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
230 SDHCI_INT_RESPONSE;
231
232 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
233 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800234
235 if (soft) {
236 /* force clock reconfiguration */
237 host->clock = 0;
238 sdhci_set_ios(host->mmc, &host->mmc->ios);
239 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300240}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800241
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300242static void sdhci_reinit(struct sdhci_host *host)
243{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800244 sdhci_init(host, 0);
Aaron Lub67c6b42012-06-29 16:17:31 +0800245 /*
246 * Retuning stuffs are affected by different cards inserted and only
247 * applicable to UHS-I cards. So reset these fields to their initial
248 * value when card is removed.
249 */
Aaron Lu973905f2012-07-04 13:29:09 +0800250 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
251 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
252
Aaron Lub67c6b42012-06-29 16:17:31 +0800253 del_timer_sync(&host->tuning_timer);
254 host->flags &= ~SDHCI_NEEDS_RETUNING;
255 host->mmc->max_blk_count =
256 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
257 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300258 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800259}
260
261static void sdhci_activate_led(struct sdhci_host *host)
262{
263 u8 ctrl;
264
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300265 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800266 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300267 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800268}
269
270static void sdhci_deactivate_led(struct sdhci_host *host)
271{
272 u8 ctrl;
273
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300274 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800275 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300276 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800277}
278
Pierre Ossmanf9134312008-12-21 17:01:48 +0100279#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100280static void sdhci_led_control(struct led_classdev *led,
281 enum led_brightness brightness)
282{
283 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
284 unsigned long flags;
285
286 spin_lock_irqsave(&host->lock, flags);
287
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300288 if (host->runtime_suspended)
289 goto out;
290
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100291 if (brightness == LED_OFF)
292 sdhci_deactivate_led(host);
293 else
294 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300295out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100296 spin_unlock_irqrestore(&host->lock, flags);
297}
298#endif
299
Pierre Ossmand129bce2006-03-24 03:18:17 -0800300/*****************************************************************************\
301 * *
302 * Core functions *
303 * *
304\*****************************************************************************/
305
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100306static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307{
Pierre Ossman76591502008-07-21 00:32:11 +0200308 unsigned long flags;
309 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700310 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200311 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800312
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800314
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100315 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200316 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800317
Pierre Ossman76591502008-07-21 00:32:11 +0200318 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800319
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100320 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200321 if (!sg_miter_next(&host->sg_miter))
322 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800323
Pierre Ossman76591502008-07-21 00:32:11 +0200324 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800325
Pierre Ossman76591502008-07-21 00:32:11 +0200326 blksize -= len;
327 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200328
Pierre Ossman76591502008-07-21 00:32:11 +0200329 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800330
Pierre Ossman76591502008-07-21 00:32:11 +0200331 while (len) {
332 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300333 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200334 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800335 }
Pierre Ossman76591502008-07-21 00:32:11 +0200336
337 *buf = scratch & 0xFF;
338
339 buf++;
340 scratch >>= 8;
341 chunk--;
342 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800343 }
344 }
Pierre Ossman76591502008-07-21 00:32:11 +0200345
346 sg_miter_stop(&host->sg_miter);
347
348 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100349}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800350
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100351static void sdhci_write_block_pio(struct sdhci_host *host)
352{
Pierre Ossman76591502008-07-21 00:32:11 +0200353 unsigned long flags;
354 size_t blksize, len, chunk;
355 u32 scratch;
356 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100357
358 DBG("PIO writing\n");
359
360 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200361 chunk = 0;
362 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100363
Pierre Ossman76591502008-07-21 00:32:11 +0200364 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100365
366 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200367 if (!sg_miter_next(&host->sg_miter))
368 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100369
Pierre Ossman76591502008-07-21 00:32:11 +0200370 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200371
Pierre Ossman76591502008-07-21 00:32:11 +0200372 blksize -= len;
373 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100374
Pierre Ossman76591502008-07-21 00:32:11 +0200375 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100376
Pierre Ossman76591502008-07-21 00:32:11 +0200377 while (len) {
378 scratch |= (u32)*buf << (chunk * 8);
379
380 buf++;
381 chunk++;
382 len--;
383
384 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300385 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200386 chunk = 0;
387 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100388 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100389 }
390 }
Pierre Ossman76591502008-07-21 00:32:11 +0200391
392 sg_miter_stop(&host->sg_miter);
393
394 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100395}
396
397static void sdhci_transfer_pio(struct sdhci_host *host)
398{
399 u32 mask;
400
401 BUG_ON(!host->data);
402
Pierre Ossman76591502008-07-21 00:32:11 +0200403 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100404 return;
405
406 if (host->data->flags & MMC_DATA_READ)
407 mask = SDHCI_DATA_AVAILABLE;
408 else
409 mask = SDHCI_SPACE_AVAILABLE;
410
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200411 /*
412 * Some controllers (JMicron JMB38x) mess up the buffer bits
413 * for transfers < 4 bytes. As long as it is just one block,
414 * we can ignore the bits.
415 */
416 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
417 (host->data->blocks == 1))
418 mask = ~0;
419
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300420 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300421 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
422 udelay(100);
423
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100424 if (host->data->flags & MMC_DATA_READ)
425 sdhci_read_block_pio(host);
426 else
427 sdhci_write_block_pio(host);
428
Pierre Ossman76591502008-07-21 00:32:11 +0200429 host->blocks--;
430 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100431 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100432 }
433
434 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800435}
436
Pierre Ossman2134a922008-06-28 18:28:51 +0200437static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
438{
439 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800440 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200441}
442
443static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
444{
Cong Wang482fce92011-11-27 13:27:00 +0800445 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200446 local_irq_restore(*flags);
447}
448
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200449static void sdhci_adma_write_desc(void *desc, u32 addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800450{
Ben Dooks9e506f32010-03-05 13:43:29 -0800451 __le32 *dataddr = (__le32 __force *)(desc + 4);
452 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800453
Ben Dooks9e506f32010-03-05 13:43:29 -0800454 /* SDHCI specification says ADMA descriptors should be 4 byte
455 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800456
Ben Dooks9e506f32010-03-05 13:43:29 -0800457 cmdlen[0] = cpu_to_le16(cmd);
458 cmdlen[1] = cpu_to_le16(len);
459
460 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800461}
462
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200463static void sdhci_adma_mark_end(void *desc)
464{
465 u8 *dma_desc = desc;
466
Adrian Hunter739d46d2014-11-04 12:42:44 +0200467 dma_desc[0] |= ADMA2_END;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200468}
469
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200470static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200471 struct mmc_data *data)
472{
473 int direction;
474
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200475 void *desc;
476 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200477 dma_addr_t addr;
478 dma_addr_t align_addr;
479 int len, offset;
480
481 struct scatterlist *sg;
482 int i;
483 char *buffer;
484 unsigned long flags;
485
486 /*
487 * The spec does not specify endianness of descriptor table.
488 * We currently guess that it is LE.
489 */
490
491 if (data->flags & MMC_DATA_READ)
492 direction = DMA_FROM_DEVICE;
493 else
494 direction = DMA_TO_DEVICE;
495
Pierre Ossman2134a922008-06-28 18:28:51 +0200496 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200497 host->align_buffer, host->align_buffer_sz, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700498 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200499 goto fail;
Adrian Hunter76fe3792014-11-04 12:42:42 +0200500 BUG_ON(host->align_addr & host->align_mask);
Pierre Ossman2134a922008-06-28 18:28:51 +0200501
502 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
503 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200504 if (host->sg_count == 0)
505 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200506
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200507 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200508 align = host->align_buffer;
509
510 align_addr = host->align_addr;
511
512 for_each_sg(data->sg, sg, host->sg_count, i) {
513 addr = sg_dma_address(sg);
514 len = sg_dma_len(sg);
515
516 /*
517 * The SDHCI specification states that ADMA
518 * addresses must be 32-bit aligned. If they
519 * aren't, then we use a bounce buffer for
520 * the (up to three) bytes that screw up the
521 * alignment.
522 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200523 offset = (host->align_sz - (addr & host->align_mask)) &
524 host->align_mask;
Pierre Ossman2134a922008-06-28 18:28:51 +0200525 if (offset) {
526 if (data->flags & MMC_DATA_WRITE) {
527 buffer = sdhci_kmap_atomic(sg, &flags);
Adrian Hunter8be78c62014-11-04 12:42:36 +0200528 WARN_ON(((long)buffer & (PAGE_SIZE - 1)) >
529 (PAGE_SIZE - offset));
Pierre Ossman2134a922008-06-28 18:28:51 +0200530 memcpy(align, buffer, offset);
531 sdhci_kunmap_atomic(buffer, &flags);
532 }
533
Ben Dooks118cd172010-03-05 13:43:26 -0800534 /* tran, valid */
Adrian Hunter739d46d2014-11-04 12:42:44 +0200535 sdhci_adma_write_desc(desc, align_addr, offset,
536 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200537
538 BUG_ON(offset > 65536);
539
Adrian Hunter76fe3792014-11-04 12:42:42 +0200540 align += host->align_sz;
541 align_addr += host->align_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200542
Adrian Hunter76fe3792014-11-04 12:42:42 +0200543 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200544
545 addr += offset;
546 len -= offset;
547 }
548
Pierre Ossman2134a922008-06-28 18:28:51 +0200549 BUG_ON(len > 65536);
550
Ben Dooks118cd172010-03-05 13:43:26 -0800551 /* tran, valid */
Adrian Hunter739d46d2014-11-04 12:42:44 +0200552 sdhci_adma_write_desc(desc, addr, len, ADMA2_TRAN_VALID);
Adrian Hunter76fe3792014-11-04 12:42:42 +0200553 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200554
555 /*
556 * If this triggers then we have a calculation bug
557 * somewhere. :/
558 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200559 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200560 }
561
Thomas Abraham70764a92010-05-26 14:42:04 -0700562 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
563 /*
564 * Mark the last descriptor as the terminating descriptor
565 */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200566 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200567 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200568 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700569 }
570 } else {
571 /*
572 * Add a terminating entry.
573 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200574
Thomas Abraham70764a92010-05-26 14:42:04 -0700575 /* nop, end, valid */
Adrian Hunter739d46d2014-11-04 12:42:44 +0200576 sdhci_adma_write_desc(desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700577 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200578
579 /*
580 * Resync align buffer as we might have changed it.
581 */
582 if (data->flags & MMC_DATA_WRITE) {
583 dma_sync_single_for_device(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200584 host->align_addr, host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200585 }
586
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200587 return 0;
588
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200589unmap_align:
590 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200591 host->align_buffer_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200592fail:
593 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200594}
595
596static void sdhci_adma_table_post(struct sdhci_host *host,
597 struct mmc_data *data)
598{
599 int direction;
600
601 struct scatterlist *sg;
602 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200603 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200604 char *buffer;
605 unsigned long flags;
Russell Kingde0b65a2014-04-25 12:58:29 +0100606 bool has_unaligned;
Pierre Ossman2134a922008-06-28 18:28:51 +0200607
608 if (data->flags & MMC_DATA_READ)
609 direction = DMA_FROM_DEVICE;
610 else
611 direction = DMA_TO_DEVICE;
612
Pierre Ossman2134a922008-06-28 18:28:51 +0200613 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200614 host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200615
Russell Kingde0b65a2014-04-25 12:58:29 +0100616 /* Do a quick scan of the SG list for any unaligned mappings */
617 has_unaligned = false;
618 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter76fe3792014-11-04 12:42:42 +0200619 if (sg_dma_address(sg) & host->align_mask) {
Russell Kingde0b65a2014-04-25 12:58:29 +0100620 has_unaligned = true;
621 break;
622 }
623
624 if (has_unaligned && data->flags & MMC_DATA_READ) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200625 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
626 data->sg_len, direction);
627
628 align = host->align_buffer;
629
630 for_each_sg(data->sg, sg, host->sg_count, i) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200631 if (sg_dma_address(sg) & host->align_mask) {
632 size = host->align_sz -
633 (sg_dma_address(sg) & host->align_mask);
Pierre Ossman2134a922008-06-28 18:28:51 +0200634
635 buffer = sdhci_kmap_atomic(sg, &flags);
Adrian Hunter8be78c62014-11-04 12:42:36 +0200636 WARN_ON(((long)buffer & (PAGE_SIZE - 1)) >
637 (PAGE_SIZE - size));
Pierre Ossman2134a922008-06-28 18:28:51 +0200638 memcpy(buffer, align, size);
639 sdhci_kunmap_atomic(buffer, &flags);
640
Adrian Hunter76fe3792014-11-04 12:42:42 +0200641 align += host->align_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200642 }
643 }
644 }
645
646 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
647 data->sg_len, direction);
648}
649
Andrei Warkentina3c77782011-04-11 16:13:42 -0500650static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800651{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700652 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500653 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700654 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800655
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200656 /*
657 * If the host controller provides us with an incorrect timeout
658 * value, just skip the check and use 0xE. The hardware may take
659 * longer to time out, but that's much better than having a too-short
660 * timeout value.
661 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200662 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200663 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200664
Andrei Warkentina3c77782011-04-11 16:13:42 -0500665 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100666 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500667 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800668
Andrei Warkentina3c77782011-04-11 16:13:42 -0500669 /* timeout in us */
670 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100671 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300672 else {
673 target_timeout = data->timeout_ns / 1000;
674 if (host->clock)
675 target_timeout += data->timeout_clks / host->clock;
676 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700677
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700678 /*
679 * Figure out needed cycles.
680 * We do this in steps in order to fit inside a 32 bit int.
681 * The first step is the minimum timeout, which will have a
682 * minimum resolution of 6 bits:
683 * (1) 2^13*1000 > 2^22,
684 * (2) host->timeout_clk < 2^16
685 * =>
686 * (1) / (2) > 2^6
687 */
688 count = 0;
689 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
690 while (current_timeout < target_timeout) {
691 count++;
692 current_timeout <<= 1;
693 if (count >= 0xF)
694 break;
695 }
696
697 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400698 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
699 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700700 count = 0xE;
701 }
702
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200703 return count;
704}
705
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300706static void sdhci_set_transfer_irqs(struct sdhci_host *host)
707{
708 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
709 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
710
711 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100712 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300713 else
Russell Kingb537f942014-04-25 12:56:01 +0100714 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
715
716 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
717 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300718}
719
Aisheng Dongb45e6682014-08-27 15:26:29 +0800720static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200721{
722 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800723
724 if (host->ops->set_timeout) {
725 host->ops->set_timeout(host, cmd);
726 } else {
727 count = sdhci_calc_timeout(host, cmd);
728 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
729 }
730}
731
732static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
733{
Pierre Ossman2134a922008-06-28 18:28:51 +0200734 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500735 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200736 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200737
738 WARN_ON(host->data);
739
Aisheng Dongb45e6682014-08-27 15:26:29 +0800740 if (data || (cmd->flags & MMC_RSP_BUSY))
741 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500742
743 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200744 return;
745
746 /* Sanity checks */
747 BUG_ON(data->blksz * data->blocks > 524288);
748 BUG_ON(data->blksz > host->mmc->max_blk_size);
749 BUG_ON(data->blocks > 65535);
750
751 host->data = data;
752 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400753 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200754
Richard Röjforsa13abc72009-09-22 16:45:30 -0700755 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100756 host->flags |= SDHCI_REQ_USE_DMA;
757
Pierre Ossman2134a922008-06-28 18:28:51 +0200758 /*
759 * FIXME: This doesn't account for merging when mapping the
760 * scatterlist.
761 */
762 if (host->flags & SDHCI_REQ_USE_DMA) {
763 int broken, i;
764 struct scatterlist *sg;
765
766 broken = 0;
767 if (host->flags & SDHCI_USE_ADMA) {
768 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
769 broken = 1;
770 } else {
771 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
772 broken = 1;
773 }
774
775 if (unlikely(broken)) {
776 for_each_sg(data->sg, sg, data->sg_len, i) {
777 if (sg->length & 0x3) {
778 DBG("Reverting to PIO because of "
779 "transfer size (%d)\n",
780 sg->length);
781 host->flags &= ~SDHCI_REQ_USE_DMA;
782 break;
783 }
784 }
785 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100786 }
787
788 /*
789 * The assumption here being that alignment is the same after
790 * translation to device address space.
791 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200792 if (host->flags & SDHCI_REQ_USE_DMA) {
793 int broken, i;
794 struct scatterlist *sg;
795
796 broken = 0;
797 if (host->flags & SDHCI_USE_ADMA) {
798 /*
799 * As we use 3 byte chunks to work around
800 * alignment problems, we need to check this
801 * quirk.
802 */
803 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
804 broken = 1;
805 } else {
806 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
807 broken = 1;
808 }
809
810 if (unlikely(broken)) {
811 for_each_sg(data->sg, sg, data->sg_len, i) {
812 if (sg->offset & 0x3) {
813 DBG("Reverting to PIO because of "
814 "bad alignment\n");
815 host->flags &= ~SDHCI_REQ_USE_DMA;
816 break;
817 }
818 }
819 }
820 }
821
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200822 if (host->flags & SDHCI_REQ_USE_DMA) {
823 if (host->flags & SDHCI_USE_ADMA) {
824 ret = sdhci_adma_table_pre(host, data);
825 if (ret) {
826 /*
827 * This only happens when someone fed
828 * us an invalid request.
829 */
830 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200831 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200832 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300833 sdhci_writel(host, host->adma_addr,
834 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200835 }
836 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300837 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200838
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300839 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200840 data->sg, data->sg_len,
841 (data->flags & MMC_DATA_READ) ?
842 DMA_FROM_DEVICE :
843 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300844 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200845 /*
846 * This only happens when someone fed
847 * us an invalid request.
848 */
849 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200850 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200851 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200852 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300853 sdhci_writel(host, sg_dma_address(data->sg),
854 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200855 }
856 }
857 }
858
Pierre Ossman2134a922008-06-28 18:28:51 +0200859 /*
860 * Always adjust the DMA selection as some controllers
861 * (e.g. JMicron) can't do PIO properly when the selection
862 * is ADMA.
863 */
864 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300865 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200866 ctrl &= ~SDHCI_CTRL_DMA_MASK;
867 if ((host->flags & SDHCI_REQ_USE_DMA) &&
868 (host->flags & SDHCI_USE_ADMA))
869 ctrl |= SDHCI_CTRL_ADMA32;
870 else
871 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300872 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100873 }
874
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200875 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200876 int flags;
877
878 flags = SG_MITER_ATOMIC;
879 if (host->data->flags & MMC_DATA_READ)
880 flags |= SG_MITER_TO_SG;
881 else
882 flags |= SG_MITER_FROM_SG;
883 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200884 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800885 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700886
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300887 sdhci_set_transfer_irqs(host);
888
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400889 /* Set the DMA boundary value and block size */
890 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
891 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300892 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700893}
894
895static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500896 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700897{
898 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500899 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700900
Dong Aisheng2b558c12013-10-30 22:09:48 +0800901 if (data == NULL) {
902 /* clear Auto CMD settings for no data CMDs */
903 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
904 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
905 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700906 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800907 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700908
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200909 WARN_ON(!host->data);
910
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700911 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500912 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
913 mode |= SDHCI_TRNS_MULTI;
914 /*
915 * If we are sending CMD23, CMD12 never gets sent
916 * on successful completion (so no Auto-CMD12).
917 */
918 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
919 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500920 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
921 mode |= SDHCI_TRNS_AUTO_CMD23;
922 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
923 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700924 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500925
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700926 if (data->flags & MMC_DATA_READ)
927 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100928 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700929 mode |= SDHCI_TRNS_DMA;
930
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300931 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800932}
933
934static void sdhci_finish_data(struct sdhci_host *host)
935{
936 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800937
938 BUG_ON(!host->data);
939
940 data = host->data;
941 host->data = NULL;
942
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100943 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200944 if (host->flags & SDHCI_USE_ADMA)
945 sdhci_adma_table_post(host, data);
946 else {
947 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
948 data->sg_len, (data->flags & MMC_DATA_READ) ?
949 DMA_FROM_DEVICE : DMA_TO_DEVICE);
950 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800951 }
952
953 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200954 * The specification states that the block count register must
955 * be updated, but it does not specify at what point in the
956 * data flow. That makes the register entirely useless to read
957 * back so we have to assume that nothing made it to the card
958 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800959 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200960 if (data->error)
961 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800962 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200963 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800964
Andrei Warkentine89d4562011-05-23 15:06:37 -0500965 /*
966 * Need to send CMD12 if -
967 * a) open-ended multiblock transfer (no CMD23)
968 * b) error in multiblock transfer
969 */
970 if (data->stop &&
971 (data->error ||
972 !host->mrq->sbc)) {
973
Pierre Ossmand129bce2006-03-24 03:18:17 -0800974 /*
975 * The controller needs a reset of internal state machines
976 * upon error conditions.
977 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200978 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100979 sdhci_do_reset(host, SDHCI_RESET_CMD);
980 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800981 }
982
983 sdhci_send_command(host, data->stop);
984 } else
985 tasklet_schedule(&host->finish_tasklet);
986}
987
Dong Aishengc0e551292013-09-13 19:11:31 +0800988void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800989{
990 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700991 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700992 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800993
994 WARN_ON(host->cmd);
995
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700997 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700998
999 mask = SDHCI_CMD_INHIBIT;
1000 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1001 mask |= SDHCI_DATA_INHIBIT;
1002
1003 /* We shouldn't wait for data inihibit for stop commands, even
1004 though they might use busy signaling */
1005 if (host->mrq->data && (cmd == host->mrq->data->stop))
1006 mask &= ~SDHCI_DATA_INHIBIT;
1007
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001008 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001009 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301010 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001011 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001013 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014 tasklet_schedule(&host->finish_tasklet);
1015 return;
1016 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001017 timeout--;
1018 mdelay(1);
1019 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001020
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001021 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001022 if (!cmd->data && cmd->busy_timeout > 9000)
1023 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001024 else
1025 timeout += 10 * HZ;
1026 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001027
1028 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001029 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030
Andrei Warkentina3c77782011-04-11 16:13:42 -05001031 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001032
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001033 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001034
Andrei Warkentine89d4562011-05-23 15:06:37 -05001035 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001036
Pierre Ossmand129bce2006-03-24 03:18:17 -08001037 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301038 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001039 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001040 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001041 tasklet_schedule(&host->finish_tasklet);
1042 return;
1043 }
1044
1045 if (!(cmd->flags & MMC_RSP_PRESENT))
1046 flags = SDHCI_CMD_RESP_NONE;
1047 else if (cmd->flags & MMC_RSP_136)
1048 flags = SDHCI_CMD_RESP_LONG;
1049 else if (cmd->flags & MMC_RSP_BUSY)
1050 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1051 else
1052 flags = SDHCI_CMD_RESP_SHORT;
1053
1054 if (cmd->flags & MMC_RSP_CRC)
1055 flags |= SDHCI_CMD_CRC;
1056 if (cmd->flags & MMC_RSP_OPCODE)
1057 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301058
1059 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301060 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1061 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001062 flags |= SDHCI_CMD_DATA;
1063
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001064 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001065}
Dong Aishengc0e551292013-09-13 19:11:31 +08001066EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001067
1068static void sdhci_finish_command(struct sdhci_host *host)
1069{
1070 int i;
1071
1072 BUG_ON(host->cmd == NULL);
1073
1074 if (host->cmd->flags & MMC_RSP_PRESENT) {
1075 if (host->cmd->flags & MMC_RSP_136) {
1076 /* CRC is stripped so we need to do some shifting. */
1077 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001078 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001079 SDHCI_RESPONSE + (3-i)*4) << 8;
1080 if (i != 3)
1081 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001082 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001083 SDHCI_RESPONSE + (3-i)*4-1);
1084 }
1085 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001086 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001087 }
1088 }
1089
Pierre Ossman17b04292007-07-22 22:18:46 +02001090 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001091
Andrei Warkentine89d4562011-05-23 15:06:37 -05001092 /* Finished CMD23, now send actual command. */
1093 if (host->cmd == host->mrq->sbc) {
1094 host->cmd = NULL;
1095 sdhci_send_command(host, host->mrq->cmd);
1096 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001097
Andrei Warkentine89d4562011-05-23 15:06:37 -05001098 /* Processed actual command. */
1099 if (host->data && host->data_early)
1100 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001101
Andrei Warkentine89d4562011-05-23 15:06:37 -05001102 if (!host->cmd->data)
1103 tasklet_schedule(&host->finish_tasklet);
1104
1105 host->cmd = NULL;
1106 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001107}
1108
Kevin Liu52983382013-01-31 11:31:37 +08001109static u16 sdhci_get_preset_value(struct sdhci_host *host)
1110{
Russell Kingd975f122014-04-25 12:59:31 +01001111 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001112
Russell Kingd975f122014-04-25 12:59:31 +01001113 switch (host->timing) {
1114 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001115 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1116 break;
Russell Kingd975f122014-04-25 12:59:31 +01001117 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001118 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1119 break;
Russell Kingd975f122014-04-25 12:59:31 +01001120 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001121 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1122 break;
Russell Kingd975f122014-04-25 12:59:31 +01001123 case MMC_TIMING_UHS_SDR104:
1124 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001125 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1126 break;
Russell Kingd975f122014-04-25 12:59:31 +01001127 case MMC_TIMING_UHS_DDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001128 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1129 break;
1130 default:
1131 pr_warn("%s: Invalid UHS-I mode selected\n",
1132 mmc_hostname(host->mmc));
1133 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1134 break;
1135 }
1136 return preset;
1137}
1138
Russell King17710592014-04-25 12:58:55 +01001139void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301141 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001142 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301143 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001144 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001145
Russell King1650d0c2014-04-25 12:58:50 +01001146 host->mmc->actual_clock = 0;
1147
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001148 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001149
1150 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001151 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001152
Zhangfei Gao85105c52010-08-06 07:10:01 +08001153 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001154 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001155 u16 pre_val;
1156
1157 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1158 pre_val = sdhci_get_preset_value(host);
1159 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1160 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1161 if (host->clk_mul &&
1162 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1163 clk = SDHCI_PROG_CLOCK_MODE;
1164 real_div = div + 1;
1165 clk_mul = host->clk_mul;
1166 } else {
1167 real_div = max_t(int, 1, div << 1);
1168 }
1169 goto clock_set;
1170 }
1171
Arindam Nathc3ed3872011-05-05 12:19:06 +05301172 /*
1173 * Check if the Host Controller supports Programmable Clock
1174 * Mode.
1175 */
1176 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001177 for (div = 1; div <= 1024; div++) {
1178 if ((host->max_clk * host->clk_mul / div)
1179 <= clock)
1180 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001181 }
Kevin Liu52983382013-01-31 11:31:37 +08001182 /*
1183 * Set Programmable Clock Mode in the Clock
1184 * Control register.
1185 */
1186 clk = SDHCI_PROG_CLOCK_MODE;
1187 real_div = div;
1188 clk_mul = host->clk_mul;
1189 div--;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301190 } else {
1191 /* Version 3.00 divisors must be a multiple of 2. */
1192 if (host->max_clk <= clock)
1193 div = 1;
1194 else {
1195 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1196 div += 2) {
1197 if ((host->max_clk / div) <= clock)
1198 break;
1199 }
1200 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001201 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301202 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001203 }
1204 } else {
1205 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001206 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001207 if ((host->max_clk / div) <= clock)
1208 break;
1209 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001210 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301211 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001212 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001213
Kevin Liu52983382013-01-31 11:31:37 +08001214clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001215 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001216 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301217 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001218 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1219 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001220 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001221 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001222
Chris Ball27f6cb12009-09-22 16:45:31 -07001223 /* Wait max 20 ms */
1224 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001225 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001226 & SDHCI_CLOCK_INT_STABLE)) {
1227 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301228 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001229 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001230 sdhci_dumpregs(host);
1231 return;
1232 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001233 timeout--;
1234 mdelay(1);
1235 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001236
1237 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001238 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001239}
Russell King17710592014-04-25 12:58:55 +01001240EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001241
Russell King24fbb3c2014-04-25 13:00:06 +01001242static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1243 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001244{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001245 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001246 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001247
Tim Kryger52221612014-06-25 00:25:34 -07001248 if (!IS_ERR(mmc->supply.vmmc)) {
1249 spin_unlock_irq(&host->lock);
Markus Mayer4e743f12014-07-03 13:27:42 -07001250 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Tim Kryger52221612014-06-25 00:25:34 -07001251 spin_lock_irq(&host->lock);
1252 return;
1253 }
1254
Russell King24fbb3c2014-04-25 13:00:06 +01001255 if (mode != MMC_POWER_OFF) {
1256 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001257 case MMC_VDD_165_195:
1258 pwr = SDHCI_POWER_180;
1259 break;
1260 case MMC_VDD_29_30:
1261 case MMC_VDD_30_31:
1262 pwr = SDHCI_POWER_300;
1263 break;
1264 case MMC_VDD_32_33:
1265 case MMC_VDD_33_34:
1266 pwr = SDHCI_POWER_330;
1267 break;
1268 default:
1269 BUG();
1270 }
1271 }
1272
1273 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001274 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001275
Pierre Ossmanae628902009-05-03 20:45:03 +02001276 host->pwr = pwr;
1277
1278 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001279 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001280 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1281 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001282 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001283 } else {
1284 /*
1285 * Spec says that we should clear the power reg before setting
1286 * a new value. Some controllers don't seem to like this though.
1287 */
1288 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1289 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001290
Russell Kinge921a8b2014-04-25 13:00:01 +01001291 /*
1292 * At least the Marvell CaFe chip gets confused if we set the
1293 * voltage and set turn on power at the same time, so set the
1294 * voltage first.
1295 */
1296 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1297 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001298
Russell Kinge921a8b2014-04-25 13:00:01 +01001299 pwr |= SDHCI_POWER_ON;
1300
Pierre Ossmanae628902009-05-03 20:45:03 +02001301 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1302
Russell Kinge921a8b2014-04-25 13:00:01 +01001303 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1304 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001305
Russell Kinge921a8b2014-04-25 13:00:01 +01001306 /*
1307 * Some controllers need an extra 10ms delay of 10ms before
1308 * they can apply clock after applying power
1309 */
1310 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1311 mdelay(10);
1312 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001313}
1314
Pierre Ossmand129bce2006-03-24 03:18:17 -08001315/*****************************************************************************\
1316 * *
1317 * MMC callbacks *
1318 * *
1319\*****************************************************************************/
1320
1321static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1322{
1323 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001324 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001325 unsigned long flags;
Aaron Lu473b0952012-07-03 17:27:49 +08001326 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001327
1328 host = mmc_priv(mmc);
1329
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001330 sdhci_runtime_pm_get(host);
1331
Pierre Ossmand129bce2006-03-24 03:18:17 -08001332 spin_lock_irqsave(&host->lock, flags);
1333
1334 WARN_ON(host->mrq != NULL);
1335
Pierre Ossmanf9134312008-12-21 17:01:48 +01001336#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001337 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001338#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001339
1340 /*
1341 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1342 * requests if Auto-CMD12 is enabled.
1343 */
1344 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001345 if (mrq->stop) {
1346 mrq->data->stop = NULL;
1347 mrq->stop = NULL;
1348 }
1349 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001350
1351 host->mrq = mrq;
1352
Shawn Guo505a8682012-12-11 15:23:42 +08001353 /*
1354 * Firstly check card presence from cd-gpio. The return could
1355 * be one of the following possibilities:
1356 * negative: cd-gpio is not available
1357 * zero: cd-gpio is used, and card is removed
1358 * one: cd-gpio is used, and card is present
1359 */
1360 present = mmc_gpio_get_cd(host->mmc);
1361 if (present < 0) {
1362 /* If polling, assume that the card is always present. */
1363 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1364 present = 1;
1365 else
1366 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1367 SDHCI_CARD_PRESENT;
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +08001368 }
1369
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001370 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001371 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001372 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301373 } else {
1374 u32 present_state;
1375
1376 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1377 /*
1378 * Check if the re-tuning timer has already expired and there
Yi Sun7756a96d2014-09-09 02:13:59 +00001379 * is no on-going data transfer and DAT0 is not busy. If so,
1380 * we need to execute tuning procedure before sending command.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301381 */
1382 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
Yi Sun7756a96d2014-09-09 02:13:59 +00001383 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ)) &&
1384 (present_state & SDHCI_DATA_0_LVL_MASK)) {
Chris Ball14efd952012-11-05 14:29:49 -05001385 if (mmc->card) {
1386 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1387 tuning_opcode =
1388 mmc->card->type == MMC_TYPE_MMC ?
1389 MMC_SEND_TUNING_BLOCK_HS200 :
1390 MMC_SEND_TUNING_BLOCK;
Chuansheng Liu63c21182013-11-05 14:52:45 +08001391
1392 /* Here we need to set the host->mrq to NULL,
1393 * in case the pending finish_tasklet
1394 * finishes it incorrectly.
1395 */
1396 host->mrq = NULL;
1397
Chris Ball14efd952012-11-05 14:29:49 -05001398 spin_unlock_irqrestore(&host->lock, flags);
1399 sdhci_execute_tuning(mmc, tuning_opcode);
1400 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301401
Chris Ball14efd952012-11-05 14:29:49 -05001402 /* Restore original mmc_request structure */
1403 host->mrq = mrq;
1404 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301405 }
1406
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001407 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001408 sdhci_send_command(host, mrq->sbc);
1409 else
1410 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301411 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001412
Pierre Ossman5f25a662006-10-04 02:15:39 -07001413 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001414 spin_unlock_irqrestore(&host->lock, flags);
1415}
1416
Russell King2317f562014-04-25 12:57:07 +01001417void sdhci_set_bus_width(struct sdhci_host *host, int width)
1418{
1419 u8 ctrl;
1420
1421 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1422 if (width == MMC_BUS_WIDTH_8) {
1423 ctrl &= ~SDHCI_CTRL_4BITBUS;
1424 if (host->version >= SDHCI_SPEC_300)
1425 ctrl |= SDHCI_CTRL_8BITBUS;
1426 } else {
1427 if (host->version >= SDHCI_SPEC_300)
1428 ctrl &= ~SDHCI_CTRL_8BITBUS;
1429 if (width == MMC_BUS_WIDTH_4)
1430 ctrl |= SDHCI_CTRL_4BITBUS;
1431 else
1432 ctrl &= ~SDHCI_CTRL_4BITBUS;
1433 }
1434 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1435}
1436EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1437
Russell King96d7b782014-04-25 12:59:26 +01001438void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1439{
1440 u16 ctrl_2;
1441
1442 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1443 /* Select Bus Speed Mode for host */
1444 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1445 if ((timing == MMC_TIMING_MMC_HS200) ||
1446 (timing == MMC_TIMING_UHS_SDR104))
1447 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1448 else if (timing == MMC_TIMING_UHS_SDR12)
1449 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1450 else if (timing == MMC_TIMING_UHS_SDR25)
1451 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1452 else if (timing == MMC_TIMING_UHS_SDR50)
1453 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1454 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1455 (timing == MMC_TIMING_MMC_DDR52))
1456 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1457 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1458}
1459EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1460
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001461static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001462{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001463 unsigned long flags;
1464 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001465 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001466
Pierre Ossmand129bce2006-03-24 03:18:17 -08001467 spin_lock_irqsave(&host->lock, flags);
1468
Adrian Hunterceb61432011-12-27 15:48:41 +02001469 if (host->flags & SDHCI_DEVICE_DEAD) {
1470 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001471 if (!IS_ERR(mmc->supply.vmmc) &&
1472 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001473 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001474 return;
1475 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001476
Pierre Ossmand129bce2006-03-24 03:18:17 -08001477 /*
1478 * Reset the chip on each power off.
1479 * Should clear out any weird states.
1480 */
1481 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001482 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001483 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001484 }
1485
Kevin Liu52983382013-01-31 11:31:37 +08001486 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001487 (ios->power_mode == MMC_POWER_UP) &&
1488 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001489 sdhci_enable_preset_value(host, false);
1490
Russell King373073e2014-04-25 12:58:45 +01001491 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001492 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001493 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001494
1495 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1496 host->clock) {
1497 host->timeout_clk = host->mmc->actual_clock ?
1498 host->mmc->actual_clock / 1000 :
1499 host->clock / 1000;
1500 host->mmc->max_busy_timeout =
1501 host->ops->get_max_timeout_count ?
1502 host->ops->get_max_timeout_count(host) :
1503 1 << 27;
1504 host->mmc->max_busy_timeout /= host->timeout_clk;
1505 }
Russell King373073e2014-04-25 12:58:45 +01001506 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001507
Russell King24fbb3c2014-04-25 13:00:06 +01001508 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001509
Philip Rakity643a81f2010-09-23 08:24:32 -07001510 if (host->ops->platform_send_init_74_clocks)
1511 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1512
Russell King2317f562014-04-25 12:57:07 +01001513 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001514
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001515 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001516
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001517 if ((ios->timing == MMC_TIMING_SD_HS ||
1518 ios->timing == MMC_TIMING_MMC_HS)
1519 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001520 ctrl |= SDHCI_CTRL_HISPD;
1521 else
1522 ctrl &= ~SDHCI_CTRL_HISPD;
1523
Arindam Nathd6d50a12011-05-05 12:18:59 +05301524 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301525 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301526
1527 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301528 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001529 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301530 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301531 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1532 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001533 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301534 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301535
Russell Kingda91a8f2014-04-25 13:00:12 +01001536 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301537 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301538 /*
1539 * We only need to set Driver Strength if the
1540 * preset value enable is not set.
1541 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001542 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301543 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1544 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1545 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1546 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1547 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1548
1549 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301550 } else {
1551 /*
1552 * According to SDHC Spec v3.00, if the Preset Value
1553 * Enable in the Host Control 2 register is set, we
1554 * need to reset SD Clock Enable before changing High
1555 * Speed Enable to avoid generating clock gliches.
1556 */
Arindam Nath758535c2011-05-05 12:19:00 +05301557
1558 /* Reset SD Clock Enable */
1559 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1560 clk &= ~SDHCI_CLOCK_CARD_EN;
1561 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1562
1563 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1564
1565 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001566 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301567 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301568
Arindam Nath49c468f2011-05-05 12:19:01 +05301569 /* Reset SD Clock Enable */
1570 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1571 clk &= ~SDHCI_CLOCK_CARD_EN;
1572 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1573
Russell King96d7b782014-04-25 12:59:26 +01001574 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001575 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301576
Kevin Liu52983382013-01-31 11:31:37 +08001577 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1578 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1579 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1580 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1581 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1582 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1583 u16 preset;
1584
1585 sdhci_enable_preset_value(host, true);
1586 preset = sdhci_get_preset_value(host);
1587 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1588 >> SDHCI_PRESET_DRV_SHIFT;
1589 }
1590
Arindam Nath49c468f2011-05-05 12:19:01 +05301591 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001592 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301593 } else
1594 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301595
Leandro Dorileob8352262007-07-25 23:47:04 +02001596 /*
1597 * Some (ENE) controllers go apeshit on some ios operation,
1598 * signalling timeout and CRC errors even on CMD0. Resetting
1599 * it on each ios seems to solve the problem.
1600 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001601 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001602 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001603
Pierre Ossman5f25a662006-10-04 02:15:39 -07001604 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001605 spin_unlock_irqrestore(&host->lock, flags);
1606}
1607
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001608static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1609{
1610 struct sdhci_host *host = mmc_priv(mmc);
1611
1612 sdhci_runtime_pm_get(host);
1613 sdhci_do_set_ios(host, ios);
1614 sdhci_runtime_pm_put(host);
1615}
1616
Kevin Liu94144a42013-02-28 17:35:53 +08001617static int sdhci_do_get_cd(struct sdhci_host *host)
1618{
1619 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1620
1621 if (host->flags & SDHCI_DEVICE_DEAD)
1622 return 0;
1623
1624 /* If polling/nonremovable, assume that the card is always present. */
1625 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1626 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1627 return 1;
1628
1629 /* Try slot gpio detect */
1630 if (!IS_ERR_VALUE(gpio_cd))
1631 return !!gpio_cd;
1632
1633 /* Host native card detect */
1634 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1635}
1636
1637static int sdhci_get_cd(struct mmc_host *mmc)
1638{
1639 struct sdhci_host *host = mmc_priv(mmc);
1640 int ret;
1641
1642 sdhci_runtime_pm_get(host);
1643 ret = sdhci_do_get_cd(host);
1644 sdhci_runtime_pm_put(host);
1645 return ret;
1646}
1647
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001648static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001649{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001650 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001651 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001652
Pierre Ossmand129bce2006-03-24 03:18:17 -08001653 spin_lock_irqsave(&host->lock, flags);
1654
Pierre Ossman1e728592008-04-16 19:13:13 +02001655 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001656 is_readonly = 0;
1657 else if (host->ops->get_ro)
1658 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001659 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001660 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1661 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001662
1663 spin_unlock_irqrestore(&host->lock, flags);
1664
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001665 /* This quirk needs to be replaced by a callback-function later */
1666 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1667 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001668}
1669
Takashi Iwai82b0e232011-04-21 20:26:38 +02001670#define SAMPLE_COUNT 5
1671
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001672static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001673{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001674 int i, ro_count;
1675
Takashi Iwai82b0e232011-04-21 20:26:38 +02001676 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001677 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001678
1679 ro_count = 0;
1680 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001681 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001682 if (++ro_count > SAMPLE_COUNT / 2)
1683 return 1;
1684 }
1685 msleep(30);
1686 }
1687 return 0;
1688}
1689
Adrian Hunter20758b62011-08-29 16:42:12 +03001690static void sdhci_hw_reset(struct mmc_host *mmc)
1691{
1692 struct sdhci_host *host = mmc_priv(mmc);
1693
1694 if (host->ops && host->ops->hw_reset)
1695 host->ops->hw_reset(host);
1696}
1697
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001698static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001699{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001700 struct sdhci_host *host = mmc_priv(mmc);
1701 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001702
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001703 sdhci_runtime_pm_get(host);
1704 ret = sdhci_do_get_ro(host);
1705 sdhci_runtime_pm_put(host);
1706 return ret;
1707}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001708
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001709static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1710{
Russell Kingbe138552014-04-25 12:55:56 +01001711 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001712 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001713 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001714 else
Russell Kingb537f942014-04-25 12:56:01 +01001715 host->ier &= ~SDHCI_INT_CARD_INT;
1716
1717 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1718 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001719 mmiowb();
1720 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001721}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001722
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001723static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1724{
1725 struct sdhci_host *host = mmc_priv(mmc);
1726 unsigned long flags;
1727
Russell Kingef104332014-04-25 12:55:41 +01001728 sdhci_runtime_pm_get(host);
1729
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001730 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001731 if (enable)
1732 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1733 else
1734 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1735
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001736 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001737 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001738
1739 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001740}
1741
Philip Rakity6231f3d2012-07-23 15:56:23 -07001742static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001743 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001744{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001745 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001746 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001747 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001748
1749 /*
1750 * Signal Voltage Switching is only applicable for Host Controllers
1751 * v3.00 and above.
1752 */
1753 if (host->version < SDHCI_SPEC_300)
1754 return 0;
1755
Philip Rakity6231f3d2012-07-23 15:56:23 -07001756 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001757
Fabio Estevam21f59982013-02-14 10:35:03 -02001758 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001759 case MMC_SIGNAL_VOLTAGE_330:
1760 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1761 ctrl &= ~SDHCI_CTRL_VDD_180;
1762 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1763
Tim Kryger3a48edc2014-06-13 10:13:56 -07001764 if (!IS_ERR(mmc->supply.vqmmc)) {
1765 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1766 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001767 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001768 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1769 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001770 return -EIO;
1771 }
1772 }
1773 /* Wait for 5ms */
1774 usleep_range(5000, 5500);
1775
1776 /* 3.3V regulator output should be stable within 5 ms */
1777 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1778 if (!(ctrl & SDHCI_CTRL_VDD_180))
1779 return 0;
1780
Joe Perches66061102014-09-12 14:56:56 -07001781 pr_warn("%s: 3.3V regulator output did not became stable\n",
1782 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001783
1784 return -EAGAIN;
1785 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001786 if (!IS_ERR(mmc->supply.vqmmc)) {
1787 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001788 1700000, 1950000);
1789 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001790 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1791 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001792 return -EIO;
1793 }
1794 }
1795
1796 /*
1797 * Enable 1.8V Signal Enable in the Host Control2
1798 * register
1799 */
1800 ctrl |= SDHCI_CTRL_VDD_180;
1801 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1802
Kevin Liu20b92a32012-12-17 19:29:26 +08001803 /* 1.8V regulator output should be stable within 5 ms */
1804 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1805 if (ctrl & SDHCI_CTRL_VDD_180)
1806 return 0;
1807
Joe Perches66061102014-09-12 14:56:56 -07001808 pr_warn("%s: 1.8V regulator output did not became stable\n",
1809 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001810
1811 return -EAGAIN;
1812 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001813 if (!IS_ERR(mmc->supply.vqmmc)) {
1814 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1815 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001816 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001817 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1818 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001819 return -EIO;
1820 }
1821 }
1822 return 0;
1823 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301824 /* No signal voltage switch required */
1825 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001826 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301827}
1828
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001829static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001830 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001831{
1832 struct sdhci_host *host = mmc_priv(mmc);
1833 int err;
1834
1835 if (host->version < SDHCI_SPEC_300)
1836 return 0;
1837 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001838 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001839 sdhci_runtime_pm_put(host);
1840 return err;
1841}
1842
Kevin Liu20b92a32012-12-17 19:29:26 +08001843static int sdhci_card_busy(struct mmc_host *mmc)
1844{
1845 struct sdhci_host *host = mmc_priv(mmc);
1846 u32 present_state;
1847
1848 sdhci_runtime_pm_get(host);
1849 /* Check whether DAT[3:0] is 0000 */
1850 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1851 sdhci_runtime_pm_put(host);
1852
1853 return !(present_state & SDHCI_DATA_LVL_MASK);
1854}
1855
Girish K S069c9f12012-01-06 09:56:39 +05301856static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301857{
Russell King4b6f37d2014-04-25 12:59:36 +01001858 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301859 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301860 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301861 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001862 unsigned long flags;
Arindam Nathb513ea22011-05-05 12:19:04 +05301863
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001864 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001865 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301866
Arindam Nathb513ea22011-05-05 12:19:04 +05301867 /*
Girish K S069c9f12012-01-06 09:56:39 +05301868 * The Host Controller needs tuning only in case of SDR104 mode
1869 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301870 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301871 * If the Host Controller supports the HS200 mode then the
1872 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301873 */
Russell King4b6f37d2014-04-25 12:59:36 +01001874 switch (host->timing) {
1875 case MMC_TIMING_MMC_HS200:
1876 case MMC_TIMING_UHS_SDR104:
1877 break;
Girish K S069c9f12012-01-06 09:56:39 +05301878
Russell King4b6f37d2014-04-25 12:59:36 +01001879 case MMC_TIMING_UHS_SDR50:
1880 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1881 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1882 break;
1883 /* FALLTHROUGH */
1884
1885 default:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001886 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001887 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301888 return 0;
1889 }
1890
Dong Aisheng45251812013-09-13 19:11:30 +08001891 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001892 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001893 err = host->ops->platform_execute_tuning(host, opcode);
1894 sdhci_runtime_pm_put(host);
1895 return err;
1896 }
1897
Russell King4b6f37d2014-04-25 12:59:36 +01001898 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1899 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Arindam Nathb513ea22011-05-05 12:19:04 +05301900 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1901
1902 /*
1903 * As per the Host Controller spec v3.00, tuning command
1904 * generates Buffer Read Ready interrupt, so enable that.
1905 *
1906 * Note: The spec clearly says that when tuning sequence
1907 * is being performed, the controller does not generate
1908 * interrupts other than Buffer Read Ready interrupt. But
1909 * to make sure we don't hit a controller bug, we _only_
1910 * enable Buffer Read Ready interrupt here.
1911 */
Russell Kingb537f942014-04-25 12:56:01 +01001912 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1913 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301914
1915 /*
1916 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1917 * of loops reaches 40 times or a timeout of 150ms occurs.
1918 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301919 do {
1920 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001921 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301922
Girish K S069c9f12012-01-06 09:56:39 +05301923 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301924 cmd.arg = 0;
1925 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1926 cmd.retries = 0;
1927 cmd.data = NULL;
1928 cmd.error = 0;
1929
Al Cooper7ce45e92014-05-09 11:34:07 -04001930 if (tuning_loop_counter-- == 0)
1931 break;
1932
Arindam Nathb513ea22011-05-05 12:19:04 +05301933 mrq.cmd = &cmd;
1934 host->mrq = &mrq;
1935
1936 /*
1937 * In response to CMD19, the card sends 64 bytes of tuning
1938 * block to the Host Controller. So we set the block size
1939 * to 64 here.
1940 */
Girish K S069c9f12012-01-06 09:56:39 +05301941 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1942 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1943 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1944 SDHCI_BLOCK_SIZE);
1945 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1946 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1947 SDHCI_BLOCK_SIZE);
1948 } else {
1949 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1950 SDHCI_BLOCK_SIZE);
1951 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301952
1953 /*
1954 * The tuning block is sent by the card to the host controller.
1955 * So we set the TRNS_READ bit in the Transfer Mode register.
1956 * This also takes care of setting DMA Enable and Multi Block
1957 * Select in the same register to 0.
1958 */
1959 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1960
1961 sdhci_send_command(host, &cmd);
1962
1963 host->cmd = NULL;
1964 host->mrq = NULL;
1965
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001966 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301967 /* Wait for Buffer Read Ready interrupt */
1968 wait_event_interruptible_timeout(host->buf_ready_int,
1969 (host->tuning_done == 1),
1970 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001971 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301972
1973 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301974 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301975 "Buffer Read Ready interrupt during tuning "
1976 "procedure, falling back to fixed sampling "
1977 "clock\n");
1978 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1979 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1980 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1981 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1982
1983 err = -EIO;
1984 goto out;
1985 }
1986
1987 host->tuning_done = 0;
1988
1989 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07001990
1991 /* eMMC spec does not require a delay between tuning cycles */
1992 if (opcode == MMC_SEND_TUNING_BLOCK)
1993 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05301994 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1995
1996 /*
1997 * The Host Driver has exhausted the maximum number of loops allowed,
1998 * so use fixed sampling frequency.
1999 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002000 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302001 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2002 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002003 }
2004 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
2005 pr_info(DRIVER_NAME ": Tuning procedure"
2006 " failed, falling back to fixed sampling"
2007 " clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002008 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302009 }
2010
2011out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302012 /*
2013 * If this is the very first time we are here, we start the retuning
2014 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2015 * flag won't be set, we check this condition before actually starting
2016 * the timer.
2017 */
2018 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2019 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
Aaron Lu973905f2012-07-04 13:29:09 +08002020 host->flags |= SDHCI_USING_RETUNING_TIMER;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302021 mod_timer(&host->tuning_timer, jiffies +
2022 host->tuning_count * HZ);
2023 /* Tuning mode 1 limits the maximum data length to 4MB */
2024 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
Arend van Spriel2bc02482014-01-04 13:51:26 +01002025 } else if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302026 host->flags &= ~SDHCI_NEEDS_RETUNING;
2027 /* Reload the new initial value for timer */
Arend van Spriel2bc02482014-01-04 13:51:26 +01002028 mod_timer(&host->tuning_timer, jiffies +
2029 host->tuning_count * HZ);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302030 }
2031
2032 /*
2033 * In case tuning fails, host controllers which support re-tuning can
2034 * try tuning again at a later time, when the re-tuning timer expires.
2035 * So for these controllers, we return 0. Since there might be other
2036 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08002037 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2038 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302039 */
Aaron Lu973905f2012-07-04 13:29:09 +08002040 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302041 err = 0;
2042
Russell Kingb537f942014-04-25 12:56:01 +01002043 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2044 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002045 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002046 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302047
2048 return err;
2049}
2050
Kevin Liu52983382013-01-31 11:31:37 +08002051
2052static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302053{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302054 /* Host Controller v3.00 defines preset value registers */
2055 if (host->version < SDHCI_SPEC_300)
2056 return;
2057
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302058 /*
2059 * We only enable or disable Preset Value if they are not already
2060 * enabled or disabled respectively. Otherwise, we bail out.
2061 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002062 if (host->preset_enabled != enable) {
2063 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2064
2065 if (enable)
2066 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2067 else
2068 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2069
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302070 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002071
2072 if (enable)
2073 host->flags |= SDHCI_PV_ENABLED;
2074 else
2075 host->flags &= ~SDHCI_PV_ENABLED;
2076
2077 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302078 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002079}
2080
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002081static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002082{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002083 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002084 unsigned long flags;
2085
Christian Daudt722e1282013-06-20 14:26:36 -07002086 /* First check if client has provided their own card event */
2087 if (host->ops->card_event)
2088 host->ops->card_event(host);
2089
Pierre Ossmand129bce2006-03-24 03:18:17 -08002090 spin_lock_irqsave(&host->lock, flags);
2091
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002092 /* Check host->mrq first in case we are runtime suspended */
Shawn Guo9668d762013-06-09 19:49:24 +08002093 if (host->mrq && !sdhci_do_get_cd(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302094 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002095 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302096 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002097 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002098
Russell King03231f92014-04-25 12:57:12 +01002099 sdhci_do_reset(host, SDHCI_RESET_CMD);
2100 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002101
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002102 host->mrq->cmd->error = -ENOMEDIUM;
2103 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002104 }
2105
2106 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002107}
2108
2109static const struct mmc_host_ops sdhci_ops = {
2110 .request = sdhci_request,
2111 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002112 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002113 .get_ro = sdhci_get_ro,
2114 .hw_reset = sdhci_hw_reset,
2115 .enable_sdio_irq = sdhci_enable_sdio_irq,
2116 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2117 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002118 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002119 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002120};
2121
2122/*****************************************************************************\
2123 * *
2124 * Tasklets *
2125 * *
2126\*****************************************************************************/
2127
Pierre Ossmand129bce2006-03-24 03:18:17 -08002128static void sdhci_tasklet_finish(unsigned long param)
2129{
2130 struct sdhci_host *host;
2131 unsigned long flags;
2132 struct mmc_request *mrq;
2133
2134 host = (struct sdhci_host*)param;
2135
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002136 spin_lock_irqsave(&host->lock, flags);
2137
Chris Ball0c9c99a2011-04-27 17:35:31 -04002138 /*
2139 * If this tasklet gets rescheduled while running, it will
2140 * be run again afterwards but without any active request.
2141 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002142 if (!host->mrq) {
2143 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002144 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002145 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002146
2147 del_timer(&host->timer);
2148
2149 mrq = host->mrq;
2150
Pierre Ossmand129bce2006-03-24 03:18:17 -08002151 /*
2152 * The controller needs a reset of internal state machines
2153 * upon error conditions.
2154 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002155 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002156 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002157 (mrq->sbc && mrq->sbc->error) ||
2158 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2159 (mrq->data->stop && mrq->data->stop->error))) ||
2160 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002161
2162 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002163 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002164 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002165 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002166
2167 /* Spec says we should do both at the same time, but Ricoh
2168 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002169 sdhci_do_reset(host, SDHCI_RESET_CMD);
2170 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002171 }
2172
2173 host->mrq = NULL;
2174 host->cmd = NULL;
2175 host->data = NULL;
2176
Pierre Ossmanf9134312008-12-21 17:01:48 +01002177#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002178 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002179#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002180
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 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002185 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002186}
2187
2188static void sdhci_timeout_timer(unsigned long data)
2189{
2190 struct sdhci_host *host;
2191 unsigned long flags;
2192
2193 host = (struct sdhci_host*)data;
2194
2195 spin_lock_irqsave(&host->lock, flags);
2196
2197 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302198 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002199 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002200 sdhci_dumpregs(host);
2201
2202 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002203 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002204 sdhci_finish_data(host);
2205 } else {
2206 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002207 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002208 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002209 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002210
2211 tasklet_schedule(&host->finish_tasklet);
2212 }
2213 }
2214
Pierre Ossman5f25a662006-10-04 02:15:39 -07002215 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002216 spin_unlock_irqrestore(&host->lock, flags);
2217}
2218
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302219static void sdhci_tuning_timer(unsigned long data)
2220{
2221 struct sdhci_host *host;
2222 unsigned long flags;
2223
2224 host = (struct sdhci_host *)data;
2225
2226 spin_lock_irqsave(&host->lock, flags);
2227
2228 host->flags |= SDHCI_NEEDS_RETUNING;
2229
2230 spin_unlock_irqrestore(&host->lock, flags);
2231}
2232
Pierre Ossmand129bce2006-03-24 03:18:17 -08002233/*****************************************************************************\
2234 * *
2235 * Interrupt handling *
2236 * *
2237\*****************************************************************************/
2238
Adrian Hunter61541392014-09-24 10:27:27 +03002239static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002240{
2241 BUG_ON(intmask == 0);
2242
2243 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302244 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002245 "though no command operation was in progress.\n",
2246 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002247 sdhci_dumpregs(host);
2248 return;
2249 }
2250
Pierre Ossman43b58b32007-07-25 23:15:27 +02002251 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002252 host->cmd->error = -ETIMEDOUT;
2253 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2254 SDHCI_INT_INDEX))
2255 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002256
Pierre Ossmane8095172008-07-25 01:09:08 +02002257 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002258 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002259 return;
2260 }
2261
2262 /*
2263 * The host can send and interrupt when the busy state has
2264 * ended, allowing us to wait without wasting CPU cycles.
2265 * Unfortunately this is overloaded on the "data complete"
2266 * interrupt, so we need to take some care when handling
2267 * it.
2268 *
2269 * Note: The 1.0 specification is a bit ambiguous about this
2270 * feature so there might be some problems with older
2271 * controllers.
2272 */
2273 if (host->cmd->flags & MMC_RSP_BUSY) {
2274 if (host->cmd->data)
2275 DBG("Cannot wait for busy signal when also "
2276 "doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002277 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2278 && !host->busy_handle) {
2279 /* Mark that command complete before busy is ended */
2280 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002281 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002282 }
Ben Dooksf9454052009-02-20 20:33:08 +03002283
2284 /* The controller does not support the end-of-busy IRQ,
2285 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002286 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2287 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2288 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002289 }
2290
2291 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002292 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002293}
2294
George G. Davis0957c332010-02-18 12:32:12 -05002295#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002296static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002297{
2298 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002299 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002300 __le32 *dma;
2301 __le16 *len;
2302 u8 attr;
2303
2304 sdhci_dumpregs(host);
2305
2306 while (true) {
2307 dma = (__le32 *)(desc + 4);
2308 len = (__le16 *)(desc + 2);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002309 attr = *(u8 *)desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002310
2311 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2312 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2313
Adrian Hunter76fe3792014-11-04 12:42:42 +02002314 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002315
Adrian Hunter739d46d2014-11-04 12:42:44 +02002316 if (attr & ADMA2_END)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002317 break;
2318 }
2319}
2320#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002321static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002322#endif
2323
Pierre Ossmand129bce2006-03-24 03:18:17 -08002324static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2325{
Girish K S069c9f12012-01-06 09:56:39 +05302326 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002327 BUG_ON(intmask == 0);
2328
Arindam Nathb513ea22011-05-05 12:19:04 +05302329 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2330 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302331 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2332 if (command == MMC_SEND_TUNING_BLOCK ||
2333 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302334 host->tuning_done = 1;
2335 wake_up(&host->buf_ready_int);
2336 return;
2337 }
2338 }
2339
Pierre Ossmand129bce2006-03-24 03:18:17 -08002340 if (!host->data) {
2341 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002342 * The "data complete" interrupt is also used to
2343 * indicate that a busy state has ended. See comment
2344 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002345 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002346 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002347 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2348 host->cmd->error = -ETIMEDOUT;
2349 tasklet_schedule(&host->finish_tasklet);
2350 return;
2351 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002352 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002353 /*
2354 * Some cards handle busy-end interrupt
2355 * before the command completed, so make
2356 * sure we do things in the proper order.
2357 */
2358 if (host->busy_handle)
2359 sdhci_finish_command(host);
2360 else
2361 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002362 return;
2363 }
2364 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002365
Girish K Sa3c76eb2011-10-11 11:44:09 +05302366 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002367 "though no data operation was in progress.\n",
2368 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002369 sdhci_dumpregs(host);
2370
2371 return;
2372 }
2373
2374 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002375 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002376 else if (intmask & SDHCI_INT_DATA_END_BIT)
2377 host->data->error = -EILSEQ;
2378 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2379 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2380 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002381 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002382 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302383 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002384 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002385 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002386 if (host->ops->adma_workaround)
2387 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002388 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002389
Pierre Ossman17b04292007-07-22 22:18:46 +02002390 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002391 sdhci_finish_data(host);
2392 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002393 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002394 sdhci_transfer_pio(host);
2395
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002396 /*
2397 * We currently don't do anything fancy with DMA
2398 * boundaries, but as we can't disable the feature
2399 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002400 *
2401 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2402 * should return a valid address to continue from, but as
2403 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002404 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002405 if (intmask & SDHCI_INT_DMA_END) {
2406 u32 dmastart, dmanow;
2407 dmastart = sg_dma_address(host->data->sg);
2408 dmanow = dmastart + host->data->bytes_xfered;
2409 /*
2410 * Force update to the next DMA block boundary.
2411 */
2412 dmanow = (dmanow &
2413 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2414 SDHCI_DEFAULT_BOUNDARY_SIZE;
2415 host->data->bytes_xfered = dmanow - dmastart;
2416 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2417 " next 0x%08x\n",
2418 mmc_hostname(host->mmc), dmastart,
2419 host->data->bytes_xfered, dmanow);
2420 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2421 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002422
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002423 if (intmask & SDHCI_INT_DATA_END) {
2424 if (host->cmd) {
2425 /*
2426 * Data managed to finish before the
2427 * command completed. Make sure we do
2428 * things in the proper order.
2429 */
2430 host->data_early = 1;
2431 } else {
2432 sdhci_finish_data(host);
2433 }
2434 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002435 }
2436}
2437
David Howells7d12e782006-10-05 14:55:46 +01002438static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002439{
Russell King781e9892014-04-25 12:55:46 +01002440 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002441 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002442 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002443 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002444
2445 spin_lock(&host->lock);
2446
Russell Kingbe138552014-04-25 12:55:56 +01002447 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002448 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002449 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002450 }
2451
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002452 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002453 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002454 result = IRQ_NONE;
2455 goto out;
2456 }
2457
Russell King41005002014-04-25 12:55:36 +01002458 do {
2459 /* Clear selected interrupts. */
2460 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2461 SDHCI_INT_BUS_POWER);
2462 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002463
Russell King41005002014-04-25 12:55:36 +01002464 DBG("*** %s got interrupt: 0x%08x\n",
2465 mmc_hostname(host->mmc), intmask);
2466
2467 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2468 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2469 SDHCI_CARD_PRESENT;
2470
2471 /*
2472 * There is a observation on i.mx esdhc. INSERT
2473 * bit will be immediately set again when it gets
2474 * cleared, if a card is inserted. We have to mask
2475 * the irq to prevent interrupt storm which will
2476 * freeze the system. And the REMOVE gets the
2477 * same situation.
2478 *
2479 * More testing are needed here to ensure it works
2480 * for other platforms though.
2481 */
Russell Kingb537f942014-04-25 12:56:01 +01002482 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2483 SDHCI_INT_CARD_REMOVE);
2484 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2485 SDHCI_INT_CARD_INSERT;
2486 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2487 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002488
2489 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2490 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002491
2492 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2493 SDHCI_INT_CARD_REMOVE);
2494 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002495 }
2496
2497 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002498 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2499 &intmask);
Russell King41005002014-04-25 12:55:36 +01002500
2501 if (intmask & SDHCI_INT_DATA_MASK)
2502 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2503
2504 if (intmask & SDHCI_INT_BUS_POWER)
2505 pr_err("%s: Card is consuming too much power!\n",
2506 mmc_hostname(host->mmc));
2507
Russell King781e9892014-04-25 12:55:46 +01002508 if (intmask & SDHCI_INT_CARD_INT) {
2509 sdhci_enable_sdio_irq_nolock(host, false);
2510 host->thread_isr |= SDHCI_INT_CARD_INT;
2511 result = IRQ_WAKE_THREAD;
2512 }
Russell King41005002014-04-25 12:55:36 +01002513
2514 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2515 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2516 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2517 SDHCI_INT_CARD_INT);
2518
2519 if (intmask) {
2520 unexpected |= intmask;
2521 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2522 }
2523
Russell King781e9892014-04-25 12:55:46 +01002524 if (result == IRQ_NONE)
2525 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002526
2527 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002528 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002529out:
2530 spin_unlock(&host->lock);
2531
Alexander Stein6379b232012-03-14 09:52:10 +01002532 if (unexpected) {
2533 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2534 mmc_hostname(host->mmc), unexpected);
2535 sdhci_dumpregs(host);
2536 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002537
Pierre Ossmand129bce2006-03-24 03:18:17 -08002538 return result;
2539}
2540
Russell King781e9892014-04-25 12:55:46 +01002541static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2542{
2543 struct sdhci_host *host = dev_id;
2544 unsigned long flags;
2545 u32 isr;
2546
2547 spin_lock_irqsave(&host->lock, flags);
2548 isr = host->thread_isr;
2549 host->thread_isr = 0;
2550 spin_unlock_irqrestore(&host->lock, flags);
2551
Russell King3560db82014-04-25 12:55:51 +01002552 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2553 sdhci_card_event(host->mmc);
2554 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2555 }
2556
Russell King781e9892014-04-25 12:55:46 +01002557 if (isr & SDHCI_INT_CARD_INT) {
2558 sdio_run_irqs(host->mmc);
2559
2560 spin_lock_irqsave(&host->lock, flags);
2561 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2562 sdhci_enable_sdio_irq_nolock(host, true);
2563 spin_unlock_irqrestore(&host->lock, flags);
2564 }
2565
2566 return isr ? IRQ_HANDLED : IRQ_NONE;
2567}
2568
Pierre Ossmand129bce2006-03-24 03:18:17 -08002569/*****************************************************************************\
2570 * *
2571 * Suspend/resume *
2572 * *
2573\*****************************************************************************/
2574
2575#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002576void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2577{
2578 u8 val;
2579 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2580 | SDHCI_WAKE_ON_INT;
2581
2582 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2583 val |= mask ;
2584 /* Avoid fake wake up */
2585 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2586 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2587 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2588}
2589EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2590
Fabio Estevam0b10f472014-08-30 14:53:13 -03002591static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002592{
2593 u8 val;
2594 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2595 | SDHCI_WAKE_ON_INT;
2596
2597 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2598 val &= ~mask;
2599 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2600}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002601
Manuel Lauss29495aa2011-11-03 11:09:45 +01002602int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002603{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002604 sdhci_disable_card_detection(host);
2605
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302606 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002607 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002608 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302609 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302610 }
2611
Kevin Liuad080d72013-01-05 17:21:33 +08002612 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002613 host->ier = 0;
2614 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2615 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002616 free_irq(host->irq, host);
2617 } else {
2618 sdhci_enable_irq_wakeups(host);
2619 enable_irq_wake(host->irq);
2620 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002621 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002622}
2623
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002624EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002625
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002626int sdhci_resume_host(struct sdhci_host *host)
2627{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002628 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002629
Richard Röjforsa13abc72009-09-22 16:45:30 -07002630 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002631 if (host->ops->enable_dma)
2632 host->ops->enable_dma(host);
2633 }
2634
Kevin Liuad080d72013-01-05 17:21:33 +08002635 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell King781e9892014-04-25 12:55:46 +01002636 ret = request_threaded_irq(host->irq, sdhci_irq,
2637 sdhci_thread_irq, IRQF_SHARED,
2638 mmc_hostname(host->mmc), host);
Kevin Liuad080d72013-01-05 17:21:33 +08002639 if (ret)
2640 return ret;
2641 } else {
2642 sdhci_disable_irq_wakeups(host);
2643 disable_irq_wake(host->irq);
2644 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002645
Adrian Hunter6308d292012-02-07 14:48:54 +02002646 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2647 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2648 /* Card keeps power but host controller does not */
2649 sdhci_init(host, 0);
2650 host->pwr = 0;
2651 host->clock = 0;
2652 sdhci_do_set_ios(host, &host->mmc->ios);
2653 } else {
2654 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2655 mmiowb();
2656 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002657
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002658 sdhci_enable_card_detection(host);
2659
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302660 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002661 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302662 host->flags |= SDHCI_NEEDS_RETUNING;
2663
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002664 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002665}
2666
2667EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002668#endif /* CONFIG_PM */
2669
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002670#ifdef CONFIG_PM_RUNTIME
2671
2672static int sdhci_runtime_pm_get(struct sdhci_host *host)
2673{
2674 return pm_runtime_get_sync(host->mmc->parent);
2675}
2676
2677static int sdhci_runtime_pm_put(struct sdhci_host *host)
2678{
2679 pm_runtime_mark_last_busy(host->mmc->parent);
2680 return pm_runtime_put_autosuspend(host->mmc->parent);
2681}
2682
Adrian Hunterf0710a52013-05-06 12:17:32 +03002683static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2684{
2685 if (host->runtime_suspended || host->bus_on)
2686 return;
2687 host->bus_on = true;
2688 pm_runtime_get_noresume(host->mmc->parent);
2689}
2690
2691static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2692{
2693 if (host->runtime_suspended || !host->bus_on)
2694 return;
2695 host->bus_on = false;
2696 pm_runtime_put_noidle(host->mmc->parent);
2697}
2698
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002699int sdhci_runtime_suspend_host(struct sdhci_host *host)
2700{
2701 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002702
2703 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002704 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002705 del_timer_sync(&host->tuning_timer);
2706 host->flags &= ~SDHCI_NEEDS_RETUNING;
2707 }
2708
2709 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002710 host->ier &= SDHCI_INT_CARD_INT;
2711 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2712 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002713 spin_unlock_irqrestore(&host->lock, flags);
2714
Russell King781e9892014-04-25 12:55:46 +01002715 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002716
2717 spin_lock_irqsave(&host->lock, flags);
2718 host->runtime_suspended = true;
2719 spin_unlock_irqrestore(&host->lock, flags);
2720
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002721 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002722}
2723EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2724
2725int sdhci_runtime_resume_host(struct sdhci_host *host)
2726{
2727 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002728 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002729
2730 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2731 if (host->ops->enable_dma)
2732 host->ops->enable_dma(host);
2733 }
2734
2735 sdhci_init(host, 0);
2736
2737 /* Force clock and power re-program */
2738 host->pwr = 0;
2739 host->clock = 0;
2740 sdhci_do_set_ios(host, &host->mmc->ios);
2741
2742 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002743 if ((host_flags & SDHCI_PV_ENABLED) &&
2744 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2745 spin_lock_irqsave(&host->lock, flags);
2746 sdhci_enable_preset_value(host, true);
2747 spin_unlock_irqrestore(&host->lock, flags);
2748 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002749
2750 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002751 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002752 host->flags |= SDHCI_NEEDS_RETUNING;
2753
2754 spin_lock_irqsave(&host->lock, flags);
2755
2756 host->runtime_suspended = false;
2757
2758 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002759 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002760 sdhci_enable_sdio_irq_nolock(host, true);
2761
2762 /* Enable Card Detection */
2763 sdhci_enable_card_detection(host);
2764
2765 spin_unlock_irqrestore(&host->lock, flags);
2766
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002767 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002768}
2769EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2770
2771#endif
2772
Pierre Ossmand129bce2006-03-24 03:18:17 -08002773/*****************************************************************************\
2774 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002775 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002776 * *
2777\*****************************************************************************/
2778
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002779struct sdhci_host *sdhci_alloc_host(struct device *dev,
2780 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002781{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002782 struct mmc_host *mmc;
2783 struct sdhci_host *host;
2784
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002785 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002786
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002787 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002788 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002789 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002790
2791 host = mmc_priv(mmc);
2792 host->mmc = mmc;
2793
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002794 return host;
2795}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002796
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002797EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002798
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002799int sdhci_add_host(struct sdhci_host *host)
2800{
2801 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002802 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302803 u32 max_current_caps;
2804 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002805 unsigned int override_timeout_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002806 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002807
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002808 WARN_ON(host == NULL);
2809 if (host == NULL)
2810 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002811
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002812 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002813
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002814 if (debug_quirks)
2815 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002816 if (debug_quirks2)
2817 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002818
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002819 override_timeout_clk = host->timeout_clk;
2820
Russell King03231f92014-04-25 12:57:12 +01002821 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002822
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002823 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002824 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2825 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002826 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302827 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002828 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002829 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002830 }
2831
Arindam Nathf2119df2011-05-05 12:18:57 +05302832 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002833 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002834
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002835 if (host->version >= SDHCI_SPEC_300)
2836 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2837 host->caps1 :
2838 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302839
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002840 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002841 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302842 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002843 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002844 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002845 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002846
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002847 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002848 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002849 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002850 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002851 }
2852
Arindam Nathf2119df2011-05-05 12:18:57 +05302853 if ((host->version >= SDHCI_SPEC_200) &&
2854 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002855 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002856
2857 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2858 (host->flags & SDHCI_USE_ADMA)) {
2859 DBG("Disabling ADMA as it is marked broken\n");
2860 host->flags &= ~SDHCI_USE_ADMA;
2861 }
2862
Richard Röjforsa13abc72009-09-22 16:45:30 -07002863 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002864 if (host->ops->enable_dma) {
2865 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07002866 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002867 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002868 host->flags &=
2869 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002870 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002871 }
2872 }
2873
Pierre Ossman2134a922008-06-28 18:28:51 +02002874 if (host->flags & SDHCI_USE_ADMA) {
2875 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002876 * The DMA descriptor table size is calculated as the maximum
2877 * number of segments times 2, to allow for an alignment
2878 * descriptor for each segment, plus 1 for a nop end descriptor,
2879 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002880 */
Adrian Hunter739d46d2014-11-04 12:42:44 +02002881 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2882 SDHCI_ADMA2_32_DESC_SZ;
2883 host->align_buffer_sz = SDHCI_MAX_SEGS *
2884 SDHCI_ADMA2_32_ALIGN;
2885 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
2886 host->align_sz = SDHCI_ADMA2_32_ALIGN;
2887 host->align_mask = SDHCI_ADMA2_32_ALIGN - 1;
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002888 host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +02002889 host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002890 &host->adma_addr,
2891 GFP_KERNEL);
Adrian Hunter76fe3792014-11-04 12:42:42 +02002892 host->align_buffer = kmalloc(host->align_buffer_sz, GFP_KERNEL);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002893 if (!host->adma_table || !host->align_buffer) {
Adrian Hunter76fe3792014-11-04 12:42:42 +02002894 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002895 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02002896 kfree(host->align_buffer);
Joe Perches66061102014-09-12 14:56:56 -07002897 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002898 mmc_hostname(mmc));
2899 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002900 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01002901 host->align_buffer = NULL;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002902 } else if (host->adma_addr & host->align_mask) {
Joe Perches66061102014-09-12 14:56:56 -07002903 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2904 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002905 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002906 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002907 host->adma_table, host->adma_addr);
Russell Kingd1e49f72014-04-25 12:58:34 +01002908 kfree(host->align_buffer);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002909 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01002910 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02002911 }
2912 }
2913
Pierre Ossman76591502008-07-21 00:32:11 +02002914 /*
2915 * If we use DMA, then it's up to the caller to set the DMA
2916 * mask, but PIO does not need the hw shim so we set a new
2917 * mask here in that case.
2918 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002919 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002920 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07002921 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02002922 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002923
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002924 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302925 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002926 >> SDHCI_CLOCK_BASE_SHIFT;
2927 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302928 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002929 >> SDHCI_CLOCK_BASE_SHIFT;
2930
Pierre Ossmand129bce2006-03-24 03:18:17 -08002931 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002932 if (host->max_clk == 0 || host->quirks &
2933 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002934 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302935 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002936 "frequency.\n", mmc_hostname(mmc));
2937 return -ENODEV;
2938 }
2939 host->max_clk = host->ops->get_max_clock(host);
2940 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002941
2942 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302943 * In case of Host Controller v3.00, find out whether clock
2944 * multiplier is supported.
2945 */
2946 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2947 SDHCI_CLOCK_MUL_SHIFT;
2948
2949 /*
2950 * In case the value in Clock Multiplier is 0, then programmable
2951 * clock mode is not supported, otherwise the actual clock
2952 * multiplier is one more than the value of Clock Multiplier
2953 * in the Capabilities Register.
2954 */
2955 if (host->clk_mul)
2956 host->clk_mul += 1;
2957
2958 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002959 * Set host parameters.
2960 */
2961 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302962 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002963 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002964 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302965 else if (host->version >= SDHCI_SPEC_300) {
2966 if (host->clk_mul) {
2967 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2968 mmc->f_max = host->max_clk * host->clk_mul;
2969 } else
2970 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2971 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002972 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002973
Aisheng Dong28aab052014-08-27 15:26:31 +08002974 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2975 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
2976 SDHCI_TIMEOUT_CLK_SHIFT;
2977 if (host->timeout_clk == 0) {
2978 if (host->ops->get_timeout_clock) {
2979 host->timeout_clk =
2980 host->ops->get_timeout_clock(host);
2981 } else {
2982 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
2983 mmc_hostname(mmc));
2984 return -ENODEV;
2985 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03002986 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03002987
Aisheng Dong28aab052014-08-27 15:26:31 +08002988 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2989 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002990
Aisheng Dong28aab052014-08-27 15:26:31 +08002991 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08002992 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08002993 mmc->max_busy_timeout /= host->timeout_clk;
2994 }
Adrian Hunter58d12462011-06-28 17:16:03 +03002995
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002996 if (override_timeout_clk)
2997 host->timeout_clk = override_timeout_clk;
2998
Andrei Warkentine89d4562011-05-23 15:06:37 -05002999 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003000 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003001
3002 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3003 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003004
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003005 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003006 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003007 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003008 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003009 host->flags |= SDHCI_AUTO_CMD23;
3010 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3011 } else {
3012 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3013 }
3014
Philip Rakity15ec4462010-11-19 16:48:39 -05003015 /*
3016 * A controller may support 8-bit width, but the board itself
3017 * might not have the pins brought out. Boards that support
3018 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3019 * their platform code before calling sdhci_add_host(), and we
3020 * won't assume 8-bit width for hosts without that CAP.
3021 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003022 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003023 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003024
Jerry Huang63ef5d82012-10-25 13:47:19 +08003025 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3026 mmc->caps &= ~MMC_CAP_CMD23;
3027
Arindam Nathf2119df2011-05-05 12:18:57 +05303028 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003029 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003030
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003031 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Markus Mayer4e743f12014-07-03 13:27:42 -07003032 !(mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003033 mmc->caps |= MMC_CAP_NEEDS_POLL;
3034
Tim Kryger3a48edc2014-06-13 10:13:56 -07003035 /* If there are external regulators, get them */
3036 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3037 return -EPROBE_DEFER;
3038
Philip Rakity6231f3d2012-07-23 15:56:23 -07003039 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003040 if (!IS_ERR(mmc->supply.vqmmc)) {
3041 ret = regulator_enable(mmc->supply.vqmmc);
3042 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3043 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003044 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3045 SDHCI_SUPPORT_SDR50 |
3046 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003047 if (ret) {
3048 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3049 mmc_hostname(mmc), ret);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003050 mmc->supply.vqmmc = NULL;
Chris Balla3361ab2013-03-11 17:51:53 -04003051 }
Kevin Liu8363c372012-11-17 17:55:51 -05003052 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003053
Daniel Drake6a661802012-11-25 13:01:19 -05003054 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3055 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3056 SDHCI_SUPPORT_DDR50);
3057
Al Cooper4188bba2012-03-16 15:54:17 -04003058 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3059 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3060 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303061 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3062
3063 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003064 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303065 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003066 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3067 * field can be promoted to support HS200.
3068 */
Chuanxiao.Dongadc82852014-08-19 11:02:41 +08003069 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200)) {
David Cohen13868bf2013-10-29 10:58:26 -07003070 mmc->caps2 |= MMC_CAP2_HS200;
Chuanxiao.Dongadc82852014-08-19 11:02:41 +08003071 if (IS_ERR(mmc->supply.vqmmc) ||
3072 !regulator_is_supported_voltage
3073 (mmc->supply.vqmmc, 1100000, 1300000))
3074 mmc->caps2 &= ~MMC_CAP2_HS200_1_2V_SDR;
3075 }
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003076 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303077 mmc->caps |= MMC_CAP_UHS_SDR50;
3078
Micky Ching9107ebb2014-02-21 18:40:35 +08003079 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3080 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303081 mmc->caps |= MMC_CAP_UHS_DDR50;
3082
Girish K S069c9f12012-01-06 09:56:39 +05303083 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303084 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3085 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3086
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003087 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303088 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003089 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303090
Arindam Nathd6d50a12011-05-05 12:18:59 +05303091 /* Driver Type(s) (A, C, D) supported by the host */
3092 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3093 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3094 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3095 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3096 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3097 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3098
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303099 /* Initial value for re-tuning timer count */
3100 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3101 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3102
3103 /*
3104 * In case Re-tuning Timer is not disabled, the actual value of
3105 * re-tuning timer will be 2 ^ (n - 1).
3106 */
3107 if (host->tuning_count)
3108 host->tuning_count = 1 << (host->tuning_count - 1);
3109
3110 /* Re-tuning mode supported by the Host Controller */
3111 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3112 SDHCI_RETUNING_MODE_SHIFT;
3113
Takashi Iwai8f230f42010-12-08 10:04:30 +01003114 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003115
Arindam Nathf2119df2011-05-05 12:18:57 +05303116 /*
3117 * According to SD Host Controller spec v3.00, if the Host System
3118 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3119 * the value is meaningful only if Voltage Support in the Capabilities
3120 * register is set. The actual current value is 4 times the register
3121 * value.
3122 */
3123 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003124 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003125 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003126 if (curr > 0) {
3127
3128 /* convert to SDHCI_MAX_CURRENT format */
3129 curr = curr/1000; /* convert to mA */
3130 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3131
3132 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3133 max_current_caps =
3134 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3135 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3136 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3137 }
3138 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303139
3140 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003141 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303142
Aaron Lu55c46652012-07-04 13:31:48 +08003143 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303144 SDHCI_MAX_CURRENT_330_MASK) >>
3145 SDHCI_MAX_CURRENT_330_SHIFT) *
3146 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303147 }
3148 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003149 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303150
Aaron Lu55c46652012-07-04 13:31:48 +08003151 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303152 SDHCI_MAX_CURRENT_300_MASK) >>
3153 SDHCI_MAX_CURRENT_300_SHIFT) *
3154 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303155 }
3156 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003157 ocr_avail |= MMC_VDD_165_195;
3158
Aaron Lu55c46652012-07-04 13:31:48 +08003159 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303160 SDHCI_MAX_CURRENT_180_MASK) >>
3161 SDHCI_MAX_CURRENT_180_SHIFT) *
3162 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303163 }
3164
Tim Kryger52221612014-06-25 00:25:34 -07003165 /* If OCR set by external regulators, use it instead */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003166 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003167 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003168
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003169 if (host->ocr_mask)
Tim Kryger3a48edc2014-06-13 10:13:56 -07003170 ocr_avail &= host->ocr_mask;
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003171
Takashi Iwai8f230f42010-12-08 10:04:30 +01003172 mmc->ocr_avail = ocr_avail;
3173 mmc->ocr_avail_sdio = ocr_avail;
3174 if (host->ocr_avail_sdio)
3175 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3176 mmc->ocr_avail_sd = ocr_avail;
3177 if (host->ocr_avail_sd)
3178 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3179 else /* normal SD controllers don't support 1.8V */
3180 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3181 mmc->ocr_avail_mmc = ocr_avail;
3182 if (host->ocr_avail_mmc)
3183 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003184
3185 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303186 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003187 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003188 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003189 }
3190
Pierre Ossmand129bce2006-03-24 03:18:17 -08003191 spin_lock_init(&host->lock);
3192
3193 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003194 * Maximum number of segments. Depends on if the hardware
3195 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003196 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003197 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003198 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003199 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003200 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003201 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003202 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003203
3204 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003205 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01003206 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08003207 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003208 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003209
3210 /*
3211 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003212 * of bytes. When doing hardware scatter/gather, each entry cannot
3213 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003214 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003215 if (host->flags & SDHCI_USE_ADMA) {
3216 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3217 mmc->max_seg_size = 65535;
3218 else
3219 mmc->max_seg_size = 65536;
3220 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003221 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003222 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003223
3224 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003225 * Maximum block size. This varies from controller to controller and
3226 * is specified in the capabilities register.
3227 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003228 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3229 mmc->max_blk_size = 2;
3230 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303231 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003232 SDHCI_MAX_BLOCK_SHIFT;
3233 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003234 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3235 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003236 mmc->max_blk_size = 0;
3237 }
3238 }
3239
3240 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003241
3242 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003243 * Maximum block count.
3244 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003245 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003246
3247 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003248 * Init tasklets.
3249 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003250 tasklet_init(&host->finish_tasklet,
3251 sdhci_tasklet_finish, (unsigned long)host);
3252
Al Viroe4cad1b2006-10-10 22:47:07 +01003253 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003254
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303255 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303256 init_waitqueue_head(&host->buf_ready_int);
3257
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303258 /* Initialize re-tuning timer */
3259 init_timer(&host->tuning_timer);
3260 host->tuning_timer.data = (unsigned long)host;
3261 host->tuning_timer.function = sdhci_tuning_timer;
3262 }
3263
Shawn Guo2af502c2013-07-05 14:38:55 +08003264 sdhci_init(host, 0);
3265
Russell King781e9892014-04-25 12:55:46 +01003266 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3267 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003268 if (ret) {
3269 pr_err("%s: Failed to request IRQ %d: %d\n",
3270 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003271 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003272 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003273
Pierre Ossmand129bce2006-03-24 03:18:17 -08003274#ifdef CONFIG_MMC_DEBUG
3275 sdhci_dumpregs(host);
3276#endif
3277
Pierre Ossmanf9134312008-12-21 17:01:48 +01003278#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003279 snprintf(host->led_name, sizeof(host->led_name),
3280 "%s::", mmc_hostname(mmc));
3281 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003282 host->led.brightness = LED_OFF;
3283 host->led.default_trigger = mmc_hostname(mmc);
3284 host->led.brightness_set = sdhci_led_control;
3285
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003286 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003287 if (ret) {
3288 pr_err("%s: Failed to register LED device: %d\n",
3289 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003290 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003291 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003292#endif
3293
Pierre Ossman5f25a662006-10-04 02:15:39 -07003294 mmiowb();
3295
Pierre Ossmand129bce2006-03-24 03:18:17 -08003296 mmc_add_host(mmc);
3297
Girish K Sa3c76eb2011-10-11 11:44:09 +05303298 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003299 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003300 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3301 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003302
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003303 sdhci_enable_card_detection(host);
3304
Pierre Ossmand129bce2006-03-24 03:18:17 -08003305 return 0;
3306
Pierre Ossmanf9134312008-12-21 17:01:48 +01003307#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003308reset:
Russell King03231f92014-04-25 12:57:12 +01003309 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003310 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3311 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003312 free_irq(host->irq, host);
3313#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003314untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003315 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003316
3317 return ret;
3318}
3319
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003320EXPORT_SYMBOL_GPL(sdhci_add_host);
3321
Pierre Ossman1e728592008-04-16 19:13:13 +02003322void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003323{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003324 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003325 unsigned long flags;
3326
3327 if (dead) {
3328 spin_lock_irqsave(&host->lock, flags);
3329
3330 host->flags |= SDHCI_DEVICE_DEAD;
3331
3332 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303333 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003334 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003335
3336 host->mrq->cmd->error = -ENOMEDIUM;
3337 tasklet_schedule(&host->finish_tasklet);
3338 }
3339
3340 spin_unlock_irqrestore(&host->lock, flags);
3341 }
3342
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003343 sdhci_disable_card_detection(host);
3344
Markus Mayer4e743f12014-07-03 13:27:42 -07003345 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003346
Pierre Ossmanf9134312008-12-21 17:01:48 +01003347#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003348 led_classdev_unregister(&host->led);
3349#endif
3350
Pierre Ossman1e728592008-04-16 19:13:13 +02003351 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003352 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003353
Russell Kingb537f942014-04-25 12:56:01 +01003354 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3355 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003356 free_irq(host->irq, host);
3357
3358 del_timer_sync(&host->timer);
3359
Pierre Ossmand129bce2006-03-24 03:18:17 -08003360 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003361
Tim Kryger3a48edc2014-06-13 10:13:56 -07003362 if (!IS_ERR(mmc->supply.vqmmc))
3363 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003364
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003365 if (host->adma_table)
Adrian Hunter76fe3792014-11-04 12:42:42 +02003366 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003367 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003368 kfree(host->align_buffer);
3369
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003370 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003371 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003372}
3373
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003374EXPORT_SYMBOL_GPL(sdhci_remove_host);
3375
3376void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003377{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003378 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003379}
3380
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003381EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003382
3383/*****************************************************************************\
3384 * *
3385 * Driver init/exit *
3386 * *
3387\*****************************************************************************/
3388
3389static int __init sdhci_drv_init(void)
3390{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303391 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003392 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303393 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003394
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003395 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003396}
3397
3398static void __exit sdhci_drv_exit(void)
3399{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003400}
3401
3402module_init(sdhci_drv_init);
3403module_exit(sdhci_drv_exit);
3404
Pierre Ossmandf673b22006-06-30 02:22:31 -07003405module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003406module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003407
Pierre Ossman32710e82009-04-08 20:14:54 +02003408MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003409MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003410MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003411
Pierre Ossmandf673b22006-06-30 02:22:31 -07003412MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003413MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");