blob: c247ae98ffb6b5d0d7c41787543542950ee697e3 [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))
Sahitya Tummala179e7382013-03-20 19:24:01 +0530200 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530201
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
Sahitya Tummala00240122013-02-28 19:50:51 +05301212 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
1213 div = 0;
1214
Arindam Nathc3ed3872011-05-05 12:19:06 +05301215 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001216 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1217 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001218 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001219 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001220
Chris Ball27f6cb12009-09-22 16:45:31 -07001221 /* Wait max 20 ms */
1222 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001223 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001224 & SDHCI_CLOCK_INT_STABLE)) {
1225 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301226 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001227 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001228 sdhci_dumpregs(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301229 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001230 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001231 timeout--;
1232 mdelay(1);
1233 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001234
1235 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001236 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001237
1238out:
1239 host->clock = clock;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301240ret:
1241 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001242}
1243
Sahitya Tummalaca422112013-02-22 12:15:54 +05301244static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001245{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001246 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001247
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001248 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001249 switch (1 << power) {
1250 case MMC_VDD_165_195:
1251 pwr = SDHCI_POWER_180;
1252 break;
1253 case MMC_VDD_29_30:
1254 case MMC_VDD_30_31:
1255 pwr = SDHCI_POWER_300;
1256 break;
1257 case MMC_VDD_32_33:
1258 case MMC_VDD_33_34:
1259 pwr = SDHCI_POWER_330;
1260 break;
1261 default:
1262 BUG();
1263 }
1264 }
1265
1266 if (host->pwr == pwr)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301267 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001268
Pierre Ossmanae628902009-05-03 20:45:03 +02001269 host->pwr = pwr;
1270
1271 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001272 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301273 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301274 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301275 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001276 }
1277
1278 /*
1279 * Spec says that we should clear the power reg before setting
1280 * a new value. Some controllers don't seem to like this though.
1281 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301282 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001283 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301284 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301285 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301286 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001287
Andres Salomone08c1692008-07-04 10:00:03 -07001288 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001289 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001290 * and set turn on power at the same time, so set the voltage first.
1291 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301292 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001293 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301294 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301295 host->ops->check_power_status(host, REQ_BUS_ON);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301296 }
Pierre Ossmanae628902009-05-03 20:45:03 +02001297
1298 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001299
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001300 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301301 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301302 host->ops->check_power_status(host, REQ_BUS_ON);
Harald Welte557b0692009-06-18 16:53:38 +02001303
1304 /*
1305 * Some controllers need an extra 10ms delay of 10ms before they
1306 * can apply clock after applying power
1307 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001308 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001309 mdelay(10);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301310
1311 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001312}
1313
Pierre Ossmand129bce2006-03-24 03:18:17 -08001314/*****************************************************************************\
1315 * *
1316 * MMC callbacks *
1317 * *
1318\*****************************************************************************/
1319
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301320static int sdhci_enable(struct mmc_host *mmc)
1321{
1322 struct sdhci_host *host = mmc_priv(mmc);
1323
1324 if (host->cpu_dma_latency_us)
1325 pm_qos_update_request(&host->pm_qos_req_dma,
1326 host->cpu_dma_latency_us);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301327 if (host->ops->platform_bus_voting)
1328 host->ops->platform_bus_voting(host, 1);
1329
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301330 return 0;
1331}
1332
1333static int sdhci_disable(struct mmc_host *mmc)
1334{
1335 struct sdhci_host *host = mmc_priv(mmc);
1336
1337 if (host->cpu_dma_latency_us)
1338 pm_qos_update_request(&host->pm_qos_req_dma,
1339 PM_QOS_DEFAULT_VALUE);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301340 if (host->ops->platform_bus_voting)
1341 host->ops->platform_bus_voting(host, 0);
1342
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301343 return 0;
1344}
1345
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001346static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1347 bool is_first_req)
1348{
1349 struct sdhci_host *host = mmc_priv(mmc);
1350
1351 if (mrq->data->host_cookie) {
1352 mrq->data->host_cookie = 0;
1353 return;
1354 }
1355
1356 if (host->flags & SDHCI_REQ_USE_DMA)
1357 if (sdhci_pre_dma_transfer(host, mrq->data, &host->next_data) < 0)
1358 mrq->data->host_cookie = 0;
1359}
1360
1361static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1362 int err)
1363{
1364 struct sdhci_host *host = mmc_priv(mmc);
1365 struct mmc_data *data = mrq->data;
1366
1367 if (host->flags & SDHCI_REQ_USE_DMA) {
1368 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1369 (data->flags & MMC_DATA_WRITE) ?
1370 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1371 data->host_cookie = 0;
1372 }
1373}
1374
Pierre Ossmand129bce2006-03-24 03:18:17 -08001375static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1376{
1377 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001378 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001379 unsigned long flags;
1380
1381 host = mmc_priv(mmc);
1382
Adrian Hunter50accb92011-10-03 15:33:34 +03001383 sdhci_runtime_pm_get(host);
1384
Pierre Ossmand129bce2006-03-24 03:18:17 -08001385 spin_lock_irqsave(&host->lock, flags);
1386
1387 WARN_ON(host->mrq != NULL);
1388
Pierre Ossmanf9134312008-12-21 17:01:48 +01001389#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001390 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001391#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001392
1393 /*
1394 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1395 * requests if Auto-CMD12 is enabled.
1396 */
1397 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001398 if (mrq->stop) {
1399 mrq->data->stop = NULL;
1400 mrq->stop = NULL;
1401 }
1402 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001403
1404 host->mrq = mrq;
1405
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001406 /* If polling, assume that the card is always present. */
1407 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1408 present = true;
1409 else
1410 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1411 SDHCI_CARD_PRESENT;
1412
1413 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001414 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001415 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301416 } else {
1417 u32 present_state;
1418
1419 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1420 /*
1421 * Check if the re-tuning timer has already expired and there
1422 * is no on-going data transfer. If so, we need to execute
1423 * tuning procedure before sending command.
1424 */
1425 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1426 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1427 spin_unlock_irqrestore(&host->lock, flags);
Girish K S2cd06dc2012-01-06 09:56:39 +05301428 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301429 spin_lock_irqsave(&host->lock, flags);
1430
1431 /* Restore original mmc_request structure */
1432 host->mrq = mrq;
1433 }
1434
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001435 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001436 sdhci_send_command(host, mrq->sbc);
1437 else
1438 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301439 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001440
Pierre Ossman5f25a662006-10-04 02:15:39 -07001441 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001442 spin_unlock_irqrestore(&host->lock, flags);
1443}
1444
Adrian Hunter50accb92011-10-03 15:33:34 +03001445static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001446{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001447 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301448 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001449 u8 ctrl;
1450
Sahitya Tummalaca422112013-02-22 12:15:54 +05301451 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301452 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1453 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1454 return;
1455 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001456
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301457 if (ios->clock)
1458 sdhci_set_clock(host, ios->clock);
1459
1460 spin_lock_irqsave(&host->lock, flags);
1461 if (!host->clock) {
1462 spin_unlock_irqrestore(&host->lock, flags);
1463 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001464 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301465 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001466
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301467 if (ios->power_mode & (MMC_POWER_UP | MMC_POWER_ON))
Sahitya Tummalaca422112013-02-22 12:15:54 +05301468 vdd_bit = sdhci_set_power(host, ios->vdd);
1469
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301470 if (host->vmmc && vdd_bit != -1)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301471 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301472
1473 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001474
Philip Rakity643a81f2010-09-23 08:24:32 -07001475 if (host->ops->platform_send_init_74_clocks)
1476 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1477
Philip Rakity15ec4462010-11-19 16:48:39 -05001478 /*
1479 * If your platform has 8-bit width support but is not a v3 controller,
1480 * or if it requires special setup code, you should implement that in
1481 * platform_8bit_width().
1482 */
1483 if (host->ops->platform_8bit_width)
1484 host->ops->platform_8bit_width(host, ios->bus_width);
1485 else {
1486 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1487 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1488 ctrl &= ~SDHCI_CTRL_4BITBUS;
1489 if (host->version >= SDHCI_SPEC_300)
1490 ctrl |= SDHCI_CTRL_8BITBUS;
1491 } else {
1492 if (host->version >= SDHCI_SPEC_300)
1493 ctrl &= ~SDHCI_CTRL_8BITBUS;
1494 if (ios->bus_width == MMC_BUS_WIDTH_4)
1495 ctrl |= SDHCI_CTRL_4BITBUS;
1496 else
1497 ctrl &= ~SDHCI_CTRL_4BITBUS;
1498 }
1499 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1500 }
1501
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001502 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001503
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001504 if ((ios->timing == MMC_TIMING_SD_HS ||
1505 ios->timing == MMC_TIMING_MMC_HS)
1506 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001507 ctrl |= SDHCI_CTRL_HISPD;
1508 else
1509 ctrl &= ~SDHCI_CTRL_HISPD;
1510
Arindam Nathd6d50a12011-05-05 12:18:59 +05301511 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301512 u16 clk, ctrl_2;
1513 unsigned int clock;
1514
1515 /* In case of UHS-I modes, set High Speed Enable */
Girish K S2cd06dc2012-01-06 09:56:39 +05301516 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1517 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301518 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1519 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001520 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301521 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301522
1523 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1524 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301525 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301526 /*
1527 * We only need to set Driver Strength if the
1528 * preset value enable is not set.
1529 */
1530 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1531 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1532 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1533 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1534 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1535
1536 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301537 } else {
1538 /*
1539 * According to SDHC Spec v3.00, if the Preset Value
1540 * Enable in the Host Control 2 register is set, we
1541 * need to reset SD Clock Enable before changing High
1542 * Speed Enable to avoid generating clock gliches.
1543 */
Arindam Nath758535c2011-05-05 12:19:00 +05301544
1545 /* Reset SD Clock Enable */
1546 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1547 clk &= ~SDHCI_CLOCK_CARD_EN;
1548 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1549
1550 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1551
1552 /* Re-enable SD Clock */
1553 clock = host->clock;
1554 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301555 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301556 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301557 spin_lock_irqsave(&host->lock, flags);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301558 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301559
Arindam Nath49c468f2011-05-05 12:19:01 +05301560 /* Reset SD Clock Enable */
1561 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1562 clk &= ~SDHCI_CLOCK_CARD_EN;
1563 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1564
Philip Rakity6322cdd2011-05-13 11:17:15 +05301565 if (host->ops->set_uhs_signaling)
1566 host->ops->set_uhs_signaling(host, ios->timing);
1567 else {
1568 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1569 /* Select Bus Speed Mode for host */
1570 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S2cd06dc2012-01-06 09:56:39 +05301571 if (ios->timing == MMC_TIMING_MMC_HS200)
1572 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1573 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301574 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1575 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1576 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1577 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1578 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1579 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1580 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1581 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1582 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1583 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1584 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301585
1586 /* Re-enable SD Clock */
1587 clock = host->clock;
1588 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301589 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath49c468f2011-05-05 12:19:01 +05301590 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301591 spin_lock_irqsave(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301592 } else
1593 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301594
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301595 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02001596 /*
1597 * Some (ENE) controllers go apeshit on some ios operation,
1598 * signalling timeout and CRC errors even on CMD0. Resetting
1599 * it on each ios seems to solve the problem.
1600 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001601 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001602 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1603
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301604 /*
1605 * Reset the chip on each power off.
1606 * Should clear out any weird states.
1607 */
1608 if (ios->power_mode == MMC_POWER_OFF) {
1609 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1610 sdhci_reinit(host);
1611 vdd_bit = sdhci_set_power(host, -1);
1612 if (host->vmmc && vdd_bit != -1)
1613 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1614 }
1615 if (!ios->clock)
1616 sdhci_set_clock(host, ios->clock);
1617
Pierre Ossman5f25a662006-10-04 02:15:39 -07001618 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001619}
1620
Adrian Hunter50accb92011-10-03 15:33:34 +03001621static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1622{
1623 struct sdhci_host *host = mmc_priv(mmc);
1624
1625 sdhci_runtime_pm_get(host);
1626 sdhci_do_set_ios(host, ios);
1627 sdhci_runtime_pm_put(host);
1628}
1629
1630static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001631{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001632 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001633 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001634
Pierre Ossmand129bce2006-03-24 03:18:17 -08001635 spin_lock_irqsave(&host->lock, flags);
1636
Pierre Ossman1e728592008-04-16 19:13:13 +02001637 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001638 is_readonly = 0;
1639 else if (host->ops->get_ro)
1640 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001641 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001642 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1643 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001644
1645 spin_unlock_irqrestore(&host->lock, flags);
1646
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001647 /* This quirk needs to be replaced by a callback-function later */
1648 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1649 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001650}
1651
Takashi Iwai82b0e232011-04-21 20:26:38 +02001652#define SAMPLE_COUNT 5
1653
Adrian Hunter50accb92011-10-03 15:33:34 +03001654static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001655{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001656 int i, ro_count;
1657
Takashi Iwai82b0e232011-04-21 20:26:38 +02001658 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001659 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001660
1661 ro_count = 0;
1662 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001663 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001664 if (++ro_count > SAMPLE_COUNT / 2)
1665 return 1;
1666 }
1667 msleep(30);
1668 }
1669 return 0;
1670}
1671
Adrian Hunter50accb92011-10-03 15:33:34 +03001672static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001673{
Adrian Hunter50accb92011-10-03 15:33:34 +03001674 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001675
Adrian Hunter50accb92011-10-03 15:33:34 +03001676 if (host->ops && host->ops->hw_reset)
1677 host->ops->hw_reset(host);
1678}
Adrian Hunter20758b62011-08-29 16:42:12 +03001679
Adrian Hunter50accb92011-10-03 15:33:34 +03001680static int sdhci_get_ro(struct mmc_host *mmc)
1681{
1682 struct sdhci_host *host = mmc_priv(mmc);
1683 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001684
Adrian Hunter50accb92011-10-03 15:33:34 +03001685 sdhci_runtime_pm_get(host);
1686 ret = sdhci_do_get_ro(host);
1687 sdhci_runtime_pm_put(host);
1688 return ret;
1689}
1690
1691static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1692{
Pierre Ossman1e728592008-04-16 19:13:13 +02001693 if (host->flags & SDHCI_DEVICE_DEAD)
1694 goto out;
1695
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001696 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001697 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1698 else
1699 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1700
1701 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1702 if (host->runtime_suspended)
1703 goto out;
1704
1705 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001706 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1707 else
1708 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001709out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001710 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001711}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001712
Adrian Hunter50accb92011-10-03 15:33:34 +03001713static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1714{
1715 struct sdhci_host *host = mmc_priv(mmc);
1716 unsigned long flags;
1717
1718 spin_lock_irqsave(&host->lock, flags);
1719 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001720 spin_unlock_irqrestore(&host->lock, flags);
1721}
1722
Adrian Hunter50accb92011-10-03 15:33:34 +03001723static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1724 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301725{
Arindam Nathf2119df2011-05-05 12:18:57 +05301726 u8 pwr;
1727 u16 clk, ctrl;
1728 u32 present_state;
1729
Arindam Nathf2119df2011-05-05 12:18:57 +05301730 /*
1731 * Signal Voltage Switching is only applicable for Host Controllers
1732 * v3.00 and above.
1733 */
1734 if (host->version < SDHCI_SPEC_300)
1735 return 0;
1736
1737 /*
1738 * We first check whether the request is to set signalling voltage
1739 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1740 */
1741 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1742 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1743 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1744 ctrl &= ~SDHCI_CTRL_VDD_180;
1745 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301746 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301747 host->ops->check_power_status(host, REQ_IO_HIGH);
Arindam Nathf2119df2011-05-05 12:18:57 +05301748
1749 /* Wait for 5ms */
1750 usleep_range(5000, 5500);
1751
1752 /* 3.3V regulator output should be stable within 5 ms */
1753 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1754 if (!(ctrl & SDHCI_CTRL_VDD_180))
1755 return 0;
1756 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301757 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301758 "signalling voltage failed\n");
1759 return -EIO;
1760 }
1761 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1762 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1763 /* Stop SDCLK */
1764 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1765 clk &= ~SDHCI_CLOCK_CARD_EN;
1766 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1767
1768 /* Check whether DAT[3:0] is 0000 */
1769 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1770 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1771 SDHCI_DATA_LVL_SHIFT)) {
1772 /*
1773 * Enable 1.8V Signal Enable in the Host Control2
1774 * register
1775 */
1776 ctrl |= SDHCI_CTRL_VDD_180;
1777 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301778 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301779 host->ops->check_power_status(host, REQ_IO_LOW);
Arindam Nathf2119df2011-05-05 12:18:57 +05301780
1781 /* Wait for 5ms */
1782 usleep_range(5000, 5500);
1783
1784 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1785 if (ctrl & SDHCI_CTRL_VDD_180) {
1786 /* Provide SDCLK again and wait for 1ms*/
1787 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1788 clk |= SDHCI_CLOCK_CARD_EN;
1789 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1790 usleep_range(1000, 1500);
1791
1792 /*
1793 * If DAT[3:0] level is 1111b, then the card
1794 * was successfully switched to 1.8V signaling.
1795 */
1796 present_state = sdhci_readl(host,
1797 SDHCI_PRESENT_STATE);
1798 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1799 SDHCI_DATA_LVL_MASK)
1800 return 0;
1801 }
1802 }
1803
1804 /*
1805 * If we are here, that means the switch to 1.8V signaling
1806 * failed. We power cycle the card, and retry initialization
1807 * sequence by setting S18R to 0.
1808 */
1809 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1810 pwr &= ~SDHCI_POWER_ON;
1811 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301812 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301813 host->ops->check_power_status(host, REQ_BUS_OFF);
Arindam Nathf2119df2011-05-05 12:18:57 +05301814
1815 /* Wait for 1ms as per the spec */
1816 usleep_range(1000, 1500);
1817 pwr |= SDHCI_POWER_ON;
1818 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301819 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301820 host->ops->check_power_status(host, REQ_BUS_ON);
Arindam Nathf2119df2011-05-05 12:18:57 +05301821
Sahitya Tummalaca422112013-02-22 12:15:54 +05301822 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301823 "voltage failed, retrying with S18R set to 0\n");
1824 return -EAGAIN;
1825 } else
1826 /* No signal voltage switch required */
1827 return 0;
1828}
1829
Adrian Hunter50accb92011-10-03 15:33:34 +03001830static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1831 struct mmc_ios *ios)
1832{
1833 struct sdhci_host *host = mmc_priv(mmc);
1834 int err;
1835
1836 if (host->version < SDHCI_SPEC_300)
1837 return 0;
1838 sdhci_runtime_pm_get(host);
1839 err = sdhci_do_start_signal_voltage_switch(host, ios);
1840 sdhci_runtime_pm_put(host);
1841 return err;
1842}
1843
Girish K S2cd06dc2012-01-06 09:56:39 +05301844static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301845{
1846 struct sdhci_host *host;
1847 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05301848 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05301849 int tuning_loop_counter = MAX_TUNING_LOOP;
1850 unsigned long timeout;
1851 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05301852 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301853
1854 host = mmc_priv(mmc);
1855
Adrian Hunter50accb92011-10-03 15:33:34 +03001856 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301857 disable_irq(host->irq);
1858 spin_lock(&host->lock);
1859
1860 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1861
1862 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05301863 * The Host Controller needs tuning only in case of SDR104 mode
1864 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301865 * Capabilities register.
Girish K S2cd06dc2012-01-06 09:56:39 +05301866 * If the Host Controller supports the HS200 mode then the
1867 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301868 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08001869 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1870 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
1871 (host->flags & SDHCI_HS200_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05301872 requires_tuning_nonuhs = true;
1873
Arindam Nathb513ea22011-05-05 12:19:04 +05301874 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301875 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301876 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1877 else {
1878 spin_unlock(&host->lock);
1879 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03001880 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301881 return 0;
1882 }
1883
Asutosh Das8ddd3482013-01-04 11:45:46 +05301884 if (host->ops->execute_tuning) {
1885 spin_unlock(&host->lock);
1886 enable_irq(host->irq);
1887 host->ops->execute_tuning(host, opcode);
1888 disable_irq(host->irq);
1889 spin_lock(&host->lock);
1890 goto out;
1891 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301892 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1893
1894 /*
1895 * As per the Host Controller spec v3.00, tuning command
1896 * generates Buffer Read Ready interrupt, so enable that.
1897 *
1898 * Note: The spec clearly says that when tuning sequence
1899 * is being performed, the controller does not generate
1900 * interrupts other than Buffer Read Ready interrupt. But
1901 * to make sure we don't hit a controller bug, we _only_
1902 * enable Buffer Read Ready interrupt here.
1903 */
1904 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1905 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1906
1907 /*
1908 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1909 * of loops reaches 40 times or a timeout of 150ms occurs.
1910 */
1911 timeout = 150;
1912 do {
1913 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03001914 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301915
1916 if (!tuning_loop_counter && !timeout)
1917 break;
1918
Girish K S2cd06dc2012-01-06 09:56:39 +05301919 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301920 cmd.arg = 0;
1921 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1922 cmd.retries = 0;
1923 cmd.data = NULL;
1924 cmd.error = 0;
1925
1926 mrq.cmd = &cmd;
1927 host->mrq = &mrq;
1928
1929 /*
1930 * In response to CMD19, the card sends 64 bytes of tuning
1931 * block to the Host Controller. So we set the block size
1932 * to 64 here.
1933 */
Girish K S2cd06dc2012-01-06 09:56:39 +05301934 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1935 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1936 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1937 SDHCI_BLOCK_SIZE);
1938 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1939 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1940 SDHCI_BLOCK_SIZE);
1941 } else {
1942 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1943 SDHCI_BLOCK_SIZE);
1944 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301945
1946 /*
1947 * The tuning block is sent by the card to the host controller.
1948 * So we set the TRNS_READ bit in the Transfer Mode register.
1949 * This also takes care of setting DMA Enable and Multi Block
1950 * Select in the same register to 0.
1951 */
1952 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1953
1954 sdhci_send_command(host, &cmd);
1955
1956 host->cmd = NULL;
1957 host->mrq = NULL;
1958
1959 spin_unlock(&host->lock);
1960 enable_irq(host->irq);
1961
1962 /* Wait for Buffer Read Ready interrupt */
1963 wait_event_interruptible_timeout(host->buf_ready_int,
1964 (host->tuning_done == 1),
1965 msecs_to_jiffies(50));
1966 disable_irq(host->irq);
1967 spin_lock(&host->lock);
1968
1969 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301970 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301971 "Buffer Read Ready interrupt during tuning "
1972 "procedure, falling back to fixed sampling "
1973 "clock\n");
1974 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1975 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1976 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1977 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1978
1979 err = -EIO;
1980 goto out;
1981 }
1982
1983 host->tuning_done = 0;
1984
1985 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1986 tuning_loop_counter--;
1987 timeout--;
1988 mdelay(1);
1989 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1990
1991 /*
1992 * The Host Driver has exhausted the maximum number of loops allowed,
1993 * so use fixed sampling frequency.
1994 */
1995 if (!tuning_loop_counter || !timeout) {
1996 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1997 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1998 } else {
1999 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302000 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05302001 " failed, falling back to fixed sampling"
2002 " clock\n");
2003 err = -EIO;
2004 }
2005 }
2006
2007out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302008 /*
2009 * If this is the very first time we are here, we start the retuning
2010 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2011 * flag won't be set, we check this condition before actually starting
2012 * the timer.
2013 */
2014 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2015 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2016 mod_timer(&host->tuning_timer, jiffies +
2017 host->tuning_count * HZ);
2018 /* Tuning mode 1 limits the maximum data length to 4MB */
2019 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2020 } else {
2021 host->flags &= ~SDHCI_NEEDS_RETUNING;
2022 /* Reload the new initial value for timer */
2023 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2024 mod_timer(&host->tuning_timer, jiffies +
2025 host->tuning_count * HZ);
2026 }
2027
2028 /*
2029 * In case tuning fails, host controllers which support re-tuning can
2030 * try tuning again at a later time, when the re-tuning timer expires.
2031 * So for these controllers, we return 0. Since there might be other
2032 * controllers who do not have this capability, we return error for
2033 * them.
2034 */
2035 if (err && host->tuning_count &&
2036 host->tuning_mode == SDHCI_TUNING_MODE_1)
2037 err = 0;
2038
Arindam Nathb513ea22011-05-05 12:19:04 +05302039 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
2040 spin_unlock(&host->lock);
2041 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002042 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302043
2044 return err;
2045}
2046
Adrian Hunter50accb92011-10-03 15:33:34 +03002047static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302048{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302049 u16 ctrl;
2050 unsigned long flags;
2051
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302052 /* Host Controller v3.00 defines preset value registers */
2053 if (host->version < SDHCI_SPEC_300)
2054 return;
2055
2056 spin_lock_irqsave(&host->lock, flags);
2057
2058 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2059
2060 /*
2061 * We only enable or disable Preset Value if they are not already
2062 * enabled or disabled respectively. Otherwise, we bail out.
2063 */
2064 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2065 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2066 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002067 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302068 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2069 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2070 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002071 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302072 }
2073
2074 spin_unlock_irqrestore(&host->lock, flags);
2075}
2076
Adrian Hunter50accb92011-10-03 15:33:34 +03002077static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
2078{
2079 struct sdhci_host *host = mmc_priv(mmc);
2080
2081 sdhci_runtime_pm_get(host);
2082 sdhci_do_enable_preset_value(host, enable);
2083 sdhci_runtime_pm_put(host);
2084}
2085
David Brownellab7aefd2006-11-12 17:55:30 -08002086static const struct mmc_host_ops sdhci_ops = {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002087 .pre_req = sdhci_pre_req,
2088 .post_req = sdhci_post_req,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002089 .request = sdhci_request,
2090 .set_ios = sdhci_set_ios,
2091 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03002092 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002093 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302094 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302095 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302096 .enable_preset_value = sdhci_enable_preset_value,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302097 .enable = sdhci_enable,
2098 .disable = sdhci_disable,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002099};
2100
2101/*****************************************************************************\
2102 * *
2103 * Tasklets *
2104 * *
2105\*****************************************************************************/
2106
2107static void sdhci_tasklet_card(unsigned long param)
2108{
2109 struct sdhci_host *host;
2110 unsigned long flags;
2111
2112 host = (struct sdhci_host*)param;
2113
2114 spin_lock_irqsave(&host->lock, flags);
2115
Adrian Hunter50accb92011-10-03 15:33:34 +03002116 /* Check host->mrq first in case we are runtime suspended */
2117 if (host->mrq &&
2118 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302119 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002120 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05302121 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002122 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002123
Adrian Hunter50accb92011-10-03 15:33:34 +03002124 sdhci_reset(host, SDHCI_RESET_CMD);
2125 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002126
Adrian Hunter50accb92011-10-03 15:33:34 +03002127 host->mrq->cmd->error = -ENOMEDIUM;
2128 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002129 }
2130
2131 spin_unlock_irqrestore(&host->lock, flags);
2132
Pierre Ossman04cf5852008-08-18 22:18:14 +02002133 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002134}
2135
2136static void sdhci_tasklet_finish(unsigned long param)
2137{
2138 struct sdhci_host *host;
2139 unsigned long flags;
2140 struct mmc_request *mrq;
2141
2142 host = (struct sdhci_host*)param;
2143
Adrian Hunter50accb92011-10-03 15:33:34 +03002144 spin_lock_irqsave(&host->lock, flags);
2145
Chris Ball0c9c99a2011-04-27 17:35:31 -04002146 /*
2147 * If this tasklet gets rescheduled while running, it will
2148 * be run again afterwards but without any active request.
2149 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002150 if (!host->mrq) {
2151 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002152 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002153 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002154
2155 del_timer(&host->timer);
2156
2157 mrq = host->mrq;
2158
Pierre Ossmand129bce2006-03-24 03:18:17 -08002159 /*
2160 * The controller needs a reset of internal state machines
2161 * upon error conditions.
2162 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002163 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002164 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002165 (mrq->data && (mrq->data->error ||
2166 (mrq->data->stop && mrq->data->stop->error))) ||
2167 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002168
2169 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002170 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002171 unsigned int clock;
2172
2173 /* This is to force an update */
2174 clock = host->clock;
2175 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302176 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002177 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302178 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002179 }
2180
2181 /* Spec says we should do both at the same time, but Ricoh
2182 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002183 sdhci_reset(host, SDHCI_RESET_CMD);
2184 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002185 } else {
2186 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2187 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002188 }
2189
2190 host->mrq = NULL;
2191 host->cmd = NULL;
2192 host->data = NULL;
2193
Pierre Ossmanf9134312008-12-21 17:01:48 +01002194#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002195 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002196#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002197
Pierre Ossman5f25a662006-10-04 02:15:39 -07002198 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002199 spin_unlock_irqrestore(&host->lock, flags);
2200
2201 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002202 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002203}
2204
2205static void sdhci_timeout_timer(unsigned long data)
2206{
2207 struct sdhci_host *host;
2208 unsigned long flags;
2209
2210 host = (struct sdhci_host*)data;
2211
2212 spin_lock_irqsave(&host->lock, flags);
2213
2214 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302215 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002216 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002217 sdhci_dumpregs(host);
2218
2219 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002220 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002221 sdhci_finish_data(host);
2222 } else {
2223 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002224 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002225 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002226 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002227
2228 tasklet_schedule(&host->finish_tasklet);
2229 }
2230 }
2231
Pierre Ossman5f25a662006-10-04 02:15:39 -07002232 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002233 spin_unlock_irqrestore(&host->lock, flags);
2234}
2235
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302236static void sdhci_tuning_timer(unsigned long data)
2237{
2238 struct sdhci_host *host;
2239 unsigned long flags;
2240
2241 host = (struct sdhci_host *)data;
2242
2243 spin_lock_irqsave(&host->lock, flags);
2244
2245 host->flags |= SDHCI_NEEDS_RETUNING;
2246
2247 spin_unlock_irqrestore(&host->lock, flags);
2248}
2249
Pierre Ossmand129bce2006-03-24 03:18:17 -08002250/*****************************************************************************\
2251 * *
2252 * Interrupt handling *
2253 * *
2254\*****************************************************************************/
2255
2256static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2257{
2258 BUG_ON(intmask == 0);
2259
2260 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302261 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002262 "though no command operation was in progress.\n",
2263 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002264 sdhci_dumpregs(host);
2265 return;
2266 }
2267
Pierre Ossman43b58b32007-07-25 23:15:27 +02002268 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002269 host->cmd->error = -ETIMEDOUT;
2270 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2271 SDHCI_INT_INDEX))
2272 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002273
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302274 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2275 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2276 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2277 if (intmask & SDHCI_INT_CRC) {
2278 sdhci_reset(host, SDHCI_RESET_CMD);
2279 host->cmd->error = 0;
2280 }
2281 }
2282 }
2283
Pierre Ossmane8095172008-07-25 01:09:08 +02002284 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002285 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002286 return;
2287 }
2288
2289 /*
2290 * The host can send and interrupt when the busy state has
2291 * ended, allowing us to wait without wasting CPU cycles.
2292 * Unfortunately this is overloaded on the "data complete"
2293 * interrupt, so we need to take some care when handling
2294 * it.
2295 *
2296 * Note: The 1.0 specification is a bit ambiguous about this
2297 * feature so there might be some problems with older
2298 * controllers.
2299 */
2300 if (host->cmd->flags & MMC_RSP_BUSY) {
2301 if (host->cmd->data)
2302 DBG("Cannot wait for busy signal when also "
2303 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002304 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002305 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002306
2307 /* The controller does not support the end-of-busy IRQ,
2308 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002309 }
2310
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302311 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2312 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2313 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2314 if (intmask & SDHCI_INT_CRC) {
2315 sdhci_finish_command(host);
2316 return;
2317 }
2318 }
2319 }
2320
Pierre Ossmane8095172008-07-25 01:09:08 +02002321 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002322 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002323}
2324
George G. Davis0957c332010-02-18 12:32:12 -05002325#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002326static void sdhci_show_adma_error(struct sdhci_host *host)
2327{
2328 const char *name = mmc_hostname(host->mmc);
2329 u8 *desc = host->adma_desc;
2330 __le32 *dma;
2331 __le16 *len;
2332 u8 attr;
2333
2334 sdhci_dumpregs(host);
2335
2336 while (true) {
2337 dma = (__le32 *)(desc + 4);
2338 len = (__le16 *)(desc + 2);
2339 attr = *desc;
2340
2341 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2342 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2343
2344 desc += 8;
2345
2346 if (attr & 2)
2347 break;
2348 }
2349}
2350#else
2351static void sdhci_show_adma_error(struct sdhci_host *host) { }
2352#endif
2353
Pierre Ossmand129bce2006-03-24 03:18:17 -08002354static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2355{
Girish K S2cd06dc2012-01-06 09:56:39 +05302356 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002357 BUG_ON(intmask == 0);
2358
Arindam Nathb513ea22011-05-05 12:19:04 +05302359 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2360 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302361 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2362 if (command == MMC_SEND_TUNING_BLOCK ||
2363 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302364 host->tuning_done = 1;
2365 wake_up(&host->buf_ready_int);
2366 return;
2367 }
2368 }
2369
Pierre Ossmand129bce2006-03-24 03:18:17 -08002370 if (!host->data) {
2371 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002372 * The "data complete" interrupt is also used to
2373 * indicate that a busy state has ended. See comment
2374 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002375 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002376 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2377 if (intmask & SDHCI_INT_DATA_END) {
2378 sdhci_finish_command(host);
2379 return;
2380 }
2381 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002382
Sahitya Tummalaca422112013-02-22 12:15:54 +05302383 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002384 "though no data operation was in progress.\n",
2385 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002386 sdhci_dumpregs(host);
2387
2388 return;
2389 }
2390
2391 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002392 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002393 else if (intmask & SDHCI_INT_DATA_END_BIT)
2394 host->data->error = -EILSEQ;
2395 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2396 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2397 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002398 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002399 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302400 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002401 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002402 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002403 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002404
Pierre Ossman17b04292007-07-22 22:18:46 +02002405 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002406 sdhci_finish_data(host);
2407 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002408 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002409 sdhci_transfer_pio(host);
2410
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002411 /*
2412 * We currently don't do anything fancy with DMA
2413 * boundaries, but as we can't disable the feature
2414 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002415 *
2416 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2417 * should return a valid address to continue from, but as
2418 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002419 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002420 if (intmask & SDHCI_INT_DMA_END) {
2421 u32 dmastart, dmanow;
2422 dmastart = sg_dma_address(host->data->sg);
2423 dmanow = dmastart + host->data->bytes_xfered;
2424 /*
2425 * Force update to the next DMA block boundary.
2426 */
2427 dmanow = (dmanow &
2428 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2429 SDHCI_DEFAULT_BOUNDARY_SIZE;
2430 host->data->bytes_xfered = dmanow - dmastart;
2431 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2432 " next 0x%08x\n",
2433 mmc_hostname(host->mmc), dmastart,
2434 host->data->bytes_xfered, dmanow);
2435 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2436 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002437
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002438 if (intmask & SDHCI_INT_DATA_END) {
2439 if (host->cmd) {
2440 /*
2441 * Data managed to finish before the
2442 * command completed. Make sure we do
2443 * things in the proper order.
2444 */
2445 host->data_early = 1;
2446 } else {
2447 sdhci_finish_data(host);
2448 }
2449 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002450 }
2451}
2452
David Howells7d12e782006-10-05 14:55:46 +01002453static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002454{
2455 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002456 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002457 u32 intmask, unexpected = 0;
2458 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002459
2460 spin_lock(&host->lock);
2461
Adrian Hunter50accb92011-10-03 15:33:34 +03002462 if (host->runtime_suspended) {
2463 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302464 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002465 mmc_hostname(host->mmc));
2466 return IRQ_HANDLED;
2467 }
2468
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002469 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002470
Mark Lord62df67a2007-03-06 13:30:13 +01002471 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002472 result = IRQ_NONE;
2473 goto out;
2474 }
2475
Alexander Stein6379b232012-03-14 09:52:10 +01002476again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002477 DBG("*** %s got interrupt: 0x%08x\n",
2478 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002479
Pierre Ossman3192a282006-06-30 02:22:26 -07002480 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302481 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2482 SDHCI_CARD_PRESENT;
2483
2484 /*
2485 * There is a observation on i.mx esdhc. INSERT bit will be
2486 * immediately set again when it gets cleared, if a card is
2487 * inserted. We have to mask the irq to prevent interrupt
2488 * storm which will freeze the system. And the REMOVE gets
2489 * the same situation.
2490 *
2491 * More testing are needed here to ensure it works for other
2492 * platforms though.
2493 */
2494 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2495 SDHCI_INT_CARD_REMOVE);
2496 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2497 SDHCI_INT_CARD_INSERT);
2498
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002499 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302500 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2501 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002502 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002503 }
2504
Pierre Ossmand129bce2006-03-24 03:18:17 -08002505 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002506 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2507 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002508 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2509 (host->clock <= 400000))
2510 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002511 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002512 }
2513
2514 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002515 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2516 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002517 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2518 (host->clock <= 400000))
2519 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002520 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002521 }
2522
2523 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2524
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002525 intmask &= ~SDHCI_INT_ERROR;
2526
Pierre Ossmand129bce2006-03-24 03:18:17 -08002527 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302528 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002529 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002530 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002531 }
2532
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002533 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002534
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002535 if (intmask & SDHCI_INT_CARD_INT)
2536 cardint = 1;
2537
2538 intmask &= ~SDHCI_INT_CARD_INT;
2539
Pierre Ossman3192a282006-06-30 02:22:26 -07002540 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002541 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002542 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002543 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002544
2545 result = IRQ_HANDLED;
2546
Alexander Stein6379b232012-03-14 09:52:10 +01002547 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2548 if (intmask && --max_loops)
2549 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002550out:
2551 spin_unlock(&host->lock);
2552
Alexander Stein6379b232012-03-14 09:52:10 +01002553 if (unexpected) {
2554 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2555 mmc_hostname(host->mmc), unexpected);
2556 sdhci_dumpregs(host);
2557 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002558 /*
2559 * We have to delay this as it calls back into the driver.
2560 */
2561 if (cardint)
2562 mmc_signal_sdio_irq(host->mmc);
2563
Pierre Ossmand129bce2006-03-24 03:18:17 -08002564 return result;
2565}
2566
2567/*****************************************************************************\
2568 * *
2569 * Suspend/resume *
2570 * *
2571\*****************************************************************************/
2572
2573#ifdef CONFIG_PM
2574
Manuel Laussd72faa62011-11-03 11:09:45 +01002575int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002576{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002577 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302578 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002579
Chris Balla1b13b42012-02-06 00:43:59 -05002580 if (host->ops->platform_suspend)
2581 host->ops->platform_suspend(host);
2582
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002583 sdhci_disable_card_detection(host);
2584
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302585 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302586 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2587 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2588 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002589 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302590 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302591 }
2592
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002593 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302594 if (ret) {
2595 if (has_tuning_timer) {
2596 host->flags |= SDHCI_NEEDS_RETUNING;
2597 mod_timer(&host->tuning_timer, jiffies +
2598 host->tuning_count * HZ);
2599 }
2600
2601 sdhci_enable_card_detection(host);
2602
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002603 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302604 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002605
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002606 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002607
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002608 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002609}
2610
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002611EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002612
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002613int sdhci_resume_host(struct sdhci_host *host)
2614{
2615 int ret;
2616
Richard Röjforsa13abc72009-09-22 16:45:30 -07002617 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002618 if (host->ops->enable_dma)
2619 host->ops->enable_dma(host);
2620 }
2621
2622 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2623 mmc_hostname(host->mmc), host);
2624 if (ret)
2625 return ret;
2626
Adrian Hunter6308d292012-02-07 14:48:54 +02002627 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2628 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2629 /* Card keeps power but host controller does not */
2630 sdhci_init(host, 0);
2631 host->pwr = 0;
2632 host->clock = 0;
2633 sdhci_do_set_ios(host, &host->mmc->ios);
2634 } else {
2635 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2636 mmiowb();
2637 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002638
2639 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002640 sdhci_enable_card_detection(host);
2641
Chris Balla1b13b42012-02-06 00:43:59 -05002642 if (host->ops->platform_resume)
2643 host->ops->platform_resume(host);
2644
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302645 /* Set the re-tuning expiration flag */
2646 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2647 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2648 host->flags |= SDHCI_NEEDS_RETUNING;
2649
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002650 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002651}
2652
2653EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002654
Daniel Drake5f619702010-11-04 22:20:39 +00002655void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2656{
2657 u8 val;
2658 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2659 val |= SDHCI_WAKE_ON_INT;
2660 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2661}
2662
2663EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2664
Pierre Ossmand129bce2006-03-24 03:18:17 -08002665#endif /* CONFIG_PM */
2666
Adrian Hunter50accb92011-10-03 15:33:34 +03002667#ifdef CONFIG_PM_RUNTIME
2668
2669static int sdhci_runtime_pm_get(struct sdhci_host *host)
2670{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302671 if (!mmc_use_core_runtime_pm(host->mmc))
2672 return pm_runtime_get_sync(host->mmc->parent);
2673 else
2674 return 0;
Adrian Hunter50accb92011-10-03 15:33:34 +03002675}
2676
2677static int sdhci_runtime_pm_put(struct sdhci_host *host)
2678{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302679 if (!mmc_use_core_runtime_pm(host->mmc)) {
2680 pm_runtime_mark_last_busy(host->mmc->parent);
2681 return pm_runtime_put_autosuspend(host->mmc->parent);
2682 } else {
2683 return 0;
2684 }
Adrian Hunter50accb92011-10-03 15:33:34 +03002685}
2686
2687int sdhci_runtime_suspend_host(struct sdhci_host *host)
2688{
2689 unsigned long flags;
2690 int ret = 0;
2691
2692 /* Disable tuning since we are suspending */
2693 if (host->version >= SDHCI_SPEC_300 &&
2694 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2695 del_timer_sync(&host->tuning_timer);
2696 host->flags &= ~SDHCI_NEEDS_RETUNING;
2697 }
2698
2699 spin_lock_irqsave(&host->lock, flags);
2700 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2701 spin_unlock_irqrestore(&host->lock, flags);
2702
2703 synchronize_irq(host->irq);
2704
2705 spin_lock_irqsave(&host->lock, flags);
2706 host->runtime_suspended = true;
2707 spin_unlock_irqrestore(&host->lock, flags);
2708
2709 return ret;
2710}
2711EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2712
2713int sdhci_runtime_resume_host(struct sdhci_host *host)
2714{
2715 unsigned long flags;
2716 int ret = 0, host_flags = host->flags;
2717
2718 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2719 if (host->ops->enable_dma)
2720 host->ops->enable_dma(host);
2721 }
2722
2723 sdhci_init(host, 0);
2724
2725 /* Force clock and power re-program */
2726 host->pwr = 0;
2727 host->clock = 0;
2728 sdhci_do_set_ios(host, &host->mmc->ios);
2729
2730 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2731 if (host_flags & SDHCI_PV_ENABLED)
2732 sdhci_do_enable_preset_value(host, true);
2733
2734 /* Set the re-tuning expiration flag */
2735 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2736 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2737 host->flags |= SDHCI_NEEDS_RETUNING;
2738
2739 spin_lock_irqsave(&host->lock, flags);
2740
2741 host->runtime_suspended = false;
2742
2743 /* Enable SDIO IRQ */
2744 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2745 sdhci_enable_sdio_irq_nolock(host, true);
2746
2747 /* Enable Card Detection */
2748 sdhci_enable_card_detection(host);
2749
2750 spin_unlock_irqrestore(&host->lock, flags);
2751
2752 return ret;
2753}
2754EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2755
2756#endif
2757
Pierre Ossmand129bce2006-03-24 03:18:17 -08002758/*****************************************************************************\
2759 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002760 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002761 * *
2762\*****************************************************************************/
2763
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002764struct sdhci_host *sdhci_alloc_host(struct device *dev,
2765 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002766{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002767 struct mmc_host *mmc;
2768 struct sdhci_host *host;
2769
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002770 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002771
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002772 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002773 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002774 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002775
2776 host = mmc_priv(mmc);
2777 host->mmc = mmc;
2778
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002779 return host;
2780}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002781
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002782EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002783
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002784int sdhci_add_host(struct sdhci_host *host)
2785{
2786 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302787 u32 caps[2];
2788 u32 max_current_caps;
2789 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002790 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002791
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002792 WARN_ON(host == NULL);
2793 if (host == NULL)
2794 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002795
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002796 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002797
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002798 if (debug_quirks)
2799 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03002800 if (debug_quirks2)
2801 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002802
Pierre Ossmand96649e2006-06-30 02:22:30 -07002803 sdhci_reset(host, SDHCI_RESET_ALL);
2804
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002805 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002806 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2807 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002808 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302809 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002810 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002811 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002812 }
2813
Arindam Nathf2119df2011-05-05 12:18:57 +05302814 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002815 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002816
Arindam Nathf2119df2011-05-05 12:18:57 +05302817 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2818 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2819
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002820 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002821 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302822 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002823 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002824 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002825 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002826
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002827 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002828 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002829 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002830 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002831 }
2832
Arindam Nathf2119df2011-05-05 12:18:57 +05302833 if ((host->version >= SDHCI_SPEC_200) &&
2834 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002835 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002836
2837 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2838 (host->flags & SDHCI_USE_ADMA)) {
2839 DBG("Disabling ADMA as it is marked broken\n");
2840 host->flags &= ~SDHCI_USE_ADMA;
2841 }
2842
Richard Röjforsa13abc72009-09-22 16:45:30 -07002843 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002844 if (host->ops->enable_dma) {
2845 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302846 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002847 "available. Falling back to PIO.\n",
2848 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002849 host->flags &=
2850 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002851 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002852 }
2853 }
2854
Pierre Ossman2134a922008-06-28 18:28:51 +02002855 if (host->flags & SDHCI_USE_ADMA) {
2856 /*
2857 * We need to allocate descriptors for all sg entries
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302858 * (128/max_segments) and potentially one alignment transfer for
Pierre Ossman2134a922008-06-28 18:28:51 +02002859 * each of those entries.
2860 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302861 if (host->ops->get_max_segments)
2862 host->adma_max_desc = host->ops->get_max_segments();
2863 else
2864 host->adma_max_desc = 128;
2865
2866 host->adma_desc_sz = (host->adma_max_desc * 2 + 1) * 4;
2867 host->align_buf_sz = host->adma_max_desc * 4;
2868
2869 pr_debug("%s: %s: dma_desc_size: %d\n",
2870 mmc_hostname(host->mmc), __func__, host->adma_desc_sz);
2871 host->adma_desc = kmalloc(host->adma_desc_sz,
2872 GFP_KERNEL);
2873 host->align_buffer = kmalloc(host->align_buf_sz,
2874 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02002875 if (!host->adma_desc || !host->align_buffer) {
2876 kfree(host->adma_desc);
2877 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302878 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002879 "buffers. Falling back to standard DMA.\n",
2880 mmc_hostname(mmc));
2881 host->flags &= ~SDHCI_USE_ADMA;
2882 }
2883 }
2884
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002885 host->next_data.cookie = 1;
2886
Pierre Ossman76591502008-07-21 00:32:11 +02002887 /*
2888 * If we use DMA, then it's up to the caller to set the DMA
2889 * mask, but PIO does not need the hw shim so we set a new
2890 * mask here in that case.
2891 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002892 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002893 host->dma_mask = DMA_BIT_MASK(64);
2894 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2895 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002896
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002897 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302898 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002899 >> SDHCI_CLOCK_BASE_SHIFT;
2900 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302901 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002902 >> SDHCI_CLOCK_BASE_SHIFT;
2903
Pierre Ossmand129bce2006-03-24 03:18:17 -08002904 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002905 if (host->max_clk == 0 || host->quirks &
2906 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002907 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302908 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002909 "frequency.\n", mmc_hostname(mmc));
2910 return -ENODEV;
2911 }
2912 host->max_clk = host->ops->get_max_clock(host);
2913 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002914
2915 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302916 * In case of Host Controller v3.00, find out whether clock
2917 * multiplier is supported.
2918 */
2919 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2920 SDHCI_CLOCK_MUL_SHIFT;
2921
2922 /*
2923 * In case the value in Clock Multiplier is 0, then programmable
2924 * clock mode is not supported, otherwise the actual clock
2925 * multiplier is one more than the value of Clock Multiplier
2926 * in the Capabilities Register.
2927 */
2928 if (host->clk_mul)
2929 host->clk_mul += 1;
2930
2931 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002932 * Set host parameters.
2933 */
2934 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302935 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002936 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002937 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302938 else if (host->version >= SDHCI_SPEC_300) {
2939 if (host->clk_mul) {
2940 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2941 mmc->f_max = host->max_clk * host->clk_mul;
2942 } else
2943 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2944 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002945 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002946
Sahitya Tummalaca422112013-02-22 12:15:54 +05302947 host->timeout_clk =
2948 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2949 if (host->timeout_clk == 0) {
2950 if (host->ops->get_timeout_clock) {
2951 host->timeout_clk = host->ops->get_timeout_clock(host);
2952 } else if (!(host->quirks &
2953 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2954 pr_err("%s: Hardware doesn't specify timeout clock "
2955 "frequency.\n", mmc_hostname(mmc));
2956 return -ENODEV;
2957 }
2958 }
2959 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2960 host->timeout_clk *= 1000;
2961
2962 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
2963 host->timeout_clk = mmc->f_max / 1000;
2964
2965 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
2966
Andrei Warkentine89d4562011-05-23 15:06:37 -05002967 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2968
2969 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2970 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002971
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002972 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002973 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002974 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002975 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002976 host->flags |= SDHCI_AUTO_CMD23;
2977 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2978 } else {
2979 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2980 }
2981
Philip Rakity15ec4462010-11-19 16:48:39 -05002982 /*
2983 * A controller may support 8-bit width, but the board itself
2984 * might not have the pins brought out. Boards that support
2985 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2986 * their platform code before calling sdhci_add_host(), and we
2987 * won't assume 8-bit width for hosts without that CAP.
2988 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002989 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002990 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002991
Arindam Nathf2119df2011-05-05 12:18:57 +05302992 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002993 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002994
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002995 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2996 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002997 mmc->caps |= MMC_CAP_NEEDS_POLL;
2998
Al Cooper4188bba2012-03-16 15:54:17 -04002999 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3000 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3001 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303002 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3003
3004 /* SDR104 supports also implies SDR50 support */
3005 if (caps[1] & SDHCI_SUPPORT_SDR104)
3006 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3007 else if (caps[1] & SDHCI_SUPPORT_SDR50)
3008 mmc->caps |= MMC_CAP_UHS_SDR50;
3009
3010 if (caps[1] & SDHCI_SUPPORT_DDR50)
3011 mmc->caps |= MMC_CAP_UHS_DDR50;
3012
Girish K S2cd06dc2012-01-06 09:56:39 +05303013 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303014 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3015 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3016
Girish K S2cd06dc2012-01-06 09:56:39 +05303017 /* Does the host need tuning for HS200? */
3018 if (mmc->caps2 & MMC_CAP2_HS200)
3019 host->flags |= SDHCI_HS200_NEEDS_TUNING;
3020
Arindam Nathd6d50a12011-05-05 12:18:59 +05303021 /* Driver Type(s) (A, C, D) supported by the host */
3022 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3023 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3024 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3025 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3026 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3027 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3028
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02003029 /* Initial value for re-tuning timer count */
3030 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3031 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3032
3033 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303034 * In case Re-tuning Timer is not disabled, the actual value of
3035 * re-tuning timer will be 2 ^ (n - 1).
3036 */
3037 if (host->tuning_count)
3038 host->tuning_count = 1 << (host->tuning_count - 1);
3039
3040 /* Re-tuning mode supported by the Host Controller */
3041 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3042 SDHCI_RETUNING_MODE_SHIFT;
3043
Takashi Iwai8f230f42010-12-08 10:04:30 +01003044 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05303045 /*
3046 * According to SD Host Controller spec v3.00, if the Host System
3047 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3048 * the value is meaningful only if Voltage Support in the Capabilities
3049 * register is set. The actual current value is 4 times the register
3050 * value.
3051 */
3052 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3053
3054 if (caps[0] & SDHCI_CAN_VDD_330) {
3055 int max_current_330;
3056
Takashi Iwai8f230f42010-12-08 10:04:30 +01003057 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303058
3059 max_current_330 = ((max_current_caps &
3060 SDHCI_MAX_CURRENT_330_MASK) >>
3061 SDHCI_MAX_CURRENT_330_SHIFT) *
3062 SDHCI_MAX_CURRENT_MULTIPLIER;
3063
3064 if (max_current_330 > 150)
3065 mmc->caps |= MMC_CAP_SET_XPC_330;
3066 }
3067 if (caps[0] & SDHCI_CAN_VDD_300) {
3068 int max_current_300;
3069
Takashi Iwai8f230f42010-12-08 10:04:30 +01003070 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303071
3072 max_current_300 = ((max_current_caps &
3073 SDHCI_MAX_CURRENT_300_MASK) >>
3074 SDHCI_MAX_CURRENT_300_SHIFT) *
3075 SDHCI_MAX_CURRENT_MULTIPLIER;
3076
3077 if (max_current_300 > 150)
3078 mmc->caps |= MMC_CAP_SET_XPC_300;
3079 }
3080 if (caps[0] & SDHCI_CAN_VDD_180) {
3081 int max_current_180;
3082
Takashi Iwai8f230f42010-12-08 10:04:30 +01003083 ocr_avail |= MMC_VDD_165_195;
3084
Arindam Nathf2119df2011-05-05 12:18:57 +05303085 max_current_180 = ((max_current_caps &
3086 SDHCI_MAX_CURRENT_180_MASK) >>
3087 SDHCI_MAX_CURRENT_180_SHIFT) *
3088 SDHCI_MAX_CURRENT_MULTIPLIER;
3089
3090 if (max_current_180 > 150)
3091 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05303092
3093 /* Maximum current capabilities of the host at 1.8V */
3094 if (max_current_180 >= 800)
3095 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
3096 else if (max_current_180 >= 600)
3097 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
3098 else if (max_current_180 >= 400)
3099 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
3100 else
3101 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05303102 }
3103
Takashi Iwai8f230f42010-12-08 10:04:30 +01003104 mmc->ocr_avail = ocr_avail;
3105 mmc->ocr_avail_sdio = ocr_avail;
3106 if (host->ocr_avail_sdio)
3107 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3108 mmc->ocr_avail_sd = ocr_avail;
3109 if (host->ocr_avail_sd)
3110 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3111 else /* normal SD controllers don't support 1.8V */
3112 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3113 mmc->ocr_avail_mmc = ocr_avail;
3114 if (host->ocr_avail_mmc)
3115 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003116
3117 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303118 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003119 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003120 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003121 }
3122
Pierre Ossmand129bce2006-03-24 03:18:17 -08003123 spin_lock_init(&host->lock);
3124
3125 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003126 * Maximum number of segments. Depends on if the hardware
3127 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003128 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003129 if (host->flags & SDHCI_USE_ADMA)
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303130 mmc->max_segs = host->adma_max_desc;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003131 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003132 mmc->max_segs = 1;
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303133 else/* PIO */
3134 mmc->max_segs = host->adma_max_desc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003135
3136 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003137 * Maximum number of sectors in one transfer. Limited by DMA boundary
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303138 * size (512KiB), unless specified by platform specific driver. Each
3139 * descriptor can transfer a maximum of 64KB.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003140 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303141 if (host->ops->get_max_segments)
3142 mmc->max_req_size = (host->adma_max_desc * 65536);
3143 else
3144 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003145
3146 /*
3147 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003148 * of bytes. When doing hardware scatter/gather, each entry cannot
3149 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003150 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003151 if (host->flags & SDHCI_USE_ADMA) {
3152 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3153 mmc->max_seg_size = 65535;
3154 else
3155 mmc->max_seg_size = 65536;
3156 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003157 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003158 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003159
3160 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003161 * Maximum block size. This varies from controller to controller and
3162 * is specified in the capabilities register.
3163 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003164 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3165 mmc->max_blk_size = 2;
3166 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303167 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003168 SDHCI_MAX_BLOCK_SHIFT;
3169 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303170 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003171 "assuming 512 bytes\n", mmc_hostname(mmc));
3172 mmc->max_blk_size = 0;
3173 }
3174 }
3175
3176 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003177
3178 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003179 * Maximum block count.
3180 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003181 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003182
3183 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003184 * Init tasklets.
3185 */
3186 tasklet_init(&host->card_tasklet,
3187 sdhci_tasklet_card, (unsigned long)host);
3188 tasklet_init(&host->finish_tasklet,
3189 sdhci_tasklet_finish, (unsigned long)host);
3190
Al Viroe4cad1b2006-10-10 22:47:07 +01003191 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003192
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303193 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303194 init_waitqueue_head(&host->buf_ready_int);
3195
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303196 /* Initialize re-tuning timer */
3197 init_timer(&host->tuning_timer);
3198 host->tuning_timer.data = (unsigned long)host;
3199 host->tuning_timer.function = sdhci_tuning_timer;
3200 }
3201
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003202 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003203 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003204 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003205 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003206
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003207 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3208 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303209 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003210 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003211 }
3212
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003213 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003214
3215#ifdef CONFIG_MMC_DEBUG
3216 sdhci_dumpregs(host);
3217#endif
3218
Pierre Ossmanf9134312008-12-21 17:01:48 +01003219#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003220 snprintf(host->led_name, sizeof(host->led_name),
3221 "%s::", mmc_hostname(mmc));
3222 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003223 host->led.brightness = LED_OFF;
3224 host->led.default_trigger = mmc_hostname(mmc);
3225 host->led.brightness_set = sdhci_led_control;
3226
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003227 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003228 if (ret)
3229 goto reset;
3230#endif
3231
Pierre Ossman5f25a662006-10-04 02:15:39 -07003232 mmiowb();
3233
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303234 if (host->cpu_dma_latency_us)
3235 pm_qos_add_request(&host->pm_qos_req_dma,
3236 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003237 mmc_add_host(mmc);
3238
Sahitya Tummalaca422112013-02-22 12:15:54 +05303239 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003240 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003241 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3242 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003243
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003244 sdhci_enable_card_detection(host);
3245
Pierre Ossmand129bce2006-03-24 03:18:17 -08003246 return 0;
3247
Pierre Ossmanf9134312008-12-21 17:01:48 +01003248#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003249reset:
3250 sdhci_reset(host, SDHCI_RESET_ALL);
3251 free_irq(host->irq, host);
3252#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003253untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003254 tasklet_kill(&host->card_tasklet);
3255 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003256
3257 return ret;
3258}
3259
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003260EXPORT_SYMBOL_GPL(sdhci_add_host);
3261
Pierre Ossman1e728592008-04-16 19:13:13 +02003262void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003263{
Pierre Ossman1e728592008-04-16 19:13:13 +02003264 unsigned long flags;
3265
3266 if (dead) {
3267 spin_lock_irqsave(&host->lock, flags);
3268
3269 host->flags |= SDHCI_DEVICE_DEAD;
3270
3271 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303272 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003273 " transfer!\n", mmc_hostname(host->mmc));
3274
3275 host->mrq->cmd->error = -ENOMEDIUM;
3276 tasklet_schedule(&host->finish_tasklet);
3277 }
3278
3279 spin_unlock_irqrestore(&host->lock, flags);
3280 }
3281
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003282 sdhci_disable_card_detection(host);
3283
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303284 if (host->cpu_dma_latency_us)
3285 pm_qos_remove_request(&host->pm_qos_req_dma);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003286 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003287
Pierre Ossmanf9134312008-12-21 17:01:48 +01003288#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003289 led_classdev_unregister(&host->led);
3290#endif
3291
Pierre Ossman1e728592008-04-16 19:13:13 +02003292 if (!dead)
3293 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003294
3295 free_irq(host->irq, host);
3296
3297 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303298 if (host->version >= SDHCI_SPEC_300)
3299 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003300
3301 tasklet_kill(&host->card_tasklet);
3302 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003303
Sahitya Tummalaca422112013-02-22 12:15:54 +05303304 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003305 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003306
Pierre Ossman2134a922008-06-28 18:28:51 +02003307 kfree(host->adma_desc);
3308 kfree(host->align_buffer);
3309
3310 host->adma_desc = NULL;
3311 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003312}
3313
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003314EXPORT_SYMBOL_GPL(sdhci_remove_host);
3315
3316void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003317{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003318 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003319}
3320
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003321EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003322
3323/*****************************************************************************\
3324 * *
3325 * Driver init/exit *
3326 * *
3327\*****************************************************************************/
3328
3329static int __init sdhci_drv_init(void)
3330{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303331 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003332 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303333 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003334
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003335 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003336}
3337
3338static void __exit sdhci_drv_exit(void)
3339{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003340}
3341
3342module_init(sdhci_drv_init);
3343module_exit(sdhci_drv_exit);
3344
Pierre Ossmandf673b22006-06-30 02:22:31 -07003345module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003346module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003347
Pierre Ossman32710e82009-04-08 20:14:54 +02003348MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003349MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003350MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003351
Pierre Ossmandf673b22006-06-30 02:22:31 -07003352MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003353MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");