blob: 97c10134768fcdd314e95f9188a21a61f295f9c9 [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 Hunter50accb92011-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>
Pierre Ossmand129bce2006-03-24 03:18:17 -080030
Pierre Ossmand129bce2006-03-24 03:18:17 -080031#include "sdhci.h"
32
33#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080034
Pierre Ossmand129bce2006-03-24 03:18:17 -080035#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010036 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmanf9134312008-12-21 17:01:48 +010038#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
39 defined(CONFIG_MMC_SDHCI_MODULE))
40#define SDHCI_USE_LEDS_CLASS
41#endif
42
Arindam Nathb513ea22011-05-05 12:19:04 +053043#define MAX_TUNING_LOOP 40
44
Pierre Ossmandf673b22006-06-30 02:22:31 -070045static unsigned int debug_quirks = 0;
Adrian Hunter50accb92011-10-03 15:33:34 +030046static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070047
Pierre Ossmand129bce2006-03-24 03:18:17 -080048static void sdhci_finish_data(struct sdhci_host *);
49
50static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
51static void sdhci_finish_command(struct sdhci_host *);
Girish K S2cd06dc2012-01-06 09:56:39 +053052static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053053static void sdhci_tuning_timer(unsigned long data);
Pierre Ossmand129bce2006-03-24 03:18:17 -080054
Adrian Hunter50accb92011-10-03 15:33:34 +030055#ifdef CONFIG_PM_RUNTIME
56static int sdhci_runtime_pm_get(struct sdhci_host *host);
57static int sdhci_runtime_pm_put(struct sdhci_host *host);
58#else
59static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
60{
61 return 0;
62}
63static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
64{
65 return 0;
66}
67#endif
68
Pierre Ossmand129bce2006-03-24 03:18:17 -080069static void sdhci_dumpregs(struct sdhci_host *host)
70{
Sahitya Tummalaca422112013-02-22 12:15:54 +053071 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070072 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080073
Sahitya Tummalaca422112013-02-22 12:15:54 +053074 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030075 sdhci_readl(host, SDHCI_DMA_ADDRESS),
76 sdhci_readw(host, SDHCI_HOST_VERSION));
Sahitya Tummalaca422112013-02-22 12:15:54 +053077 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030078 sdhci_readw(host, SDHCI_BLOCK_SIZE),
79 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Sahitya Tummalaca422112013-02-22 12:15:54 +053080 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030081 sdhci_readl(host, SDHCI_ARGUMENT),
82 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Sahitya Tummalaca422112013-02-22 12:15:54 +053083 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030084 sdhci_readl(host, SDHCI_PRESENT_STATE),
85 sdhci_readb(host, SDHCI_HOST_CONTROL));
Sahitya Tummalaca422112013-02-22 12:15:54 +053086 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030087 sdhci_readb(host, SDHCI_POWER_CONTROL),
88 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Sahitya Tummalaca422112013-02-22 12:15:54 +053089 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030090 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
91 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Sahitya Tummalaca422112013-02-22 12:15:54 +053092 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030093 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
94 sdhci_readl(host, SDHCI_INT_STATUS));
Sahitya Tummalaca422112013-02-22 12:15:54 +053095 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030096 sdhci_readl(host, SDHCI_INT_ENABLE),
97 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Sahitya Tummalaca422112013-02-22 12:15:54 +053098 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030099 sdhci_readw(host, SDHCI_ACMD12_ERR),
100 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Sahitya Tummalaca422112013-02-22 12:15:54 +0530101 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300102 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500103 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Sahitya Tummalaca422112013-02-22 12:15:54 +0530104 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500105 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readl(host, SDHCI_MAX_CURRENT));
Sahitya Tummalaca422112013-02-22 12:15:54 +0530107 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530108 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800109
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100110 if (host->flags & SDHCI_USE_ADMA)
Sahitya Tummalaca422112013-02-22 12:15:54 +0530111 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100112 readl(host->ioaddr + SDHCI_ADMA_ERROR),
113 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
114
Sahitya Tummalaca422112013-02-22 12:15:54 +0530115 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800116}
117
118/*****************************************************************************\
119 * *
120 * Low level functions *
121 * *
122\*****************************************************************************/
123
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300124static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
125{
126 u32 ier;
127
128 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
129 ier &= ~clear;
130 ier |= set;
131 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
132 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
133}
134
135static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
136{
137 sdhci_clear_set_irqs(host, 0, irqs);
138}
139
140static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
141{
142 sdhci_clear_set_irqs(host, irqs, 0);
143}
144
145static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
146{
Sahitya Tummalaca422112013-02-22 12:15:54 +0530147 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300148
Adrian Hunterc79396c2011-12-27 15:48:42 +0200149 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100150 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300151 return;
152
Sahitya Tummalaca422112013-02-22 12:15:54 +0530153 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
154 SDHCI_CARD_PRESENT;
155 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
Adrian Hunter50accb92011-10-03 15:33:34 +0300156
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300157 if (enable)
158 sdhci_unmask_irqs(host, irqs);
159 else
160 sdhci_mask_irqs(host, irqs);
161}
162
163static void sdhci_enable_card_detection(struct sdhci_host *host)
164{
165 sdhci_set_card_detection(host, true);
166}
167
168static void sdhci_disable_card_detection(struct sdhci_host *host)
169{
170 sdhci_set_card_detection(host, false);
171}
172
Pierre Ossmand129bce2006-03-24 03:18:17 -0800173static void sdhci_reset(struct sdhci_host *host, u8 mask)
174{
Pierre Ossmane16514d2006-06-30 02:22:24 -0700175 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300176 u32 uninitialized_var(ier);
Pierre Ossmane16514d2006-06-30 02:22:24 -0700177
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100178 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300179 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700180 SDHCI_CARD_PRESENT))
181 return;
182 }
183
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300184 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
185 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
186
Philip Rakity393c1a32011-01-21 11:26:40 -0800187 if (host->ops->platform_reset_enter)
188 host->ops->platform_reset_enter(host, mask);
189
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300190 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800191
Pierre Ossmane16514d2006-06-30 02:22:24 -0700192 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800193 host->clock = 0;
194
Pierre Ossmane16514d2006-06-30 02:22:24 -0700195 /* Wait max 100 ms */
196 timeout = 100;
197
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530198 if (host->ops->check_power_status && host->pwr &&
199 (mask & SDHCI_RESET_ALL))
200 host->ops->check_power_status(host);
201
Pierre Ossmane16514d2006-06-30 02:22:24 -0700202 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300203 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d2006-06-30 02:22:24 -0700204 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +0530205 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d2006-06-30 02:22:24 -0700206 mmc_hostname(host->mmc), (int)mask);
207 sdhci_dumpregs(host);
208 return;
209 }
210 timeout--;
211 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800212 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300213
Philip Rakity393c1a32011-01-21 11:26:40 -0800214 if (host->ops->platform_reset_exit)
215 host->ops->platform_reset_exit(host, mask);
216
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300217 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
218 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Sahitya Tummalaca422112013-02-22 12:15:54 +0530219
220 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
221 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
222 host->ops->enable_dma(host);
223 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800224}
225
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800226static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
227
228static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800229{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800230 if (soft)
231 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
232 else
233 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800234
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300235 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
236 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700237 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
238 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300239 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800240
241 if (soft) {
242 /* force clock reconfiguration */
243 host->clock = 0;
244 sdhci_set_ios(host->mmc, &host->mmc->ios);
245 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300246}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800247
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300248static void sdhci_reinit(struct sdhci_host *host)
249{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800250 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300251 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800252}
253
254static void sdhci_activate_led(struct sdhci_host *host)
255{
256 u8 ctrl;
257
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300258 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800259 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300260 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261}
262
263static void sdhci_deactivate_led(struct sdhci_host *host)
264{
265 u8 ctrl;
266
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300267 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800268 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300269 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800270}
271
Pierre Ossmanf9134312008-12-21 17:01:48 +0100272#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100273static void sdhci_led_control(struct led_classdev *led,
274 enum led_brightness brightness)
275{
276 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
277 unsigned long flags;
278
279 spin_lock_irqsave(&host->lock, flags);
280
Adrian Hunter50accb92011-10-03 15:33:34 +0300281 if (host->runtime_suspended)
282 goto out;
283
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100284 if (brightness == LED_OFF)
285 sdhci_deactivate_led(host);
286 else
287 sdhci_activate_led(host);
Adrian Hunter50accb92011-10-03 15:33:34 +0300288out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100289 spin_unlock_irqrestore(&host->lock, flags);
290}
291#endif
292
Pierre Ossmand129bce2006-03-24 03:18:17 -0800293/*****************************************************************************\
294 * *
295 * Core functions *
296 * *
297\*****************************************************************************/
298
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100299static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800300{
Pierre Ossman76591502008-07-21 00:32:11 +0200301 unsigned long flags;
302 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700303 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200304 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800305
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100306 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100308 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200309 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800310
Pierre Ossman76591502008-07-21 00:32:11 +0200311 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800312
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200314 if (!sg_miter_next(&host->sg_miter))
315 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800316
Pierre Ossman76591502008-07-21 00:32:11 +0200317 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800318
Pierre Ossman76591502008-07-21 00:32:11 +0200319 blksize -= len;
320 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200321
Pierre Ossman76591502008-07-21 00:32:11 +0200322 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800323
Pierre Ossman76591502008-07-21 00:32:11 +0200324 while (len) {
325 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300326 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200327 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800328 }
Pierre Ossman76591502008-07-21 00:32:11 +0200329
330 *buf = scratch & 0xFF;
331
332 buf++;
333 scratch >>= 8;
334 chunk--;
335 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800336 }
337 }
Pierre Ossman76591502008-07-21 00:32:11 +0200338
339 sg_miter_stop(&host->sg_miter);
340
341 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100342}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800343
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100344static void sdhci_write_block_pio(struct sdhci_host *host)
345{
Pierre Ossman76591502008-07-21 00:32:11 +0200346 unsigned long flags;
347 size_t blksize, len, chunk;
348 u32 scratch;
349 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100350
351 DBG("PIO writing\n");
352
353 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200354 chunk = 0;
355 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100356
Pierre Ossman76591502008-07-21 00:32:11 +0200357 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358
359 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200360 if (!sg_miter_next(&host->sg_miter))
361 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100362
Pierre Ossman76591502008-07-21 00:32:11 +0200363 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200364
Pierre Ossman76591502008-07-21 00:32:11 +0200365 blksize -= len;
366 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100367
Pierre Ossman76591502008-07-21 00:32:11 +0200368 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100369
Pierre Ossman76591502008-07-21 00:32:11 +0200370 while (len) {
371 scratch |= (u32)*buf << (chunk * 8);
372
373 buf++;
374 chunk++;
375 len--;
376
377 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300378 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200379 chunk = 0;
380 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100382 }
383 }
Pierre Ossman76591502008-07-21 00:32:11 +0200384
385 sg_miter_stop(&host->sg_miter);
386
387 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100388}
389
390static void sdhci_transfer_pio(struct sdhci_host *host)
391{
392 u32 mask;
393
394 BUG_ON(!host->data);
395
Pierre Ossman76591502008-07-21 00:32:11 +0200396 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100397 return;
398
399 if (host->data->flags & MMC_DATA_READ)
400 mask = SDHCI_DATA_AVAILABLE;
401 else
402 mask = SDHCI_SPACE_AVAILABLE;
403
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200404 /*
405 * Some controllers (JMicron JMB38x) mess up the buffer bits
406 * for transfers < 4 bytes. As long as it is just one block,
407 * we can ignore the bits.
408 */
409 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
410 (host->data->blocks == 1))
411 mask = ~0;
412
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300413 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300414 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
415 udelay(100);
416
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100417 if (host->data->flags & MMC_DATA_READ)
418 sdhci_read_block_pio(host);
419 else
420 sdhci_write_block_pio(host);
421
Pierre Ossman76591502008-07-21 00:32:11 +0200422 host->blocks--;
423 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100424 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100425 }
426
427 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800428}
429
Pierre Ossman2134a922008-06-28 18:28:51 +0200430static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
431{
432 local_irq_save(*flags);
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800433 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200434}
435
436static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
437{
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800438 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200439 local_irq_restore(*flags);
440}
441
Ben Dooks118cd172010-03-05 13:43:26 -0800442static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
443{
Ben Dooks9e506f32010-03-05 13:43:29 -0800444 __le32 *dataddr = (__le32 __force *)(desc + 4);
445 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800446
Ben Dooks9e506f32010-03-05 13:43:29 -0800447 /* SDHCI specification says ADMA descriptors should be 4 byte
448 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800449
Ben Dooks9e506f32010-03-05 13:43:29 -0800450 cmdlen[0] = cpu_to_le16(cmd);
451 cmdlen[1] = cpu_to_le16(len);
452
453 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800454}
455
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200456static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200457 struct mmc_data *data)
458{
459 int direction;
460
461 u8 *desc;
462 u8 *align;
463 dma_addr_t addr;
464 dma_addr_t align_addr;
465 int len, offset;
466
467 struct scatterlist *sg;
468 int i;
469 char *buffer;
470 unsigned long flags;
471
472 /*
473 * The spec does not specify endianness of descriptor table.
474 * We currently guess that it is LE.
475 */
476
477 if (data->flags & MMC_DATA_READ)
478 direction = DMA_FROM_DEVICE;
479 else
480 direction = DMA_TO_DEVICE;
481
482 /*
483 * The ADMA descriptor table is mapped further down as we
484 * need to fill it with data first.
485 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200486 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530487 host->align_buffer,
488 host->align_buf_sz,
489 direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700490 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200491 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200492 BUG_ON(host->align_addr & 0x3);
493
494 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
495 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200496 if (host->sg_count == 0)
497 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200498
499 desc = host->adma_desc;
500 align = host->align_buffer;
501
502 align_addr = host->align_addr;
503
504 for_each_sg(data->sg, sg, host->sg_count, i) {
505 addr = sg_dma_address(sg);
506 len = sg_dma_len(sg);
507
508 /*
509 * The SDHCI specification states that ADMA
510 * addresses must be 32-bit aligned. If they
511 * aren't, then we use a bounce buffer for
512 * the (up to three) bytes that screw up the
513 * alignment.
514 */
515 offset = (4 - (addr & 0x3)) & 0x3;
516 if (offset) {
517 if (data->flags & MMC_DATA_WRITE) {
518 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200519 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200520 memcpy(align, buffer, offset);
521 sdhci_kunmap_atomic(buffer, &flags);
522 }
523
Ben Dooks118cd172010-03-05 13:43:26 -0800524 /* tran, valid */
525 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200526
527 BUG_ON(offset > 65536);
528
Pierre Ossman2134a922008-06-28 18:28:51 +0200529 align += 4;
530 align_addr += 4;
531
532 desc += 8;
533
534 addr += offset;
535 len -= offset;
536 }
537
Pierre Ossman2134a922008-06-28 18:28:51 +0200538 BUG_ON(len > 65536);
539
Ben Dooks118cd172010-03-05 13:43:26 -0800540 /* tran, valid */
541 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200542 desc += 8;
543
544 /*
545 * If this triggers then we have a calculation bug
546 * somewhere. :/
547 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530548 WARN_ON((desc - host->adma_desc) > host->adma_desc_sz);
549
Pierre Ossman2134a922008-06-28 18:28:51 +0200550 }
551
Thomas Abraham70764a92010-05-26 14:42:04 -0700552 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
553 /*
554 * Mark the last descriptor as the terminating descriptor
555 */
556 if (desc != host->adma_desc) {
557 desc -= 8;
558 desc[0] |= 0x2; /* end */
559 }
560 } else {
561 /*
562 * Add a terminating entry.
563 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200564
Thomas Abraham70764a92010-05-26 14:42:04 -0700565 /* nop, end, valid */
566 sdhci_set_adma_desc(desc, 0, 0, 0x3);
567 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200568
569 /*
570 * Resync align buffer as we might have changed it.
571 */
572 if (data->flags & MMC_DATA_WRITE) {
573 dma_sync_single_for_device(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530574 host->align_addr,
575 host->align_buf_sz,
576 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200577 }
578
579 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530580 host->adma_desc,
581 host->adma_desc_sz,
582 DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200583 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200584 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200585 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200586
587 return 0;
588
589unmap_entries:
590 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
591 data->sg_len, direction);
592unmap_align:
593 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530594 host->align_buf_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200595fail:
596 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200597}
598
599static void sdhci_adma_table_post(struct sdhci_host *host,
600 struct mmc_data *data)
601{
602 int direction;
603
604 struct scatterlist *sg;
605 int i, size;
606 u8 *align;
607 char *buffer;
608 unsigned long flags;
609
610 if (data->flags & MMC_DATA_READ)
611 direction = DMA_FROM_DEVICE;
612 else
613 direction = DMA_TO_DEVICE;
614
615 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530616 host->adma_desc_sz, DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200617
618 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530619 host->align_buf_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200620
621 if (data->flags & MMC_DATA_READ) {
622 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
623 data->sg_len, direction);
624
625 align = host->align_buffer;
626
627 for_each_sg(data->sg, sg, host->sg_count, i) {
628 if (sg_dma_address(sg) & 0x3) {
629 size = 4 - (sg_dma_address(sg) & 0x3);
630
631 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200632 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200633 memcpy(buffer, align, size);
634 sdhci_kunmap_atomic(buffer, &flags);
635
636 align += 4;
637 }
638 }
639 }
640
641 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
642 data->sg_len, direction);
643}
644
Andrei Warkentina3c77782011-04-11 16:13:42 -0500645static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800646{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700647 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500648 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700649 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800650
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200651 /*
652 * If the host controller provides us with an incorrect timeout
653 * value, just skip the check and use 0xE. The hardware may take
654 * longer to time out, but that's much better than having a too-short
655 * timeout value.
656 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200657 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200658 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200659
Andrei Warkentina3c77782011-04-11 16:13:42 -0500660 /* Unspecified timeout, assume max */
661 if (!data && !cmd->cmd_timeout_ms)
662 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800663
Andrei Warkentina3c77782011-04-11 16:13:42 -0500664 /* timeout in us */
665 if (!data)
666 target_timeout = cmd->cmd_timeout_ms * 1000;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530667 else {
668 target_timeout = data->timeout_ns / 1000;
669 if (host->clock)
670 target_timeout += data->timeout_clks / host->clock;
671 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700672
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700673 /*
674 * Figure out needed cycles.
675 * We do this in steps in order to fit inside a 32 bit int.
676 * The first step is the minimum timeout, which will have a
677 * minimum resolution of 6 bits:
678 * (1) 2^13*1000 > 2^22,
679 * (2) host->timeout_clk < 2^16
680 * =>
681 * (1) / (2) > 2^6
682 */
683 count = 0;
684 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
685 while (current_timeout < target_timeout) {
686 count++;
687 current_timeout <<= 1;
688 if (count >= 0xF)
689 break;
690 }
691
Sahitya Tummalaca422112013-02-22 12:15:54 +0530692 if (count >= 0xF) {
Chris Ball34ca2092012-06-01 10:39:45 -0400693 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
694 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700695 count = 0xE;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530696 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700697
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200698 return count;
699}
700
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300701static void sdhci_set_transfer_irqs(struct sdhci_host *host)
702{
703 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
704 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
705
706 if (host->flags & SDHCI_REQ_USE_DMA)
707 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
708 else
709 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
710}
711
Andrei Warkentina3c77782011-04-11 16:13:42 -0500712static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200713{
714 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200715 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500716 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200717 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200718
719 WARN_ON(host->data);
720
Andrei Warkentina3c77782011-04-11 16:13:42 -0500721 if (data || (cmd->flags & MMC_RSP_BUSY)) {
722 count = sdhci_calc_timeout(host, cmd);
723 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
724 }
725
726 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200727 return;
728
729 /* Sanity checks */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530730 BUG_ON(data->blksz * data->blocks > host->mmc->max_req_size);
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200731 BUG_ON(data->blksz > host->mmc->max_blk_size);
732 BUG_ON(data->blocks > 65535);
733
734 host->data = data;
735 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400736 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200737
Richard Röjforsa13abc72009-09-22 16:45:30 -0700738 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100739 host->flags |= SDHCI_REQ_USE_DMA;
740
Pierre Ossman2134a922008-06-28 18:28:51 +0200741 /*
742 * FIXME: This doesn't account for merging when mapping the
743 * scatterlist.
744 */
745 if (host->flags & SDHCI_REQ_USE_DMA) {
746 int broken, i;
747 struct scatterlist *sg;
748
749 broken = 0;
750 if (host->flags & SDHCI_USE_ADMA) {
751 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
752 broken = 1;
753 } else {
754 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
755 broken = 1;
756 }
757
758 if (unlikely(broken)) {
759 for_each_sg(data->sg, sg, data->sg_len, i) {
760 if (sg->length & 0x3) {
761 DBG("Reverting to PIO because of "
762 "transfer size (%d)\n",
763 sg->length);
764 host->flags &= ~SDHCI_REQ_USE_DMA;
765 break;
766 }
767 }
768 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100769 }
770
771 /*
772 * The assumption here being that alignment is the same after
773 * translation to device address space.
774 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200775 if (host->flags & SDHCI_REQ_USE_DMA) {
776 int broken, i;
777 struct scatterlist *sg;
778
779 broken = 0;
780 if (host->flags & SDHCI_USE_ADMA) {
781 /*
782 * As we use 3 byte chunks to work around
783 * alignment problems, we need to check this
784 * quirk.
785 */
786 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
787 broken = 1;
788 } else {
789 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
790 broken = 1;
791 }
792
793 if (unlikely(broken)) {
794 for_each_sg(data->sg, sg, data->sg_len, i) {
795 if (sg->offset & 0x3) {
796 DBG("Reverting to PIO because of "
797 "bad alignment\n");
798 host->flags &= ~SDHCI_REQ_USE_DMA;
799 break;
800 }
801 }
802 }
803 }
804
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200805 if (host->flags & SDHCI_REQ_USE_DMA) {
806 if (host->flags & SDHCI_USE_ADMA) {
807 ret = sdhci_adma_table_pre(host, data);
808 if (ret) {
809 /*
810 * This only happens when someone fed
811 * us an invalid request.
812 */
813 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200814 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200815 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300816 sdhci_writel(host, host->adma_addr,
817 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200818 }
819 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300820 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200821
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300822 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200823 data->sg, data->sg_len,
824 (data->flags & MMC_DATA_READ) ?
825 DMA_FROM_DEVICE :
826 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300827 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200828 /*
829 * This only happens when someone fed
830 * us an invalid request.
831 */
832 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200833 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200834 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200835 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300836 sdhci_writel(host, sg_dma_address(data->sg),
837 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200838 }
839 }
840 }
841
Pierre Ossman2134a922008-06-28 18:28:51 +0200842 /*
843 * Always adjust the DMA selection as some controllers
844 * (e.g. JMicron) can't do PIO properly when the selection
845 * is ADMA.
846 */
847 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300848 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200849 ctrl &= ~SDHCI_CTRL_DMA_MASK;
850 if ((host->flags & SDHCI_REQ_USE_DMA) &&
851 (host->flags & SDHCI_USE_ADMA))
852 ctrl |= SDHCI_CTRL_ADMA32;
853 else
854 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300855 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100856 }
857
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200858 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200859 int flags;
860
861 flags = SG_MITER_ATOMIC;
862 if (host->data->flags & MMC_DATA_READ)
863 flags |= SG_MITER_TO_SG;
864 else
865 flags |= SG_MITER_FROM_SG;
866 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200867 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800868 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700869
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300870 sdhci_set_transfer_irqs(host);
871
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400872 /* Set the DMA boundary value and block size */
873 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
874 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300875 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700876}
877
878static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500879 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700880{
881 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500882 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700883
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700884 if (data == NULL)
885 return;
886
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200887 WARN_ON(!host->data);
888
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700889 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500890 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
891 mode |= SDHCI_TRNS_MULTI;
892 /*
893 * If we are sending CMD23, CMD12 never gets sent
894 * on successful completion (so no Auto-CMD12).
895 */
896 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
897 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500898 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
899 mode |= SDHCI_TRNS_AUTO_CMD23;
900 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
901 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700902 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500903
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530904 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700905 mode |= SDHCI_TRNS_READ;
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530906 if (host->ops->toggle_cdr)
907 host->ops->toggle_cdr(host, true);
908 }
909 if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE))
910 host->ops->toggle_cdr(host, false);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100911 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700912 mode |= SDHCI_TRNS_DMA;
913
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300914 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800915}
916
917static void sdhci_finish_data(struct sdhci_host *host)
918{
919 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800920
921 BUG_ON(!host->data);
922
923 data = host->data;
924 host->data = NULL;
925
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100926 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200927 if (host->flags & SDHCI_USE_ADMA)
928 sdhci_adma_table_post(host, data);
929 else {
930 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
931 data->sg_len, (data->flags & MMC_DATA_READ) ?
932 DMA_FROM_DEVICE : DMA_TO_DEVICE);
933 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800934 }
935
936 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200937 * The specification states that the block count register must
938 * be updated, but it does not specify at what point in the
939 * data flow. That makes the register entirely useless to read
940 * back so we have to assume that nothing made it to the card
941 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800942 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200943 if (data->error)
944 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800945 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200946 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800947
Andrei Warkentine89d4562011-05-23 15:06:37 -0500948 /*
949 * Need to send CMD12 if -
950 * a) open-ended multiblock transfer (no CMD23)
951 * b) error in multiblock transfer
952 */
953 if (data->stop &&
954 (data->error ||
955 !host->mrq->sbc)) {
956
Pierre Ossmand129bce2006-03-24 03:18:17 -0800957 /*
958 * The controller needs a reset of internal state machines
959 * upon error conditions.
960 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200961 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800962 sdhci_reset(host, SDHCI_RESET_CMD);
963 sdhci_reset(host, SDHCI_RESET_DATA);
964 }
965
966 sdhci_send_command(host, data->stop);
967 } else
968 tasklet_schedule(&host->finish_tasklet);
969}
970
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +0530971#define SDHCI_REQUEST_TIMEOUT 10 /* Default request timeout in seconds */
972
Pierre Ossmand129bce2006-03-24 03:18:17 -0800973static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
974{
975 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700976 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700977 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800978
979 WARN_ON(host->cmd);
980
Pierre Ossmand129bce2006-03-24 03:18:17 -0800981 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700982 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700983
984 mask = SDHCI_CMD_INHIBIT;
985 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
986 mask |= SDHCI_DATA_INHIBIT;
987
988 /* We shouldn't wait for data inihibit for stop commands, even
989 though they might use busy signaling */
990 if (host->mrq->data && (cmd == host->mrq->data->stop))
991 mask &= ~SDHCI_DATA_INHIBIT;
992
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300993 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700994 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +0530995 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100996 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200998 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800999 tasklet_schedule(&host->finish_tasklet);
1000 return;
1001 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001002 timeout--;
1003 mdelay(1);
1004 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001005
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301006 mod_timer(&host->timer, jiffies + SDHCI_REQUEST_TIMEOUT * HZ);
1007
1008 if (cmd->cmd_timeout_ms > SDHCI_REQUEST_TIMEOUT * MSEC_PER_SEC)
1009 mod_timer(&host->timer, jiffies +
1010 (msecs_to_jiffies(cmd->cmd_timeout_ms * 2)));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001011
1012 host->cmd = cmd;
1013
Andrei Warkentina3c77782011-04-11 16:13:42 -05001014 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001015
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001016 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001017
Andrei Warkentine89d4562011-05-23 15:06:37 -05001018 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001019
Pierre Ossmand129bce2006-03-24 03:18:17 -08001020 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301021 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001022 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001023 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001024 tasklet_schedule(&host->finish_tasklet);
1025 return;
1026 }
1027
1028 if (!(cmd->flags & MMC_RSP_PRESENT))
1029 flags = SDHCI_CMD_RESP_NONE;
1030 else if (cmd->flags & MMC_RSP_136)
1031 flags = SDHCI_CMD_RESP_LONG;
1032 else if (cmd->flags & MMC_RSP_BUSY)
1033 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1034 else
1035 flags = SDHCI_CMD_RESP_SHORT;
1036
1037 if (cmd->flags & MMC_RSP_CRC)
1038 flags |= SDHCI_CMD_CRC;
1039 if (cmd->flags & MMC_RSP_OPCODE)
1040 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301041
1042 /* CMD19 is special in that the Data Present Select should be set */
Girish K S2cd06dc2012-01-06 09:56:39 +05301043 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1044 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001045 flags |= SDHCI_CMD_DATA;
1046
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001047 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001048}
1049
1050static void sdhci_finish_command(struct sdhci_host *host)
1051{
1052 int i;
1053
1054 BUG_ON(host->cmd == NULL);
1055
1056 if (host->cmd->flags & MMC_RSP_PRESENT) {
1057 if (host->cmd->flags & MMC_RSP_136) {
1058 /* CRC is stripped so we need to do some shifting. */
1059 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001060 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001061 SDHCI_RESPONSE + (3-i)*4) << 8;
1062 if (i != 3)
1063 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001064 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001065 SDHCI_RESPONSE + (3-i)*4-1);
1066 }
1067 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001068 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001069 }
1070 }
1071
Pierre Ossman17b04292007-07-22 22:18:46 +02001072 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001073
Andrei Warkentine89d4562011-05-23 15:06:37 -05001074 /* Finished CMD23, now send actual command. */
1075 if (host->cmd == host->mrq->sbc) {
1076 host->cmd = NULL;
1077 sdhci_send_command(host, host->mrq->cmd);
1078 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001079
Andrei Warkentine89d4562011-05-23 15:06:37 -05001080 /* Processed actual command. */
1081 if (host->data && host->data_early)
1082 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001083
Andrei Warkentine89d4562011-05-23 15:06:37 -05001084 if (!host->cmd->data)
1085 tasklet_schedule(&host->finish_tasklet);
1086
1087 host->cmd = NULL;
1088 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001089}
1090
1091static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1092{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301093 int div = 0; /* Initialized for compiler warning */
Sahitya Tummalaca422112013-02-22 12:15:54 +05301094 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301095 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001096 unsigned long timeout;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301097 unsigned long flags;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001098
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301099 spin_lock_irqsave(&host->lock, flags);
Todd Poynor30832ab2011-12-27 15:48:46 +02001100 if (clock && clock == host->clock)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301101 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001102
Sahitya Tummalaca422112013-02-22 12:15:54 +05301103 host->mmc->actual_clock = 0;
1104
Anton Vorontsov81146342009-03-17 00:13:59 +03001105 if (host->ops->set_clock) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301106 spin_unlock_irqrestore(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001107 host->ops->set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301108 spin_lock_irqsave(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001109 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301110 goto ret;
Anton Vorontsov81146342009-03-17 00:13:59 +03001111 }
1112
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301113 if (host->clock)
1114 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001115
1116 if (clock == 0)
1117 goto out;
1118
Zhangfei Gao85105c52010-08-06 07:10:01 +08001119 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301120 /*
1121 * Check if the Host Controller supports Programmable Clock
1122 * Mode.
1123 */
1124 if (host->clk_mul) {
1125 u16 ctrl;
1126
1127 /*
1128 * We need to figure out whether the Host Driver needs
1129 * to select Programmable Clock Mode, or the value can
1130 * be set automatically by the Host Controller based on
1131 * the Preset Value registers.
1132 */
1133 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1134 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1135 for (div = 1; div <= 1024; div++) {
1136 if (((host->max_clk * host->clk_mul) /
1137 div) <= clock)
1138 break;
1139 }
1140 /*
1141 * Set Programmable Clock Mode in the Clock
1142 * Control register.
1143 */
1144 clk = SDHCI_PROG_CLOCK_MODE;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301145 real_div = div;
1146 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301147 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001148 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301149 } else {
1150 /* Version 3.00 divisors must be a multiple of 2. */
1151 if (host->max_clk <= clock)
1152 div = 1;
1153 else {
1154 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1155 div += 2) {
1156 if ((host->max_clk / div) <= clock)
1157 break;
1158 }
1159 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301160 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301161 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001162 }
1163 } else {
1164 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001165 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001166 if ((host->max_clk / div) <= clock)
1167 break;
1168 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301169 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301170 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001171 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001172
Sahitya Tummalaca422112013-02-22 12:15:54 +05301173 if (real_div)
1174 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1175
Arindam Nathc3ed3872011-05-05 12:19:06 +05301176 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001177 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1178 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001179 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001180 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001181
Chris Ball27f6cb12009-09-22 16:45:31 -07001182 /* Wait max 20 ms */
1183 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001184 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001185 & SDHCI_CLOCK_INT_STABLE)) {
1186 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301187 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001188 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001189 sdhci_dumpregs(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301190 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001191 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001192 timeout--;
1193 mdelay(1);
1194 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001195
1196 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001197 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001198
1199out:
1200 host->clock = clock;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301201ret:
1202 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001203}
1204
Sahitya Tummalaca422112013-02-22 12:15:54 +05301205static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001206{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001207 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001208
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001209 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001210 switch (1 << power) {
1211 case MMC_VDD_165_195:
1212 pwr = SDHCI_POWER_180;
1213 break;
1214 case MMC_VDD_29_30:
1215 case MMC_VDD_30_31:
1216 pwr = SDHCI_POWER_300;
1217 break;
1218 case MMC_VDD_32_33:
1219 case MMC_VDD_33_34:
1220 pwr = SDHCI_POWER_330;
1221 break;
1222 default:
1223 BUG();
1224 }
1225 }
1226
1227 if (host->pwr == pwr)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301228 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001229
Pierre Ossmanae628902009-05-03 20:45:03 +02001230 host->pwr = pwr;
1231
1232 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001233 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301234 if (host->ops->check_power_status)
1235 host->ops->check_power_status(host);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301236 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001237 }
1238
1239 /*
1240 * Spec says that we should clear the power reg before setting
1241 * a new value. Some controllers don't seem to like this though.
1242 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301243 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001244 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301245 if (host->ops->check_power_status)
1246 host->ops->check_power_status(host);
1247 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001248
Andres Salomone08c1692008-07-04 10:00:03 -07001249 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001250 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001251 * and set turn on power at the same time, so set the voltage first.
1252 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301253 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001254 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301255 if (host->ops->check_power_status)
1256 host->ops->check_power_status(host);
1257 }
Pierre Ossmanae628902009-05-03 20:45:03 +02001258
1259 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001260
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001261 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301262 if (host->ops->check_power_status)
1263 host->ops->check_power_status(host);
Harald Welte557b0692009-06-18 16:53:38 +02001264
1265 /*
1266 * Some controllers need an extra 10ms delay of 10ms before they
1267 * can apply clock after applying power
1268 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001269 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001270 mdelay(10);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301271
1272 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001273}
1274
Pierre Ossmand129bce2006-03-24 03:18:17 -08001275/*****************************************************************************\
1276 * *
1277 * MMC callbacks *
1278 * *
1279\*****************************************************************************/
1280
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301281static int sdhci_enable(struct mmc_host *mmc)
1282{
1283 struct sdhci_host *host = mmc_priv(mmc);
1284
1285 if (host->cpu_dma_latency_us)
1286 pm_qos_update_request(&host->pm_qos_req_dma,
1287 host->cpu_dma_latency_us);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301288 if (host->ops->platform_bus_voting)
1289 host->ops->platform_bus_voting(host, 1);
1290
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301291 return 0;
1292}
1293
1294static int sdhci_disable(struct mmc_host *mmc)
1295{
1296 struct sdhci_host *host = mmc_priv(mmc);
1297
1298 if (host->cpu_dma_latency_us)
1299 pm_qos_update_request(&host->pm_qos_req_dma,
1300 PM_QOS_DEFAULT_VALUE);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301301 if (host->ops->platform_bus_voting)
1302 host->ops->platform_bus_voting(host, 0);
1303
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301304 return 0;
1305}
1306
Pierre Ossmand129bce2006-03-24 03:18:17 -08001307static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1308{
1309 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001310 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001311 unsigned long flags;
1312
1313 host = mmc_priv(mmc);
1314
Adrian Hunter50accb92011-10-03 15:33:34 +03001315 sdhci_runtime_pm_get(host);
1316
Pierre Ossmand129bce2006-03-24 03:18:17 -08001317 spin_lock_irqsave(&host->lock, flags);
1318
1319 WARN_ON(host->mrq != NULL);
1320
Pierre Ossmanf9134312008-12-21 17:01:48 +01001321#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001322 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001323#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001324
1325 /*
1326 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1327 * requests if Auto-CMD12 is enabled.
1328 */
1329 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001330 if (mrq->stop) {
1331 mrq->data->stop = NULL;
1332 mrq->stop = NULL;
1333 }
1334 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001335
1336 host->mrq = mrq;
1337
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001338 /* If polling, assume that the card is always present. */
1339 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1340 present = true;
1341 else
1342 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1343 SDHCI_CARD_PRESENT;
1344
1345 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001346 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001347 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301348 } else {
1349 u32 present_state;
1350
1351 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1352 /*
1353 * Check if the re-tuning timer has already expired and there
1354 * is no on-going data transfer. If so, we need to execute
1355 * tuning procedure before sending command.
1356 */
1357 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1358 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1359 spin_unlock_irqrestore(&host->lock, flags);
Girish K S2cd06dc2012-01-06 09:56:39 +05301360 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301361 spin_lock_irqsave(&host->lock, flags);
1362
1363 /* Restore original mmc_request structure */
1364 host->mrq = mrq;
1365 }
1366
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001367 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001368 sdhci_send_command(host, mrq->sbc);
1369 else
1370 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301371 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001372
Pierre Ossman5f25a662006-10-04 02:15:39 -07001373 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001374 spin_unlock_irqrestore(&host->lock, flags);
1375}
1376
Adrian Hunter50accb92011-10-03 15:33:34 +03001377static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001378{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001379 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301380 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001381 u8 ctrl;
1382
Sahitya Tummalaca422112013-02-22 12:15:54 +05301383 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301384 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1385 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1386 return;
1387 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001388
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301389 if (ios->clock)
1390 sdhci_set_clock(host, ios->clock);
1391
1392 spin_lock_irqsave(&host->lock, flags);
1393 if (!host->clock) {
1394 spin_unlock_irqrestore(&host->lock, flags);
1395 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001396 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301397 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001398
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301399 if (ios->power_mode & (MMC_POWER_UP | MMC_POWER_ON))
Sahitya Tummalaca422112013-02-22 12:15:54 +05301400 vdd_bit = sdhci_set_power(host, ios->vdd);
1401
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301402 if (host->vmmc && vdd_bit != -1)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301403 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301404
1405 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001406
Philip Rakity643a81f2010-09-23 08:24:32 -07001407 if (host->ops->platform_send_init_74_clocks)
1408 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1409
Philip Rakity15ec4462010-11-19 16:48:39 -05001410 /*
1411 * If your platform has 8-bit width support but is not a v3 controller,
1412 * or if it requires special setup code, you should implement that in
1413 * platform_8bit_width().
1414 */
1415 if (host->ops->platform_8bit_width)
1416 host->ops->platform_8bit_width(host, ios->bus_width);
1417 else {
1418 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1419 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1420 ctrl &= ~SDHCI_CTRL_4BITBUS;
1421 if (host->version >= SDHCI_SPEC_300)
1422 ctrl |= SDHCI_CTRL_8BITBUS;
1423 } else {
1424 if (host->version >= SDHCI_SPEC_300)
1425 ctrl &= ~SDHCI_CTRL_8BITBUS;
1426 if (ios->bus_width == MMC_BUS_WIDTH_4)
1427 ctrl |= SDHCI_CTRL_4BITBUS;
1428 else
1429 ctrl &= ~SDHCI_CTRL_4BITBUS;
1430 }
1431 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1432 }
1433
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001434 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001435
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001436 if ((ios->timing == MMC_TIMING_SD_HS ||
1437 ios->timing == MMC_TIMING_MMC_HS)
1438 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001439 ctrl |= SDHCI_CTRL_HISPD;
1440 else
1441 ctrl &= ~SDHCI_CTRL_HISPD;
1442
Arindam Nathd6d50a12011-05-05 12:18:59 +05301443 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301444 u16 clk, ctrl_2;
1445 unsigned int clock;
1446
1447 /* In case of UHS-I modes, set High Speed Enable */
Girish K S2cd06dc2012-01-06 09:56:39 +05301448 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1449 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301450 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1451 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001452 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301453 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301454
1455 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1456 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301457 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301458 /*
1459 * We only need to set Driver Strength if the
1460 * preset value enable is not set.
1461 */
1462 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1463 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1464 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1465 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1466 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1467
1468 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301469 } else {
1470 /*
1471 * According to SDHC Spec v3.00, if the Preset Value
1472 * Enable in the Host Control 2 register is set, we
1473 * need to reset SD Clock Enable before changing High
1474 * Speed Enable to avoid generating clock gliches.
1475 */
Arindam Nath758535c2011-05-05 12:19:00 +05301476
1477 /* Reset SD Clock Enable */
1478 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1479 clk &= ~SDHCI_CLOCK_CARD_EN;
1480 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1481
1482 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1483
1484 /* Re-enable SD Clock */
1485 clock = host->clock;
1486 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301487 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301488 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301489 spin_lock_irqsave(&host->lock, flags);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301490 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301491
Arindam Nath49c468f2011-05-05 12:19:01 +05301492 /* Reset SD Clock Enable */
1493 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1494 clk &= ~SDHCI_CLOCK_CARD_EN;
1495 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1496
Philip Rakity6322cdd2011-05-13 11:17:15 +05301497 if (host->ops->set_uhs_signaling)
1498 host->ops->set_uhs_signaling(host, ios->timing);
1499 else {
1500 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1501 /* Select Bus Speed Mode for host */
1502 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S2cd06dc2012-01-06 09:56:39 +05301503 if (ios->timing == MMC_TIMING_MMC_HS200)
1504 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1505 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301506 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1507 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1508 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1509 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1510 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1511 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1512 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1513 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1514 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1515 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1516 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301517
1518 /* Re-enable SD Clock */
1519 clock = host->clock;
1520 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301521 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath49c468f2011-05-05 12:19:01 +05301522 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301523 spin_lock_irqsave(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301524 } else
1525 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301526
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301527 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02001528 /*
1529 * Some (ENE) controllers go apeshit on some ios operation,
1530 * signalling timeout and CRC errors even on CMD0. Resetting
1531 * it on each ios seems to solve the problem.
1532 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001533 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001534 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1535
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301536 /*
1537 * Reset the chip on each power off.
1538 * Should clear out any weird states.
1539 */
1540 if (ios->power_mode == MMC_POWER_OFF) {
1541 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1542 sdhci_reinit(host);
1543 vdd_bit = sdhci_set_power(host, -1);
1544 if (host->vmmc && vdd_bit != -1)
1545 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1546 }
1547 if (!ios->clock)
1548 sdhci_set_clock(host, ios->clock);
1549
Pierre Ossman5f25a662006-10-04 02:15:39 -07001550 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001551}
1552
Adrian Hunter50accb92011-10-03 15:33:34 +03001553static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1554{
1555 struct sdhci_host *host = mmc_priv(mmc);
1556
1557 sdhci_runtime_pm_get(host);
1558 sdhci_do_set_ios(host, ios);
1559 sdhci_runtime_pm_put(host);
1560}
1561
1562static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001563{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001564 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001565 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001566
Pierre Ossmand129bce2006-03-24 03:18:17 -08001567 spin_lock_irqsave(&host->lock, flags);
1568
Pierre Ossman1e728592008-04-16 19:13:13 +02001569 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001570 is_readonly = 0;
1571 else if (host->ops->get_ro)
1572 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001573 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001574 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1575 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001576
1577 spin_unlock_irqrestore(&host->lock, flags);
1578
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001579 /* This quirk needs to be replaced by a callback-function later */
1580 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1581 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001582}
1583
Takashi Iwai82b0e232011-04-21 20:26:38 +02001584#define SAMPLE_COUNT 5
1585
Adrian Hunter50accb92011-10-03 15:33:34 +03001586static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001587{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001588 int i, ro_count;
1589
Takashi Iwai82b0e232011-04-21 20:26:38 +02001590 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001591 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001592
1593 ro_count = 0;
1594 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001595 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001596 if (++ro_count > SAMPLE_COUNT / 2)
1597 return 1;
1598 }
1599 msleep(30);
1600 }
1601 return 0;
1602}
1603
Adrian Hunter50accb92011-10-03 15:33:34 +03001604static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001605{
Adrian Hunter50accb92011-10-03 15:33:34 +03001606 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001607
Adrian Hunter50accb92011-10-03 15:33:34 +03001608 if (host->ops && host->ops->hw_reset)
1609 host->ops->hw_reset(host);
1610}
Adrian Hunter20758b62011-08-29 16:42:12 +03001611
Adrian Hunter50accb92011-10-03 15:33:34 +03001612static int sdhci_get_ro(struct mmc_host *mmc)
1613{
1614 struct sdhci_host *host = mmc_priv(mmc);
1615 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001616
Adrian Hunter50accb92011-10-03 15:33:34 +03001617 sdhci_runtime_pm_get(host);
1618 ret = sdhci_do_get_ro(host);
1619 sdhci_runtime_pm_put(host);
1620 return ret;
1621}
1622
1623static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1624{
Pierre Ossman1e728592008-04-16 19:13:13 +02001625 if (host->flags & SDHCI_DEVICE_DEAD)
1626 goto out;
1627
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001628 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001629 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1630 else
1631 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1632
1633 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1634 if (host->runtime_suspended)
1635 goto out;
1636
1637 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001638 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1639 else
1640 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001641out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001642 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001643}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001644
Adrian Hunter50accb92011-10-03 15:33:34 +03001645static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1646{
1647 struct sdhci_host *host = mmc_priv(mmc);
1648 unsigned long flags;
1649
1650 spin_lock_irqsave(&host->lock, flags);
1651 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001652 spin_unlock_irqrestore(&host->lock, flags);
1653}
1654
Adrian Hunter50accb92011-10-03 15:33:34 +03001655static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1656 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301657{
Arindam Nathf2119df2011-05-05 12:18:57 +05301658 u8 pwr;
1659 u16 clk, ctrl;
1660 u32 present_state;
1661
Arindam Nathf2119df2011-05-05 12:18:57 +05301662 /*
1663 * Signal Voltage Switching is only applicable for Host Controllers
1664 * v3.00 and above.
1665 */
1666 if (host->version < SDHCI_SPEC_300)
1667 return 0;
1668
1669 /*
1670 * We first check whether the request is to set signalling voltage
1671 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1672 */
1673 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1674 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1675 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1676 ctrl &= ~SDHCI_CTRL_VDD_180;
1677 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301678 if (host->ops->check_power_status)
1679 host->ops->check_power_status(host);
Arindam Nathf2119df2011-05-05 12:18:57 +05301680
1681 /* Wait for 5ms */
1682 usleep_range(5000, 5500);
1683
1684 /* 3.3V regulator output should be stable within 5 ms */
1685 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1686 if (!(ctrl & SDHCI_CTRL_VDD_180))
1687 return 0;
1688 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301689 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301690 "signalling voltage failed\n");
1691 return -EIO;
1692 }
1693 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1694 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1695 /* Stop SDCLK */
1696 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1697 clk &= ~SDHCI_CLOCK_CARD_EN;
1698 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1699
1700 /* Check whether DAT[3:0] is 0000 */
1701 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1702 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1703 SDHCI_DATA_LVL_SHIFT)) {
1704 /*
1705 * Enable 1.8V Signal Enable in the Host Control2
1706 * register
1707 */
1708 ctrl |= SDHCI_CTRL_VDD_180;
1709 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301710 if (host->ops->check_power_status)
1711 host->ops->check_power_status(host);
Arindam Nathf2119df2011-05-05 12:18:57 +05301712
1713 /* Wait for 5ms */
1714 usleep_range(5000, 5500);
1715
1716 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1717 if (ctrl & SDHCI_CTRL_VDD_180) {
1718 /* Provide SDCLK again and wait for 1ms*/
1719 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1720 clk |= SDHCI_CLOCK_CARD_EN;
1721 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1722 usleep_range(1000, 1500);
1723
1724 /*
1725 * If DAT[3:0] level is 1111b, then the card
1726 * was successfully switched to 1.8V signaling.
1727 */
1728 present_state = sdhci_readl(host,
1729 SDHCI_PRESENT_STATE);
1730 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1731 SDHCI_DATA_LVL_MASK)
1732 return 0;
1733 }
1734 }
1735
1736 /*
1737 * If we are here, that means the switch to 1.8V signaling
1738 * failed. We power cycle the card, and retry initialization
1739 * sequence by setting S18R to 0.
1740 */
1741 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1742 pwr &= ~SDHCI_POWER_ON;
1743 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301744 if (host->ops->check_power_status)
1745 host->ops->check_power_status(host);
Arindam Nathf2119df2011-05-05 12:18:57 +05301746
1747 /* Wait for 1ms as per the spec */
1748 usleep_range(1000, 1500);
1749 pwr |= SDHCI_POWER_ON;
1750 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301751 if (host->ops->check_power_status)
1752 host->ops->check_power_status(host);
Arindam Nathf2119df2011-05-05 12:18:57 +05301753
Sahitya Tummalaca422112013-02-22 12:15:54 +05301754 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301755 "voltage failed, retrying with S18R set to 0\n");
1756 return -EAGAIN;
1757 } else
1758 /* No signal voltage switch required */
1759 return 0;
1760}
1761
Adrian Hunter50accb92011-10-03 15:33:34 +03001762static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1763 struct mmc_ios *ios)
1764{
1765 struct sdhci_host *host = mmc_priv(mmc);
1766 int err;
1767
1768 if (host->version < SDHCI_SPEC_300)
1769 return 0;
1770 sdhci_runtime_pm_get(host);
1771 err = sdhci_do_start_signal_voltage_switch(host, ios);
1772 sdhci_runtime_pm_put(host);
1773 return err;
1774}
1775
Girish K S2cd06dc2012-01-06 09:56:39 +05301776static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301777{
1778 struct sdhci_host *host;
1779 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05301780 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05301781 int tuning_loop_counter = MAX_TUNING_LOOP;
1782 unsigned long timeout;
1783 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05301784 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301785
1786 host = mmc_priv(mmc);
1787
Adrian Hunter50accb92011-10-03 15:33:34 +03001788 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301789 disable_irq(host->irq);
1790 spin_lock(&host->lock);
1791
1792 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1793
1794 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05301795 * The Host Controller needs tuning only in case of SDR104 mode
1796 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301797 * Capabilities register.
Girish K S2cd06dc2012-01-06 09:56:39 +05301798 * If the Host Controller supports the HS200 mode then the
1799 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301800 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08001801 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1802 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
1803 (host->flags & SDHCI_HS200_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05301804 requires_tuning_nonuhs = true;
1805
Arindam Nathb513ea22011-05-05 12:19:04 +05301806 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301807 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301808 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1809 else {
1810 spin_unlock(&host->lock);
1811 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03001812 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301813 return 0;
1814 }
1815
Asutosh Das8ddd3482013-01-04 11:45:46 +05301816 if (host->ops->execute_tuning) {
1817 spin_unlock(&host->lock);
1818 enable_irq(host->irq);
1819 host->ops->execute_tuning(host, opcode);
1820 disable_irq(host->irq);
1821 spin_lock(&host->lock);
1822 goto out;
1823 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301824 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1825
1826 /*
1827 * As per the Host Controller spec v3.00, tuning command
1828 * generates Buffer Read Ready interrupt, so enable that.
1829 *
1830 * Note: The spec clearly says that when tuning sequence
1831 * is being performed, the controller does not generate
1832 * interrupts other than Buffer Read Ready interrupt. But
1833 * to make sure we don't hit a controller bug, we _only_
1834 * enable Buffer Read Ready interrupt here.
1835 */
1836 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1837 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1838
1839 /*
1840 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1841 * of loops reaches 40 times or a timeout of 150ms occurs.
1842 */
1843 timeout = 150;
1844 do {
1845 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03001846 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301847
1848 if (!tuning_loop_counter && !timeout)
1849 break;
1850
Girish K S2cd06dc2012-01-06 09:56:39 +05301851 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301852 cmd.arg = 0;
1853 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1854 cmd.retries = 0;
1855 cmd.data = NULL;
1856 cmd.error = 0;
1857
1858 mrq.cmd = &cmd;
1859 host->mrq = &mrq;
1860
1861 /*
1862 * In response to CMD19, the card sends 64 bytes of tuning
1863 * block to the Host Controller. So we set the block size
1864 * to 64 here.
1865 */
Girish K S2cd06dc2012-01-06 09:56:39 +05301866 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1867 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1868 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1869 SDHCI_BLOCK_SIZE);
1870 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1871 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1872 SDHCI_BLOCK_SIZE);
1873 } else {
1874 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1875 SDHCI_BLOCK_SIZE);
1876 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301877
1878 /*
1879 * The tuning block is sent by the card to the host controller.
1880 * So we set the TRNS_READ bit in the Transfer Mode register.
1881 * This also takes care of setting DMA Enable and Multi Block
1882 * Select in the same register to 0.
1883 */
1884 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1885
1886 sdhci_send_command(host, &cmd);
1887
1888 host->cmd = NULL;
1889 host->mrq = NULL;
1890
1891 spin_unlock(&host->lock);
1892 enable_irq(host->irq);
1893
1894 /* Wait for Buffer Read Ready interrupt */
1895 wait_event_interruptible_timeout(host->buf_ready_int,
1896 (host->tuning_done == 1),
1897 msecs_to_jiffies(50));
1898 disable_irq(host->irq);
1899 spin_lock(&host->lock);
1900
1901 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301902 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301903 "Buffer Read Ready interrupt during tuning "
1904 "procedure, falling back to fixed sampling "
1905 "clock\n");
1906 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1907 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1908 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1909 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1910
1911 err = -EIO;
1912 goto out;
1913 }
1914
1915 host->tuning_done = 0;
1916
1917 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1918 tuning_loop_counter--;
1919 timeout--;
1920 mdelay(1);
1921 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1922
1923 /*
1924 * The Host Driver has exhausted the maximum number of loops allowed,
1925 * so use fixed sampling frequency.
1926 */
1927 if (!tuning_loop_counter || !timeout) {
1928 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1929 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1930 } else {
1931 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301932 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301933 " failed, falling back to fixed sampling"
1934 " clock\n");
1935 err = -EIO;
1936 }
1937 }
1938
1939out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301940 /*
1941 * If this is the very first time we are here, we start the retuning
1942 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1943 * flag won't be set, we check this condition before actually starting
1944 * the timer.
1945 */
1946 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1947 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
1948 mod_timer(&host->tuning_timer, jiffies +
1949 host->tuning_count * HZ);
1950 /* Tuning mode 1 limits the maximum data length to 4MB */
1951 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1952 } else {
1953 host->flags &= ~SDHCI_NEEDS_RETUNING;
1954 /* Reload the new initial value for timer */
1955 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1956 mod_timer(&host->tuning_timer, jiffies +
1957 host->tuning_count * HZ);
1958 }
1959
1960 /*
1961 * In case tuning fails, host controllers which support re-tuning can
1962 * try tuning again at a later time, when the re-tuning timer expires.
1963 * So for these controllers, we return 0. Since there might be other
1964 * controllers who do not have this capability, we return error for
1965 * them.
1966 */
1967 if (err && host->tuning_count &&
1968 host->tuning_mode == SDHCI_TUNING_MODE_1)
1969 err = 0;
1970
Arindam Nathb513ea22011-05-05 12:19:04 +05301971 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1972 spin_unlock(&host->lock);
1973 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03001974 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301975
1976 return err;
1977}
1978
Adrian Hunter50accb92011-10-03 15:33:34 +03001979static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301980{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301981 u16 ctrl;
1982 unsigned long flags;
1983
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301984 /* Host Controller v3.00 defines preset value registers */
1985 if (host->version < SDHCI_SPEC_300)
1986 return;
1987
1988 spin_lock_irqsave(&host->lock, flags);
1989
1990 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1991
1992 /*
1993 * We only enable or disable Preset Value if they are not already
1994 * enabled or disabled respectively. Otherwise, we bail out.
1995 */
1996 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1997 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1998 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03001999 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302000 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2001 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2002 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002003 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302004 }
2005
2006 spin_unlock_irqrestore(&host->lock, flags);
2007}
2008
Adrian Hunter50accb92011-10-03 15:33:34 +03002009static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
2010{
2011 struct sdhci_host *host = mmc_priv(mmc);
2012
2013 sdhci_runtime_pm_get(host);
2014 sdhci_do_enable_preset_value(host, enable);
2015 sdhci_runtime_pm_put(host);
2016}
2017
David Brownellab7aefd2006-11-12 17:55:30 -08002018static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002019 .request = sdhci_request,
2020 .set_ios = sdhci_set_ios,
2021 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03002022 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002023 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302024 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302025 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302026 .enable_preset_value = sdhci_enable_preset_value,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302027 .enable = sdhci_enable,
2028 .disable = sdhci_disable,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002029};
2030
2031/*****************************************************************************\
2032 * *
2033 * Tasklets *
2034 * *
2035\*****************************************************************************/
2036
2037static void sdhci_tasklet_card(unsigned long param)
2038{
2039 struct sdhci_host *host;
2040 unsigned long flags;
2041
2042 host = (struct sdhci_host*)param;
2043
2044 spin_lock_irqsave(&host->lock, flags);
2045
Adrian Hunter50accb92011-10-03 15:33:34 +03002046 /* Check host->mrq first in case we are runtime suspended */
2047 if (host->mrq &&
2048 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302049 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002050 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05302051 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002052 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002053
Adrian Hunter50accb92011-10-03 15:33:34 +03002054 sdhci_reset(host, SDHCI_RESET_CMD);
2055 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002056
Adrian Hunter50accb92011-10-03 15:33:34 +03002057 host->mrq->cmd->error = -ENOMEDIUM;
2058 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002059 }
2060
2061 spin_unlock_irqrestore(&host->lock, flags);
2062
Pierre Ossman04cf5852008-08-18 22:18:14 +02002063 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002064}
2065
2066static void sdhci_tasklet_finish(unsigned long param)
2067{
2068 struct sdhci_host *host;
2069 unsigned long flags;
2070 struct mmc_request *mrq;
2071
2072 host = (struct sdhci_host*)param;
2073
Adrian Hunter50accb92011-10-03 15:33:34 +03002074 spin_lock_irqsave(&host->lock, flags);
2075
Chris Ball0c9c99a2011-04-27 17:35:31 -04002076 /*
2077 * If this tasklet gets rescheduled while running, it will
2078 * be run again afterwards but without any active request.
2079 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002080 if (!host->mrq) {
2081 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002082 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002083 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002084
2085 del_timer(&host->timer);
2086
2087 mrq = host->mrq;
2088
Pierre Ossmand129bce2006-03-24 03:18:17 -08002089 /*
2090 * The controller needs a reset of internal state machines
2091 * upon error conditions.
2092 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002093 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002094 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002095 (mrq->data && (mrq->data->error ||
2096 (mrq->data->stop && mrq->data->stop->error))) ||
2097 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002098
2099 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002100 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002101 unsigned int clock;
2102
2103 /* This is to force an update */
2104 clock = host->clock;
2105 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302106 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002107 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302108 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002109 }
2110
2111 /* Spec says we should do both at the same time, but Ricoh
2112 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002113 sdhci_reset(host, SDHCI_RESET_CMD);
2114 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002115 } else {
2116 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2117 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002118 }
2119
2120 host->mrq = NULL;
2121 host->cmd = NULL;
2122 host->data = NULL;
2123
Pierre Ossmanf9134312008-12-21 17:01:48 +01002124#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002125 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002126#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002127
Pierre Ossman5f25a662006-10-04 02:15:39 -07002128 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002129 spin_unlock_irqrestore(&host->lock, flags);
2130
2131 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002132 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002133}
2134
2135static void sdhci_timeout_timer(unsigned long data)
2136{
2137 struct sdhci_host *host;
2138 unsigned long flags;
2139
2140 host = (struct sdhci_host*)data;
2141
2142 spin_lock_irqsave(&host->lock, flags);
2143
2144 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302145 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002146 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002147 sdhci_dumpregs(host);
2148
2149 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002150 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002151 sdhci_finish_data(host);
2152 } else {
2153 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002154 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002155 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002156 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002157
2158 tasklet_schedule(&host->finish_tasklet);
2159 }
2160 }
2161
Pierre Ossman5f25a662006-10-04 02:15:39 -07002162 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002163 spin_unlock_irqrestore(&host->lock, flags);
2164}
2165
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302166static void sdhci_tuning_timer(unsigned long data)
2167{
2168 struct sdhci_host *host;
2169 unsigned long flags;
2170
2171 host = (struct sdhci_host *)data;
2172
2173 spin_lock_irqsave(&host->lock, flags);
2174
2175 host->flags |= SDHCI_NEEDS_RETUNING;
2176
2177 spin_unlock_irqrestore(&host->lock, flags);
2178}
2179
Pierre Ossmand129bce2006-03-24 03:18:17 -08002180/*****************************************************************************\
2181 * *
2182 * Interrupt handling *
2183 * *
2184\*****************************************************************************/
2185
2186static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2187{
2188 BUG_ON(intmask == 0);
2189
2190 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302191 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002192 "though no command operation was in progress.\n",
2193 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002194 sdhci_dumpregs(host);
2195 return;
2196 }
2197
Pierre Ossman43b58b32007-07-25 23:15:27 +02002198 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002199 host->cmd->error = -ETIMEDOUT;
2200 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2201 SDHCI_INT_INDEX))
2202 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002203
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302204 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2205 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2206 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2207 if (intmask & SDHCI_INT_CRC) {
2208 sdhci_reset(host, SDHCI_RESET_CMD);
2209 host->cmd->error = 0;
2210 }
2211 }
2212 }
2213
Pierre Ossmane8095172008-07-25 01:09:08 +02002214 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002215 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002216 return;
2217 }
2218
2219 /*
2220 * The host can send and interrupt when the busy state has
2221 * ended, allowing us to wait without wasting CPU cycles.
2222 * Unfortunately this is overloaded on the "data complete"
2223 * interrupt, so we need to take some care when handling
2224 * it.
2225 *
2226 * Note: The 1.0 specification is a bit ambiguous about this
2227 * feature so there might be some problems with older
2228 * controllers.
2229 */
2230 if (host->cmd->flags & MMC_RSP_BUSY) {
2231 if (host->cmd->data)
2232 DBG("Cannot wait for busy signal when also "
2233 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002234 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002235 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002236
2237 /* The controller does not support the end-of-busy IRQ,
2238 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002239 }
2240
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302241 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2242 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2243 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2244 if (intmask & SDHCI_INT_CRC) {
2245 sdhci_finish_command(host);
2246 return;
2247 }
2248 }
2249 }
2250
Pierre Ossmane8095172008-07-25 01:09:08 +02002251 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002252 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002253}
2254
George G. Davis0957c332010-02-18 12:32:12 -05002255#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002256static void sdhci_show_adma_error(struct sdhci_host *host)
2257{
2258 const char *name = mmc_hostname(host->mmc);
2259 u8 *desc = host->adma_desc;
2260 __le32 *dma;
2261 __le16 *len;
2262 u8 attr;
2263
2264 sdhci_dumpregs(host);
2265
2266 while (true) {
2267 dma = (__le32 *)(desc + 4);
2268 len = (__le16 *)(desc + 2);
2269 attr = *desc;
2270
2271 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2272 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2273
2274 desc += 8;
2275
2276 if (attr & 2)
2277 break;
2278 }
2279}
2280#else
2281static void sdhci_show_adma_error(struct sdhci_host *host) { }
2282#endif
2283
Pierre Ossmand129bce2006-03-24 03:18:17 -08002284static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2285{
Girish K S2cd06dc2012-01-06 09:56:39 +05302286 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002287 BUG_ON(intmask == 0);
2288
Arindam Nathb513ea22011-05-05 12:19:04 +05302289 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2290 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302291 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2292 if (command == MMC_SEND_TUNING_BLOCK ||
2293 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302294 host->tuning_done = 1;
2295 wake_up(&host->buf_ready_int);
2296 return;
2297 }
2298 }
2299
Pierre Ossmand129bce2006-03-24 03:18:17 -08002300 if (!host->data) {
2301 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002302 * The "data complete" interrupt is also used to
2303 * indicate that a busy state has ended. See comment
2304 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002305 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002306 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2307 if (intmask & SDHCI_INT_DATA_END) {
2308 sdhci_finish_command(host);
2309 return;
2310 }
2311 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002312
Sahitya Tummalaca422112013-02-22 12:15:54 +05302313 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002314 "though no data operation was in progress.\n",
2315 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002316 sdhci_dumpregs(host);
2317
2318 return;
2319 }
2320
2321 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002322 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002323 else if (intmask & SDHCI_INT_DATA_END_BIT)
2324 host->data->error = -EILSEQ;
2325 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2326 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2327 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002328 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002329 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302330 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002331 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002332 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002333 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002334
Pierre Ossman17b04292007-07-22 22:18:46 +02002335 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002336 sdhci_finish_data(host);
2337 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002338 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002339 sdhci_transfer_pio(host);
2340
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002341 /*
2342 * We currently don't do anything fancy with DMA
2343 * boundaries, but as we can't disable the feature
2344 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002345 *
2346 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2347 * should return a valid address to continue from, but as
2348 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002349 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002350 if (intmask & SDHCI_INT_DMA_END) {
2351 u32 dmastart, dmanow;
2352 dmastart = sg_dma_address(host->data->sg);
2353 dmanow = dmastart + host->data->bytes_xfered;
2354 /*
2355 * Force update to the next DMA block boundary.
2356 */
2357 dmanow = (dmanow &
2358 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2359 SDHCI_DEFAULT_BOUNDARY_SIZE;
2360 host->data->bytes_xfered = dmanow - dmastart;
2361 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2362 " next 0x%08x\n",
2363 mmc_hostname(host->mmc), dmastart,
2364 host->data->bytes_xfered, dmanow);
2365 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2366 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002367
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002368 if (intmask & SDHCI_INT_DATA_END) {
2369 if (host->cmd) {
2370 /*
2371 * Data managed to finish before the
2372 * command completed. Make sure we do
2373 * things in the proper order.
2374 */
2375 host->data_early = 1;
2376 } else {
2377 sdhci_finish_data(host);
2378 }
2379 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002380 }
2381}
2382
David Howells7d12e782006-10-05 14:55:46 +01002383static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002384{
2385 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002386 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002387 u32 intmask, unexpected = 0;
2388 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002389
2390 spin_lock(&host->lock);
2391
Adrian Hunter50accb92011-10-03 15:33:34 +03002392 if (host->runtime_suspended) {
2393 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302394 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002395 mmc_hostname(host->mmc));
2396 return IRQ_HANDLED;
2397 }
2398
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002399 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002400
Mark Lord62df67a2007-03-06 13:30:13 +01002401 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002402 result = IRQ_NONE;
2403 goto out;
2404 }
2405
Alexander Stein6379b232012-03-14 09:52:10 +01002406again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002407 DBG("*** %s got interrupt: 0x%08x\n",
2408 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002409
Pierre Ossman3192a282006-06-30 02:22:26 -07002410 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302411 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2412 SDHCI_CARD_PRESENT;
2413
2414 /*
2415 * There is a observation on i.mx esdhc. INSERT bit will be
2416 * immediately set again when it gets cleared, if a card is
2417 * inserted. We have to mask the irq to prevent interrupt
2418 * storm which will freeze the system. And the REMOVE gets
2419 * the same situation.
2420 *
2421 * More testing are needed here to ensure it works for other
2422 * platforms though.
2423 */
2424 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2425 SDHCI_INT_CARD_REMOVE);
2426 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2427 SDHCI_INT_CARD_INSERT);
2428
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002429 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302430 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2431 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002432 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002433 }
2434
Pierre Ossmand129bce2006-03-24 03:18:17 -08002435 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002436 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2437 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002438 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2439 (host->clock <= 400000))
2440 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002441 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002442 }
2443
2444 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002445 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2446 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002447 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2448 (host->clock <= 400000))
2449 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002450 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002451 }
2452
2453 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2454
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002455 intmask &= ~SDHCI_INT_ERROR;
2456
Pierre Ossmand129bce2006-03-24 03:18:17 -08002457 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302458 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002459 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002460 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002461 }
2462
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002463 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002464
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002465 if (intmask & SDHCI_INT_CARD_INT)
2466 cardint = 1;
2467
2468 intmask &= ~SDHCI_INT_CARD_INT;
2469
Pierre Ossman3192a282006-06-30 02:22:26 -07002470 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002471 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002472 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002473 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002474
2475 result = IRQ_HANDLED;
2476
Alexander Stein6379b232012-03-14 09:52:10 +01002477 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2478 if (intmask && --max_loops)
2479 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002480out:
2481 spin_unlock(&host->lock);
2482
Alexander Stein6379b232012-03-14 09:52:10 +01002483 if (unexpected) {
2484 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2485 mmc_hostname(host->mmc), unexpected);
2486 sdhci_dumpregs(host);
2487 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002488 /*
2489 * We have to delay this as it calls back into the driver.
2490 */
2491 if (cardint)
2492 mmc_signal_sdio_irq(host->mmc);
2493
Pierre Ossmand129bce2006-03-24 03:18:17 -08002494 return result;
2495}
2496
2497/*****************************************************************************\
2498 * *
2499 * Suspend/resume *
2500 * *
2501\*****************************************************************************/
2502
2503#ifdef CONFIG_PM
2504
Manuel Laussd72faa62011-11-03 11:09:45 +01002505int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002506{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002507 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302508 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002509
Chris Balla1b13b42012-02-06 00:43:59 -05002510 if (host->ops->platform_suspend)
2511 host->ops->platform_suspend(host);
2512
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002513 sdhci_disable_card_detection(host);
2514
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302515 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302516 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2517 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2518 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002519 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302520 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302521 }
2522
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002523 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302524 if (ret) {
2525 if (has_tuning_timer) {
2526 host->flags |= SDHCI_NEEDS_RETUNING;
2527 mod_timer(&host->tuning_timer, jiffies +
2528 host->tuning_count * HZ);
2529 }
2530
2531 sdhci_enable_card_detection(host);
2532
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002533 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302534 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002535
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002536 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002537
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002538 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002539}
2540
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002541EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002542
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002543int sdhci_resume_host(struct sdhci_host *host)
2544{
2545 int ret;
2546
Richard Röjforsa13abc72009-09-22 16:45:30 -07002547 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002548 if (host->ops->enable_dma)
2549 host->ops->enable_dma(host);
2550 }
2551
2552 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2553 mmc_hostname(host->mmc), host);
2554 if (ret)
2555 return ret;
2556
Adrian Hunter6308d292012-02-07 14:48:54 +02002557 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2558 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2559 /* Card keeps power but host controller does not */
2560 sdhci_init(host, 0);
2561 host->pwr = 0;
2562 host->clock = 0;
2563 sdhci_do_set_ios(host, &host->mmc->ios);
2564 } else {
2565 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2566 mmiowb();
2567 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002568
2569 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002570 sdhci_enable_card_detection(host);
2571
Chris Balla1b13b42012-02-06 00:43:59 -05002572 if (host->ops->platform_resume)
2573 host->ops->platform_resume(host);
2574
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302575 /* Set the re-tuning expiration flag */
2576 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2577 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2578 host->flags |= SDHCI_NEEDS_RETUNING;
2579
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002580 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002581}
2582
2583EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002584
Daniel Drake5f619702010-11-04 22:20:39 +00002585void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2586{
2587 u8 val;
2588 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2589 val |= SDHCI_WAKE_ON_INT;
2590 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2591}
2592
2593EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2594
Pierre Ossmand129bce2006-03-24 03:18:17 -08002595#endif /* CONFIG_PM */
2596
Adrian Hunter50accb92011-10-03 15:33:34 +03002597#ifdef CONFIG_PM_RUNTIME
2598
2599static int sdhci_runtime_pm_get(struct sdhci_host *host)
2600{
2601 return pm_runtime_get_sync(host->mmc->parent);
2602}
2603
2604static int sdhci_runtime_pm_put(struct sdhci_host *host)
2605{
2606 pm_runtime_mark_last_busy(host->mmc->parent);
2607 return pm_runtime_put_autosuspend(host->mmc->parent);
2608}
2609
2610int sdhci_runtime_suspend_host(struct sdhci_host *host)
2611{
2612 unsigned long flags;
2613 int ret = 0;
2614
2615 /* Disable tuning since we are suspending */
2616 if (host->version >= SDHCI_SPEC_300 &&
2617 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2618 del_timer_sync(&host->tuning_timer);
2619 host->flags &= ~SDHCI_NEEDS_RETUNING;
2620 }
2621
2622 spin_lock_irqsave(&host->lock, flags);
2623 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2624 spin_unlock_irqrestore(&host->lock, flags);
2625
2626 synchronize_irq(host->irq);
2627
2628 spin_lock_irqsave(&host->lock, flags);
2629 host->runtime_suspended = true;
2630 spin_unlock_irqrestore(&host->lock, flags);
2631
2632 return ret;
2633}
2634EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2635
2636int sdhci_runtime_resume_host(struct sdhci_host *host)
2637{
2638 unsigned long flags;
2639 int ret = 0, host_flags = host->flags;
2640
2641 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2642 if (host->ops->enable_dma)
2643 host->ops->enable_dma(host);
2644 }
2645
2646 sdhci_init(host, 0);
2647
2648 /* Force clock and power re-program */
2649 host->pwr = 0;
2650 host->clock = 0;
2651 sdhci_do_set_ios(host, &host->mmc->ios);
2652
2653 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2654 if (host_flags & SDHCI_PV_ENABLED)
2655 sdhci_do_enable_preset_value(host, true);
2656
2657 /* Set the re-tuning expiration flag */
2658 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2659 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2660 host->flags |= SDHCI_NEEDS_RETUNING;
2661
2662 spin_lock_irqsave(&host->lock, flags);
2663
2664 host->runtime_suspended = false;
2665
2666 /* Enable SDIO IRQ */
2667 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2668 sdhci_enable_sdio_irq_nolock(host, true);
2669
2670 /* Enable Card Detection */
2671 sdhci_enable_card_detection(host);
2672
2673 spin_unlock_irqrestore(&host->lock, flags);
2674
2675 return ret;
2676}
2677EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2678
2679#endif
2680
Pierre Ossmand129bce2006-03-24 03:18:17 -08002681/*****************************************************************************\
2682 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002683 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002684 * *
2685\*****************************************************************************/
2686
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002687struct sdhci_host *sdhci_alloc_host(struct device *dev,
2688 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002689{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002690 struct mmc_host *mmc;
2691 struct sdhci_host *host;
2692
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002693 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002694
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002695 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002696 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002697 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002698
2699 host = mmc_priv(mmc);
2700 host->mmc = mmc;
2701
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002702 return host;
2703}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002704
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002705EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002706
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002707int sdhci_add_host(struct sdhci_host *host)
2708{
2709 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302710 u32 caps[2];
2711 u32 max_current_caps;
2712 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002713 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002714
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002715 WARN_ON(host == NULL);
2716 if (host == NULL)
2717 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002718
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002719 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002720
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002721 if (debug_quirks)
2722 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03002723 if (debug_quirks2)
2724 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002725
Pierre Ossmand96649e2006-06-30 02:22:30 -07002726 sdhci_reset(host, SDHCI_RESET_ALL);
2727
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002728 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002729 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2730 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002731 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302732 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002733 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002734 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002735 }
2736
Arindam Nathf2119df2011-05-05 12:18:57 +05302737 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002738 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002739
Arindam Nathf2119df2011-05-05 12:18:57 +05302740 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2741 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2742
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002743 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002744 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302745 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002746 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002747 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002748 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002749
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002750 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002751 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002752 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002753 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002754 }
2755
Arindam Nathf2119df2011-05-05 12:18:57 +05302756 if ((host->version >= SDHCI_SPEC_200) &&
2757 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002758 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002759
2760 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2761 (host->flags & SDHCI_USE_ADMA)) {
2762 DBG("Disabling ADMA as it is marked broken\n");
2763 host->flags &= ~SDHCI_USE_ADMA;
2764 }
2765
Richard Röjforsa13abc72009-09-22 16:45:30 -07002766 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002767 if (host->ops->enable_dma) {
2768 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302769 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002770 "available. Falling back to PIO.\n",
2771 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002772 host->flags &=
2773 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002774 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002775 }
2776 }
2777
Pierre Ossman2134a922008-06-28 18:28:51 +02002778 if (host->flags & SDHCI_USE_ADMA) {
2779 /*
2780 * We need to allocate descriptors for all sg entries
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302781 * (128/max_segments) and potentially one alignment transfer for
Pierre Ossman2134a922008-06-28 18:28:51 +02002782 * each of those entries.
2783 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302784 if (host->ops->get_max_segments)
2785 host->adma_max_desc = host->ops->get_max_segments();
2786 else
2787 host->adma_max_desc = 128;
2788
2789 host->adma_desc_sz = (host->adma_max_desc * 2 + 1) * 4;
2790 host->align_buf_sz = host->adma_max_desc * 4;
2791
2792 pr_debug("%s: %s: dma_desc_size: %d\n",
2793 mmc_hostname(host->mmc), __func__, host->adma_desc_sz);
2794 host->adma_desc = kmalloc(host->adma_desc_sz,
2795 GFP_KERNEL);
2796 host->align_buffer = kmalloc(host->align_buf_sz,
2797 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02002798 if (!host->adma_desc || !host->align_buffer) {
2799 kfree(host->adma_desc);
2800 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302801 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002802 "buffers. Falling back to standard DMA.\n",
2803 mmc_hostname(mmc));
2804 host->flags &= ~SDHCI_USE_ADMA;
2805 }
2806 }
2807
Pierre Ossman76591502008-07-21 00:32:11 +02002808 /*
2809 * If we use DMA, then it's up to the caller to set the DMA
2810 * mask, but PIO does not need the hw shim so we set a new
2811 * mask here in that case.
2812 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002813 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002814 host->dma_mask = DMA_BIT_MASK(64);
2815 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2816 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002817
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002818 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302819 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002820 >> SDHCI_CLOCK_BASE_SHIFT;
2821 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302822 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002823 >> SDHCI_CLOCK_BASE_SHIFT;
2824
Pierre Ossmand129bce2006-03-24 03:18:17 -08002825 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002826 if (host->max_clk == 0 || host->quirks &
2827 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002828 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302829 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002830 "frequency.\n", mmc_hostname(mmc));
2831 return -ENODEV;
2832 }
2833 host->max_clk = host->ops->get_max_clock(host);
2834 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002835
2836 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302837 * In case of Host Controller v3.00, find out whether clock
2838 * multiplier is supported.
2839 */
2840 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2841 SDHCI_CLOCK_MUL_SHIFT;
2842
2843 /*
2844 * In case the value in Clock Multiplier is 0, then programmable
2845 * clock mode is not supported, otherwise the actual clock
2846 * multiplier is one more than the value of Clock Multiplier
2847 * in the Capabilities Register.
2848 */
2849 if (host->clk_mul)
2850 host->clk_mul += 1;
2851
2852 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002853 * Set host parameters.
2854 */
2855 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302856 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002857 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002858 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302859 else if (host->version >= SDHCI_SPEC_300) {
2860 if (host->clk_mul) {
2861 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2862 mmc->f_max = host->max_clk * host->clk_mul;
2863 } else
2864 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2865 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002866 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002867
Sahitya Tummalaca422112013-02-22 12:15:54 +05302868 host->timeout_clk =
2869 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2870 if (host->timeout_clk == 0) {
2871 if (host->ops->get_timeout_clock) {
2872 host->timeout_clk = host->ops->get_timeout_clock(host);
2873 } else if (!(host->quirks &
2874 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2875 pr_err("%s: Hardware doesn't specify timeout clock "
2876 "frequency.\n", mmc_hostname(mmc));
2877 return -ENODEV;
2878 }
2879 }
2880 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2881 host->timeout_clk *= 1000;
2882
2883 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
2884 host->timeout_clk = mmc->f_max / 1000;
2885
2886 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
2887
Andrei Warkentine89d4562011-05-23 15:06:37 -05002888 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2889
2890 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2891 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002892
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002893 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002894 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002895 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002896 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002897 host->flags |= SDHCI_AUTO_CMD23;
2898 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2899 } else {
2900 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2901 }
2902
Philip Rakity15ec4462010-11-19 16:48:39 -05002903 /*
2904 * A controller may support 8-bit width, but the board itself
2905 * might not have the pins brought out. Boards that support
2906 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2907 * their platform code before calling sdhci_add_host(), and we
2908 * won't assume 8-bit width for hosts without that CAP.
2909 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002910 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002911 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002912
Arindam Nathf2119df2011-05-05 12:18:57 +05302913 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002914 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002915
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002916 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2917 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002918 mmc->caps |= MMC_CAP_NEEDS_POLL;
2919
Al Cooper4188bba2012-03-16 15:54:17 -04002920 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
2921 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2922 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05302923 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2924
2925 /* SDR104 supports also implies SDR50 support */
2926 if (caps[1] & SDHCI_SUPPORT_SDR104)
2927 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2928 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2929 mmc->caps |= MMC_CAP_UHS_SDR50;
2930
2931 if (caps[1] & SDHCI_SUPPORT_DDR50)
2932 mmc->caps |= MMC_CAP_UHS_DDR50;
2933
Girish K S2cd06dc2012-01-06 09:56:39 +05302934 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05302935 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2936 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2937
Girish K S2cd06dc2012-01-06 09:56:39 +05302938 /* Does the host need tuning for HS200? */
2939 if (mmc->caps2 & MMC_CAP2_HS200)
2940 host->flags |= SDHCI_HS200_NEEDS_TUNING;
2941
Arindam Nathd6d50a12011-05-05 12:18:59 +05302942 /* Driver Type(s) (A, C, D) supported by the host */
2943 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2944 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2945 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2946 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2947 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2948 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2949
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02002950 /* Initial value for re-tuning timer count */
2951 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2952 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2953
2954 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302955 * In case Re-tuning Timer is not disabled, the actual value of
2956 * re-tuning timer will be 2 ^ (n - 1).
2957 */
2958 if (host->tuning_count)
2959 host->tuning_count = 1 << (host->tuning_count - 1);
2960
2961 /* Re-tuning mode supported by the Host Controller */
2962 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2963 SDHCI_RETUNING_MODE_SHIFT;
2964
Takashi Iwai8f230f42010-12-08 10:04:30 +01002965 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05302966 /*
2967 * According to SD Host Controller spec v3.00, if the Host System
2968 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2969 * the value is meaningful only if Voltage Support in the Capabilities
2970 * register is set. The actual current value is 4 times the register
2971 * value.
2972 */
2973 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2974
2975 if (caps[0] & SDHCI_CAN_VDD_330) {
2976 int max_current_330;
2977
Takashi Iwai8f230f42010-12-08 10:04:30 +01002978 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302979
2980 max_current_330 = ((max_current_caps &
2981 SDHCI_MAX_CURRENT_330_MASK) >>
2982 SDHCI_MAX_CURRENT_330_SHIFT) *
2983 SDHCI_MAX_CURRENT_MULTIPLIER;
2984
2985 if (max_current_330 > 150)
2986 mmc->caps |= MMC_CAP_SET_XPC_330;
2987 }
2988 if (caps[0] & SDHCI_CAN_VDD_300) {
2989 int max_current_300;
2990
Takashi Iwai8f230f42010-12-08 10:04:30 +01002991 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302992
2993 max_current_300 = ((max_current_caps &
2994 SDHCI_MAX_CURRENT_300_MASK) >>
2995 SDHCI_MAX_CURRENT_300_SHIFT) *
2996 SDHCI_MAX_CURRENT_MULTIPLIER;
2997
2998 if (max_current_300 > 150)
2999 mmc->caps |= MMC_CAP_SET_XPC_300;
3000 }
3001 if (caps[0] & SDHCI_CAN_VDD_180) {
3002 int max_current_180;
3003
Takashi Iwai8f230f42010-12-08 10:04:30 +01003004 ocr_avail |= MMC_VDD_165_195;
3005
Arindam Nathf2119df2011-05-05 12:18:57 +05303006 max_current_180 = ((max_current_caps &
3007 SDHCI_MAX_CURRENT_180_MASK) >>
3008 SDHCI_MAX_CURRENT_180_SHIFT) *
3009 SDHCI_MAX_CURRENT_MULTIPLIER;
3010
3011 if (max_current_180 > 150)
3012 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05303013
3014 /* Maximum current capabilities of the host at 1.8V */
3015 if (max_current_180 >= 800)
3016 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
3017 else if (max_current_180 >= 600)
3018 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
3019 else if (max_current_180 >= 400)
3020 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
3021 else
3022 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05303023 }
3024
Takashi Iwai8f230f42010-12-08 10:04:30 +01003025 mmc->ocr_avail = ocr_avail;
3026 mmc->ocr_avail_sdio = ocr_avail;
3027 if (host->ocr_avail_sdio)
3028 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3029 mmc->ocr_avail_sd = ocr_avail;
3030 if (host->ocr_avail_sd)
3031 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3032 else /* normal SD controllers don't support 1.8V */
3033 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3034 mmc->ocr_avail_mmc = ocr_avail;
3035 if (host->ocr_avail_mmc)
3036 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003037
3038 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303039 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003040 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003041 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003042 }
3043
Pierre Ossmand129bce2006-03-24 03:18:17 -08003044 spin_lock_init(&host->lock);
3045
3046 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003047 * Maximum number of segments. Depends on if the hardware
3048 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003049 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003050 if (host->flags & SDHCI_USE_ADMA)
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303051 mmc->max_segs = host->adma_max_desc;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003052 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003053 mmc->max_segs = 1;
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303054 else/* PIO */
3055 mmc->max_segs = host->adma_max_desc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003056
3057 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003058 * Maximum number of sectors in one transfer. Limited by DMA boundary
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303059 * size (512KiB), unless specified by platform specific driver. Each
3060 * descriptor can transfer a maximum of 64KB.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003061 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303062 if (host->ops->get_max_segments)
3063 mmc->max_req_size = (host->adma_max_desc * 65536);
3064 else
3065 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003066
3067 /*
3068 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003069 * of bytes. When doing hardware scatter/gather, each entry cannot
3070 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003071 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003072 if (host->flags & SDHCI_USE_ADMA) {
3073 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3074 mmc->max_seg_size = 65535;
3075 else
3076 mmc->max_seg_size = 65536;
3077 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003078 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003079 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003080
3081 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003082 * Maximum block size. This varies from controller to controller and
3083 * is specified in the capabilities register.
3084 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003085 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3086 mmc->max_blk_size = 2;
3087 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303088 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003089 SDHCI_MAX_BLOCK_SHIFT;
3090 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303091 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003092 "assuming 512 bytes\n", mmc_hostname(mmc));
3093 mmc->max_blk_size = 0;
3094 }
3095 }
3096
3097 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003098
3099 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003100 * Maximum block count.
3101 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003102 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003103
3104 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003105 * Init tasklets.
3106 */
3107 tasklet_init(&host->card_tasklet,
3108 sdhci_tasklet_card, (unsigned long)host);
3109 tasklet_init(&host->finish_tasklet,
3110 sdhci_tasklet_finish, (unsigned long)host);
3111
Al Viroe4cad1b2006-10-10 22:47:07 +01003112 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003113
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303114 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303115 init_waitqueue_head(&host->buf_ready_int);
3116
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303117 /* Initialize re-tuning timer */
3118 init_timer(&host->tuning_timer);
3119 host->tuning_timer.data = (unsigned long)host;
3120 host->tuning_timer.function = sdhci_tuning_timer;
3121 }
3122
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003123 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003124 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003125 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003126 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003127
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003128 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3129 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303130 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003131 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003132 }
3133
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003134 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003135
3136#ifdef CONFIG_MMC_DEBUG
3137 sdhci_dumpregs(host);
3138#endif
3139
Pierre Ossmanf9134312008-12-21 17:01:48 +01003140#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003141 snprintf(host->led_name, sizeof(host->led_name),
3142 "%s::", mmc_hostname(mmc));
3143 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003144 host->led.brightness = LED_OFF;
3145 host->led.default_trigger = mmc_hostname(mmc);
3146 host->led.brightness_set = sdhci_led_control;
3147
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003148 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003149 if (ret)
3150 goto reset;
3151#endif
3152
Pierre Ossman5f25a662006-10-04 02:15:39 -07003153 mmiowb();
3154
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303155 if (host->cpu_dma_latency_us)
3156 pm_qos_add_request(&host->pm_qos_req_dma,
3157 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003158 mmc_add_host(mmc);
3159
Sahitya Tummalaca422112013-02-22 12:15:54 +05303160 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003161 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003162 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3163 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003164
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003165 sdhci_enable_card_detection(host);
3166
Pierre Ossmand129bce2006-03-24 03:18:17 -08003167 return 0;
3168
Pierre Ossmanf9134312008-12-21 17:01:48 +01003169#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003170reset:
3171 sdhci_reset(host, SDHCI_RESET_ALL);
3172 free_irq(host->irq, host);
3173#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003174untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003175 tasklet_kill(&host->card_tasklet);
3176 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003177
3178 return ret;
3179}
3180
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003181EXPORT_SYMBOL_GPL(sdhci_add_host);
3182
Pierre Ossman1e728592008-04-16 19:13:13 +02003183void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003184{
Pierre Ossman1e728592008-04-16 19:13:13 +02003185 unsigned long flags;
3186
3187 if (dead) {
3188 spin_lock_irqsave(&host->lock, flags);
3189
3190 host->flags |= SDHCI_DEVICE_DEAD;
3191
3192 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303193 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003194 " transfer!\n", mmc_hostname(host->mmc));
3195
3196 host->mrq->cmd->error = -ENOMEDIUM;
3197 tasklet_schedule(&host->finish_tasklet);
3198 }
3199
3200 spin_unlock_irqrestore(&host->lock, flags);
3201 }
3202
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003203 sdhci_disable_card_detection(host);
3204
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303205 if (host->cpu_dma_latency_us)
3206 pm_qos_remove_request(&host->pm_qos_req_dma);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003207 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003208
Pierre Ossmanf9134312008-12-21 17:01:48 +01003209#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003210 led_classdev_unregister(&host->led);
3211#endif
3212
Pierre Ossman1e728592008-04-16 19:13:13 +02003213 if (!dead)
3214 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003215
3216 free_irq(host->irq, host);
3217
3218 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303219 if (host->version >= SDHCI_SPEC_300)
3220 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003221
3222 tasklet_kill(&host->card_tasklet);
3223 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003224
Sahitya Tummalaca422112013-02-22 12:15:54 +05303225 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003226 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003227
Pierre Ossman2134a922008-06-28 18:28:51 +02003228 kfree(host->adma_desc);
3229 kfree(host->align_buffer);
3230
3231 host->adma_desc = NULL;
3232 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003233}
3234
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003235EXPORT_SYMBOL_GPL(sdhci_remove_host);
3236
3237void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003238{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003239 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003240}
3241
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003242EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003243
3244/*****************************************************************************\
3245 * *
3246 * Driver init/exit *
3247 * *
3248\*****************************************************************************/
3249
3250static int __init sdhci_drv_init(void)
3251{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303252 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003253 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303254 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003255
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003256 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003257}
3258
3259static void __exit sdhci_drv_exit(void)
3260{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003261}
3262
3263module_init(sdhci_drv_init);
3264module_exit(sdhci_drv_exit);
3265
Pierre Ossmandf673b22006-06-30 02:22:31 -07003266module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003267module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003268
Pierre Ossman32710e82009-04-08 20:14:54 +02003269MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003270MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003271MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003272
Pierre Ossmandf673b22006-06-30 02:22:31 -07003273MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003274MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");