blob: df640207db522833b072f0018815de029fa331a7 [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
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800456static int sdhci_pre_dma_transfer(struct sdhci_host *host,
457 struct mmc_data *data,
458 struct sdhci_next *next)
459{
460 int sg_count;
461
462 if (!next && data->host_cookie &&
463 data->host_cookie != host->next_data.cookie) {
464 printk(KERN_WARNING "[%s] invalid cookie: data->host_cookie %d"
465 " host->next_data.cookie %d\n",
466 __func__, data->host_cookie, host->next_data.cookie);
467 data->host_cookie = 0;
468 }
469
470 /* Check if next job is already prepared */
471 if (next ||
472 (!next && data->host_cookie != host->next_data.cookie)) {
473 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg,
474 data->sg_len,
475 (data->flags & MMC_DATA_WRITE) ?
476 DMA_TO_DEVICE : DMA_FROM_DEVICE);
477 } else {
478 sg_count = host->next_data.sg_count;
479 host->next_data.sg_count = 0;
480 }
481
482 if (sg_count == 0)
483 return -EINVAL;
484
485 if (next) {
486 next->sg_count = sg_count;
487 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
488 } else
489 host->sg_count = sg_count;
490
491 return sg_count;
492}
493
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200494static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200495 struct mmc_data *data)
496{
497 int direction;
498
499 u8 *desc;
500 u8 *align;
501 dma_addr_t addr;
502 dma_addr_t align_addr;
503 int len, offset;
504
505 struct scatterlist *sg;
506 int i;
507 char *buffer;
508 unsigned long flags;
509
510 /*
511 * The spec does not specify endianness of descriptor table.
512 * We currently guess that it is LE.
513 */
514
515 if (data->flags & MMC_DATA_READ)
516 direction = DMA_FROM_DEVICE;
517 else
518 direction = DMA_TO_DEVICE;
519
520 /*
521 * The ADMA descriptor table is mapped further down as we
522 * need to fill it with data first.
523 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200524 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530525 host->align_buffer,
526 host->align_buf_sz,
527 direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700528 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200529 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200530 BUG_ON(host->align_addr & 0x3);
531
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800532 host->sg_count = sdhci_pre_dma_transfer(host, data, NULL);
533 if (host->sg_count < 0)
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200534 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200535
536 desc = host->adma_desc;
537 align = host->align_buffer;
538
539 align_addr = host->align_addr;
540
541 for_each_sg(data->sg, sg, host->sg_count, i) {
542 addr = sg_dma_address(sg);
543 len = sg_dma_len(sg);
544
545 /*
546 * The SDHCI specification states that ADMA
547 * addresses must be 32-bit aligned. If they
548 * aren't, then we use a bounce buffer for
549 * the (up to three) bytes that screw up the
550 * alignment.
551 */
552 offset = (4 - (addr & 0x3)) & 0x3;
553 if (offset) {
554 if (data->flags & MMC_DATA_WRITE) {
555 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200556 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200557 memcpy(align, buffer, offset);
558 sdhci_kunmap_atomic(buffer, &flags);
559 }
560
Ben Dooks118cd172010-03-05 13:43:26 -0800561 /* tran, valid */
562 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200563
564 BUG_ON(offset > 65536);
565
Pierre Ossman2134a922008-06-28 18:28:51 +0200566 align += 4;
567 align_addr += 4;
568
569 desc += 8;
570
571 addr += offset;
572 len -= offset;
573 }
574
Pierre Ossman2134a922008-06-28 18:28:51 +0200575 BUG_ON(len > 65536);
576
Ben Dooks118cd172010-03-05 13:43:26 -0800577 /* tran, valid */
578 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200579 desc += 8;
580
581 /*
582 * If this triggers then we have a calculation bug
583 * somewhere. :/
584 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530585 WARN_ON((desc - host->adma_desc) > host->adma_desc_sz);
586
Pierre Ossman2134a922008-06-28 18:28:51 +0200587 }
588
Thomas Abraham70764a92010-05-26 14:42:04 -0700589 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
590 /*
591 * Mark the last descriptor as the terminating descriptor
592 */
593 if (desc != host->adma_desc) {
594 desc -= 8;
595 desc[0] |= 0x2; /* end */
596 }
597 } else {
598 /*
599 * Add a terminating entry.
600 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200601
Thomas Abraham70764a92010-05-26 14:42:04 -0700602 /* nop, end, valid */
603 sdhci_set_adma_desc(desc, 0, 0, 0x3);
604 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200605
606 /*
607 * Resync align buffer as we might have changed it.
608 */
609 if (data->flags & MMC_DATA_WRITE) {
610 dma_sync_single_for_device(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530611 host->align_addr,
612 host->align_buf_sz,
613 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200614 }
615
616 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530617 host->adma_desc,
618 host->adma_desc_sz,
619 DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200620 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200621 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200622 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200623
624 return 0;
625
626unmap_entries:
627 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
628 data->sg_len, direction);
629unmap_align:
630 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530631 host->align_buf_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200632fail:
633 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200634}
635
636static void sdhci_adma_table_post(struct sdhci_host *host,
637 struct mmc_data *data)
638{
639 int direction;
640
641 struct scatterlist *sg;
642 int i, size;
643 u8 *align;
644 char *buffer;
645 unsigned long flags;
646
647 if (data->flags & MMC_DATA_READ)
648 direction = DMA_FROM_DEVICE;
649 else
650 direction = DMA_TO_DEVICE;
651
652 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530653 host->adma_desc_sz, DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200654
655 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530656 host->align_buf_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200657
658 if (data->flags & MMC_DATA_READ) {
659 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
660 data->sg_len, direction);
661
662 align = host->align_buffer;
663
664 for_each_sg(data->sg, sg, host->sg_count, i) {
665 if (sg_dma_address(sg) & 0x3) {
666 size = 4 - (sg_dma_address(sg) & 0x3);
667
668 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200669 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200670 memcpy(buffer, align, size);
671 sdhci_kunmap_atomic(buffer, &flags);
672
673 align += 4;
674 }
675 }
676 }
677
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800678 if (!data->host_cookie)
679 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
680 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200681}
682
Andrei Warkentina3c77782011-04-11 16:13:42 -0500683static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800684{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700685 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500686 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700687 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800688
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200689 /*
690 * If the host controller provides us with an incorrect timeout
691 * value, just skip the check and use 0xE. The hardware may take
692 * longer to time out, but that's much better than having a too-short
693 * timeout value.
694 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200695 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200696 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200697
Andrei Warkentina3c77782011-04-11 16:13:42 -0500698 /* Unspecified timeout, assume max */
699 if (!data && !cmd->cmd_timeout_ms)
700 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800701
Andrei Warkentina3c77782011-04-11 16:13:42 -0500702 /* timeout in us */
703 if (!data)
704 target_timeout = cmd->cmd_timeout_ms * 1000;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530705 else {
706 target_timeout = data->timeout_ns / 1000;
707 if (host->clock)
708 target_timeout += data->timeout_clks / host->clock;
709 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700710
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700711 /*
712 * Figure out needed cycles.
713 * We do this in steps in order to fit inside a 32 bit int.
714 * The first step is the minimum timeout, which will have a
715 * minimum resolution of 6 bits:
716 * (1) 2^13*1000 > 2^22,
717 * (2) host->timeout_clk < 2^16
718 * =>
719 * (1) / (2) > 2^6
720 */
721 count = 0;
722 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
723 while (current_timeout < target_timeout) {
724 count++;
725 current_timeout <<= 1;
726 if (count >= 0xF)
727 break;
728 }
729
Sahitya Tummalaca422112013-02-22 12:15:54 +0530730 if (count >= 0xF) {
Chris Ball34ca2092012-06-01 10:39:45 -0400731 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
732 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700733 count = 0xE;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530734 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700735
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200736 return count;
737}
738
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300739static void sdhci_set_transfer_irqs(struct sdhci_host *host)
740{
741 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
742 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
743
744 if (host->flags & SDHCI_REQ_USE_DMA)
745 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
746 else
747 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
748}
749
Andrei Warkentina3c77782011-04-11 16:13:42 -0500750static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200751{
752 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200753 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500754 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200755 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200756
757 WARN_ON(host->data);
758
Andrei Warkentina3c77782011-04-11 16:13:42 -0500759 if (data || (cmd->flags & MMC_RSP_BUSY)) {
760 count = sdhci_calc_timeout(host, cmd);
761 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
762 }
763
764 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200765 return;
766
767 /* Sanity checks */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530768 BUG_ON(data->blksz * data->blocks > host->mmc->max_req_size);
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200769 BUG_ON(data->blksz > host->mmc->max_blk_size);
770 BUG_ON(data->blocks > 65535);
771
772 host->data = data;
773 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400774 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200775
Richard Röjforsa13abc72009-09-22 16:45:30 -0700776 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100777 host->flags |= SDHCI_REQ_USE_DMA;
778
Pierre Ossman2134a922008-06-28 18:28:51 +0200779 /*
780 * FIXME: This doesn't account for merging when mapping the
781 * scatterlist.
782 */
783 if (host->flags & SDHCI_REQ_USE_DMA) {
784 int broken, i;
785 struct scatterlist *sg;
786
787 broken = 0;
788 if (host->flags & SDHCI_USE_ADMA) {
789 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
790 broken = 1;
791 } else {
792 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
793 broken = 1;
794 }
795
796 if (unlikely(broken)) {
797 for_each_sg(data->sg, sg, data->sg_len, i) {
798 if (sg->length & 0x3) {
799 DBG("Reverting to PIO because of "
800 "transfer size (%d)\n",
801 sg->length);
802 host->flags &= ~SDHCI_REQ_USE_DMA;
803 break;
804 }
805 }
806 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100807 }
808
809 /*
810 * The assumption here being that alignment is the same after
811 * translation to device address space.
812 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200813 if (host->flags & SDHCI_REQ_USE_DMA) {
814 int broken, i;
815 struct scatterlist *sg;
816
817 broken = 0;
818 if (host->flags & SDHCI_USE_ADMA) {
819 /*
820 * As we use 3 byte chunks to work around
821 * alignment problems, we need to check this
822 * quirk.
823 */
824 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
825 broken = 1;
826 } else {
827 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
828 broken = 1;
829 }
830
831 if (unlikely(broken)) {
832 for_each_sg(data->sg, sg, data->sg_len, i) {
833 if (sg->offset & 0x3) {
834 DBG("Reverting to PIO because of "
835 "bad alignment\n");
836 host->flags &= ~SDHCI_REQ_USE_DMA;
837 break;
838 }
839 }
840 }
841 }
842
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200843 if (host->flags & SDHCI_REQ_USE_DMA) {
844 if (host->flags & SDHCI_USE_ADMA) {
845 ret = sdhci_adma_table_pre(host, data);
846 if (ret) {
847 /*
848 * This only happens when someone fed
849 * us an invalid request.
850 */
851 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200852 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200853 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300854 sdhci_writel(host, host->adma_addr,
855 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200856 }
857 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300858 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200859
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800860 sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300861 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200862 /*
863 * This only happens when someone fed
864 * us an invalid request.
865 */
866 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200867 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200868 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200869 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300870 sdhci_writel(host, sg_dma_address(data->sg),
871 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200872 }
873 }
874 }
875
Pierre Ossman2134a922008-06-28 18:28:51 +0200876 /*
877 * Always adjust the DMA selection as some controllers
878 * (e.g. JMicron) can't do PIO properly when the selection
879 * is ADMA.
880 */
881 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300882 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200883 ctrl &= ~SDHCI_CTRL_DMA_MASK;
884 if ((host->flags & SDHCI_REQ_USE_DMA) &&
885 (host->flags & SDHCI_USE_ADMA))
886 ctrl |= SDHCI_CTRL_ADMA32;
887 else
888 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300889 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100890 }
891
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200892 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200893 int flags;
894
895 flags = SG_MITER_ATOMIC;
896 if (host->data->flags & MMC_DATA_READ)
897 flags |= SG_MITER_TO_SG;
898 else
899 flags |= SG_MITER_FROM_SG;
900 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200901 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800902 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700903
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300904 sdhci_set_transfer_irqs(host);
905
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400906 /* Set the DMA boundary value and block size */
907 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
908 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300909 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700910}
911
912static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500913 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700914{
915 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500916 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700917
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700918 if (data == NULL)
919 return;
920
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200921 WARN_ON(!host->data);
922
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700923 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500924 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
925 mode |= SDHCI_TRNS_MULTI;
926 /*
927 * If we are sending CMD23, CMD12 never gets sent
928 * on successful completion (so no Auto-CMD12).
929 */
930 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
931 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500932 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
933 mode |= SDHCI_TRNS_AUTO_CMD23;
934 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
935 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700936 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500937
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530938 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700939 mode |= SDHCI_TRNS_READ;
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530940 if (host->ops->toggle_cdr)
941 host->ops->toggle_cdr(host, true);
942 }
943 if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE))
944 host->ops->toggle_cdr(host, false);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100945 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700946 mode |= SDHCI_TRNS_DMA;
947
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300948 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800949}
950
951static void sdhci_finish_data(struct sdhci_host *host)
952{
953 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800954
955 BUG_ON(!host->data);
956
957 data = host->data;
958 host->data = NULL;
959
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100960 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200961 if (host->flags & SDHCI_USE_ADMA)
962 sdhci_adma_table_post(host, data);
963 else {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800964 if (!data->host_cookie)
965 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
966 data->sg_len,
967 (data->flags & MMC_DATA_READ) ?
968 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200969 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800970 }
971
972 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200973 * The specification states that the block count register must
974 * be updated, but it does not specify at what point in the
975 * data flow. That makes the register entirely useless to read
976 * back so we have to assume that nothing made it to the card
977 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800978 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200979 if (data->error)
980 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800981 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200982 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983
Andrei Warkentine89d4562011-05-23 15:06:37 -0500984 /*
985 * Need to send CMD12 if -
986 * a) open-ended multiblock transfer (no CMD23)
987 * b) error in multiblock transfer
988 */
989 if (data->stop &&
990 (data->error ||
991 !host->mrq->sbc)) {
992
Pierre Ossmand129bce2006-03-24 03:18:17 -0800993 /*
994 * The controller needs a reset of internal state machines
995 * upon error conditions.
996 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200997 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998 sdhci_reset(host, SDHCI_RESET_CMD);
999 sdhci_reset(host, SDHCI_RESET_DATA);
1000 }
1001
1002 sdhci_send_command(host, data->stop);
1003 } else
1004 tasklet_schedule(&host->finish_tasklet);
1005}
1006
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301007#define SDHCI_REQUEST_TIMEOUT 10 /* Default request timeout in seconds */
1008
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
1010{
1011 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001012 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001013 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014
1015 WARN_ON(host->cmd);
1016
Pierre Ossmand129bce2006-03-24 03:18:17 -08001017 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001018 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001019
1020 mask = SDHCI_CMD_INHIBIT;
1021 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1022 mask |= SDHCI_DATA_INHIBIT;
1023
1024 /* We shouldn't wait for data inihibit for stop commands, even
1025 though they might use busy signaling */
1026 if (host->mrq->data && (cmd == host->mrq->data->stop))
1027 mask &= ~SDHCI_DATA_INHIBIT;
1028
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001029 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001030 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301031 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001032 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001034 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001035 tasklet_schedule(&host->finish_tasklet);
1036 return;
1037 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001038 timeout--;
1039 mdelay(1);
1040 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001041
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301042 mod_timer(&host->timer, jiffies + SDHCI_REQUEST_TIMEOUT * HZ);
1043
1044 if (cmd->cmd_timeout_ms > SDHCI_REQUEST_TIMEOUT * MSEC_PER_SEC)
1045 mod_timer(&host->timer, jiffies +
1046 (msecs_to_jiffies(cmd->cmd_timeout_ms * 2)));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047
1048 host->cmd = cmd;
1049
Andrei Warkentina3c77782011-04-11 16:13:42 -05001050 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001051
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001052 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001053
Andrei Warkentine89d4562011-05-23 15:06:37 -05001054 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001055
Pierre Ossmand129bce2006-03-24 03:18:17 -08001056 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301057 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001058 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001059 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001060 tasklet_schedule(&host->finish_tasklet);
1061 return;
1062 }
1063
1064 if (!(cmd->flags & MMC_RSP_PRESENT))
1065 flags = SDHCI_CMD_RESP_NONE;
1066 else if (cmd->flags & MMC_RSP_136)
1067 flags = SDHCI_CMD_RESP_LONG;
1068 else if (cmd->flags & MMC_RSP_BUSY)
1069 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1070 else
1071 flags = SDHCI_CMD_RESP_SHORT;
1072
1073 if (cmd->flags & MMC_RSP_CRC)
1074 flags |= SDHCI_CMD_CRC;
1075 if (cmd->flags & MMC_RSP_OPCODE)
1076 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301077
1078 /* CMD19 is special in that the Data Present Select should be set */
Girish K S2cd06dc2012-01-06 09:56:39 +05301079 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1080 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001081 flags |= SDHCI_CMD_DATA;
1082
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001083 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001084}
1085
1086static void sdhci_finish_command(struct sdhci_host *host)
1087{
1088 int i;
1089
1090 BUG_ON(host->cmd == NULL);
1091
1092 if (host->cmd->flags & MMC_RSP_PRESENT) {
1093 if (host->cmd->flags & MMC_RSP_136) {
1094 /* CRC is stripped so we need to do some shifting. */
1095 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001096 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001097 SDHCI_RESPONSE + (3-i)*4) << 8;
1098 if (i != 3)
1099 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001100 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001101 SDHCI_RESPONSE + (3-i)*4-1);
1102 }
1103 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001104 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001105 }
1106 }
1107
Pierre Ossman17b04292007-07-22 22:18:46 +02001108 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001109
Andrei Warkentine89d4562011-05-23 15:06:37 -05001110 /* Finished CMD23, now send actual command. */
1111 if (host->cmd == host->mrq->sbc) {
1112 host->cmd = NULL;
1113 sdhci_send_command(host, host->mrq->cmd);
1114 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001115
Andrei Warkentine89d4562011-05-23 15:06:37 -05001116 /* Processed actual command. */
1117 if (host->data && host->data_early)
1118 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001119
Andrei Warkentine89d4562011-05-23 15:06:37 -05001120 if (!host->cmd->data)
1121 tasklet_schedule(&host->finish_tasklet);
1122
1123 host->cmd = NULL;
1124 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001125}
1126
1127static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1128{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301129 int div = 0; /* Initialized for compiler warning */
Sahitya Tummalaca422112013-02-22 12:15:54 +05301130 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301131 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001132 unsigned long timeout;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301133 unsigned long flags;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001134
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301135 spin_lock_irqsave(&host->lock, flags);
Todd Poynor30832ab2011-12-27 15:48:46 +02001136 if (clock && clock == host->clock)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301137 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001138
Sahitya Tummalaca422112013-02-22 12:15:54 +05301139 host->mmc->actual_clock = 0;
1140
Anton Vorontsov81146342009-03-17 00:13:59 +03001141 if (host->ops->set_clock) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301142 spin_unlock_irqrestore(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001143 host->ops->set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301144 spin_lock_irqsave(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001145 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301146 goto ret;
Anton Vorontsov81146342009-03-17 00:13:59 +03001147 }
1148
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301149 if (host->clock)
1150 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001151
1152 if (clock == 0)
1153 goto out;
1154
Zhangfei Gao85105c52010-08-06 07:10:01 +08001155 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301156 /*
1157 * Check if the Host Controller supports Programmable Clock
1158 * Mode.
1159 */
1160 if (host->clk_mul) {
1161 u16 ctrl;
1162
1163 /*
1164 * We need to figure out whether the Host Driver needs
1165 * to select Programmable Clock Mode, or the value can
1166 * be set automatically by the Host Controller based on
1167 * the Preset Value registers.
1168 */
1169 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1170 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1171 for (div = 1; div <= 1024; div++) {
1172 if (((host->max_clk * host->clk_mul) /
1173 div) <= clock)
1174 break;
1175 }
1176 /*
1177 * Set Programmable Clock Mode in the Clock
1178 * Control register.
1179 */
1180 clk = SDHCI_PROG_CLOCK_MODE;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301181 real_div = div;
1182 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301183 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001184 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301185 } else {
1186 /* Version 3.00 divisors must be a multiple of 2. */
1187 if (host->max_clk <= clock)
1188 div = 1;
1189 else {
1190 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1191 div += 2) {
1192 if ((host->max_clk / div) <= clock)
1193 break;
1194 }
1195 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301196 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301197 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001198 }
1199 } else {
1200 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001201 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001202 if ((host->max_clk / div) <= clock)
1203 break;
1204 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301205 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301206 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001207 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001208
Sahitya Tummalaca422112013-02-22 12:15:54 +05301209 if (real_div)
1210 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1211
Arindam Nathc3ed3872011-05-05 12:19:06 +05301212 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001213 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1214 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001215 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001216 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001217
Chris Ball27f6cb12009-09-22 16:45:31 -07001218 /* Wait max 20 ms */
1219 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001220 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001221 & SDHCI_CLOCK_INT_STABLE)) {
1222 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301223 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001224 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001225 sdhci_dumpregs(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301226 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001227 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001228 timeout--;
1229 mdelay(1);
1230 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001231
1232 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001233 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001234
1235out:
1236 host->clock = clock;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301237ret:
1238 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001239}
1240
Sahitya Tummalaca422112013-02-22 12:15:54 +05301241static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001242{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001243 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001244
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001245 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001246 switch (1 << power) {
1247 case MMC_VDD_165_195:
1248 pwr = SDHCI_POWER_180;
1249 break;
1250 case MMC_VDD_29_30:
1251 case MMC_VDD_30_31:
1252 pwr = SDHCI_POWER_300;
1253 break;
1254 case MMC_VDD_32_33:
1255 case MMC_VDD_33_34:
1256 pwr = SDHCI_POWER_330;
1257 break;
1258 default:
1259 BUG();
1260 }
1261 }
1262
1263 if (host->pwr == pwr)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301264 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001265
Pierre Ossmanae628902009-05-03 20:45:03 +02001266 host->pwr = pwr;
1267
1268 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001269 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301270 if (host->ops->check_power_status)
1271 host->ops->check_power_status(host);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301272 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001273 }
1274
1275 /*
1276 * Spec says that we should clear the power reg before setting
1277 * a new value. Some controllers don't seem to like this though.
1278 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301279 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001280 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301281 if (host->ops->check_power_status)
1282 host->ops->check_power_status(host);
1283 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001284
Andres Salomone08c1692008-07-04 10:00:03 -07001285 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001286 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001287 * and set turn on power at the same time, so set the voltage first.
1288 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301289 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001290 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301291 if (host->ops->check_power_status)
1292 host->ops->check_power_status(host);
1293 }
Pierre Ossmanae628902009-05-03 20:45:03 +02001294
1295 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001296
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001297 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301298 if (host->ops->check_power_status)
1299 host->ops->check_power_status(host);
Harald Welte557b0692009-06-18 16:53:38 +02001300
1301 /*
1302 * Some controllers need an extra 10ms delay of 10ms before they
1303 * can apply clock after applying power
1304 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001305 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001306 mdelay(10);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301307
1308 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001309}
1310
Pierre Ossmand129bce2006-03-24 03:18:17 -08001311/*****************************************************************************\
1312 * *
1313 * MMC callbacks *
1314 * *
1315\*****************************************************************************/
1316
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301317static int sdhci_enable(struct mmc_host *mmc)
1318{
1319 struct sdhci_host *host = mmc_priv(mmc);
1320
1321 if (host->cpu_dma_latency_us)
1322 pm_qos_update_request(&host->pm_qos_req_dma,
1323 host->cpu_dma_latency_us);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301324 if (host->ops->platform_bus_voting)
1325 host->ops->platform_bus_voting(host, 1);
1326
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301327 return 0;
1328}
1329
1330static int sdhci_disable(struct mmc_host *mmc)
1331{
1332 struct sdhci_host *host = mmc_priv(mmc);
1333
1334 if (host->cpu_dma_latency_us)
1335 pm_qos_update_request(&host->pm_qos_req_dma,
1336 PM_QOS_DEFAULT_VALUE);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301337 if (host->ops->platform_bus_voting)
1338 host->ops->platform_bus_voting(host, 0);
1339
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301340 return 0;
1341}
1342
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001343static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1344 bool is_first_req)
1345{
1346 struct sdhci_host *host = mmc_priv(mmc);
1347
1348 if (mrq->data->host_cookie) {
1349 mrq->data->host_cookie = 0;
1350 return;
1351 }
1352
1353 if (host->flags & SDHCI_REQ_USE_DMA)
1354 if (sdhci_pre_dma_transfer(host, mrq->data, &host->next_data) < 0)
1355 mrq->data->host_cookie = 0;
1356}
1357
1358static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1359 int err)
1360{
1361 struct sdhci_host *host = mmc_priv(mmc);
1362 struct mmc_data *data = mrq->data;
1363
1364 if (host->flags & SDHCI_REQ_USE_DMA) {
1365 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1366 (data->flags & MMC_DATA_WRITE) ?
1367 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1368 data->host_cookie = 0;
1369 }
1370}
1371
Pierre Ossmand129bce2006-03-24 03:18:17 -08001372static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1373{
1374 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001375 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001376 unsigned long flags;
1377
1378 host = mmc_priv(mmc);
1379
Adrian Hunter50accb92011-10-03 15:33:34 +03001380 sdhci_runtime_pm_get(host);
1381
Pierre Ossmand129bce2006-03-24 03:18:17 -08001382 spin_lock_irqsave(&host->lock, flags);
1383
1384 WARN_ON(host->mrq != NULL);
1385
Pierre Ossmanf9134312008-12-21 17:01:48 +01001386#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001387 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001388#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001389
1390 /*
1391 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1392 * requests if Auto-CMD12 is enabled.
1393 */
1394 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001395 if (mrq->stop) {
1396 mrq->data->stop = NULL;
1397 mrq->stop = NULL;
1398 }
1399 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001400
1401 host->mrq = mrq;
1402
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001403 /* If polling, assume that the card is always present. */
1404 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1405 present = true;
1406 else
1407 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1408 SDHCI_CARD_PRESENT;
1409
1410 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001411 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001412 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301413 } else {
1414 u32 present_state;
1415
1416 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1417 /*
1418 * Check if the re-tuning timer has already expired and there
1419 * is no on-going data transfer. If so, we need to execute
1420 * tuning procedure before sending command.
1421 */
1422 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1423 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1424 spin_unlock_irqrestore(&host->lock, flags);
Girish K S2cd06dc2012-01-06 09:56:39 +05301425 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301426 spin_lock_irqsave(&host->lock, flags);
1427
1428 /* Restore original mmc_request structure */
1429 host->mrq = mrq;
1430 }
1431
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001432 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001433 sdhci_send_command(host, mrq->sbc);
1434 else
1435 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301436 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001437
Pierre Ossman5f25a662006-10-04 02:15:39 -07001438 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001439 spin_unlock_irqrestore(&host->lock, flags);
1440}
1441
Adrian Hunter50accb92011-10-03 15:33:34 +03001442static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001443{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001444 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301445 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001446 u8 ctrl;
1447
Sahitya Tummalaca422112013-02-22 12:15:54 +05301448 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301449 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1450 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1451 return;
1452 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001453
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301454 if (ios->clock)
1455 sdhci_set_clock(host, ios->clock);
1456
1457 spin_lock_irqsave(&host->lock, flags);
1458 if (!host->clock) {
1459 spin_unlock_irqrestore(&host->lock, flags);
1460 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001461 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301462 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001463
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301464 if (ios->power_mode & (MMC_POWER_UP | MMC_POWER_ON))
Sahitya Tummalaca422112013-02-22 12:15:54 +05301465 vdd_bit = sdhci_set_power(host, ios->vdd);
1466
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301467 if (host->vmmc && vdd_bit != -1)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301468 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301469
1470 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001471
Philip Rakity643a81f2010-09-23 08:24:32 -07001472 if (host->ops->platform_send_init_74_clocks)
1473 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1474
Philip Rakity15ec4462010-11-19 16:48:39 -05001475 /*
1476 * If your platform has 8-bit width support but is not a v3 controller,
1477 * or if it requires special setup code, you should implement that in
1478 * platform_8bit_width().
1479 */
1480 if (host->ops->platform_8bit_width)
1481 host->ops->platform_8bit_width(host, ios->bus_width);
1482 else {
1483 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1484 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1485 ctrl &= ~SDHCI_CTRL_4BITBUS;
1486 if (host->version >= SDHCI_SPEC_300)
1487 ctrl |= SDHCI_CTRL_8BITBUS;
1488 } else {
1489 if (host->version >= SDHCI_SPEC_300)
1490 ctrl &= ~SDHCI_CTRL_8BITBUS;
1491 if (ios->bus_width == MMC_BUS_WIDTH_4)
1492 ctrl |= SDHCI_CTRL_4BITBUS;
1493 else
1494 ctrl &= ~SDHCI_CTRL_4BITBUS;
1495 }
1496 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1497 }
1498
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001499 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001500
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001501 if ((ios->timing == MMC_TIMING_SD_HS ||
1502 ios->timing == MMC_TIMING_MMC_HS)
1503 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001504 ctrl |= SDHCI_CTRL_HISPD;
1505 else
1506 ctrl &= ~SDHCI_CTRL_HISPD;
1507
Arindam Nathd6d50a12011-05-05 12:18:59 +05301508 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301509 u16 clk, ctrl_2;
1510 unsigned int clock;
1511
1512 /* In case of UHS-I modes, set High Speed Enable */
Girish K S2cd06dc2012-01-06 09:56:39 +05301513 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1514 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301515 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1516 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001517 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301518 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301519
1520 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1521 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301522 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301523 /*
1524 * We only need to set Driver Strength if the
1525 * preset value enable is not set.
1526 */
1527 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1528 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1529 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1530 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1531 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1532
1533 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301534 } else {
1535 /*
1536 * According to SDHC Spec v3.00, if the Preset Value
1537 * Enable in the Host Control 2 register is set, we
1538 * need to reset SD Clock Enable before changing High
1539 * Speed Enable to avoid generating clock gliches.
1540 */
Arindam Nath758535c2011-05-05 12:19:00 +05301541
1542 /* Reset SD Clock Enable */
1543 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1544 clk &= ~SDHCI_CLOCK_CARD_EN;
1545 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1546
1547 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1548
1549 /* Re-enable SD Clock */
1550 clock = host->clock;
1551 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301552 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301553 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301554 spin_lock_irqsave(&host->lock, flags);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301555 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301556
Arindam Nath49c468f2011-05-05 12:19:01 +05301557 /* Reset SD Clock Enable */
1558 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1559 clk &= ~SDHCI_CLOCK_CARD_EN;
1560 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1561
Philip Rakity6322cdd2011-05-13 11:17:15 +05301562 if (host->ops->set_uhs_signaling)
1563 host->ops->set_uhs_signaling(host, ios->timing);
1564 else {
1565 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1566 /* Select Bus Speed Mode for host */
1567 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S2cd06dc2012-01-06 09:56:39 +05301568 if (ios->timing == MMC_TIMING_MMC_HS200)
1569 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1570 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301571 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1572 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1573 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1574 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1575 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1576 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1577 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1578 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1579 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1580 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1581 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301582
1583 /* Re-enable SD Clock */
1584 clock = host->clock;
1585 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301586 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath49c468f2011-05-05 12:19:01 +05301587 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301588 spin_lock_irqsave(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301589 } else
1590 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301591
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301592 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02001593 /*
1594 * Some (ENE) controllers go apeshit on some ios operation,
1595 * signalling timeout and CRC errors even on CMD0. Resetting
1596 * it on each ios seems to solve the problem.
1597 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001598 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001599 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1600
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301601 /*
1602 * Reset the chip on each power off.
1603 * Should clear out any weird states.
1604 */
1605 if (ios->power_mode == MMC_POWER_OFF) {
1606 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1607 sdhci_reinit(host);
1608 vdd_bit = sdhci_set_power(host, -1);
1609 if (host->vmmc && vdd_bit != -1)
1610 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1611 }
1612 if (!ios->clock)
1613 sdhci_set_clock(host, ios->clock);
1614
Pierre Ossman5f25a662006-10-04 02:15:39 -07001615 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001616}
1617
Adrian Hunter50accb92011-10-03 15:33:34 +03001618static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1619{
1620 struct sdhci_host *host = mmc_priv(mmc);
1621
1622 sdhci_runtime_pm_get(host);
1623 sdhci_do_set_ios(host, ios);
1624 sdhci_runtime_pm_put(host);
1625}
1626
1627static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001628{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001629 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001630 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001631
Pierre Ossmand129bce2006-03-24 03:18:17 -08001632 spin_lock_irqsave(&host->lock, flags);
1633
Pierre Ossman1e728592008-04-16 19:13:13 +02001634 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001635 is_readonly = 0;
1636 else if (host->ops->get_ro)
1637 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001638 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001639 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1640 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001641
1642 spin_unlock_irqrestore(&host->lock, flags);
1643
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001644 /* This quirk needs to be replaced by a callback-function later */
1645 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1646 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001647}
1648
Takashi Iwai82b0e232011-04-21 20:26:38 +02001649#define SAMPLE_COUNT 5
1650
Adrian Hunter50accb92011-10-03 15:33:34 +03001651static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001652{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001653 int i, ro_count;
1654
Takashi Iwai82b0e232011-04-21 20:26:38 +02001655 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001656 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001657
1658 ro_count = 0;
1659 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001660 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001661 if (++ro_count > SAMPLE_COUNT / 2)
1662 return 1;
1663 }
1664 msleep(30);
1665 }
1666 return 0;
1667}
1668
Adrian Hunter50accb92011-10-03 15:33:34 +03001669static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001670{
Adrian Hunter50accb92011-10-03 15:33:34 +03001671 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001672
Adrian Hunter50accb92011-10-03 15:33:34 +03001673 if (host->ops && host->ops->hw_reset)
1674 host->ops->hw_reset(host);
1675}
Adrian Hunter20758b62011-08-29 16:42:12 +03001676
Adrian Hunter50accb92011-10-03 15:33:34 +03001677static int sdhci_get_ro(struct mmc_host *mmc)
1678{
1679 struct sdhci_host *host = mmc_priv(mmc);
1680 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001681
Adrian Hunter50accb92011-10-03 15:33:34 +03001682 sdhci_runtime_pm_get(host);
1683 ret = sdhci_do_get_ro(host);
1684 sdhci_runtime_pm_put(host);
1685 return ret;
1686}
1687
1688static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1689{
Pierre Ossman1e728592008-04-16 19:13:13 +02001690 if (host->flags & SDHCI_DEVICE_DEAD)
1691 goto out;
1692
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001693 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001694 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1695 else
1696 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1697
1698 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1699 if (host->runtime_suspended)
1700 goto out;
1701
1702 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001703 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1704 else
1705 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001706out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001707 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001708}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001709
Adrian Hunter50accb92011-10-03 15:33:34 +03001710static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1711{
1712 struct sdhci_host *host = mmc_priv(mmc);
1713 unsigned long flags;
1714
1715 spin_lock_irqsave(&host->lock, flags);
1716 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001717 spin_unlock_irqrestore(&host->lock, flags);
1718}
1719
Adrian Hunter50accb92011-10-03 15:33:34 +03001720static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1721 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301722{
Arindam Nathf2119df2011-05-05 12:18:57 +05301723 u8 pwr;
1724 u16 clk, ctrl;
1725 u32 present_state;
1726
Arindam Nathf2119df2011-05-05 12:18:57 +05301727 /*
1728 * Signal Voltage Switching is only applicable for Host Controllers
1729 * v3.00 and above.
1730 */
1731 if (host->version < SDHCI_SPEC_300)
1732 return 0;
1733
1734 /*
1735 * We first check whether the request is to set signalling voltage
1736 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1737 */
1738 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1739 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1740 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1741 ctrl &= ~SDHCI_CTRL_VDD_180;
1742 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301743 if (host->ops->check_power_status)
1744 host->ops->check_power_status(host);
Arindam Nathf2119df2011-05-05 12:18:57 +05301745
1746 /* Wait for 5ms */
1747 usleep_range(5000, 5500);
1748
1749 /* 3.3V regulator output should be stable within 5 ms */
1750 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1751 if (!(ctrl & SDHCI_CTRL_VDD_180))
1752 return 0;
1753 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301754 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301755 "signalling voltage failed\n");
1756 return -EIO;
1757 }
1758 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1759 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1760 /* Stop SDCLK */
1761 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1762 clk &= ~SDHCI_CLOCK_CARD_EN;
1763 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1764
1765 /* Check whether DAT[3:0] is 0000 */
1766 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1767 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1768 SDHCI_DATA_LVL_SHIFT)) {
1769 /*
1770 * Enable 1.8V Signal Enable in the Host Control2
1771 * register
1772 */
1773 ctrl |= SDHCI_CTRL_VDD_180;
1774 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301775 if (host->ops->check_power_status)
1776 host->ops->check_power_status(host);
Arindam Nathf2119df2011-05-05 12:18:57 +05301777
1778 /* Wait for 5ms */
1779 usleep_range(5000, 5500);
1780
1781 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1782 if (ctrl & SDHCI_CTRL_VDD_180) {
1783 /* Provide SDCLK again and wait for 1ms*/
1784 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1785 clk |= SDHCI_CLOCK_CARD_EN;
1786 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1787 usleep_range(1000, 1500);
1788
1789 /*
1790 * If DAT[3:0] level is 1111b, then the card
1791 * was successfully switched to 1.8V signaling.
1792 */
1793 present_state = sdhci_readl(host,
1794 SDHCI_PRESENT_STATE);
1795 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1796 SDHCI_DATA_LVL_MASK)
1797 return 0;
1798 }
1799 }
1800
1801 /*
1802 * If we are here, that means the switch to 1.8V signaling
1803 * failed. We power cycle the card, and retry initialization
1804 * sequence by setting S18R to 0.
1805 */
1806 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1807 pwr &= ~SDHCI_POWER_ON;
1808 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301809 if (host->ops->check_power_status)
1810 host->ops->check_power_status(host);
Arindam Nathf2119df2011-05-05 12:18:57 +05301811
1812 /* Wait for 1ms as per the spec */
1813 usleep_range(1000, 1500);
1814 pwr |= SDHCI_POWER_ON;
1815 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301816 if (host->ops->check_power_status)
1817 host->ops->check_power_status(host);
Arindam Nathf2119df2011-05-05 12:18:57 +05301818
Sahitya Tummalaca422112013-02-22 12:15:54 +05301819 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301820 "voltage failed, retrying with S18R set to 0\n");
1821 return -EAGAIN;
1822 } else
1823 /* No signal voltage switch required */
1824 return 0;
1825}
1826
Adrian Hunter50accb92011-10-03 15:33:34 +03001827static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1828 struct mmc_ios *ios)
1829{
1830 struct sdhci_host *host = mmc_priv(mmc);
1831 int err;
1832
1833 if (host->version < SDHCI_SPEC_300)
1834 return 0;
1835 sdhci_runtime_pm_get(host);
1836 err = sdhci_do_start_signal_voltage_switch(host, ios);
1837 sdhci_runtime_pm_put(host);
1838 return err;
1839}
1840
Girish K S2cd06dc2012-01-06 09:56:39 +05301841static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301842{
1843 struct sdhci_host *host;
1844 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05301845 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05301846 int tuning_loop_counter = MAX_TUNING_LOOP;
1847 unsigned long timeout;
1848 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05301849 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301850
1851 host = mmc_priv(mmc);
1852
Adrian Hunter50accb92011-10-03 15:33:34 +03001853 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301854 disable_irq(host->irq);
1855 spin_lock(&host->lock);
1856
1857 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1858
1859 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05301860 * The Host Controller needs tuning only in case of SDR104 mode
1861 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301862 * Capabilities register.
Girish K S2cd06dc2012-01-06 09:56:39 +05301863 * If the Host Controller supports the HS200 mode then the
1864 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301865 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08001866 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1867 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
1868 (host->flags & SDHCI_HS200_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05301869 requires_tuning_nonuhs = true;
1870
Arindam Nathb513ea22011-05-05 12:19:04 +05301871 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301872 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301873 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1874 else {
1875 spin_unlock(&host->lock);
1876 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03001877 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301878 return 0;
1879 }
1880
Asutosh Das8ddd3482013-01-04 11:45:46 +05301881 if (host->ops->execute_tuning) {
1882 spin_unlock(&host->lock);
1883 enable_irq(host->irq);
1884 host->ops->execute_tuning(host, opcode);
1885 disable_irq(host->irq);
1886 spin_lock(&host->lock);
1887 goto out;
1888 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301889 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1890
1891 /*
1892 * As per the Host Controller spec v3.00, tuning command
1893 * generates Buffer Read Ready interrupt, so enable that.
1894 *
1895 * Note: The spec clearly says that when tuning sequence
1896 * is being performed, the controller does not generate
1897 * interrupts other than Buffer Read Ready interrupt. But
1898 * to make sure we don't hit a controller bug, we _only_
1899 * enable Buffer Read Ready interrupt here.
1900 */
1901 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1902 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1903
1904 /*
1905 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1906 * of loops reaches 40 times or a timeout of 150ms occurs.
1907 */
1908 timeout = 150;
1909 do {
1910 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03001911 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301912
1913 if (!tuning_loop_counter && !timeout)
1914 break;
1915
Girish K S2cd06dc2012-01-06 09:56:39 +05301916 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301917 cmd.arg = 0;
1918 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1919 cmd.retries = 0;
1920 cmd.data = NULL;
1921 cmd.error = 0;
1922
1923 mrq.cmd = &cmd;
1924 host->mrq = &mrq;
1925
1926 /*
1927 * In response to CMD19, the card sends 64 bytes of tuning
1928 * block to the Host Controller. So we set the block size
1929 * to 64 here.
1930 */
Girish K S2cd06dc2012-01-06 09:56:39 +05301931 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1932 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1933 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1934 SDHCI_BLOCK_SIZE);
1935 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1936 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1937 SDHCI_BLOCK_SIZE);
1938 } else {
1939 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1940 SDHCI_BLOCK_SIZE);
1941 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301942
1943 /*
1944 * The tuning block is sent by the card to the host controller.
1945 * So we set the TRNS_READ bit in the Transfer Mode register.
1946 * This also takes care of setting DMA Enable and Multi Block
1947 * Select in the same register to 0.
1948 */
1949 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1950
1951 sdhci_send_command(host, &cmd);
1952
1953 host->cmd = NULL;
1954 host->mrq = NULL;
1955
1956 spin_unlock(&host->lock);
1957 enable_irq(host->irq);
1958
1959 /* Wait for Buffer Read Ready interrupt */
1960 wait_event_interruptible_timeout(host->buf_ready_int,
1961 (host->tuning_done == 1),
1962 msecs_to_jiffies(50));
1963 disable_irq(host->irq);
1964 spin_lock(&host->lock);
1965
1966 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301967 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301968 "Buffer Read Ready interrupt during tuning "
1969 "procedure, falling back to fixed sampling "
1970 "clock\n");
1971 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1972 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1973 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1974 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1975
1976 err = -EIO;
1977 goto out;
1978 }
1979
1980 host->tuning_done = 0;
1981
1982 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1983 tuning_loop_counter--;
1984 timeout--;
1985 mdelay(1);
1986 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1987
1988 /*
1989 * The Host Driver has exhausted the maximum number of loops allowed,
1990 * so use fixed sampling frequency.
1991 */
1992 if (!tuning_loop_counter || !timeout) {
1993 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1994 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1995 } else {
1996 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301997 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301998 " failed, falling back to fixed sampling"
1999 " clock\n");
2000 err = -EIO;
2001 }
2002 }
2003
2004out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302005 /*
2006 * If this is the very first time we are here, we start the retuning
2007 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2008 * flag won't be set, we check this condition before actually starting
2009 * the timer.
2010 */
2011 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2012 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2013 mod_timer(&host->tuning_timer, jiffies +
2014 host->tuning_count * HZ);
2015 /* Tuning mode 1 limits the maximum data length to 4MB */
2016 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2017 } else {
2018 host->flags &= ~SDHCI_NEEDS_RETUNING;
2019 /* Reload the new initial value for timer */
2020 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2021 mod_timer(&host->tuning_timer, jiffies +
2022 host->tuning_count * HZ);
2023 }
2024
2025 /*
2026 * In case tuning fails, host controllers which support re-tuning can
2027 * try tuning again at a later time, when the re-tuning timer expires.
2028 * So for these controllers, we return 0. Since there might be other
2029 * controllers who do not have this capability, we return error for
2030 * them.
2031 */
2032 if (err && host->tuning_count &&
2033 host->tuning_mode == SDHCI_TUNING_MODE_1)
2034 err = 0;
2035
Arindam Nathb513ea22011-05-05 12:19:04 +05302036 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
2037 spin_unlock(&host->lock);
2038 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002039 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302040
2041 return err;
2042}
2043
Adrian Hunter50accb92011-10-03 15:33:34 +03002044static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302045{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302046 u16 ctrl;
2047 unsigned long flags;
2048
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302049 /* Host Controller v3.00 defines preset value registers */
2050 if (host->version < SDHCI_SPEC_300)
2051 return;
2052
2053 spin_lock_irqsave(&host->lock, flags);
2054
2055 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2056
2057 /*
2058 * We only enable or disable Preset Value if they are not already
2059 * enabled or disabled respectively. Otherwise, we bail out.
2060 */
2061 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2062 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2063 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002064 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302065 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2066 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2067 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002068 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302069 }
2070
2071 spin_unlock_irqrestore(&host->lock, flags);
2072}
2073
Adrian Hunter50accb92011-10-03 15:33:34 +03002074static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
2075{
2076 struct sdhci_host *host = mmc_priv(mmc);
2077
2078 sdhci_runtime_pm_get(host);
2079 sdhci_do_enable_preset_value(host, enable);
2080 sdhci_runtime_pm_put(host);
2081}
2082
David Brownellab7aefd2006-11-12 17:55:30 -08002083static const struct mmc_host_ops sdhci_ops = {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002084 .pre_req = sdhci_pre_req,
2085 .post_req = sdhci_post_req,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002086 .request = sdhci_request,
2087 .set_ios = sdhci_set_ios,
2088 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03002089 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002090 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302091 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302092 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302093 .enable_preset_value = sdhci_enable_preset_value,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302094 .enable = sdhci_enable,
2095 .disable = sdhci_disable,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002096};
2097
2098/*****************************************************************************\
2099 * *
2100 * Tasklets *
2101 * *
2102\*****************************************************************************/
2103
2104static void sdhci_tasklet_card(unsigned long param)
2105{
2106 struct sdhci_host *host;
2107 unsigned long flags;
2108
2109 host = (struct sdhci_host*)param;
2110
2111 spin_lock_irqsave(&host->lock, flags);
2112
Adrian Hunter50accb92011-10-03 15:33:34 +03002113 /* Check host->mrq first in case we are runtime suspended */
2114 if (host->mrq &&
2115 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302116 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002117 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05302118 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002119 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002120
Adrian Hunter50accb92011-10-03 15:33:34 +03002121 sdhci_reset(host, SDHCI_RESET_CMD);
2122 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002123
Adrian Hunter50accb92011-10-03 15:33:34 +03002124 host->mrq->cmd->error = -ENOMEDIUM;
2125 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002126 }
2127
2128 spin_unlock_irqrestore(&host->lock, flags);
2129
Pierre Ossman04cf5852008-08-18 22:18:14 +02002130 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002131}
2132
2133static void sdhci_tasklet_finish(unsigned long param)
2134{
2135 struct sdhci_host *host;
2136 unsigned long flags;
2137 struct mmc_request *mrq;
2138
2139 host = (struct sdhci_host*)param;
2140
Adrian Hunter50accb92011-10-03 15:33:34 +03002141 spin_lock_irqsave(&host->lock, flags);
2142
Chris Ball0c9c99a2011-04-27 17:35:31 -04002143 /*
2144 * If this tasklet gets rescheduled while running, it will
2145 * be run again afterwards but without any active request.
2146 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002147 if (!host->mrq) {
2148 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002149 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002150 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002151
2152 del_timer(&host->timer);
2153
2154 mrq = host->mrq;
2155
Pierre Ossmand129bce2006-03-24 03:18:17 -08002156 /*
2157 * The controller needs a reset of internal state machines
2158 * upon error conditions.
2159 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002160 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002161 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002162 (mrq->data && (mrq->data->error ||
2163 (mrq->data->stop && mrq->data->stop->error))) ||
2164 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002165
2166 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002167 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002168 unsigned int clock;
2169
2170 /* This is to force an update */
2171 clock = host->clock;
2172 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302173 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002174 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302175 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002176 }
2177
2178 /* Spec says we should do both at the same time, but Ricoh
2179 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002180 sdhci_reset(host, SDHCI_RESET_CMD);
2181 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002182 } else {
2183 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2184 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002185 }
2186
2187 host->mrq = NULL;
2188 host->cmd = NULL;
2189 host->data = NULL;
2190
Pierre Ossmanf9134312008-12-21 17:01:48 +01002191#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002192 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002193#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002194
Pierre Ossman5f25a662006-10-04 02:15:39 -07002195 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002196 spin_unlock_irqrestore(&host->lock, flags);
2197
2198 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002199 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002200}
2201
2202static void sdhci_timeout_timer(unsigned long data)
2203{
2204 struct sdhci_host *host;
2205 unsigned long flags;
2206
2207 host = (struct sdhci_host*)data;
2208
2209 spin_lock_irqsave(&host->lock, flags);
2210
2211 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302212 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002213 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002214 sdhci_dumpregs(host);
2215
2216 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002217 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002218 sdhci_finish_data(host);
2219 } else {
2220 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002221 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002222 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002223 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002224
2225 tasklet_schedule(&host->finish_tasklet);
2226 }
2227 }
2228
Pierre Ossman5f25a662006-10-04 02:15:39 -07002229 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002230 spin_unlock_irqrestore(&host->lock, flags);
2231}
2232
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302233static void sdhci_tuning_timer(unsigned long data)
2234{
2235 struct sdhci_host *host;
2236 unsigned long flags;
2237
2238 host = (struct sdhci_host *)data;
2239
2240 spin_lock_irqsave(&host->lock, flags);
2241
2242 host->flags |= SDHCI_NEEDS_RETUNING;
2243
2244 spin_unlock_irqrestore(&host->lock, flags);
2245}
2246
Pierre Ossmand129bce2006-03-24 03:18:17 -08002247/*****************************************************************************\
2248 * *
2249 * Interrupt handling *
2250 * *
2251\*****************************************************************************/
2252
2253static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2254{
2255 BUG_ON(intmask == 0);
2256
2257 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302258 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002259 "though no command operation was in progress.\n",
2260 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002261 sdhci_dumpregs(host);
2262 return;
2263 }
2264
Pierre Ossman43b58b32007-07-25 23:15:27 +02002265 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002266 host->cmd->error = -ETIMEDOUT;
2267 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2268 SDHCI_INT_INDEX))
2269 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002270
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302271 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2272 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2273 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2274 if (intmask & SDHCI_INT_CRC) {
2275 sdhci_reset(host, SDHCI_RESET_CMD);
2276 host->cmd->error = 0;
2277 }
2278 }
2279 }
2280
Pierre Ossmane8095172008-07-25 01:09:08 +02002281 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002282 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002283 return;
2284 }
2285
2286 /*
2287 * The host can send and interrupt when the busy state has
2288 * ended, allowing us to wait without wasting CPU cycles.
2289 * Unfortunately this is overloaded on the "data complete"
2290 * interrupt, so we need to take some care when handling
2291 * it.
2292 *
2293 * Note: The 1.0 specification is a bit ambiguous about this
2294 * feature so there might be some problems with older
2295 * controllers.
2296 */
2297 if (host->cmd->flags & MMC_RSP_BUSY) {
2298 if (host->cmd->data)
2299 DBG("Cannot wait for busy signal when also "
2300 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002301 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002302 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002303
2304 /* The controller does not support the end-of-busy IRQ,
2305 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002306 }
2307
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302308 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2309 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2310 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2311 if (intmask & SDHCI_INT_CRC) {
2312 sdhci_finish_command(host);
2313 return;
2314 }
2315 }
2316 }
2317
Pierre Ossmane8095172008-07-25 01:09:08 +02002318 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002319 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002320}
2321
George G. Davis0957c332010-02-18 12:32:12 -05002322#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002323static void sdhci_show_adma_error(struct sdhci_host *host)
2324{
2325 const char *name = mmc_hostname(host->mmc);
2326 u8 *desc = host->adma_desc;
2327 __le32 *dma;
2328 __le16 *len;
2329 u8 attr;
2330
2331 sdhci_dumpregs(host);
2332
2333 while (true) {
2334 dma = (__le32 *)(desc + 4);
2335 len = (__le16 *)(desc + 2);
2336 attr = *desc;
2337
2338 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2339 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2340
2341 desc += 8;
2342
2343 if (attr & 2)
2344 break;
2345 }
2346}
2347#else
2348static void sdhci_show_adma_error(struct sdhci_host *host) { }
2349#endif
2350
Pierre Ossmand129bce2006-03-24 03:18:17 -08002351static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2352{
Girish K S2cd06dc2012-01-06 09:56:39 +05302353 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002354 BUG_ON(intmask == 0);
2355
Arindam Nathb513ea22011-05-05 12:19:04 +05302356 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2357 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302358 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2359 if (command == MMC_SEND_TUNING_BLOCK ||
2360 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302361 host->tuning_done = 1;
2362 wake_up(&host->buf_ready_int);
2363 return;
2364 }
2365 }
2366
Pierre Ossmand129bce2006-03-24 03:18:17 -08002367 if (!host->data) {
2368 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002369 * The "data complete" interrupt is also used to
2370 * indicate that a busy state has ended. See comment
2371 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002372 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002373 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2374 if (intmask & SDHCI_INT_DATA_END) {
2375 sdhci_finish_command(host);
2376 return;
2377 }
2378 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002379
Sahitya Tummalaca422112013-02-22 12:15:54 +05302380 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002381 "though no data operation was in progress.\n",
2382 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002383 sdhci_dumpregs(host);
2384
2385 return;
2386 }
2387
2388 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002389 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002390 else if (intmask & SDHCI_INT_DATA_END_BIT)
2391 host->data->error = -EILSEQ;
2392 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2393 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2394 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002395 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002396 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302397 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002398 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002399 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002400 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002401
Pierre Ossman17b04292007-07-22 22:18:46 +02002402 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002403 sdhci_finish_data(host);
2404 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002405 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002406 sdhci_transfer_pio(host);
2407
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002408 /*
2409 * We currently don't do anything fancy with DMA
2410 * boundaries, but as we can't disable the feature
2411 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002412 *
2413 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2414 * should return a valid address to continue from, but as
2415 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002416 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002417 if (intmask & SDHCI_INT_DMA_END) {
2418 u32 dmastart, dmanow;
2419 dmastart = sg_dma_address(host->data->sg);
2420 dmanow = dmastart + host->data->bytes_xfered;
2421 /*
2422 * Force update to the next DMA block boundary.
2423 */
2424 dmanow = (dmanow &
2425 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2426 SDHCI_DEFAULT_BOUNDARY_SIZE;
2427 host->data->bytes_xfered = dmanow - dmastart;
2428 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2429 " next 0x%08x\n",
2430 mmc_hostname(host->mmc), dmastart,
2431 host->data->bytes_xfered, dmanow);
2432 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2433 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002434
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002435 if (intmask & SDHCI_INT_DATA_END) {
2436 if (host->cmd) {
2437 /*
2438 * Data managed to finish before the
2439 * command completed. Make sure we do
2440 * things in the proper order.
2441 */
2442 host->data_early = 1;
2443 } else {
2444 sdhci_finish_data(host);
2445 }
2446 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002447 }
2448}
2449
David Howells7d12e782006-10-05 14:55:46 +01002450static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002451{
2452 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002453 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002454 u32 intmask, unexpected = 0;
2455 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002456
2457 spin_lock(&host->lock);
2458
Adrian Hunter50accb92011-10-03 15:33:34 +03002459 if (host->runtime_suspended) {
2460 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302461 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002462 mmc_hostname(host->mmc));
2463 return IRQ_HANDLED;
2464 }
2465
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002466 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002467
Mark Lord62df67a2007-03-06 13:30:13 +01002468 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002469 result = IRQ_NONE;
2470 goto out;
2471 }
2472
Alexander Stein6379b232012-03-14 09:52:10 +01002473again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002474 DBG("*** %s got interrupt: 0x%08x\n",
2475 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002476
Pierre Ossman3192a282006-06-30 02:22:26 -07002477 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302478 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2479 SDHCI_CARD_PRESENT;
2480
2481 /*
2482 * There is a observation on i.mx esdhc. INSERT bit will be
2483 * immediately set again when it gets cleared, if a card is
2484 * inserted. We have to mask the irq to prevent interrupt
2485 * storm which will freeze the system. And the REMOVE gets
2486 * the same situation.
2487 *
2488 * More testing are needed here to ensure it works for other
2489 * platforms though.
2490 */
2491 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2492 SDHCI_INT_CARD_REMOVE);
2493 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2494 SDHCI_INT_CARD_INSERT);
2495
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002496 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302497 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2498 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002499 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002500 }
2501
Pierre Ossmand129bce2006-03-24 03:18:17 -08002502 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002503 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2504 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002505 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2506 (host->clock <= 400000))
2507 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002508 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002509 }
2510
2511 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002512 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2513 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002514 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2515 (host->clock <= 400000))
2516 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002517 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002518 }
2519
2520 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2521
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002522 intmask &= ~SDHCI_INT_ERROR;
2523
Pierre Ossmand129bce2006-03-24 03:18:17 -08002524 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302525 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002526 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002527 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002528 }
2529
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002530 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002531
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002532 if (intmask & SDHCI_INT_CARD_INT)
2533 cardint = 1;
2534
2535 intmask &= ~SDHCI_INT_CARD_INT;
2536
Pierre Ossman3192a282006-06-30 02:22:26 -07002537 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002538 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002539 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002540 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002541
2542 result = IRQ_HANDLED;
2543
Alexander Stein6379b232012-03-14 09:52:10 +01002544 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2545 if (intmask && --max_loops)
2546 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002547out:
2548 spin_unlock(&host->lock);
2549
Alexander Stein6379b232012-03-14 09:52:10 +01002550 if (unexpected) {
2551 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2552 mmc_hostname(host->mmc), unexpected);
2553 sdhci_dumpregs(host);
2554 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002555 /*
2556 * We have to delay this as it calls back into the driver.
2557 */
2558 if (cardint)
2559 mmc_signal_sdio_irq(host->mmc);
2560
Pierre Ossmand129bce2006-03-24 03:18:17 -08002561 return result;
2562}
2563
2564/*****************************************************************************\
2565 * *
2566 * Suspend/resume *
2567 * *
2568\*****************************************************************************/
2569
2570#ifdef CONFIG_PM
2571
Manuel Laussd72faa62011-11-03 11:09:45 +01002572int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002573{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002574 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302575 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002576
Chris Balla1b13b42012-02-06 00:43:59 -05002577 if (host->ops->platform_suspend)
2578 host->ops->platform_suspend(host);
2579
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002580 sdhci_disable_card_detection(host);
2581
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302582 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302583 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2584 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2585 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002586 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302587 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302588 }
2589
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002590 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302591 if (ret) {
2592 if (has_tuning_timer) {
2593 host->flags |= SDHCI_NEEDS_RETUNING;
2594 mod_timer(&host->tuning_timer, jiffies +
2595 host->tuning_count * HZ);
2596 }
2597
2598 sdhci_enable_card_detection(host);
2599
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002600 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302601 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002602
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002603 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002604
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002605 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002606}
2607
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002608EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002609
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002610int sdhci_resume_host(struct sdhci_host *host)
2611{
2612 int ret;
2613
Richard Röjforsa13abc72009-09-22 16:45:30 -07002614 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002615 if (host->ops->enable_dma)
2616 host->ops->enable_dma(host);
2617 }
2618
2619 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2620 mmc_hostname(host->mmc), host);
2621 if (ret)
2622 return ret;
2623
Adrian Hunter6308d292012-02-07 14:48:54 +02002624 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2625 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2626 /* Card keeps power but host controller does not */
2627 sdhci_init(host, 0);
2628 host->pwr = 0;
2629 host->clock = 0;
2630 sdhci_do_set_ios(host, &host->mmc->ios);
2631 } else {
2632 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2633 mmiowb();
2634 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002635
2636 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002637 sdhci_enable_card_detection(host);
2638
Chris Balla1b13b42012-02-06 00:43:59 -05002639 if (host->ops->platform_resume)
2640 host->ops->platform_resume(host);
2641
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302642 /* Set the re-tuning expiration flag */
2643 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2644 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2645 host->flags |= SDHCI_NEEDS_RETUNING;
2646
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002647 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002648}
2649
2650EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002651
Daniel Drake5f619702010-11-04 22:20:39 +00002652void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2653{
2654 u8 val;
2655 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2656 val |= SDHCI_WAKE_ON_INT;
2657 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2658}
2659
2660EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2661
Pierre Ossmand129bce2006-03-24 03:18:17 -08002662#endif /* CONFIG_PM */
2663
Adrian Hunter50accb92011-10-03 15:33:34 +03002664#ifdef CONFIG_PM_RUNTIME
2665
2666static int sdhci_runtime_pm_get(struct sdhci_host *host)
2667{
2668 return pm_runtime_get_sync(host->mmc->parent);
2669}
2670
2671static int sdhci_runtime_pm_put(struct sdhci_host *host)
2672{
2673 pm_runtime_mark_last_busy(host->mmc->parent);
2674 return pm_runtime_put_autosuspend(host->mmc->parent);
2675}
2676
2677int sdhci_runtime_suspend_host(struct sdhci_host *host)
2678{
2679 unsigned long flags;
2680 int ret = 0;
2681
2682 /* Disable tuning since we are suspending */
2683 if (host->version >= SDHCI_SPEC_300 &&
2684 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2685 del_timer_sync(&host->tuning_timer);
2686 host->flags &= ~SDHCI_NEEDS_RETUNING;
2687 }
2688
2689 spin_lock_irqsave(&host->lock, flags);
2690 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2691 spin_unlock_irqrestore(&host->lock, flags);
2692
2693 synchronize_irq(host->irq);
2694
2695 spin_lock_irqsave(&host->lock, flags);
2696 host->runtime_suspended = true;
2697 spin_unlock_irqrestore(&host->lock, flags);
2698
2699 return ret;
2700}
2701EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2702
2703int sdhci_runtime_resume_host(struct sdhci_host *host)
2704{
2705 unsigned long flags;
2706 int ret = 0, host_flags = host->flags;
2707
2708 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2709 if (host->ops->enable_dma)
2710 host->ops->enable_dma(host);
2711 }
2712
2713 sdhci_init(host, 0);
2714
2715 /* Force clock and power re-program */
2716 host->pwr = 0;
2717 host->clock = 0;
2718 sdhci_do_set_ios(host, &host->mmc->ios);
2719
2720 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2721 if (host_flags & SDHCI_PV_ENABLED)
2722 sdhci_do_enable_preset_value(host, true);
2723
2724 /* Set the re-tuning expiration flag */
2725 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2726 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2727 host->flags |= SDHCI_NEEDS_RETUNING;
2728
2729 spin_lock_irqsave(&host->lock, flags);
2730
2731 host->runtime_suspended = false;
2732
2733 /* Enable SDIO IRQ */
2734 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2735 sdhci_enable_sdio_irq_nolock(host, true);
2736
2737 /* Enable Card Detection */
2738 sdhci_enable_card_detection(host);
2739
2740 spin_unlock_irqrestore(&host->lock, flags);
2741
2742 return ret;
2743}
2744EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2745
2746#endif
2747
Pierre Ossmand129bce2006-03-24 03:18:17 -08002748/*****************************************************************************\
2749 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002750 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002751 * *
2752\*****************************************************************************/
2753
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002754struct sdhci_host *sdhci_alloc_host(struct device *dev,
2755 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002756{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002757 struct mmc_host *mmc;
2758 struct sdhci_host *host;
2759
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002760 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002761
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002762 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002763 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002764 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002765
2766 host = mmc_priv(mmc);
2767 host->mmc = mmc;
2768
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002769 return host;
2770}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002771
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002772EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002773
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002774int sdhci_add_host(struct sdhci_host *host)
2775{
2776 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302777 u32 caps[2];
2778 u32 max_current_caps;
2779 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002780 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002781
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002782 WARN_ON(host == NULL);
2783 if (host == NULL)
2784 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002785
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002786 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002787
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002788 if (debug_quirks)
2789 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03002790 if (debug_quirks2)
2791 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002792
Pierre Ossmand96649e2006-06-30 02:22:30 -07002793 sdhci_reset(host, SDHCI_RESET_ALL);
2794
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002795 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002796 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2797 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002798 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302799 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002800 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002801 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002802 }
2803
Arindam Nathf2119df2011-05-05 12:18:57 +05302804 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002805 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002806
Arindam Nathf2119df2011-05-05 12:18:57 +05302807 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2808 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2809
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002810 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002811 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302812 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002813 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002814 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002815 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002816
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002817 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002818 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002819 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002820 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002821 }
2822
Arindam Nathf2119df2011-05-05 12:18:57 +05302823 if ((host->version >= SDHCI_SPEC_200) &&
2824 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002825 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002826
2827 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2828 (host->flags & SDHCI_USE_ADMA)) {
2829 DBG("Disabling ADMA as it is marked broken\n");
2830 host->flags &= ~SDHCI_USE_ADMA;
2831 }
2832
Richard Röjforsa13abc72009-09-22 16:45:30 -07002833 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002834 if (host->ops->enable_dma) {
2835 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302836 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002837 "available. Falling back to PIO.\n",
2838 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002839 host->flags &=
2840 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002841 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002842 }
2843 }
2844
Pierre Ossman2134a922008-06-28 18:28:51 +02002845 if (host->flags & SDHCI_USE_ADMA) {
2846 /*
2847 * We need to allocate descriptors for all sg entries
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302848 * (128/max_segments) and potentially one alignment transfer for
Pierre Ossman2134a922008-06-28 18:28:51 +02002849 * each of those entries.
2850 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302851 if (host->ops->get_max_segments)
2852 host->adma_max_desc = host->ops->get_max_segments();
2853 else
2854 host->adma_max_desc = 128;
2855
2856 host->adma_desc_sz = (host->adma_max_desc * 2 + 1) * 4;
2857 host->align_buf_sz = host->adma_max_desc * 4;
2858
2859 pr_debug("%s: %s: dma_desc_size: %d\n",
2860 mmc_hostname(host->mmc), __func__, host->adma_desc_sz);
2861 host->adma_desc = kmalloc(host->adma_desc_sz,
2862 GFP_KERNEL);
2863 host->align_buffer = kmalloc(host->align_buf_sz,
2864 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02002865 if (!host->adma_desc || !host->align_buffer) {
2866 kfree(host->adma_desc);
2867 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302868 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002869 "buffers. Falling back to standard DMA.\n",
2870 mmc_hostname(mmc));
2871 host->flags &= ~SDHCI_USE_ADMA;
2872 }
2873 }
2874
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002875 host->next_data.cookie = 1;
2876
Pierre Ossman76591502008-07-21 00:32:11 +02002877 /*
2878 * If we use DMA, then it's up to the caller to set the DMA
2879 * mask, but PIO does not need the hw shim so we set a new
2880 * mask here in that case.
2881 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002882 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002883 host->dma_mask = DMA_BIT_MASK(64);
2884 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2885 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002886
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002887 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302888 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002889 >> SDHCI_CLOCK_BASE_SHIFT;
2890 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302891 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002892 >> SDHCI_CLOCK_BASE_SHIFT;
2893
Pierre Ossmand129bce2006-03-24 03:18:17 -08002894 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002895 if (host->max_clk == 0 || host->quirks &
2896 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002897 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302898 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002899 "frequency.\n", mmc_hostname(mmc));
2900 return -ENODEV;
2901 }
2902 host->max_clk = host->ops->get_max_clock(host);
2903 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002904
2905 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302906 * In case of Host Controller v3.00, find out whether clock
2907 * multiplier is supported.
2908 */
2909 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2910 SDHCI_CLOCK_MUL_SHIFT;
2911
2912 /*
2913 * In case the value in Clock Multiplier is 0, then programmable
2914 * clock mode is not supported, otherwise the actual clock
2915 * multiplier is one more than the value of Clock Multiplier
2916 * in the Capabilities Register.
2917 */
2918 if (host->clk_mul)
2919 host->clk_mul += 1;
2920
2921 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002922 * Set host parameters.
2923 */
2924 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302925 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002926 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002927 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302928 else if (host->version >= SDHCI_SPEC_300) {
2929 if (host->clk_mul) {
2930 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2931 mmc->f_max = host->max_clk * host->clk_mul;
2932 } else
2933 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2934 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002935 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002936
Sahitya Tummalaca422112013-02-22 12:15:54 +05302937 host->timeout_clk =
2938 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2939 if (host->timeout_clk == 0) {
2940 if (host->ops->get_timeout_clock) {
2941 host->timeout_clk = host->ops->get_timeout_clock(host);
2942 } else if (!(host->quirks &
2943 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2944 pr_err("%s: Hardware doesn't specify timeout clock "
2945 "frequency.\n", mmc_hostname(mmc));
2946 return -ENODEV;
2947 }
2948 }
2949 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2950 host->timeout_clk *= 1000;
2951
2952 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
2953 host->timeout_clk = mmc->f_max / 1000;
2954
2955 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
2956
Andrei Warkentine89d4562011-05-23 15:06:37 -05002957 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2958
2959 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2960 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002961
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002962 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002963 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002964 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002965 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002966 host->flags |= SDHCI_AUTO_CMD23;
2967 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2968 } else {
2969 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2970 }
2971
Philip Rakity15ec4462010-11-19 16:48:39 -05002972 /*
2973 * A controller may support 8-bit width, but the board itself
2974 * might not have the pins brought out. Boards that support
2975 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2976 * their platform code before calling sdhci_add_host(), and we
2977 * won't assume 8-bit width for hosts without that CAP.
2978 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002979 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002980 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002981
Arindam Nathf2119df2011-05-05 12:18:57 +05302982 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002983 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002984
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002985 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2986 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002987 mmc->caps |= MMC_CAP_NEEDS_POLL;
2988
Al Cooper4188bba2012-03-16 15:54:17 -04002989 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
2990 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2991 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05302992 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2993
2994 /* SDR104 supports also implies SDR50 support */
2995 if (caps[1] & SDHCI_SUPPORT_SDR104)
2996 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2997 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2998 mmc->caps |= MMC_CAP_UHS_SDR50;
2999
3000 if (caps[1] & SDHCI_SUPPORT_DDR50)
3001 mmc->caps |= MMC_CAP_UHS_DDR50;
3002
Girish K S2cd06dc2012-01-06 09:56:39 +05303003 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303004 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3005 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3006
Girish K S2cd06dc2012-01-06 09:56:39 +05303007 /* Does the host need tuning for HS200? */
3008 if (mmc->caps2 & MMC_CAP2_HS200)
3009 host->flags |= SDHCI_HS200_NEEDS_TUNING;
3010
Arindam Nathd6d50a12011-05-05 12:18:59 +05303011 /* Driver Type(s) (A, C, D) supported by the host */
3012 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3013 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3014 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3015 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3016 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3017 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3018
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02003019 /* Initial value for re-tuning timer count */
3020 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3021 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3022
3023 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303024 * In case Re-tuning Timer is not disabled, the actual value of
3025 * re-tuning timer will be 2 ^ (n - 1).
3026 */
3027 if (host->tuning_count)
3028 host->tuning_count = 1 << (host->tuning_count - 1);
3029
3030 /* Re-tuning mode supported by the Host Controller */
3031 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3032 SDHCI_RETUNING_MODE_SHIFT;
3033
Takashi Iwai8f230f42010-12-08 10:04:30 +01003034 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05303035 /*
3036 * According to SD Host Controller spec v3.00, if the Host System
3037 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3038 * the value is meaningful only if Voltage Support in the Capabilities
3039 * register is set. The actual current value is 4 times the register
3040 * value.
3041 */
3042 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3043
3044 if (caps[0] & SDHCI_CAN_VDD_330) {
3045 int max_current_330;
3046
Takashi Iwai8f230f42010-12-08 10:04:30 +01003047 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303048
3049 max_current_330 = ((max_current_caps &
3050 SDHCI_MAX_CURRENT_330_MASK) >>
3051 SDHCI_MAX_CURRENT_330_SHIFT) *
3052 SDHCI_MAX_CURRENT_MULTIPLIER;
3053
3054 if (max_current_330 > 150)
3055 mmc->caps |= MMC_CAP_SET_XPC_330;
3056 }
3057 if (caps[0] & SDHCI_CAN_VDD_300) {
3058 int max_current_300;
3059
Takashi Iwai8f230f42010-12-08 10:04:30 +01003060 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303061
3062 max_current_300 = ((max_current_caps &
3063 SDHCI_MAX_CURRENT_300_MASK) >>
3064 SDHCI_MAX_CURRENT_300_SHIFT) *
3065 SDHCI_MAX_CURRENT_MULTIPLIER;
3066
3067 if (max_current_300 > 150)
3068 mmc->caps |= MMC_CAP_SET_XPC_300;
3069 }
3070 if (caps[0] & SDHCI_CAN_VDD_180) {
3071 int max_current_180;
3072
Takashi Iwai8f230f42010-12-08 10:04:30 +01003073 ocr_avail |= MMC_VDD_165_195;
3074
Arindam Nathf2119df2011-05-05 12:18:57 +05303075 max_current_180 = ((max_current_caps &
3076 SDHCI_MAX_CURRENT_180_MASK) >>
3077 SDHCI_MAX_CURRENT_180_SHIFT) *
3078 SDHCI_MAX_CURRENT_MULTIPLIER;
3079
3080 if (max_current_180 > 150)
3081 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05303082
3083 /* Maximum current capabilities of the host at 1.8V */
3084 if (max_current_180 >= 800)
3085 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
3086 else if (max_current_180 >= 600)
3087 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
3088 else if (max_current_180 >= 400)
3089 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
3090 else
3091 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05303092 }
3093
Takashi Iwai8f230f42010-12-08 10:04:30 +01003094 mmc->ocr_avail = ocr_avail;
3095 mmc->ocr_avail_sdio = ocr_avail;
3096 if (host->ocr_avail_sdio)
3097 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3098 mmc->ocr_avail_sd = ocr_avail;
3099 if (host->ocr_avail_sd)
3100 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3101 else /* normal SD controllers don't support 1.8V */
3102 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3103 mmc->ocr_avail_mmc = ocr_avail;
3104 if (host->ocr_avail_mmc)
3105 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003106
3107 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303108 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003109 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003110 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003111 }
3112
Pierre Ossmand129bce2006-03-24 03:18:17 -08003113 spin_lock_init(&host->lock);
3114
3115 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003116 * Maximum number of segments. Depends on if the hardware
3117 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003118 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003119 if (host->flags & SDHCI_USE_ADMA)
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303120 mmc->max_segs = host->adma_max_desc;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003121 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003122 mmc->max_segs = 1;
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303123 else/* PIO */
3124 mmc->max_segs = host->adma_max_desc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003125
3126 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003127 * Maximum number of sectors in one transfer. Limited by DMA boundary
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303128 * size (512KiB), unless specified by platform specific driver. Each
3129 * descriptor can transfer a maximum of 64KB.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003130 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303131 if (host->ops->get_max_segments)
3132 mmc->max_req_size = (host->adma_max_desc * 65536);
3133 else
3134 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003135
3136 /*
3137 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003138 * of bytes. When doing hardware scatter/gather, each entry cannot
3139 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003140 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003141 if (host->flags & SDHCI_USE_ADMA) {
3142 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3143 mmc->max_seg_size = 65535;
3144 else
3145 mmc->max_seg_size = 65536;
3146 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003147 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003148 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003149
3150 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003151 * Maximum block size. This varies from controller to controller and
3152 * is specified in the capabilities register.
3153 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003154 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3155 mmc->max_blk_size = 2;
3156 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303157 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003158 SDHCI_MAX_BLOCK_SHIFT;
3159 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303160 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003161 "assuming 512 bytes\n", mmc_hostname(mmc));
3162 mmc->max_blk_size = 0;
3163 }
3164 }
3165
3166 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003167
3168 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003169 * Maximum block count.
3170 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003171 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003172
3173 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003174 * Init tasklets.
3175 */
3176 tasklet_init(&host->card_tasklet,
3177 sdhci_tasklet_card, (unsigned long)host);
3178 tasklet_init(&host->finish_tasklet,
3179 sdhci_tasklet_finish, (unsigned long)host);
3180
Al Viroe4cad1b2006-10-10 22:47:07 +01003181 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003182
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303183 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303184 init_waitqueue_head(&host->buf_ready_int);
3185
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303186 /* Initialize re-tuning timer */
3187 init_timer(&host->tuning_timer);
3188 host->tuning_timer.data = (unsigned long)host;
3189 host->tuning_timer.function = sdhci_tuning_timer;
3190 }
3191
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003192 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003193 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003194 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003195 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003196
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003197 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3198 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303199 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003200 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003201 }
3202
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003203 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003204
3205#ifdef CONFIG_MMC_DEBUG
3206 sdhci_dumpregs(host);
3207#endif
3208
Pierre Ossmanf9134312008-12-21 17:01:48 +01003209#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003210 snprintf(host->led_name, sizeof(host->led_name),
3211 "%s::", mmc_hostname(mmc));
3212 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003213 host->led.brightness = LED_OFF;
3214 host->led.default_trigger = mmc_hostname(mmc);
3215 host->led.brightness_set = sdhci_led_control;
3216
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003217 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003218 if (ret)
3219 goto reset;
3220#endif
3221
Pierre Ossman5f25a662006-10-04 02:15:39 -07003222 mmiowb();
3223
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303224 if (host->cpu_dma_latency_us)
3225 pm_qos_add_request(&host->pm_qos_req_dma,
3226 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003227 mmc_add_host(mmc);
3228
Sahitya Tummalaca422112013-02-22 12:15:54 +05303229 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003230 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003231 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3232 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003233
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003234 sdhci_enable_card_detection(host);
3235
Pierre Ossmand129bce2006-03-24 03:18:17 -08003236 return 0;
3237
Pierre Ossmanf9134312008-12-21 17:01:48 +01003238#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003239reset:
3240 sdhci_reset(host, SDHCI_RESET_ALL);
3241 free_irq(host->irq, host);
3242#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003243untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003244 tasklet_kill(&host->card_tasklet);
3245 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003246
3247 return ret;
3248}
3249
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003250EXPORT_SYMBOL_GPL(sdhci_add_host);
3251
Pierre Ossman1e728592008-04-16 19:13:13 +02003252void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003253{
Pierre Ossman1e728592008-04-16 19:13:13 +02003254 unsigned long flags;
3255
3256 if (dead) {
3257 spin_lock_irqsave(&host->lock, flags);
3258
3259 host->flags |= SDHCI_DEVICE_DEAD;
3260
3261 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303262 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003263 " transfer!\n", mmc_hostname(host->mmc));
3264
3265 host->mrq->cmd->error = -ENOMEDIUM;
3266 tasklet_schedule(&host->finish_tasklet);
3267 }
3268
3269 spin_unlock_irqrestore(&host->lock, flags);
3270 }
3271
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003272 sdhci_disable_card_detection(host);
3273
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303274 if (host->cpu_dma_latency_us)
3275 pm_qos_remove_request(&host->pm_qos_req_dma);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003276 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003277
Pierre Ossmanf9134312008-12-21 17:01:48 +01003278#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003279 led_classdev_unregister(&host->led);
3280#endif
3281
Pierre Ossman1e728592008-04-16 19:13:13 +02003282 if (!dead)
3283 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003284
3285 free_irq(host->irq, host);
3286
3287 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303288 if (host->version >= SDHCI_SPEC_300)
3289 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003290
3291 tasklet_kill(&host->card_tasklet);
3292 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003293
Sahitya Tummalaca422112013-02-22 12:15:54 +05303294 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003295 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003296
Pierre Ossman2134a922008-06-28 18:28:51 +02003297 kfree(host->adma_desc);
3298 kfree(host->align_buffer);
3299
3300 host->adma_desc = NULL;
3301 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003302}
3303
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003304EXPORT_SYMBOL_GPL(sdhci_remove_host);
3305
3306void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003307{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003308 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003309}
3310
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003311EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003312
3313/*****************************************************************************\
3314 * *
3315 * Driver init/exit *
3316 * *
3317\*****************************************************************************/
3318
3319static int __init sdhci_drv_init(void)
3320{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303321 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003322 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303323 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003324
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003325 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003326}
3327
3328static void __exit sdhci_drv_exit(void)
3329{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003330}
3331
3332module_init(sdhci_drv_init);
3333module_exit(sdhci_drv_exit);
3334
Pierre Ossmandf673b22006-06-30 02:22:31 -07003335module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003336module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003337
Pierre Ossman32710e82009-04-08 20:14:54 +02003338MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003339MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003340MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003341
Pierre Ossmandf673b22006-06-30 02:22:31 -07003342MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003343MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");