blob: 564e91053a1ac86ec938db0a6d205c78d812ab5f [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter50accb92011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080030
Pierre Ossmand129bce2006-03-24 03:18:17 -080031#include "sdhci.h"
32
33#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080034
Pierre Ossmand129bce2006-03-24 03:18:17 -080035#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010036 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmanf9134312008-12-21 17:01:48 +010038#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
39 defined(CONFIG_MMC_SDHCI_MODULE))
40#define SDHCI_USE_LEDS_CLASS
41#endif
42
Arindam Nathb513ea22011-05-05 12:19:04 +053043#define MAX_TUNING_LOOP 40
44
Pierre Ossmandf673b22006-06-30 02:22:31 -070045static unsigned int debug_quirks = 0;
Adrian Hunter50accb92011-10-03 15:33:34 +030046static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070047
Pierre Ossmand129bce2006-03-24 03:18:17 -080048static void sdhci_finish_data(struct sdhci_host *);
49
50static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
51static void sdhci_finish_command(struct sdhci_host *);
Girish K S2cd06dc2012-01-06 09:56:39 +053052static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053053static void sdhci_tuning_timer(unsigned long data);
Pierre Ossmand129bce2006-03-24 03:18:17 -080054
Adrian Hunter50accb92011-10-03 15:33:34 +030055#ifdef CONFIG_PM_RUNTIME
56static int sdhci_runtime_pm_get(struct sdhci_host *host);
57static int sdhci_runtime_pm_put(struct sdhci_host *host);
58#else
59static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
60{
61 return 0;
62}
63static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
64{
65 return 0;
66}
67#endif
68
Pierre Ossmand129bce2006-03-24 03:18:17 -080069static void sdhci_dumpregs(struct sdhci_host *host)
70{
Sahitya Tummalaca422112013-02-22 12:15:54 +053071 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070072 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080073
Sahitya Tummalaca422112013-02-22 12:15:54 +053074 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030075 sdhci_readl(host, SDHCI_DMA_ADDRESS),
76 sdhci_readw(host, SDHCI_HOST_VERSION));
Sahitya Tummalaca422112013-02-22 12:15:54 +053077 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030078 sdhci_readw(host, SDHCI_BLOCK_SIZE),
79 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Sahitya Tummalaca422112013-02-22 12:15:54 +053080 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030081 sdhci_readl(host, SDHCI_ARGUMENT),
82 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Sahitya Tummalaca422112013-02-22 12:15:54 +053083 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030084 sdhci_readl(host, SDHCI_PRESENT_STATE),
85 sdhci_readb(host, SDHCI_HOST_CONTROL));
Sahitya Tummalaca422112013-02-22 12:15:54 +053086 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030087 sdhci_readb(host, SDHCI_POWER_CONTROL),
88 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Sahitya Tummalaca422112013-02-22 12:15:54 +053089 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030090 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
91 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Sahitya Tummalaca422112013-02-22 12:15:54 +053092 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030093 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
94 sdhci_readl(host, SDHCI_INT_STATUS));
Sahitya Tummalaca422112013-02-22 12:15:54 +053095 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030096 sdhci_readl(host, SDHCI_INT_ENABLE),
97 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Sahitya Tummalaca422112013-02-22 12:15:54 +053098 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030099 sdhci_readw(host, SDHCI_ACMD12_ERR),
100 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Sahitya Tummalaca422112013-02-22 12:15:54 +0530101 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300102 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500103 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Sahitya Tummalaca422112013-02-22 12:15:54 +0530104 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500105 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readl(host, SDHCI_MAX_CURRENT));
Sahitya Tummalaca422112013-02-22 12:15:54 +0530107 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530108 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800109
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100110 if (host->flags & SDHCI_USE_ADMA)
Sahitya Tummalaca422112013-02-22 12:15:54 +0530111 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100112 readl(host->ioaddr + SDHCI_ADMA_ERROR),
113 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
114
Sahitya Tummalaca422112013-02-22 12:15:54 +0530115 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800116}
117
118/*****************************************************************************\
119 * *
120 * Low level functions *
121 * *
122\*****************************************************************************/
123
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300124static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
125{
126 u32 ier;
127
128 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
129 ier &= ~clear;
130 ier |= set;
131 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
132 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
133}
134
135static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
136{
137 sdhci_clear_set_irqs(host, 0, irqs);
138}
139
140static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
141{
142 sdhci_clear_set_irqs(host, irqs, 0);
143}
144
145static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
146{
Sahitya Tummalaca422112013-02-22 12:15:54 +0530147 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300148
Adrian Hunterc79396c2011-12-27 15:48:42 +0200149 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100150 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300151 return;
152
Sahitya Tummalaca422112013-02-22 12:15:54 +0530153 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
154 SDHCI_CARD_PRESENT;
155 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
Adrian Hunter50accb92011-10-03 15:33:34 +0300156
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300157 if (enable)
158 sdhci_unmask_irqs(host, irqs);
159 else
160 sdhci_mask_irqs(host, irqs);
161}
162
163static void sdhci_enable_card_detection(struct sdhci_host *host)
164{
165 sdhci_set_card_detection(host, true);
166}
167
168static void sdhci_disable_card_detection(struct sdhci_host *host)
169{
170 sdhci_set_card_detection(host, false);
171}
172
Pierre Ossmand129bce2006-03-24 03:18:17 -0800173static void sdhci_reset(struct sdhci_host *host, u8 mask)
174{
Pierre Ossmane16514d2006-06-30 02:22:24 -0700175 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300176 u32 uninitialized_var(ier);
Pierre Ossmane16514d2006-06-30 02:22:24 -0700177
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100178 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300179 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700180 SDHCI_CARD_PRESENT))
181 return;
182 }
183
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300184 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
185 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
186
Philip Rakity393c1a32011-01-21 11:26:40 -0800187 if (host->ops->platform_reset_enter)
188 host->ops->platform_reset_enter(host, mask);
189
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300190 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800191
Pierre Ossmane16514d2006-06-30 02:22:24 -0700192 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800193 host->clock = 0;
194
Pierre Ossmane16514d2006-06-30 02:22:24 -0700195 /* Wait max 100 ms */
196 timeout = 100;
197
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530198 if (host->ops->check_power_status && host->pwr &&
199 (mask & SDHCI_RESET_ALL))
200 host->ops->check_power_status(host);
201
Pierre Ossmane16514d2006-06-30 02:22:24 -0700202 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300203 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d2006-06-30 02:22:24 -0700204 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +0530205 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d2006-06-30 02:22:24 -0700206 mmc_hostname(host->mmc), (int)mask);
207 sdhci_dumpregs(host);
208 return;
209 }
210 timeout--;
211 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800212 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300213
Philip Rakity393c1a32011-01-21 11:26:40 -0800214 if (host->ops->platform_reset_exit)
215 host->ops->platform_reset_exit(host, mask);
216
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300217 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
218 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Sahitya Tummalaca422112013-02-22 12:15:54 +0530219
220 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
221 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
222 host->ops->enable_dma(host);
223 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800224}
225
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800226static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
227
228static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800229{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800230 if (soft)
231 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
232 else
233 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800234
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300235 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
236 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700237 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
238 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300239 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800240
241 if (soft) {
242 /* force clock reconfiguration */
243 host->clock = 0;
244 sdhci_set_ios(host->mmc, &host->mmc->ios);
245 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300246}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800247
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300248static void sdhci_reinit(struct sdhci_host *host)
249{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800250 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300251 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800252}
253
254static void sdhci_activate_led(struct sdhci_host *host)
255{
256 u8 ctrl;
257
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300258 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800259 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300260 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261}
262
263static void sdhci_deactivate_led(struct sdhci_host *host)
264{
265 u8 ctrl;
266
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300267 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800268 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300269 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800270}
271
Pierre Ossmanf9134312008-12-21 17:01:48 +0100272#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100273static void sdhci_led_control(struct led_classdev *led,
274 enum led_brightness brightness)
275{
276 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
277 unsigned long flags;
278
279 spin_lock_irqsave(&host->lock, flags);
280
Adrian Hunter50accb92011-10-03 15:33:34 +0300281 if (host->runtime_suspended)
282 goto out;
283
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100284 if (brightness == LED_OFF)
285 sdhci_deactivate_led(host);
286 else
287 sdhci_activate_led(host);
Adrian Hunter50accb92011-10-03 15:33:34 +0300288out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100289 spin_unlock_irqrestore(&host->lock, flags);
290}
291#endif
292
Pierre Ossmand129bce2006-03-24 03:18:17 -0800293/*****************************************************************************\
294 * *
295 * Core functions *
296 * *
297\*****************************************************************************/
298
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100299static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800300{
Pierre Ossman76591502008-07-21 00:32:11 +0200301 unsigned long flags;
302 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700303 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200304 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800305
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100306 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100308 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200309 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800310
Pierre Ossman76591502008-07-21 00:32:11 +0200311 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800312
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200314 if (!sg_miter_next(&host->sg_miter))
315 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800316
Pierre Ossman76591502008-07-21 00:32:11 +0200317 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800318
Pierre Ossman76591502008-07-21 00:32:11 +0200319 blksize -= len;
320 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200321
Pierre Ossman76591502008-07-21 00:32:11 +0200322 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800323
Pierre Ossman76591502008-07-21 00:32:11 +0200324 while (len) {
325 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300326 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200327 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800328 }
Pierre Ossman76591502008-07-21 00:32:11 +0200329
330 *buf = scratch & 0xFF;
331
332 buf++;
333 scratch >>= 8;
334 chunk--;
335 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800336 }
337 }
Pierre Ossman76591502008-07-21 00:32:11 +0200338
339 sg_miter_stop(&host->sg_miter);
340
341 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100342}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800343
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100344static void sdhci_write_block_pio(struct sdhci_host *host)
345{
Pierre Ossman76591502008-07-21 00:32:11 +0200346 unsigned long flags;
347 size_t blksize, len, chunk;
348 u32 scratch;
349 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100350
351 DBG("PIO writing\n");
352
353 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200354 chunk = 0;
355 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100356
Pierre Ossman76591502008-07-21 00:32:11 +0200357 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358
359 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200360 if (!sg_miter_next(&host->sg_miter))
361 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100362
Pierre Ossman76591502008-07-21 00:32:11 +0200363 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200364
Pierre Ossman76591502008-07-21 00:32:11 +0200365 blksize -= len;
366 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100367
Pierre Ossman76591502008-07-21 00:32:11 +0200368 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100369
Pierre Ossman76591502008-07-21 00:32:11 +0200370 while (len) {
371 scratch |= (u32)*buf << (chunk * 8);
372
373 buf++;
374 chunk++;
375 len--;
376
377 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300378 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200379 chunk = 0;
380 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100382 }
383 }
Pierre Ossman76591502008-07-21 00:32:11 +0200384
385 sg_miter_stop(&host->sg_miter);
386
387 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100388}
389
390static void sdhci_transfer_pio(struct sdhci_host *host)
391{
392 u32 mask;
393
394 BUG_ON(!host->data);
395
Pierre Ossman76591502008-07-21 00:32:11 +0200396 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100397 return;
398
399 if (host->data->flags & MMC_DATA_READ)
400 mask = SDHCI_DATA_AVAILABLE;
401 else
402 mask = SDHCI_SPACE_AVAILABLE;
403
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200404 /*
405 * Some controllers (JMicron JMB38x) mess up the buffer bits
406 * for transfers < 4 bytes. As long as it is just one block,
407 * we can ignore the bits.
408 */
409 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
410 (host->data->blocks == 1))
411 mask = ~0;
412
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300413 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300414 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
415 udelay(100);
416
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100417 if (host->data->flags & MMC_DATA_READ)
418 sdhci_read_block_pio(host);
419 else
420 sdhci_write_block_pio(host);
421
Pierre Ossman76591502008-07-21 00:32:11 +0200422 host->blocks--;
423 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100424 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100425 }
426
427 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800428}
429
Pierre Ossman2134a922008-06-28 18:28:51 +0200430static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
431{
432 local_irq_save(*flags);
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800433 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200434}
435
436static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
437{
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800438 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200439 local_irq_restore(*flags);
440}
441
Ben Dooks118cd172010-03-05 13:43:26 -0800442static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
443{
Ben Dooks9e506f32010-03-05 13:43:29 -0800444 __le32 *dataddr = (__le32 __force *)(desc + 4);
445 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800446
Ben Dooks9e506f32010-03-05 13:43:29 -0800447 /* SDHCI specification says ADMA descriptors should be 4 byte
448 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800449
Ben Dooks9e506f32010-03-05 13:43:29 -0800450 cmdlen[0] = cpu_to_le16(cmd);
451 cmdlen[1] = cpu_to_le16(len);
452
453 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800454}
455
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200456static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200457 struct mmc_data *data)
458{
459 int direction;
460
461 u8 *desc;
462 u8 *align;
463 dma_addr_t addr;
464 dma_addr_t align_addr;
465 int len, offset;
466
467 struct scatterlist *sg;
468 int i;
469 char *buffer;
470 unsigned long flags;
471
472 /*
473 * The spec does not specify endianness of descriptor table.
474 * We currently guess that it is LE.
475 */
476
477 if (data->flags & MMC_DATA_READ)
478 direction = DMA_FROM_DEVICE;
479 else
480 direction = DMA_TO_DEVICE;
481
482 /*
483 * The ADMA descriptor table is mapped further down as we
484 * need to fill it with data first.
485 */
486
487 host->align_addr = dma_map_single(mmc_dev(host->mmc),
488 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700489 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200490 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200491 BUG_ON(host->align_addr & 0x3);
492
493 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
494 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200495 if (host->sg_count == 0)
496 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200497
498 desc = host->adma_desc;
499 align = host->align_buffer;
500
501 align_addr = host->align_addr;
502
503 for_each_sg(data->sg, sg, host->sg_count, i) {
504 addr = sg_dma_address(sg);
505 len = sg_dma_len(sg);
506
507 /*
508 * The SDHCI specification states that ADMA
509 * addresses must be 32-bit aligned. If they
510 * aren't, then we use a bounce buffer for
511 * the (up to three) bytes that screw up the
512 * alignment.
513 */
514 offset = (4 - (addr & 0x3)) & 0x3;
515 if (offset) {
516 if (data->flags & MMC_DATA_WRITE) {
517 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200518 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200519 memcpy(align, buffer, offset);
520 sdhci_kunmap_atomic(buffer, &flags);
521 }
522
Ben Dooks118cd172010-03-05 13:43:26 -0800523 /* tran, valid */
524 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200525
526 BUG_ON(offset > 65536);
527
Pierre Ossman2134a922008-06-28 18:28:51 +0200528 align += 4;
529 align_addr += 4;
530
531 desc += 8;
532
533 addr += offset;
534 len -= offset;
535 }
536
Pierre Ossman2134a922008-06-28 18:28:51 +0200537 BUG_ON(len > 65536);
538
Ben Dooks118cd172010-03-05 13:43:26 -0800539 /* tran, valid */
540 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200541 desc += 8;
542
543 /*
544 * If this triggers then we have a calculation bug
545 * somewhere. :/
546 */
547 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
548 }
549
Thomas Abraham70764a92010-05-26 14:42:04 -0700550 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
551 /*
552 * Mark the last descriptor as the terminating descriptor
553 */
554 if (desc != host->adma_desc) {
555 desc -= 8;
556 desc[0] |= 0x2; /* end */
557 }
558 } else {
559 /*
560 * Add a terminating entry.
561 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200562
Thomas Abraham70764a92010-05-26 14:42:04 -0700563 /* nop, end, valid */
564 sdhci_set_adma_desc(desc, 0, 0, 0x3);
565 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200566
567 /*
568 * Resync align buffer as we might have changed it.
569 */
570 if (data->flags & MMC_DATA_WRITE) {
571 dma_sync_single_for_device(mmc_dev(host->mmc),
572 host->align_addr, 128 * 4, direction);
573 }
574
575 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
576 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200577 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200578 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200579 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200580
581 return 0;
582
583unmap_entries:
584 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
585 data->sg_len, direction);
586unmap_align:
587 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
588 128 * 4, direction);
589fail:
590 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200591}
592
593static void sdhci_adma_table_post(struct sdhci_host *host,
594 struct mmc_data *data)
595{
596 int direction;
597
598 struct scatterlist *sg;
599 int i, size;
600 u8 *align;
601 char *buffer;
602 unsigned long flags;
603
604 if (data->flags & MMC_DATA_READ)
605 direction = DMA_FROM_DEVICE;
606 else
607 direction = DMA_TO_DEVICE;
608
609 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
610 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
611
612 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
613 128 * 4, direction);
614
615 if (data->flags & MMC_DATA_READ) {
616 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
617 data->sg_len, direction);
618
619 align = host->align_buffer;
620
621 for_each_sg(data->sg, sg, host->sg_count, i) {
622 if (sg_dma_address(sg) & 0x3) {
623 size = 4 - (sg_dma_address(sg) & 0x3);
624
625 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200626 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200627 memcpy(buffer, align, size);
628 sdhci_kunmap_atomic(buffer, &flags);
629
630 align += 4;
631 }
632 }
633 }
634
635 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
636 data->sg_len, direction);
637}
638
Andrei Warkentina3c77782011-04-11 16:13:42 -0500639static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800640{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700641 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500642 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700643 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800644
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200645 /*
646 * If the host controller provides us with an incorrect timeout
647 * value, just skip the check and use 0xE. The hardware may take
648 * longer to time out, but that's much better than having a too-short
649 * timeout value.
650 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200651 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200652 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200653
Andrei Warkentina3c77782011-04-11 16:13:42 -0500654 /* Unspecified timeout, assume max */
655 if (!data && !cmd->cmd_timeout_ms)
656 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800657
Andrei Warkentina3c77782011-04-11 16:13:42 -0500658 /* timeout in us */
659 if (!data)
660 target_timeout = cmd->cmd_timeout_ms * 1000;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530661 else {
662 target_timeout = data->timeout_ns / 1000;
663 if (host->clock)
664 target_timeout += data->timeout_clks / host->clock;
665 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700666
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700667 /*
668 * Figure out needed cycles.
669 * We do this in steps in order to fit inside a 32 bit int.
670 * The first step is the minimum timeout, which will have a
671 * minimum resolution of 6 bits:
672 * (1) 2^13*1000 > 2^22,
673 * (2) host->timeout_clk < 2^16
674 * =>
675 * (1) / (2) > 2^6
676 */
677 count = 0;
678 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
679 while (current_timeout < target_timeout) {
680 count++;
681 current_timeout <<= 1;
682 if (count >= 0xF)
683 break;
684 }
685
Sahitya Tummalaca422112013-02-22 12:15:54 +0530686 if (count >= 0xF) {
Chris Ball34ca2092012-06-01 10:39:45 -0400687 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
688 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700689 count = 0xE;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530690 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700691
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200692 return count;
693}
694
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300695static void sdhci_set_transfer_irqs(struct sdhci_host *host)
696{
697 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
698 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
699
700 if (host->flags & SDHCI_REQ_USE_DMA)
701 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
702 else
703 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
704}
705
Andrei Warkentina3c77782011-04-11 16:13:42 -0500706static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200707{
708 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200709 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500710 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200711 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200712
713 WARN_ON(host->data);
714
Andrei Warkentina3c77782011-04-11 16:13:42 -0500715 if (data || (cmd->flags & MMC_RSP_BUSY)) {
716 count = sdhci_calc_timeout(host, cmd);
717 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
718 }
719
720 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200721 return;
722
723 /* Sanity checks */
724 BUG_ON(data->blksz * data->blocks > 524288);
725 BUG_ON(data->blksz > host->mmc->max_blk_size);
726 BUG_ON(data->blocks > 65535);
727
728 host->data = data;
729 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400730 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200731
Richard Röjforsa13abc72009-09-22 16:45:30 -0700732 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100733 host->flags |= SDHCI_REQ_USE_DMA;
734
Pierre Ossman2134a922008-06-28 18:28:51 +0200735 /*
736 * FIXME: This doesn't account for merging when mapping the
737 * scatterlist.
738 */
739 if (host->flags & SDHCI_REQ_USE_DMA) {
740 int broken, i;
741 struct scatterlist *sg;
742
743 broken = 0;
744 if (host->flags & SDHCI_USE_ADMA) {
745 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
746 broken = 1;
747 } else {
748 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
749 broken = 1;
750 }
751
752 if (unlikely(broken)) {
753 for_each_sg(data->sg, sg, data->sg_len, i) {
754 if (sg->length & 0x3) {
755 DBG("Reverting to PIO because of "
756 "transfer size (%d)\n",
757 sg->length);
758 host->flags &= ~SDHCI_REQ_USE_DMA;
759 break;
760 }
761 }
762 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100763 }
764
765 /*
766 * The assumption here being that alignment is the same after
767 * translation to device address space.
768 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200769 if (host->flags & SDHCI_REQ_USE_DMA) {
770 int broken, i;
771 struct scatterlist *sg;
772
773 broken = 0;
774 if (host->flags & SDHCI_USE_ADMA) {
775 /*
776 * As we use 3 byte chunks to work around
777 * alignment problems, we need to check this
778 * quirk.
779 */
780 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
781 broken = 1;
782 } else {
783 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
784 broken = 1;
785 }
786
787 if (unlikely(broken)) {
788 for_each_sg(data->sg, sg, data->sg_len, i) {
789 if (sg->offset & 0x3) {
790 DBG("Reverting to PIO because of "
791 "bad alignment\n");
792 host->flags &= ~SDHCI_REQ_USE_DMA;
793 break;
794 }
795 }
796 }
797 }
798
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200799 if (host->flags & SDHCI_REQ_USE_DMA) {
800 if (host->flags & SDHCI_USE_ADMA) {
801 ret = sdhci_adma_table_pre(host, data);
802 if (ret) {
803 /*
804 * This only happens when someone fed
805 * us an invalid request.
806 */
807 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200808 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200809 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300810 sdhci_writel(host, host->adma_addr,
811 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200812 }
813 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300814 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200815
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300816 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200817 data->sg, data->sg_len,
818 (data->flags & MMC_DATA_READ) ?
819 DMA_FROM_DEVICE :
820 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300821 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200822 /*
823 * This only happens when someone fed
824 * us an invalid request.
825 */
826 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200827 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200828 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200829 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300830 sdhci_writel(host, sg_dma_address(data->sg),
831 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200832 }
833 }
834 }
835
Pierre Ossman2134a922008-06-28 18:28:51 +0200836 /*
837 * Always adjust the DMA selection as some controllers
838 * (e.g. JMicron) can't do PIO properly when the selection
839 * is ADMA.
840 */
841 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300842 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200843 ctrl &= ~SDHCI_CTRL_DMA_MASK;
844 if ((host->flags & SDHCI_REQ_USE_DMA) &&
845 (host->flags & SDHCI_USE_ADMA))
846 ctrl |= SDHCI_CTRL_ADMA32;
847 else
848 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300849 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100850 }
851
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200852 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200853 int flags;
854
855 flags = SG_MITER_ATOMIC;
856 if (host->data->flags & MMC_DATA_READ)
857 flags |= SG_MITER_TO_SG;
858 else
859 flags |= SG_MITER_FROM_SG;
860 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200861 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800862 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700863
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300864 sdhci_set_transfer_irqs(host);
865
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400866 /* Set the DMA boundary value and block size */
867 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
868 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300869 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700870}
871
872static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500873 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700874{
875 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500876 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700877
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700878 if (data == NULL)
879 return;
880
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200881 WARN_ON(!host->data);
882
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700883 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500884 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
885 mode |= SDHCI_TRNS_MULTI;
886 /*
887 * If we are sending CMD23, CMD12 never gets sent
888 * on successful completion (so no Auto-CMD12).
889 */
890 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
891 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500892 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
893 mode |= SDHCI_TRNS_AUTO_CMD23;
894 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
895 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700896 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500897
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530898 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700899 mode |= SDHCI_TRNS_READ;
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530900 if (host->ops->toggle_cdr)
901 host->ops->toggle_cdr(host, true);
902 }
903 if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE))
904 host->ops->toggle_cdr(host, false);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100905 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700906 mode |= SDHCI_TRNS_DMA;
907
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300908 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800909}
910
911static void sdhci_finish_data(struct sdhci_host *host)
912{
913 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800914
915 BUG_ON(!host->data);
916
917 data = host->data;
918 host->data = NULL;
919
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100920 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200921 if (host->flags & SDHCI_USE_ADMA)
922 sdhci_adma_table_post(host, data);
923 else {
924 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
925 data->sg_len, (data->flags & MMC_DATA_READ) ?
926 DMA_FROM_DEVICE : DMA_TO_DEVICE);
927 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800928 }
929
930 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200931 * The specification states that the block count register must
932 * be updated, but it does not specify at what point in the
933 * data flow. That makes the register entirely useless to read
934 * back so we have to assume that nothing made it to the card
935 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800936 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200937 if (data->error)
938 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800939 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200940 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800941
Andrei Warkentine89d4562011-05-23 15:06:37 -0500942 /*
943 * Need to send CMD12 if -
944 * a) open-ended multiblock transfer (no CMD23)
945 * b) error in multiblock transfer
946 */
947 if (data->stop &&
948 (data->error ||
949 !host->mrq->sbc)) {
950
Pierre Ossmand129bce2006-03-24 03:18:17 -0800951 /*
952 * The controller needs a reset of internal state machines
953 * upon error conditions.
954 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200955 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800956 sdhci_reset(host, SDHCI_RESET_CMD);
957 sdhci_reset(host, SDHCI_RESET_DATA);
958 }
959
960 sdhci_send_command(host, data->stop);
961 } else
962 tasklet_schedule(&host->finish_tasklet);
963}
964
965static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
966{
967 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700968 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700969 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800970
971 WARN_ON(host->cmd);
972
Pierre Ossmand129bce2006-03-24 03:18:17 -0800973 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700974 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700975
976 mask = SDHCI_CMD_INHIBIT;
977 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
978 mask |= SDHCI_DATA_INHIBIT;
979
980 /* We shouldn't wait for data inihibit for stop commands, even
981 though they might use busy signaling */
982 if (host->mrq->data && (cmd == host->mrq->data->stop))
983 mask &= ~SDHCI_DATA_INHIBIT;
984
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300985 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700986 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +0530987 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100988 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800989 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200990 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800991 tasklet_schedule(&host->finish_tasklet);
992 return;
993 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700994 timeout--;
995 mdelay(1);
996 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997
998 mod_timer(&host->timer, jiffies + 10 * HZ);
999
1000 host->cmd = cmd;
1001
Andrei Warkentina3c77782011-04-11 16:13:42 -05001002 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001003
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001004 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001005
Andrei Warkentine89d4562011-05-23 15:06:37 -05001006 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001007
Pierre Ossmand129bce2006-03-24 03:18:17 -08001008 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301009 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001010 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001011 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012 tasklet_schedule(&host->finish_tasklet);
1013 return;
1014 }
1015
1016 if (!(cmd->flags & MMC_RSP_PRESENT))
1017 flags = SDHCI_CMD_RESP_NONE;
1018 else if (cmd->flags & MMC_RSP_136)
1019 flags = SDHCI_CMD_RESP_LONG;
1020 else if (cmd->flags & MMC_RSP_BUSY)
1021 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1022 else
1023 flags = SDHCI_CMD_RESP_SHORT;
1024
1025 if (cmd->flags & MMC_RSP_CRC)
1026 flags |= SDHCI_CMD_CRC;
1027 if (cmd->flags & MMC_RSP_OPCODE)
1028 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301029
1030 /* CMD19 is special in that the Data Present Select should be set */
Girish K S2cd06dc2012-01-06 09:56:39 +05301031 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1032 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033 flags |= SDHCI_CMD_DATA;
1034
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001035 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001036}
1037
1038static void sdhci_finish_command(struct sdhci_host *host)
1039{
1040 int i;
1041
1042 BUG_ON(host->cmd == NULL);
1043
1044 if (host->cmd->flags & MMC_RSP_PRESENT) {
1045 if (host->cmd->flags & MMC_RSP_136) {
1046 /* CRC is stripped so we need to do some shifting. */
1047 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001048 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001049 SDHCI_RESPONSE + (3-i)*4) << 8;
1050 if (i != 3)
1051 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001052 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001053 SDHCI_RESPONSE + (3-i)*4-1);
1054 }
1055 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001056 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057 }
1058 }
1059
Pierre Ossman17b04292007-07-22 22:18:46 +02001060 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001061
Andrei Warkentine89d4562011-05-23 15:06:37 -05001062 /* Finished CMD23, now send actual command. */
1063 if (host->cmd == host->mrq->sbc) {
1064 host->cmd = NULL;
1065 sdhci_send_command(host, host->mrq->cmd);
1066 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001067
Andrei Warkentine89d4562011-05-23 15:06:37 -05001068 /* Processed actual command. */
1069 if (host->data && host->data_early)
1070 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001071
Andrei Warkentine89d4562011-05-23 15:06:37 -05001072 if (!host->cmd->data)
1073 tasklet_schedule(&host->finish_tasklet);
1074
1075 host->cmd = NULL;
1076 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001077}
1078
1079static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1080{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301081 int div = 0; /* Initialized for compiler warning */
Sahitya Tummalaca422112013-02-22 12:15:54 +05301082 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301083 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001084 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001085
Todd Poynor30832ab2011-12-27 15:48:46 +02001086 if (clock && clock == host->clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001087 return;
1088
Sahitya Tummalaca422112013-02-22 12:15:54 +05301089 host->mmc->actual_clock = 0;
1090
Anton Vorontsov81146342009-03-17 00:13:59 +03001091 if (host->ops->set_clock) {
1092 host->ops->set_clock(host, clock);
1093 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1094 return;
1095 }
1096
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001097 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001098
1099 if (clock == 0)
1100 goto out;
1101
Zhangfei Gao85105c52010-08-06 07:10:01 +08001102 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301103 /*
1104 * Check if the Host Controller supports Programmable Clock
1105 * Mode.
1106 */
1107 if (host->clk_mul) {
1108 u16 ctrl;
1109
1110 /*
1111 * We need to figure out whether the Host Driver needs
1112 * to select Programmable Clock Mode, or the value can
1113 * be set automatically by the Host Controller based on
1114 * the Preset Value registers.
1115 */
1116 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1117 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1118 for (div = 1; div <= 1024; div++) {
1119 if (((host->max_clk * host->clk_mul) /
1120 div) <= clock)
1121 break;
1122 }
1123 /*
1124 * Set Programmable Clock Mode in the Clock
1125 * Control register.
1126 */
1127 clk = SDHCI_PROG_CLOCK_MODE;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301128 real_div = div;
1129 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301130 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001131 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301132 } else {
1133 /* Version 3.00 divisors must be a multiple of 2. */
1134 if (host->max_clk <= clock)
1135 div = 1;
1136 else {
1137 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1138 div += 2) {
1139 if ((host->max_clk / div) <= clock)
1140 break;
1141 }
1142 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301143 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301144 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001145 }
1146 } else {
1147 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001148 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001149 if ((host->max_clk / div) <= clock)
1150 break;
1151 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301152 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301153 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001154 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001155
Sahitya Tummalaca422112013-02-22 12:15:54 +05301156 if (real_div)
1157 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1158
Arindam Nathc3ed3872011-05-05 12:19:06 +05301159 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001160 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1161 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001162 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001163 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001164
Chris Ball27f6cb12009-09-22 16:45:31 -07001165 /* Wait max 20 ms */
1166 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001167 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001168 & SDHCI_CLOCK_INT_STABLE)) {
1169 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301170 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001171 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001172 sdhci_dumpregs(host);
1173 return;
1174 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001175 timeout--;
1176 mdelay(1);
1177 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001178
1179 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001180 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001181
1182out:
1183 host->clock = clock;
1184}
1185
Sahitya Tummalaca422112013-02-22 12:15:54 +05301186static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001187{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001188 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001189
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001190 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001191 switch (1 << power) {
1192 case MMC_VDD_165_195:
1193 pwr = SDHCI_POWER_180;
1194 break;
1195 case MMC_VDD_29_30:
1196 case MMC_VDD_30_31:
1197 pwr = SDHCI_POWER_300;
1198 break;
1199 case MMC_VDD_32_33:
1200 case MMC_VDD_33_34:
1201 pwr = SDHCI_POWER_330;
1202 break;
1203 default:
1204 BUG();
1205 }
1206 }
1207
1208 if (host->pwr == pwr)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301209 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001210
Pierre Ossmanae628902009-05-03 20:45:03 +02001211 host->pwr = pwr;
1212
1213 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001214 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301215 if (host->ops->check_power_status)
1216 host->ops->check_power_status(host);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301217 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001218 }
1219
1220 /*
1221 * Spec says that we should clear the power reg before setting
1222 * a new value. Some controllers don't seem to like this though.
1223 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301224 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001225 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301226 if (host->ops->check_power_status)
1227 host->ops->check_power_status(host);
1228 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001229
Andres Salomone08c1692008-07-04 10:00:03 -07001230 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001231 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001232 * and set turn on power at the same time, so set the voltage first.
1233 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301234 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001235 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301236 if (host->ops->check_power_status)
1237 host->ops->check_power_status(host);
1238 }
Pierre Ossmanae628902009-05-03 20:45:03 +02001239
1240 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001241
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001242 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301243 if (host->ops->check_power_status)
1244 host->ops->check_power_status(host);
Harald Welte557b0692009-06-18 16:53:38 +02001245
1246 /*
1247 * Some controllers need an extra 10ms delay of 10ms before they
1248 * can apply clock after applying power
1249 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001250 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001251 mdelay(10);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301252
1253 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001254}
1255
Pierre Ossmand129bce2006-03-24 03:18:17 -08001256/*****************************************************************************\
1257 * *
1258 * MMC callbacks *
1259 * *
1260\*****************************************************************************/
1261
1262static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1263{
1264 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001265 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001266 unsigned long flags;
1267
1268 host = mmc_priv(mmc);
1269
Adrian Hunter50accb92011-10-03 15:33:34 +03001270 sdhci_runtime_pm_get(host);
1271
Pierre Ossmand129bce2006-03-24 03:18:17 -08001272 spin_lock_irqsave(&host->lock, flags);
1273
1274 WARN_ON(host->mrq != NULL);
1275
Pierre Ossmanf9134312008-12-21 17:01:48 +01001276#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001277 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001278#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001279
1280 /*
1281 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1282 * requests if Auto-CMD12 is enabled.
1283 */
1284 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001285 if (mrq->stop) {
1286 mrq->data->stop = NULL;
1287 mrq->stop = NULL;
1288 }
1289 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001290
1291 host->mrq = mrq;
1292
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001293 /* If polling, assume that the card is always present. */
1294 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1295 present = true;
1296 else
1297 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1298 SDHCI_CARD_PRESENT;
1299
1300 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001301 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001302 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301303 } else {
1304 u32 present_state;
1305
1306 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1307 /*
1308 * Check if the re-tuning timer has already expired and there
1309 * is no on-going data transfer. If so, we need to execute
1310 * tuning procedure before sending command.
1311 */
1312 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1313 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1314 spin_unlock_irqrestore(&host->lock, flags);
Girish K S2cd06dc2012-01-06 09:56:39 +05301315 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301316 spin_lock_irqsave(&host->lock, flags);
1317
1318 /* Restore original mmc_request structure */
1319 host->mrq = mrq;
1320 }
1321
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001322 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001323 sdhci_send_command(host, mrq->sbc);
1324 else
1325 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301326 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001327
Pierre Ossman5f25a662006-10-04 02:15:39 -07001328 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001329 spin_unlock_irqrestore(&host->lock, flags);
1330}
1331
Adrian Hunter50accb92011-10-03 15:33:34 +03001332static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001333{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001334 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301335 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001336 u8 ctrl;
1337
Sahitya Tummalaca422112013-02-22 12:15:54 +05301338 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301339 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1340 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1341 return;
1342 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001343
Pierre Ossmand129bce2006-03-24 03:18:17 -08001344 /*
1345 * Reset the chip on each power off.
1346 * Should clear out any weird states.
1347 */
1348 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001349 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001350 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001351 }
1352
1353 sdhci_set_clock(host, ios->clock);
1354
1355 if (ios->power_mode == MMC_POWER_OFF)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301356 vdd_bit = sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001357 else
Sahitya Tummalaca422112013-02-22 12:15:54 +05301358 vdd_bit = sdhci_set_power(host, ios->vdd);
1359
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301360 if (host->vmmc && vdd_bit != -1)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301361 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301362
1363 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001364
Philip Rakity643a81f2010-09-23 08:24:32 -07001365 if (host->ops->platform_send_init_74_clocks)
1366 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1367
Philip Rakity15ec4462010-11-19 16:48:39 -05001368 /*
1369 * If your platform has 8-bit width support but is not a v3 controller,
1370 * or if it requires special setup code, you should implement that in
1371 * platform_8bit_width().
1372 */
1373 if (host->ops->platform_8bit_width)
1374 host->ops->platform_8bit_width(host, ios->bus_width);
1375 else {
1376 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1377 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1378 ctrl &= ~SDHCI_CTRL_4BITBUS;
1379 if (host->version >= SDHCI_SPEC_300)
1380 ctrl |= SDHCI_CTRL_8BITBUS;
1381 } else {
1382 if (host->version >= SDHCI_SPEC_300)
1383 ctrl &= ~SDHCI_CTRL_8BITBUS;
1384 if (ios->bus_width == MMC_BUS_WIDTH_4)
1385 ctrl |= SDHCI_CTRL_4BITBUS;
1386 else
1387 ctrl &= ~SDHCI_CTRL_4BITBUS;
1388 }
1389 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1390 }
1391
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001392 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001393
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001394 if ((ios->timing == MMC_TIMING_SD_HS ||
1395 ios->timing == MMC_TIMING_MMC_HS)
1396 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001397 ctrl |= SDHCI_CTRL_HISPD;
1398 else
1399 ctrl &= ~SDHCI_CTRL_HISPD;
1400
Arindam Nathd6d50a12011-05-05 12:18:59 +05301401 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301402 u16 clk, ctrl_2;
1403 unsigned int clock;
1404
1405 /* In case of UHS-I modes, set High Speed Enable */
Girish K S2cd06dc2012-01-06 09:56:39 +05301406 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1407 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301408 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1409 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001410 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301411 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301412
1413 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1414 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301415 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301416 /*
1417 * We only need to set Driver Strength if the
1418 * preset value enable is not set.
1419 */
1420 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1421 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1422 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1423 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1424 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1425
1426 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301427 } else {
1428 /*
1429 * According to SDHC Spec v3.00, if the Preset Value
1430 * Enable in the Host Control 2 register is set, we
1431 * need to reset SD Clock Enable before changing High
1432 * Speed Enable to avoid generating clock gliches.
1433 */
Arindam Nath758535c2011-05-05 12:19:00 +05301434
1435 /* Reset SD Clock Enable */
1436 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1437 clk &= ~SDHCI_CLOCK_CARD_EN;
1438 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1439
1440 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1441
1442 /* Re-enable SD Clock */
1443 clock = host->clock;
1444 host->clock = 0;
1445 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301446 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301447
Arindam Nath49c468f2011-05-05 12:19:01 +05301448
1449 /* Reset SD Clock Enable */
1450 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1451 clk &= ~SDHCI_CLOCK_CARD_EN;
1452 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1453
Philip Rakity6322cdd2011-05-13 11:17:15 +05301454 if (host->ops->set_uhs_signaling)
1455 host->ops->set_uhs_signaling(host, ios->timing);
1456 else {
1457 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1458 /* Select Bus Speed Mode for host */
1459 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S2cd06dc2012-01-06 09:56:39 +05301460 if (ios->timing == MMC_TIMING_MMC_HS200)
1461 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1462 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301463 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1464 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1465 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1466 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1467 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1468 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1469 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1470 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1471 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1472 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1473 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301474
1475 /* Re-enable SD Clock */
1476 clock = host->clock;
1477 host->clock = 0;
1478 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301479 } else
1480 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301481
Leandro Dorileob8352262007-07-25 23:47:04 +02001482 /*
1483 * Some (ENE) controllers go apeshit on some ios operation,
1484 * signalling timeout and CRC errors even on CMD0. Resetting
1485 * it on each ios seems to solve the problem.
1486 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001487 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001488 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1489
Pierre Ossman5f25a662006-10-04 02:15:39 -07001490 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001491 spin_unlock_irqrestore(&host->lock, flags);
1492}
1493
Adrian Hunter50accb92011-10-03 15:33:34 +03001494static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1495{
1496 struct sdhci_host *host = mmc_priv(mmc);
1497
1498 sdhci_runtime_pm_get(host);
1499 sdhci_do_set_ios(host, ios);
1500 sdhci_runtime_pm_put(host);
1501}
1502
1503static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001504{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001505 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001506 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001507
Pierre Ossmand129bce2006-03-24 03:18:17 -08001508 spin_lock_irqsave(&host->lock, flags);
1509
Pierre Ossman1e728592008-04-16 19:13:13 +02001510 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001511 is_readonly = 0;
1512 else if (host->ops->get_ro)
1513 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001514 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001515 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1516 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001517
1518 spin_unlock_irqrestore(&host->lock, flags);
1519
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001520 /* This quirk needs to be replaced by a callback-function later */
1521 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1522 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001523}
1524
Takashi Iwai82b0e232011-04-21 20:26:38 +02001525#define SAMPLE_COUNT 5
1526
Adrian Hunter50accb92011-10-03 15:33:34 +03001527static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001528{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001529 int i, ro_count;
1530
Takashi Iwai82b0e232011-04-21 20:26:38 +02001531 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001532 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001533
1534 ro_count = 0;
1535 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001536 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001537 if (++ro_count > SAMPLE_COUNT / 2)
1538 return 1;
1539 }
1540 msleep(30);
1541 }
1542 return 0;
1543}
1544
Adrian Hunter50accb92011-10-03 15:33:34 +03001545static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001546{
Adrian Hunter50accb92011-10-03 15:33:34 +03001547 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001548
Adrian Hunter50accb92011-10-03 15:33:34 +03001549 if (host->ops && host->ops->hw_reset)
1550 host->ops->hw_reset(host);
1551}
Adrian Hunter20758b62011-08-29 16:42:12 +03001552
Adrian Hunter50accb92011-10-03 15:33:34 +03001553static int sdhci_get_ro(struct mmc_host *mmc)
1554{
1555 struct sdhci_host *host = mmc_priv(mmc);
1556 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001557
Adrian Hunter50accb92011-10-03 15:33:34 +03001558 sdhci_runtime_pm_get(host);
1559 ret = sdhci_do_get_ro(host);
1560 sdhci_runtime_pm_put(host);
1561 return ret;
1562}
1563
1564static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1565{
Pierre Ossman1e728592008-04-16 19:13:13 +02001566 if (host->flags & SDHCI_DEVICE_DEAD)
1567 goto out;
1568
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001569 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001570 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1571 else
1572 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1573
1574 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1575 if (host->runtime_suspended)
1576 goto out;
1577
1578 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001579 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1580 else
1581 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001582out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001583 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001584}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001585
Adrian Hunter50accb92011-10-03 15:33:34 +03001586static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1587{
1588 struct sdhci_host *host = mmc_priv(mmc);
1589 unsigned long flags;
1590
1591 spin_lock_irqsave(&host->lock, flags);
1592 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001593 spin_unlock_irqrestore(&host->lock, flags);
1594}
1595
Adrian Hunter50accb92011-10-03 15:33:34 +03001596static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1597 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301598{
Arindam Nathf2119df2011-05-05 12:18:57 +05301599 u8 pwr;
1600 u16 clk, ctrl;
1601 u32 present_state;
1602
Arindam Nathf2119df2011-05-05 12:18:57 +05301603 /*
1604 * Signal Voltage Switching is only applicable for Host Controllers
1605 * v3.00 and above.
1606 */
1607 if (host->version < SDHCI_SPEC_300)
1608 return 0;
1609
1610 /*
1611 * We first check whether the request is to set signalling voltage
1612 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1613 */
1614 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1615 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1616 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1617 ctrl &= ~SDHCI_CTRL_VDD_180;
1618 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301619 if (host->ops->check_power_status)
1620 host->ops->check_power_status(host);
Arindam Nathf2119df2011-05-05 12:18:57 +05301621
1622 /* Wait for 5ms */
1623 usleep_range(5000, 5500);
1624
1625 /* 3.3V regulator output should be stable within 5 ms */
1626 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1627 if (!(ctrl & SDHCI_CTRL_VDD_180))
1628 return 0;
1629 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301630 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301631 "signalling voltage failed\n");
1632 return -EIO;
1633 }
1634 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1635 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1636 /* Stop SDCLK */
1637 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1638 clk &= ~SDHCI_CLOCK_CARD_EN;
1639 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1640
1641 /* Check whether DAT[3:0] is 0000 */
1642 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1643 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1644 SDHCI_DATA_LVL_SHIFT)) {
1645 /*
1646 * Enable 1.8V Signal Enable in the Host Control2
1647 * register
1648 */
1649 ctrl |= SDHCI_CTRL_VDD_180;
1650 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301651 if (host->ops->check_power_status)
1652 host->ops->check_power_status(host);
Arindam Nathf2119df2011-05-05 12:18:57 +05301653
1654 /* Wait for 5ms */
1655 usleep_range(5000, 5500);
1656
1657 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1658 if (ctrl & SDHCI_CTRL_VDD_180) {
1659 /* Provide SDCLK again and wait for 1ms*/
1660 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1661 clk |= SDHCI_CLOCK_CARD_EN;
1662 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1663 usleep_range(1000, 1500);
1664
1665 /*
1666 * If DAT[3:0] level is 1111b, then the card
1667 * was successfully switched to 1.8V signaling.
1668 */
1669 present_state = sdhci_readl(host,
1670 SDHCI_PRESENT_STATE);
1671 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1672 SDHCI_DATA_LVL_MASK)
1673 return 0;
1674 }
1675 }
1676
1677 /*
1678 * If we are here, that means the switch to 1.8V signaling
1679 * failed. We power cycle the card, and retry initialization
1680 * sequence by setting S18R to 0.
1681 */
1682 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1683 pwr &= ~SDHCI_POWER_ON;
1684 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301685 if (host->ops->check_power_status)
1686 host->ops->check_power_status(host);
Arindam Nathf2119df2011-05-05 12:18:57 +05301687
1688 /* Wait for 1ms as per the spec */
1689 usleep_range(1000, 1500);
1690 pwr |= SDHCI_POWER_ON;
1691 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301692 if (host->ops->check_power_status)
1693 host->ops->check_power_status(host);
Arindam Nathf2119df2011-05-05 12:18:57 +05301694
Sahitya Tummalaca422112013-02-22 12:15:54 +05301695 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301696 "voltage failed, retrying with S18R set to 0\n");
1697 return -EAGAIN;
1698 } else
1699 /* No signal voltage switch required */
1700 return 0;
1701}
1702
Adrian Hunter50accb92011-10-03 15:33:34 +03001703static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1704 struct mmc_ios *ios)
1705{
1706 struct sdhci_host *host = mmc_priv(mmc);
1707 int err;
1708
1709 if (host->version < SDHCI_SPEC_300)
1710 return 0;
1711 sdhci_runtime_pm_get(host);
1712 err = sdhci_do_start_signal_voltage_switch(host, ios);
1713 sdhci_runtime_pm_put(host);
1714 return err;
1715}
1716
Girish K S2cd06dc2012-01-06 09:56:39 +05301717static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301718{
1719 struct sdhci_host *host;
1720 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05301721 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05301722 int tuning_loop_counter = MAX_TUNING_LOOP;
1723 unsigned long timeout;
1724 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05301725 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301726
1727 host = mmc_priv(mmc);
1728
Adrian Hunter50accb92011-10-03 15:33:34 +03001729 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301730 disable_irq(host->irq);
1731 spin_lock(&host->lock);
1732
1733 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1734
1735 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05301736 * The Host Controller needs tuning only in case of SDR104 mode
1737 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301738 * Capabilities register.
Girish K S2cd06dc2012-01-06 09:56:39 +05301739 * If the Host Controller supports the HS200 mode then the
1740 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301741 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08001742 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1743 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
1744 (host->flags & SDHCI_HS200_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05301745 requires_tuning_nonuhs = true;
1746
Arindam Nathb513ea22011-05-05 12:19:04 +05301747 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301748 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301749 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1750 else {
1751 spin_unlock(&host->lock);
1752 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03001753 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301754 return 0;
1755 }
1756
Asutosh Das8ddd3482013-01-04 11:45:46 +05301757 if (host->ops->execute_tuning) {
1758 spin_unlock(&host->lock);
1759 enable_irq(host->irq);
1760 host->ops->execute_tuning(host, opcode);
1761 disable_irq(host->irq);
1762 spin_lock(&host->lock);
1763 goto out;
1764 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301765 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1766
1767 /*
1768 * As per the Host Controller spec v3.00, tuning command
1769 * generates Buffer Read Ready interrupt, so enable that.
1770 *
1771 * Note: The spec clearly says that when tuning sequence
1772 * is being performed, the controller does not generate
1773 * interrupts other than Buffer Read Ready interrupt. But
1774 * to make sure we don't hit a controller bug, we _only_
1775 * enable Buffer Read Ready interrupt here.
1776 */
1777 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1778 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1779
1780 /*
1781 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1782 * of loops reaches 40 times or a timeout of 150ms occurs.
1783 */
1784 timeout = 150;
1785 do {
1786 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03001787 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301788
1789 if (!tuning_loop_counter && !timeout)
1790 break;
1791
Girish K S2cd06dc2012-01-06 09:56:39 +05301792 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301793 cmd.arg = 0;
1794 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1795 cmd.retries = 0;
1796 cmd.data = NULL;
1797 cmd.error = 0;
1798
1799 mrq.cmd = &cmd;
1800 host->mrq = &mrq;
1801
1802 /*
1803 * In response to CMD19, the card sends 64 bytes of tuning
1804 * block to the Host Controller. So we set the block size
1805 * to 64 here.
1806 */
Girish K S2cd06dc2012-01-06 09:56:39 +05301807 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1808 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1809 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1810 SDHCI_BLOCK_SIZE);
1811 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1812 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1813 SDHCI_BLOCK_SIZE);
1814 } else {
1815 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1816 SDHCI_BLOCK_SIZE);
1817 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301818
1819 /*
1820 * The tuning block is sent by the card to the host controller.
1821 * So we set the TRNS_READ bit in the Transfer Mode register.
1822 * This also takes care of setting DMA Enable and Multi Block
1823 * Select in the same register to 0.
1824 */
1825 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1826
1827 sdhci_send_command(host, &cmd);
1828
1829 host->cmd = NULL;
1830 host->mrq = NULL;
1831
1832 spin_unlock(&host->lock);
1833 enable_irq(host->irq);
1834
1835 /* Wait for Buffer Read Ready interrupt */
1836 wait_event_interruptible_timeout(host->buf_ready_int,
1837 (host->tuning_done == 1),
1838 msecs_to_jiffies(50));
1839 disable_irq(host->irq);
1840 spin_lock(&host->lock);
1841
1842 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301843 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301844 "Buffer Read Ready interrupt during tuning "
1845 "procedure, falling back to fixed sampling "
1846 "clock\n");
1847 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1848 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1849 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1850 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1851
1852 err = -EIO;
1853 goto out;
1854 }
1855
1856 host->tuning_done = 0;
1857
1858 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1859 tuning_loop_counter--;
1860 timeout--;
1861 mdelay(1);
1862 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1863
1864 /*
1865 * The Host Driver has exhausted the maximum number of loops allowed,
1866 * so use fixed sampling frequency.
1867 */
1868 if (!tuning_loop_counter || !timeout) {
1869 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1870 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1871 } else {
1872 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301873 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301874 " failed, falling back to fixed sampling"
1875 " clock\n");
1876 err = -EIO;
1877 }
1878 }
1879
1880out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301881 /*
1882 * If this is the very first time we are here, we start the retuning
1883 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1884 * flag won't be set, we check this condition before actually starting
1885 * the timer.
1886 */
1887 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1888 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
1889 mod_timer(&host->tuning_timer, jiffies +
1890 host->tuning_count * HZ);
1891 /* Tuning mode 1 limits the maximum data length to 4MB */
1892 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1893 } else {
1894 host->flags &= ~SDHCI_NEEDS_RETUNING;
1895 /* Reload the new initial value for timer */
1896 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1897 mod_timer(&host->tuning_timer, jiffies +
1898 host->tuning_count * HZ);
1899 }
1900
1901 /*
1902 * In case tuning fails, host controllers which support re-tuning can
1903 * try tuning again at a later time, when the re-tuning timer expires.
1904 * So for these controllers, we return 0. Since there might be other
1905 * controllers who do not have this capability, we return error for
1906 * them.
1907 */
1908 if (err && host->tuning_count &&
1909 host->tuning_mode == SDHCI_TUNING_MODE_1)
1910 err = 0;
1911
Arindam Nathb513ea22011-05-05 12:19:04 +05301912 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1913 spin_unlock(&host->lock);
1914 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03001915 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301916
1917 return err;
1918}
1919
Adrian Hunter50accb92011-10-03 15:33:34 +03001920static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301921{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301922 u16 ctrl;
1923 unsigned long flags;
1924
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301925 /* Host Controller v3.00 defines preset value registers */
1926 if (host->version < SDHCI_SPEC_300)
1927 return;
1928
1929 spin_lock_irqsave(&host->lock, flags);
1930
1931 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1932
1933 /*
1934 * We only enable or disable Preset Value if they are not already
1935 * enabled or disabled respectively. Otherwise, we bail out.
1936 */
1937 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1938 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1939 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03001940 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301941 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1942 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1943 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03001944 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301945 }
1946
1947 spin_unlock_irqrestore(&host->lock, flags);
1948}
1949
Adrian Hunter50accb92011-10-03 15:33:34 +03001950static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1951{
1952 struct sdhci_host *host = mmc_priv(mmc);
1953
1954 sdhci_runtime_pm_get(host);
1955 sdhci_do_enable_preset_value(host, enable);
1956 sdhci_runtime_pm_put(host);
1957}
1958
David Brownellab7aefd2006-11-12 17:55:30 -08001959static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001960 .request = sdhci_request,
1961 .set_ios = sdhci_set_ios,
1962 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03001963 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001964 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05301965 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05301966 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301967 .enable_preset_value = sdhci_enable_preset_value,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001968};
1969
1970/*****************************************************************************\
1971 * *
1972 * Tasklets *
1973 * *
1974\*****************************************************************************/
1975
1976static void sdhci_tasklet_card(unsigned long param)
1977{
1978 struct sdhci_host *host;
1979 unsigned long flags;
1980
1981 host = (struct sdhci_host*)param;
1982
1983 spin_lock_irqsave(&host->lock, flags);
1984
Adrian Hunter50accb92011-10-03 15:33:34 +03001985 /* Check host->mrq first in case we are runtime suspended */
1986 if (host->mrq &&
1987 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301988 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03001989 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05301990 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03001991 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001992
Adrian Hunter50accb92011-10-03 15:33:34 +03001993 sdhci_reset(host, SDHCI_RESET_CMD);
1994 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001995
Adrian Hunter50accb92011-10-03 15:33:34 +03001996 host->mrq->cmd->error = -ENOMEDIUM;
1997 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001998 }
1999
2000 spin_unlock_irqrestore(&host->lock, flags);
2001
Pierre Ossman04cf5852008-08-18 22:18:14 +02002002 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002003}
2004
2005static void sdhci_tasklet_finish(unsigned long param)
2006{
2007 struct sdhci_host *host;
2008 unsigned long flags;
2009 struct mmc_request *mrq;
2010
2011 host = (struct sdhci_host*)param;
2012
Adrian Hunter50accb92011-10-03 15:33:34 +03002013 spin_lock_irqsave(&host->lock, flags);
2014
Chris Ball0c9c99a2011-04-27 17:35:31 -04002015 /*
2016 * If this tasklet gets rescheduled while running, it will
2017 * be run again afterwards but without any active request.
2018 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002019 if (!host->mrq) {
2020 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002021 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002022 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002023
2024 del_timer(&host->timer);
2025
2026 mrq = host->mrq;
2027
Pierre Ossmand129bce2006-03-24 03:18:17 -08002028 /*
2029 * The controller needs a reset of internal state machines
2030 * upon error conditions.
2031 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002032 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002033 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002034 (mrq->data && (mrq->data->error ||
2035 (mrq->data->stop && mrq->data->stop->error))) ||
2036 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002037
2038 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002039 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002040 unsigned int clock;
2041
2042 /* This is to force an update */
2043 clock = host->clock;
2044 host->clock = 0;
2045 sdhci_set_clock(host, clock);
2046 }
2047
2048 /* Spec says we should do both at the same time, but Ricoh
2049 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002050 sdhci_reset(host, SDHCI_RESET_CMD);
2051 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002052 } else {
2053 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2054 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002055 }
2056
2057 host->mrq = NULL;
2058 host->cmd = NULL;
2059 host->data = NULL;
2060
Pierre Ossmanf9134312008-12-21 17:01:48 +01002061#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002062 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002063#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002064
Pierre Ossman5f25a662006-10-04 02:15:39 -07002065 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002066 spin_unlock_irqrestore(&host->lock, flags);
2067
2068 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002069 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002070}
2071
2072static void sdhci_timeout_timer(unsigned long data)
2073{
2074 struct sdhci_host *host;
2075 unsigned long flags;
2076
2077 host = (struct sdhci_host*)data;
2078
2079 spin_lock_irqsave(&host->lock, flags);
2080
2081 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302082 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002083 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002084 sdhci_dumpregs(host);
2085
2086 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002087 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002088 sdhci_finish_data(host);
2089 } else {
2090 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002091 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002092 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002093 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002094
2095 tasklet_schedule(&host->finish_tasklet);
2096 }
2097 }
2098
Pierre Ossman5f25a662006-10-04 02:15:39 -07002099 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002100 spin_unlock_irqrestore(&host->lock, flags);
2101}
2102
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302103static void sdhci_tuning_timer(unsigned long data)
2104{
2105 struct sdhci_host *host;
2106 unsigned long flags;
2107
2108 host = (struct sdhci_host *)data;
2109
2110 spin_lock_irqsave(&host->lock, flags);
2111
2112 host->flags |= SDHCI_NEEDS_RETUNING;
2113
2114 spin_unlock_irqrestore(&host->lock, flags);
2115}
2116
Pierre Ossmand129bce2006-03-24 03:18:17 -08002117/*****************************************************************************\
2118 * *
2119 * Interrupt handling *
2120 * *
2121\*****************************************************************************/
2122
2123static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2124{
2125 BUG_ON(intmask == 0);
2126
2127 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302128 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002129 "though no command operation was in progress.\n",
2130 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002131 sdhci_dumpregs(host);
2132 return;
2133 }
2134
Pierre Ossman43b58b32007-07-25 23:15:27 +02002135 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002136 host->cmd->error = -ETIMEDOUT;
2137 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2138 SDHCI_INT_INDEX))
2139 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002140
Pierre Ossmane8095172008-07-25 01:09:08 +02002141 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002142 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002143 return;
2144 }
2145
2146 /*
2147 * The host can send and interrupt when the busy state has
2148 * ended, allowing us to wait without wasting CPU cycles.
2149 * Unfortunately this is overloaded on the "data complete"
2150 * interrupt, so we need to take some care when handling
2151 * it.
2152 *
2153 * Note: The 1.0 specification is a bit ambiguous about this
2154 * feature so there might be some problems with older
2155 * controllers.
2156 */
2157 if (host->cmd->flags & MMC_RSP_BUSY) {
2158 if (host->cmd->data)
2159 DBG("Cannot wait for busy signal when also "
2160 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002161 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002162 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002163
2164 /* The controller does not support the end-of-busy IRQ,
2165 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002166 }
2167
2168 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002169 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002170}
2171
George G. Davis0957c332010-02-18 12:32:12 -05002172#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002173static void sdhci_show_adma_error(struct sdhci_host *host)
2174{
2175 const char *name = mmc_hostname(host->mmc);
2176 u8 *desc = host->adma_desc;
2177 __le32 *dma;
2178 __le16 *len;
2179 u8 attr;
2180
2181 sdhci_dumpregs(host);
2182
2183 while (true) {
2184 dma = (__le32 *)(desc + 4);
2185 len = (__le16 *)(desc + 2);
2186 attr = *desc;
2187
2188 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2189 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2190
2191 desc += 8;
2192
2193 if (attr & 2)
2194 break;
2195 }
2196}
2197#else
2198static void sdhci_show_adma_error(struct sdhci_host *host) { }
2199#endif
2200
Pierre Ossmand129bce2006-03-24 03:18:17 -08002201static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2202{
Girish K S2cd06dc2012-01-06 09:56:39 +05302203 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002204 BUG_ON(intmask == 0);
2205
Arindam Nathb513ea22011-05-05 12:19:04 +05302206 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2207 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302208 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2209 if (command == MMC_SEND_TUNING_BLOCK ||
2210 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302211 host->tuning_done = 1;
2212 wake_up(&host->buf_ready_int);
2213 return;
2214 }
2215 }
2216
Pierre Ossmand129bce2006-03-24 03:18:17 -08002217 if (!host->data) {
2218 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002219 * The "data complete" interrupt is also used to
2220 * indicate that a busy state has ended. See comment
2221 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002222 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002223 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2224 if (intmask & SDHCI_INT_DATA_END) {
2225 sdhci_finish_command(host);
2226 return;
2227 }
2228 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002229
Sahitya Tummalaca422112013-02-22 12:15:54 +05302230 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002231 "though no data operation was in progress.\n",
2232 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002233 sdhci_dumpregs(host);
2234
2235 return;
2236 }
2237
2238 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002239 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002240 else if (intmask & SDHCI_INT_DATA_END_BIT)
2241 host->data->error = -EILSEQ;
2242 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2243 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2244 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002245 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002246 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302247 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002248 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002249 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002250 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002251
Pierre Ossman17b04292007-07-22 22:18:46 +02002252 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002253 sdhci_finish_data(host);
2254 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002255 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002256 sdhci_transfer_pio(host);
2257
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002258 /*
2259 * We currently don't do anything fancy with DMA
2260 * boundaries, but as we can't disable the feature
2261 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002262 *
2263 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2264 * should return a valid address to continue from, but as
2265 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002266 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002267 if (intmask & SDHCI_INT_DMA_END) {
2268 u32 dmastart, dmanow;
2269 dmastart = sg_dma_address(host->data->sg);
2270 dmanow = dmastart + host->data->bytes_xfered;
2271 /*
2272 * Force update to the next DMA block boundary.
2273 */
2274 dmanow = (dmanow &
2275 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2276 SDHCI_DEFAULT_BOUNDARY_SIZE;
2277 host->data->bytes_xfered = dmanow - dmastart;
2278 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2279 " next 0x%08x\n",
2280 mmc_hostname(host->mmc), dmastart,
2281 host->data->bytes_xfered, dmanow);
2282 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2283 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002284
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002285 if (intmask & SDHCI_INT_DATA_END) {
2286 if (host->cmd) {
2287 /*
2288 * Data managed to finish before the
2289 * command completed. Make sure we do
2290 * things in the proper order.
2291 */
2292 host->data_early = 1;
2293 } else {
2294 sdhci_finish_data(host);
2295 }
2296 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002297 }
2298}
2299
David Howells7d12e782006-10-05 14:55:46 +01002300static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002301{
2302 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002303 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002304 u32 intmask, unexpected = 0;
2305 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002306
2307 spin_lock(&host->lock);
2308
Adrian Hunter50accb92011-10-03 15:33:34 +03002309 if (host->runtime_suspended) {
2310 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302311 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002312 mmc_hostname(host->mmc));
2313 return IRQ_HANDLED;
2314 }
2315
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002316 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002317
Mark Lord62df67a2007-03-06 13:30:13 +01002318 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002319 result = IRQ_NONE;
2320 goto out;
2321 }
2322
Alexander Stein6379b232012-03-14 09:52:10 +01002323again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002324 DBG("*** %s got interrupt: 0x%08x\n",
2325 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002326
Pierre Ossman3192a282006-06-30 02:22:26 -07002327 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302328 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2329 SDHCI_CARD_PRESENT;
2330
2331 /*
2332 * There is a observation on i.mx esdhc. INSERT bit will be
2333 * immediately set again when it gets cleared, if a card is
2334 * inserted. We have to mask the irq to prevent interrupt
2335 * storm which will freeze the system. And the REMOVE gets
2336 * the same situation.
2337 *
2338 * More testing are needed here to ensure it works for other
2339 * platforms though.
2340 */
2341 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2342 SDHCI_INT_CARD_REMOVE);
2343 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2344 SDHCI_INT_CARD_INSERT);
2345
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002346 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302347 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2348 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002349 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002350 }
2351
Pierre Ossmand129bce2006-03-24 03:18:17 -08002352 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002353 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2354 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002355 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2356 (host->clock <= 400000))
2357 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002358 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002359 }
2360
2361 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002362 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2363 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002364 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2365 (host->clock <= 400000))
2366 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002367 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002368 }
2369
2370 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2371
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002372 intmask &= ~SDHCI_INT_ERROR;
2373
Pierre Ossmand129bce2006-03-24 03:18:17 -08002374 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302375 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002376 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002377 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002378 }
2379
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002380 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002381
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002382 if (intmask & SDHCI_INT_CARD_INT)
2383 cardint = 1;
2384
2385 intmask &= ~SDHCI_INT_CARD_INT;
2386
Pierre Ossman3192a282006-06-30 02:22:26 -07002387 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002388 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002389 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002390 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002391
2392 result = IRQ_HANDLED;
2393
Alexander Stein6379b232012-03-14 09:52:10 +01002394 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2395 if (intmask && --max_loops)
2396 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002397out:
2398 spin_unlock(&host->lock);
2399
Alexander Stein6379b232012-03-14 09:52:10 +01002400 if (unexpected) {
2401 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2402 mmc_hostname(host->mmc), unexpected);
2403 sdhci_dumpregs(host);
2404 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002405 /*
2406 * We have to delay this as it calls back into the driver.
2407 */
2408 if (cardint)
2409 mmc_signal_sdio_irq(host->mmc);
2410
Pierre Ossmand129bce2006-03-24 03:18:17 -08002411 return result;
2412}
2413
2414/*****************************************************************************\
2415 * *
2416 * Suspend/resume *
2417 * *
2418\*****************************************************************************/
2419
2420#ifdef CONFIG_PM
2421
Manuel Laussd72faa62011-11-03 11:09:45 +01002422int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002423{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002424 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302425 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002426
Chris Balla1b13b42012-02-06 00:43:59 -05002427 if (host->ops->platform_suspend)
2428 host->ops->platform_suspend(host);
2429
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002430 sdhci_disable_card_detection(host);
2431
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302432 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302433 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2434 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2435 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002436 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302437 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302438 }
2439
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002440 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302441 if (ret) {
2442 if (has_tuning_timer) {
2443 host->flags |= SDHCI_NEEDS_RETUNING;
2444 mod_timer(&host->tuning_timer, jiffies +
2445 host->tuning_count * HZ);
2446 }
2447
2448 sdhci_enable_card_detection(host);
2449
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002450 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302451 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002452
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002453 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002454
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002455 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002456}
2457
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002458EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002459
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002460int sdhci_resume_host(struct sdhci_host *host)
2461{
2462 int ret;
2463
Richard Röjforsa13abc72009-09-22 16:45:30 -07002464 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002465 if (host->ops->enable_dma)
2466 host->ops->enable_dma(host);
2467 }
2468
2469 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2470 mmc_hostname(host->mmc), host);
2471 if (ret)
2472 return ret;
2473
Adrian Hunter6308d292012-02-07 14:48:54 +02002474 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2475 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2476 /* Card keeps power but host controller does not */
2477 sdhci_init(host, 0);
2478 host->pwr = 0;
2479 host->clock = 0;
2480 sdhci_do_set_ios(host, &host->mmc->ios);
2481 } else {
2482 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2483 mmiowb();
2484 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002485
2486 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002487 sdhci_enable_card_detection(host);
2488
Chris Balla1b13b42012-02-06 00:43:59 -05002489 if (host->ops->platform_resume)
2490 host->ops->platform_resume(host);
2491
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302492 /* Set the re-tuning expiration flag */
2493 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2494 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2495 host->flags |= SDHCI_NEEDS_RETUNING;
2496
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002497 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002498}
2499
2500EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002501
Daniel Drake5f619702010-11-04 22:20:39 +00002502void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2503{
2504 u8 val;
2505 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2506 val |= SDHCI_WAKE_ON_INT;
2507 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2508}
2509
2510EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2511
Pierre Ossmand129bce2006-03-24 03:18:17 -08002512#endif /* CONFIG_PM */
2513
Adrian Hunter50accb92011-10-03 15:33:34 +03002514#ifdef CONFIG_PM_RUNTIME
2515
2516static int sdhci_runtime_pm_get(struct sdhci_host *host)
2517{
2518 return pm_runtime_get_sync(host->mmc->parent);
2519}
2520
2521static int sdhci_runtime_pm_put(struct sdhci_host *host)
2522{
2523 pm_runtime_mark_last_busy(host->mmc->parent);
2524 return pm_runtime_put_autosuspend(host->mmc->parent);
2525}
2526
2527int sdhci_runtime_suspend_host(struct sdhci_host *host)
2528{
2529 unsigned long flags;
2530 int ret = 0;
2531
2532 /* Disable tuning since we are suspending */
2533 if (host->version >= SDHCI_SPEC_300 &&
2534 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2535 del_timer_sync(&host->tuning_timer);
2536 host->flags &= ~SDHCI_NEEDS_RETUNING;
2537 }
2538
2539 spin_lock_irqsave(&host->lock, flags);
2540 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2541 spin_unlock_irqrestore(&host->lock, flags);
2542
2543 synchronize_irq(host->irq);
2544
2545 spin_lock_irqsave(&host->lock, flags);
2546 host->runtime_suspended = true;
2547 spin_unlock_irqrestore(&host->lock, flags);
2548
2549 return ret;
2550}
2551EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2552
2553int sdhci_runtime_resume_host(struct sdhci_host *host)
2554{
2555 unsigned long flags;
2556 int ret = 0, host_flags = host->flags;
2557
2558 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2559 if (host->ops->enable_dma)
2560 host->ops->enable_dma(host);
2561 }
2562
2563 sdhci_init(host, 0);
2564
2565 /* Force clock and power re-program */
2566 host->pwr = 0;
2567 host->clock = 0;
2568 sdhci_do_set_ios(host, &host->mmc->ios);
2569
2570 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2571 if (host_flags & SDHCI_PV_ENABLED)
2572 sdhci_do_enable_preset_value(host, true);
2573
2574 /* Set the re-tuning expiration flag */
2575 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2576 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2577 host->flags |= SDHCI_NEEDS_RETUNING;
2578
2579 spin_lock_irqsave(&host->lock, flags);
2580
2581 host->runtime_suspended = false;
2582
2583 /* Enable SDIO IRQ */
2584 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2585 sdhci_enable_sdio_irq_nolock(host, true);
2586
2587 /* Enable Card Detection */
2588 sdhci_enable_card_detection(host);
2589
2590 spin_unlock_irqrestore(&host->lock, flags);
2591
2592 return ret;
2593}
2594EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2595
2596#endif
2597
Pierre Ossmand129bce2006-03-24 03:18:17 -08002598/*****************************************************************************\
2599 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002600 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002601 * *
2602\*****************************************************************************/
2603
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002604struct sdhci_host *sdhci_alloc_host(struct device *dev,
2605 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002606{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002607 struct mmc_host *mmc;
2608 struct sdhci_host *host;
2609
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002610 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002611
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002612 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002613 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002614 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002615
2616 host = mmc_priv(mmc);
2617 host->mmc = mmc;
2618
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002619 return host;
2620}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002621
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002622EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002623
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002624int sdhci_add_host(struct sdhci_host *host)
2625{
2626 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302627 u32 caps[2];
2628 u32 max_current_caps;
2629 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002630 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002631
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002632 WARN_ON(host == NULL);
2633 if (host == NULL)
2634 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002635
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002636 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002637
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002638 if (debug_quirks)
2639 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03002640 if (debug_quirks2)
2641 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002642
Pierre Ossmand96649e2006-06-30 02:22:30 -07002643 sdhci_reset(host, SDHCI_RESET_ALL);
2644
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002645 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002646 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2647 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002648 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302649 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002650 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002651 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002652 }
2653
Arindam Nathf2119df2011-05-05 12:18:57 +05302654 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002655 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002656
Arindam Nathf2119df2011-05-05 12:18:57 +05302657 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2658 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2659
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002660 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002661 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302662 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002663 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002664 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002665 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002666
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002667 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002668 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002669 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002670 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002671 }
2672
Arindam Nathf2119df2011-05-05 12:18:57 +05302673 if ((host->version >= SDHCI_SPEC_200) &&
2674 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002675 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002676
2677 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2678 (host->flags & SDHCI_USE_ADMA)) {
2679 DBG("Disabling ADMA as it is marked broken\n");
2680 host->flags &= ~SDHCI_USE_ADMA;
2681 }
2682
Richard Röjforsa13abc72009-09-22 16:45:30 -07002683 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002684 if (host->ops->enable_dma) {
2685 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302686 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002687 "available. Falling back to PIO.\n",
2688 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002689 host->flags &=
2690 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002691 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002692 }
2693 }
2694
Pierre Ossman2134a922008-06-28 18:28:51 +02002695 if (host->flags & SDHCI_USE_ADMA) {
2696 /*
2697 * We need to allocate descriptors for all sg entries
2698 * (128) and potentially one alignment transfer for
2699 * each of those entries.
2700 */
2701 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2702 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2703 if (!host->adma_desc || !host->align_buffer) {
2704 kfree(host->adma_desc);
2705 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302706 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002707 "buffers. Falling back to standard DMA.\n",
2708 mmc_hostname(mmc));
2709 host->flags &= ~SDHCI_USE_ADMA;
2710 }
2711 }
2712
Pierre Ossman76591502008-07-21 00:32:11 +02002713 /*
2714 * If we use DMA, then it's up to the caller to set the DMA
2715 * mask, but PIO does not need the hw shim so we set a new
2716 * mask here in that case.
2717 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002718 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002719 host->dma_mask = DMA_BIT_MASK(64);
2720 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2721 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002722
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002723 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302724 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002725 >> SDHCI_CLOCK_BASE_SHIFT;
2726 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302727 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002728 >> SDHCI_CLOCK_BASE_SHIFT;
2729
Pierre Ossmand129bce2006-03-24 03:18:17 -08002730 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002731 if (host->max_clk == 0 || host->quirks &
2732 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002733 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302734 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002735 "frequency.\n", mmc_hostname(mmc));
2736 return -ENODEV;
2737 }
2738 host->max_clk = host->ops->get_max_clock(host);
2739 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002740
2741 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302742 * In case of Host Controller v3.00, find out whether clock
2743 * multiplier is supported.
2744 */
2745 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2746 SDHCI_CLOCK_MUL_SHIFT;
2747
2748 /*
2749 * In case the value in Clock Multiplier is 0, then programmable
2750 * clock mode is not supported, otherwise the actual clock
2751 * multiplier is one more than the value of Clock Multiplier
2752 * in the Capabilities Register.
2753 */
2754 if (host->clk_mul)
2755 host->clk_mul += 1;
2756
2757 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002758 * Set host parameters.
2759 */
2760 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302761 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002762 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002763 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302764 else if (host->version >= SDHCI_SPEC_300) {
2765 if (host->clk_mul) {
2766 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2767 mmc->f_max = host->max_clk * host->clk_mul;
2768 } else
2769 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2770 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002771 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002772
Sahitya Tummalaca422112013-02-22 12:15:54 +05302773 host->timeout_clk =
2774 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2775 if (host->timeout_clk == 0) {
2776 if (host->ops->get_timeout_clock) {
2777 host->timeout_clk = host->ops->get_timeout_clock(host);
2778 } else if (!(host->quirks &
2779 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2780 pr_err("%s: Hardware doesn't specify timeout clock "
2781 "frequency.\n", mmc_hostname(mmc));
2782 return -ENODEV;
2783 }
2784 }
2785 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2786 host->timeout_clk *= 1000;
2787
2788 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
2789 host->timeout_clk = mmc->f_max / 1000;
2790
2791 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
2792
Andrei Warkentine89d4562011-05-23 15:06:37 -05002793 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2794
2795 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2796 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002797
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002798 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002799 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002800 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002801 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002802 host->flags |= SDHCI_AUTO_CMD23;
2803 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2804 } else {
2805 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2806 }
2807
Philip Rakity15ec4462010-11-19 16:48:39 -05002808 /*
2809 * A controller may support 8-bit width, but the board itself
2810 * might not have the pins brought out. Boards that support
2811 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2812 * their platform code before calling sdhci_add_host(), and we
2813 * won't assume 8-bit width for hosts without that CAP.
2814 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002815 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002816 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002817
Arindam Nathf2119df2011-05-05 12:18:57 +05302818 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002819 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002820
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002821 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2822 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002823 mmc->caps |= MMC_CAP_NEEDS_POLL;
2824
Al Cooper4188bba2012-03-16 15:54:17 -04002825 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
2826 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2827 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05302828 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2829
2830 /* SDR104 supports also implies SDR50 support */
2831 if (caps[1] & SDHCI_SUPPORT_SDR104)
2832 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2833 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2834 mmc->caps |= MMC_CAP_UHS_SDR50;
2835
2836 if (caps[1] & SDHCI_SUPPORT_DDR50)
2837 mmc->caps |= MMC_CAP_UHS_DDR50;
2838
Girish K S2cd06dc2012-01-06 09:56:39 +05302839 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05302840 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2841 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2842
Girish K S2cd06dc2012-01-06 09:56:39 +05302843 /* Does the host need tuning for HS200? */
2844 if (mmc->caps2 & MMC_CAP2_HS200)
2845 host->flags |= SDHCI_HS200_NEEDS_TUNING;
2846
Arindam Nathd6d50a12011-05-05 12:18:59 +05302847 /* Driver Type(s) (A, C, D) supported by the host */
2848 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2849 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2850 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2851 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2852 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2853 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2854
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02002855 /* Initial value for re-tuning timer count */
2856 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2857 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2858
2859 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302860 * In case Re-tuning Timer is not disabled, the actual value of
2861 * re-tuning timer will be 2 ^ (n - 1).
2862 */
2863 if (host->tuning_count)
2864 host->tuning_count = 1 << (host->tuning_count - 1);
2865
2866 /* Re-tuning mode supported by the Host Controller */
2867 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2868 SDHCI_RETUNING_MODE_SHIFT;
2869
Takashi Iwai8f230f42010-12-08 10:04:30 +01002870 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05302871 /*
2872 * According to SD Host Controller spec v3.00, if the Host System
2873 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2874 * the value is meaningful only if Voltage Support in the Capabilities
2875 * register is set. The actual current value is 4 times the register
2876 * value.
2877 */
2878 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2879
2880 if (caps[0] & SDHCI_CAN_VDD_330) {
2881 int max_current_330;
2882
Takashi Iwai8f230f42010-12-08 10:04:30 +01002883 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302884
2885 max_current_330 = ((max_current_caps &
2886 SDHCI_MAX_CURRENT_330_MASK) >>
2887 SDHCI_MAX_CURRENT_330_SHIFT) *
2888 SDHCI_MAX_CURRENT_MULTIPLIER;
2889
2890 if (max_current_330 > 150)
2891 mmc->caps |= MMC_CAP_SET_XPC_330;
2892 }
2893 if (caps[0] & SDHCI_CAN_VDD_300) {
2894 int max_current_300;
2895
Takashi Iwai8f230f42010-12-08 10:04:30 +01002896 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302897
2898 max_current_300 = ((max_current_caps &
2899 SDHCI_MAX_CURRENT_300_MASK) >>
2900 SDHCI_MAX_CURRENT_300_SHIFT) *
2901 SDHCI_MAX_CURRENT_MULTIPLIER;
2902
2903 if (max_current_300 > 150)
2904 mmc->caps |= MMC_CAP_SET_XPC_300;
2905 }
2906 if (caps[0] & SDHCI_CAN_VDD_180) {
2907 int max_current_180;
2908
Takashi Iwai8f230f42010-12-08 10:04:30 +01002909 ocr_avail |= MMC_VDD_165_195;
2910
Arindam Nathf2119df2011-05-05 12:18:57 +05302911 max_current_180 = ((max_current_caps &
2912 SDHCI_MAX_CURRENT_180_MASK) >>
2913 SDHCI_MAX_CURRENT_180_SHIFT) *
2914 SDHCI_MAX_CURRENT_MULTIPLIER;
2915
2916 if (max_current_180 > 150)
2917 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05302918
2919 /* Maximum current capabilities of the host at 1.8V */
2920 if (max_current_180 >= 800)
2921 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2922 else if (max_current_180 >= 600)
2923 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2924 else if (max_current_180 >= 400)
2925 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2926 else
2927 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05302928 }
2929
Takashi Iwai8f230f42010-12-08 10:04:30 +01002930 mmc->ocr_avail = ocr_avail;
2931 mmc->ocr_avail_sdio = ocr_avail;
2932 if (host->ocr_avail_sdio)
2933 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2934 mmc->ocr_avail_sd = ocr_avail;
2935 if (host->ocr_avail_sd)
2936 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2937 else /* normal SD controllers don't support 1.8V */
2938 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2939 mmc->ocr_avail_mmc = ocr_avail;
2940 if (host->ocr_avail_mmc)
2941 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07002942
2943 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302944 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002945 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002946 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07002947 }
2948
Pierre Ossmand129bce2006-03-24 03:18:17 -08002949 spin_lock_init(&host->lock);
2950
2951 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02002952 * Maximum number of segments. Depends on if the hardware
2953 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002954 */
Pierre Ossman2134a922008-06-28 18:28:51 +02002955 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002956 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07002957 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002958 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02002959 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002960 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002961
2962 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01002963 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01002964 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08002965 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002966 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002967
2968 /*
2969 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02002970 * of bytes. When doing hardware scatter/gather, each entry cannot
2971 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002972 */
Olof Johansson30652aa2011-01-01 18:37:32 -06002973 if (host->flags & SDHCI_USE_ADMA) {
2974 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2975 mmc->max_seg_size = 65535;
2976 else
2977 mmc->max_seg_size = 65536;
2978 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002979 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002980 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002981
2982 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002983 * Maximum block size. This varies from controller to controller and
2984 * is specified in the capabilities register.
2985 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002986 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2987 mmc->max_blk_size = 2;
2988 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05302989 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03002990 SDHCI_MAX_BLOCK_SHIFT;
2991 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302992 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03002993 "assuming 512 bytes\n", mmc_hostname(mmc));
2994 mmc->max_blk_size = 0;
2995 }
2996 }
2997
2998 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002999
3000 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003001 * Maximum block count.
3002 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003003 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003004
3005 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003006 * Init tasklets.
3007 */
3008 tasklet_init(&host->card_tasklet,
3009 sdhci_tasklet_card, (unsigned long)host);
3010 tasklet_init(&host->finish_tasklet,
3011 sdhci_tasklet_finish, (unsigned long)host);
3012
Al Viroe4cad1b2006-10-10 22:47:07 +01003013 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003014
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303015 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303016 init_waitqueue_head(&host->buf_ready_int);
3017
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303018 /* Initialize re-tuning timer */
3019 init_timer(&host->tuning_timer);
3020 host->tuning_timer.data = (unsigned long)host;
3021 host->tuning_timer.function = sdhci_tuning_timer;
3022 }
3023
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003024 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003025 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003026 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003027 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003028
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003029 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3030 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303031 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003032 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003033 }
3034
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003035 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003036
3037#ifdef CONFIG_MMC_DEBUG
3038 sdhci_dumpregs(host);
3039#endif
3040
Pierre Ossmanf9134312008-12-21 17:01:48 +01003041#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003042 snprintf(host->led_name, sizeof(host->led_name),
3043 "%s::", mmc_hostname(mmc));
3044 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003045 host->led.brightness = LED_OFF;
3046 host->led.default_trigger = mmc_hostname(mmc);
3047 host->led.brightness_set = sdhci_led_control;
3048
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003049 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003050 if (ret)
3051 goto reset;
3052#endif
3053
Pierre Ossman5f25a662006-10-04 02:15:39 -07003054 mmiowb();
3055
Pierre Ossmand129bce2006-03-24 03:18:17 -08003056 mmc_add_host(mmc);
3057
Sahitya Tummalaca422112013-02-22 12:15:54 +05303058 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003059 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003060 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3061 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003062
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003063 sdhci_enable_card_detection(host);
3064
Pierre Ossmand129bce2006-03-24 03:18:17 -08003065 return 0;
3066
Pierre Ossmanf9134312008-12-21 17:01:48 +01003067#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003068reset:
3069 sdhci_reset(host, SDHCI_RESET_ALL);
3070 free_irq(host->irq, host);
3071#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003072untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003073 tasklet_kill(&host->card_tasklet);
3074 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003075
3076 return ret;
3077}
3078
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003079EXPORT_SYMBOL_GPL(sdhci_add_host);
3080
Pierre Ossman1e728592008-04-16 19:13:13 +02003081void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003082{
Pierre Ossman1e728592008-04-16 19:13:13 +02003083 unsigned long flags;
3084
3085 if (dead) {
3086 spin_lock_irqsave(&host->lock, flags);
3087
3088 host->flags |= SDHCI_DEVICE_DEAD;
3089
3090 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303091 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003092 " transfer!\n", mmc_hostname(host->mmc));
3093
3094 host->mrq->cmd->error = -ENOMEDIUM;
3095 tasklet_schedule(&host->finish_tasklet);
3096 }
3097
3098 spin_unlock_irqrestore(&host->lock, flags);
3099 }
3100
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003101 sdhci_disable_card_detection(host);
3102
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003103 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003104
Pierre Ossmanf9134312008-12-21 17:01:48 +01003105#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003106 led_classdev_unregister(&host->led);
3107#endif
3108
Pierre Ossman1e728592008-04-16 19:13:13 +02003109 if (!dead)
3110 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003111
3112 free_irq(host->irq, host);
3113
3114 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303115 if (host->version >= SDHCI_SPEC_300)
3116 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003117
3118 tasklet_kill(&host->card_tasklet);
3119 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003120
Sahitya Tummalaca422112013-02-22 12:15:54 +05303121 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003122 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003123
Pierre Ossman2134a922008-06-28 18:28:51 +02003124 kfree(host->adma_desc);
3125 kfree(host->align_buffer);
3126
3127 host->adma_desc = NULL;
3128 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003129}
3130
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003131EXPORT_SYMBOL_GPL(sdhci_remove_host);
3132
3133void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003134{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003135 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003136}
3137
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003138EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003139
3140/*****************************************************************************\
3141 * *
3142 * Driver init/exit *
3143 * *
3144\*****************************************************************************/
3145
3146static int __init sdhci_drv_init(void)
3147{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303148 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003149 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303150 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003151
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003152 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003153}
3154
3155static void __exit sdhci_drv_exit(void)
3156{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003157}
3158
3159module_init(sdhci_drv_init);
3160module_exit(sdhci_drv_exit);
3161
Pierre Ossmandf673b22006-06-30 02:22:31 -07003162module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003163module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003164
Pierre Ossman32710e82009-04-08 20:14:54 +02003165MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003166MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003167MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003168
Pierre Ossmandf673b22006-06-30 02:22:31 -07003169MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003170MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");