blob: 8f61f8d6e0ca0dc48fd2f8e5fe5cdba85eff7a5e [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
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 Hunter66fd8ad2011-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 S069c9f12012-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 Hunter66fd8ad2011-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{
Girish K Sa3c76eb2011-10-11 11:44:09 +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
Girish K Sa3c76eb2011-10-11 11:44:09 +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));
Girish K Sa3c76eb2011-10-11 11:44:09 +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));
Girish K Sa3c76eb2011-10-11 11:44:09 +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));
Girish K Sa3c76eb2011-10-11 11:44:09 +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));
Girish K Sa3c76eb2011-10-11 11:44:09 +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));
Girish K Sa3c76eb2011-10-11 11:44:09 +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));
Girish K Sa3c76eb2011-10-11 11:44:09 +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));
Girish K Sa3c76eb2011-10-11 11:44:09 +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));
Girish K Sa3c76eb2011-10-11 11:44:09 +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));
Girish K Sa3c76eb2011-10-11 11:44:09 +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));
Girish K Sa3c76eb2011-10-11 11:44:09 +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));
Girish K Sa3c76eb2011-10-11 11:44:09 +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)
Girish K Sa3c76eb2011-10-11 11:44:09 +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
Girish K Sa3c76eb2011-10-11 11:44:09 +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{
Shawn Guod25928d2011-06-21 22:41:48 +0800147 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
Shawn Guod25928d2011-06-21 22:41:48 +0800153 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
154 SDHCI_CARD_PRESENT;
155 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
156
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 Ossmane16514d82006-06-30 02:22:24 -0700175 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300176 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-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 Ossmane16514d82006-06-30 02:22:24 -0700192 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800193 host->clock = 0;
194
Pierre Ossmane16514d82006-06-30 02:22:24 -0700195 /* Wait max 100 ms */
196 timeout = 100;
197
198 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300199 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700200 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530201 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700202 mmc_hostname(host->mmc), (int)mask);
203 sdhci_dumpregs(host);
204 return;
205 }
206 timeout--;
207 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800208 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300209
Philip Rakity393c1a32011-01-21 11:26:40 -0800210 if (host->ops->platform_reset_exit)
211 host->ops->platform_reset_exit(host, mask);
212
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300213 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
214 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800215
216 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
217 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
218 host->ops->enable_dma(host);
219 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800220}
221
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800222static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
223
224static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800225{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800226 if (soft)
227 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
228 else
229 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800230
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300231 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
232 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700233 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
234 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300235 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800236
237 if (soft) {
238 /* force clock reconfiguration */
239 host->clock = 0;
240 sdhci_set_ios(host->mmc, &host->mmc->ios);
241 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300242}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800243
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300244static void sdhci_reinit(struct sdhci_host *host)
245{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800246 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300247 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800248}
249
250static void sdhci_activate_led(struct sdhci_host *host)
251{
252 u8 ctrl;
253
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300254 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800255 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300256 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800257}
258
259static void sdhci_deactivate_led(struct sdhci_host *host)
260{
261 u8 ctrl;
262
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300263 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800264 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300265 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800266}
267
Pierre Ossmanf9134312008-12-21 17:01:48 +0100268#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100269static void sdhci_led_control(struct led_classdev *led,
270 enum led_brightness brightness)
271{
272 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
273 unsigned long flags;
274
275 spin_lock_irqsave(&host->lock, flags);
276
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300277 if (host->runtime_suspended)
278 goto out;
279
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100280 if (brightness == LED_OFF)
281 sdhci_deactivate_led(host);
282 else
283 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300284out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100285 spin_unlock_irqrestore(&host->lock, flags);
286}
287#endif
288
Pierre Ossmand129bce2006-03-24 03:18:17 -0800289/*****************************************************************************\
290 * *
291 * Core functions *
292 * *
293\*****************************************************************************/
294
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100295static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800296{
Pierre Ossman76591502008-07-21 00:32:11 +0200297 unsigned long flags;
298 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700299 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200300 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800301
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100302 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800303
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100304 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200305 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800306
Pierre Ossman76591502008-07-21 00:32:11 +0200307 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800308
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100309 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200310 if (!sg_miter_next(&host->sg_miter))
311 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800312
Pierre Ossman76591502008-07-21 00:32:11 +0200313 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800314
Pierre Ossman76591502008-07-21 00:32:11 +0200315 blksize -= len;
316 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200317
Pierre Ossman76591502008-07-21 00:32:11 +0200318 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800319
Pierre Ossman76591502008-07-21 00:32:11 +0200320 while (len) {
321 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300322 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200323 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800324 }
Pierre Ossman76591502008-07-21 00:32:11 +0200325
326 *buf = scratch & 0xFF;
327
328 buf++;
329 scratch >>= 8;
330 chunk--;
331 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800332 }
333 }
Pierre Ossman76591502008-07-21 00:32:11 +0200334
335 sg_miter_stop(&host->sg_miter);
336
337 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100338}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800339
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100340static void sdhci_write_block_pio(struct sdhci_host *host)
341{
Pierre Ossman76591502008-07-21 00:32:11 +0200342 unsigned long flags;
343 size_t blksize, len, chunk;
344 u32 scratch;
345 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100346
347 DBG("PIO writing\n");
348
349 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200350 chunk = 0;
351 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100352
Pierre Ossman76591502008-07-21 00:32:11 +0200353 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100354
355 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200356 if (!sg_miter_next(&host->sg_miter))
357 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358
Pierre Ossman76591502008-07-21 00:32:11 +0200359 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200360
Pierre Ossman76591502008-07-21 00:32:11 +0200361 blksize -= len;
362 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100363
Pierre Ossman76591502008-07-21 00:32:11 +0200364 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100365
Pierre Ossman76591502008-07-21 00:32:11 +0200366 while (len) {
367 scratch |= (u32)*buf << (chunk * 8);
368
369 buf++;
370 chunk++;
371 len--;
372
373 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300374 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200375 chunk = 0;
376 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100377 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100378 }
379 }
Pierre Ossman76591502008-07-21 00:32:11 +0200380
381 sg_miter_stop(&host->sg_miter);
382
383 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100384}
385
386static void sdhci_transfer_pio(struct sdhci_host *host)
387{
388 u32 mask;
389
390 BUG_ON(!host->data);
391
Pierre Ossman76591502008-07-21 00:32:11 +0200392 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100393 return;
394
395 if (host->data->flags & MMC_DATA_READ)
396 mask = SDHCI_DATA_AVAILABLE;
397 else
398 mask = SDHCI_SPACE_AVAILABLE;
399
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200400 /*
401 * Some controllers (JMicron JMB38x) mess up the buffer bits
402 * for transfers < 4 bytes. As long as it is just one block,
403 * we can ignore the bits.
404 */
405 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
406 (host->data->blocks == 1))
407 mask = ~0;
408
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300409 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300410 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
411 udelay(100);
412
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100413 if (host->data->flags & MMC_DATA_READ)
414 sdhci_read_block_pio(host);
415 else
416 sdhci_write_block_pio(host);
417
Pierre Ossman76591502008-07-21 00:32:11 +0200418 host->blocks--;
419 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100420 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100421 }
422
423 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800424}
425
Pierre Ossman2134a922008-06-28 18:28:51 +0200426static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
427{
428 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800429 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200430}
431
432static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
433{
Cong Wang482fce92011-11-27 13:27:00 +0800434 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200435 local_irq_restore(*flags);
436}
437
Ben Dooks118cd172010-03-05 13:43:26 -0800438static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
439{
Ben Dooks9e506f32010-03-05 13:43:29 -0800440 __le32 *dataddr = (__le32 __force *)(desc + 4);
441 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800442
Ben Dooks9e506f32010-03-05 13:43:29 -0800443 /* SDHCI specification says ADMA descriptors should be 4 byte
444 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800445
Ben Dooks9e506f32010-03-05 13:43:29 -0800446 cmdlen[0] = cpu_to_le16(cmd);
447 cmdlen[1] = cpu_to_le16(len);
448
449 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800450}
451
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200452static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200453 struct mmc_data *data)
454{
455 int direction;
456
457 u8 *desc;
458 u8 *align;
459 dma_addr_t addr;
460 dma_addr_t align_addr;
461 int len, offset;
462
463 struct scatterlist *sg;
464 int i;
465 char *buffer;
466 unsigned long flags;
467
468 /*
469 * The spec does not specify endianness of descriptor table.
470 * We currently guess that it is LE.
471 */
472
473 if (data->flags & MMC_DATA_READ)
474 direction = DMA_FROM_DEVICE;
475 else
476 direction = DMA_TO_DEVICE;
477
478 /*
479 * The ADMA descriptor table is mapped further down as we
480 * need to fill it with data first.
481 */
482
483 host->align_addr = dma_map_single(mmc_dev(host->mmc),
484 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700485 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200486 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200487 BUG_ON(host->align_addr & 0x3);
488
489 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
490 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200491 if (host->sg_count == 0)
492 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200493
494 desc = host->adma_desc;
495 align = host->align_buffer;
496
497 align_addr = host->align_addr;
498
499 for_each_sg(data->sg, sg, host->sg_count, i) {
500 addr = sg_dma_address(sg);
501 len = sg_dma_len(sg);
502
503 /*
504 * The SDHCI specification states that ADMA
505 * addresses must be 32-bit aligned. If they
506 * aren't, then we use a bounce buffer for
507 * the (up to three) bytes that screw up the
508 * alignment.
509 */
510 offset = (4 - (addr & 0x3)) & 0x3;
511 if (offset) {
512 if (data->flags & MMC_DATA_WRITE) {
513 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200514 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200515 memcpy(align, buffer, offset);
516 sdhci_kunmap_atomic(buffer, &flags);
517 }
518
Ben Dooks118cd172010-03-05 13:43:26 -0800519 /* tran, valid */
520 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200521
522 BUG_ON(offset > 65536);
523
Pierre Ossman2134a922008-06-28 18:28:51 +0200524 align += 4;
525 align_addr += 4;
526
527 desc += 8;
528
529 addr += offset;
530 len -= offset;
531 }
532
Pierre Ossman2134a922008-06-28 18:28:51 +0200533 BUG_ON(len > 65536);
534
Ben Dooks118cd172010-03-05 13:43:26 -0800535 /* tran, valid */
536 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200537 desc += 8;
538
539 /*
540 * If this triggers then we have a calculation bug
541 * somewhere. :/
542 */
543 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
544 }
545
Thomas Abraham70764a92010-05-26 14:42:04 -0700546 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
547 /*
548 * Mark the last descriptor as the terminating descriptor
549 */
550 if (desc != host->adma_desc) {
551 desc -= 8;
552 desc[0] |= 0x2; /* end */
553 }
554 } else {
555 /*
556 * Add a terminating entry.
557 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200558
Thomas Abraham70764a92010-05-26 14:42:04 -0700559 /* nop, end, valid */
560 sdhci_set_adma_desc(desc, 0, 0, 0x3);
561 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200562
563 /*
564 * Resync align buffer as we might have changed it.
565 */
566 if (data->flags & MMC_DATA_WRITE) {
567 dma_sync_single_for_device(mmc_dev(host->mmc),
568 host->align_addr, 128 * 4, direction);
569 }
570
571 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
572 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200573 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200574 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200575 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200576
577 return 0;
578
579unmap_entries:
580 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
581 data->sg_len, direction);
582unmap_align:
583 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
584 128 * 4, direction);
585fail:
586 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200587}
588
589static void sdhci_adma_table_post(struct sdhci_host *host,
590 struct mmc_data *data)
591{
592 int direction;
593
594 struct scatterlist *sg;
595 int i, size;
596 u8 *align;
597 char *buffer;
598 unsigned long flags;
599
600 if (data->flags & MMC_DATA_READ)
601 direction = DMA_FROM_DEVICE;
602 else
603 direction = DMA_TO_DEVICE;
604
605 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
606 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
607
608 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
609 128 * 4, direction);
610
611 if (data->flags & MMC_DATA_READ) {
612 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
613 data->sg_len, direction);
614
615 align = host->align_buffer;
616
617 for_each_sg(data->sg, sg, host->sg_count, i) {
618 if (sg_dma_address(sg) & 0x3) {
619 size = 4 - (sg_dma_address(sg) & 0x3);
620
621 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200622 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200623 memcpy(buffer, align, size);
624 sdhci_kunmap_atomic(buffer, &flags);
625
626 align += 4;
627 }
628 }
629 }
630
631 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
632 data->sg_len, direction);
633}
634
Andrei Warkentina3c77782011-04-11 16:13:42 -0500635static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800636{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700637 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500638 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700639 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800640
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200641 /*
642 * If the host controller provides us with an incorrect timeout
643 * value, just skip the check and use 0xE. The hardware may take
644 * longer to time out, but that's much better than having a too-short
645 * timeout value.
646 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200647 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200648 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200649
Andrei Warkentina3c77782011-04-11 16:13:42 -0500650 /* Unspecified timeout, assume max */
651 if (!data && !cmd->cmd_timeout_ms)
652 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800653
Andrei Warkentina3c77782011-04-11 16:13:42 -0500654 /* timeout in us */
655 if (!data)
656 target_timeout = cmd->cmd_timeout_ms * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300657 else {
658 target_timeout = data->timeout_ns / 1000;
659 if (host->clock)
660 target_timeout += data->timeout_clks / host->clock;
661 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700662
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700663 /*
664 * Figure out needed cycles.
665 * We do this in steps in order to fit inside a 32 bit int.
666 * The first step is the minimum timeout, which will have a
667 * minimum resolution of 6 bits:
668 * (1) 2^13*1000 > 2^22,
669 * (2) host->timeout_clk < 2^16
670 * =>
671 * (1) / (2) > 2^6
672 */
673 count = 0;
674 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
675 while (current_timeout < target_timeout) {
676 count++;
677 current_timeout <<= 1;
678 if (count >= 0xF)
679 break;
680 }
681
682 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400683 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
684 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700685 count = 0xE;
686 }
687
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200688 return count;
689}
690
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300691static void sdhci_set_transfer_irqs(struct sdhci_host *host)
692{
693 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
694 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
695
696 if (host->flags & SDHCI_REQ_USE_DMA)
697 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
698 else
699 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
700}
701
Andrei Warkentina3c77782011-04-11 16:13:42 -0500702static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200703{
704 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200705 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500706 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200707 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200708
709 WARN_ON(host->data);
710
Andrei Warkentina3c77782011-04-11 16:13:42 -0500711 if (data || (cmd->flags & MMC_RSP_BUSY)) {
712 count = sdhci_calc_timeout(host, cmd);
713 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
714 }
715
716 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200717 return;
718
719 /* Sanity checks */
720 BUG_ON(data->blksz * data->blocks > 524288);
721 BUG_ON(data->blksz > host->mmc->max_blk_size);
722 BUG_ON(data->blocks > 65535);
723
724 host->data = data;
725 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400726 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200727
Richard Röjforsa13abc72009-09-22 16:45:30 -0700728 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100729 host->flags |= SDHCI_REQ_USE_DMA;
730
Pierre Ossman2134a922008-06-28 18:28:51 +0200731 /*
732 * FIXME: This doesn't account for merging when mapping the
733 * scatterlist.
734 */
735 if (host->flags & SDHCI_REQ_USE_DMA) {
736 int broken, i;
737 struct scatterlist *sg;
738
739 broken = 0;
740 if (host->flags & SDHCI_USE_ADMA) {
741 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
742 broken = 1;
743 } else {
744 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
745 broken = 1;
746 }
747
748 if (unlikely(broken)) {
749 for_each_sg(data->sg, sg, data->sg_len, i) {
750 if (sg->length & 0x3) {
751 DBG("Reverting to PIO because of "
752 "transfer size (%d)\n",
753 sg->length);
754 host->flags &= ~SDHCI_REQ_USE_DMA;
755 break;
756 }
757 }
758 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100759 }
760
761 /*
762 * The assumption here being that alignment is the same after
763 * translation to device address space.
764 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200765 if (host->flags & SDHCI_REQ_USE_DMA) {
766 int broken, i;
767 struct scatterlist *sg;
768
769 broken = 0;
770 if (host->flags & SDHCI_USE_ADMA) {
771 /*
772 * As we use 3 byte chunks to work around
773 * alignment problems, we need to check this
774 * quirk.
775 */
776 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
777 broken = 1;
778 } else {
779 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
780 broken = 1;
781 }
782
783 if (unlikely(broken)) {
784 for_each_sg(data->sg, sg, data->sg_len, i) {
785 if (sg->offset & 0x3) {
786 DBG("Reverting to PIO because of "
787 "bad alignment\n");
788 host->flags &= ~SDHCI_REQ_USE_DMA;
789 break;
790 }
791 }
792 }
793 }
794
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200795 if (host->flags & SDHCI_REQ_USE_DMA) {
796 if (host->flags & SDHCI_USE_ADMA) {
797 ret = sdhci_adma_table_pre(host, data);
798 if (ret) {
799 /*
800 * This only happens when someone fed
801 * us an invalid request.
802 */
803 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200804 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200805 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300806 sdhci_writel(host, host->adma_addr,
807 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200808 }
809 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300810 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200811
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300812 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200813 data->sg, data->sg_len,
814 (data->flags & MMC_DATA_READ) ?
815 DMA_FROM_DEVICE :
816 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300817 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200818 /*
819 * This only happens when someone fed
820 * us an invalid request.
821 */
822 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200823 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200824 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200825 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300826 sdhci_writel(host, sg_dma_address(data->sg),
827 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200828 }
829 }
830 }
831
Pierre Ossman2134a922008-06-28 18:28:51 +0200832 /*
833 * Always adjust the DMA selection as some controllers
834 * (e.g. JMicron) can't do PIO properly when the selection
835 * is ADMA.
836 */
837 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300838 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200839 ctrl &= ~SDHCI_CTRL_DMA_MASK;
840 if ((host->flags & SDHCI_REQ_USE_DMA) &&
841 (host->flags & SDHCI_USE_ADMA))
842 ctrl |= SDHCI_CTRL_ADMA32;
843 else
844 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300845 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100846 }
847
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200848 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200849 int flags;
850
851 flags = SG_MITER_ATOMIC;
852 if (host->data->flags & MMC_DATA_READ)
853 flags |= SG_MITER_TO_SG;
854 else
855 flags |= SG_MITER_FROM_SG;
856 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200857 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800858 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700859
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300860 sdhci_set_transfer_irqs(host);
861
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400862 /* Set the DMA boundary value and block size */
863 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
864 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300865 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700866}
867
868static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500869 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700870{
871 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500872 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700873
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700874 if (data == NULL)
875 return;
876
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200877 WARN_ON(!host->data);
878
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700879 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500880 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
881 mode |= SDHCI_TRNS_MULTI;
882 /*
883 * If we are sending CMD23, CMD12 never gets sent
884 * on successful completion (so no Auto-CMD12).
885 */
886 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
887 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500888 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
889 mode |= SDHCI_TRNS_AUTO_CMD23;
890 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
891 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700892 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500893
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700894 if (data->flags & MMC_DATA_READ)
895 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100896 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700897 mode |= SDHCI_TRNS_DMA;
898
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300899 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800900}
901
902static void sdhci_finish_data(struct sdhci_host *host)
903{
904 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800905
906 BUG_ON(!host->data);
907
908 data = host->data;
909 host->data = NULL;
910
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100911 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200912 if (host->flags & SDHCI_USE_ADMA)
913 sdhci_adma_table_post(host, data);
914 else {
915 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
916 data->sg_len, (data->flags & MMC_DATA_READ) ?
917 DMA_FROM_DEVICE : DMA_TO_DEVICE);
918 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800919 }
920
921 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200922 * The specification states that the block count register must
923 * be updated, but it does not specify at what point in the
924 * data flow. That makes the register entirely useless to read
925 * back so we have to assume that nothing made it to the card
926 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800927 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200928 if (data->error)
929 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800930 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200931 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800932
Andrei Warkentine89d4562011-05-23 15:06:37 -0500933 /*
934 * Need to send CMD12 if -
935 * a) open-ended multiblock transfer (no CMD23)
936 * b) error in multiblock transfer
937 */
938 if (data->stop &&
939 (data->error ||
940 !host->mrq->sbc)) {
941
Pierre Ossmand129bce2006-03-24 03:18:17 -0800942 /*
943 * The controller needs a reset of internal state machines
944 * upon error conditions.
945 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200946 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800947 sdhci_reset(host, SDHCI_RESET_CMD);
948 sdhci_reset(host, SDHCI_RESET_DATA);
949 }
950
951 sdhci_send_command(host, data->stop);
952 } else
953 tasklet_schedule(&host->finish_tasklet);
954}
955
956static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
957{
958 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700959 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700960 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800961
962 WARN_ON(host->cmd);
963
Pierre Ossmand129bce2006-03-24 03:18:17 -0800964 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700965 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700966
967 mask = SDHCI_CMD_INHIBIT;
968 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
969 mask |= SDHCI_DATA_INHIBIT;
970
971 /* We shouldn't wait for data inihibit for stop commands, even
972 though they might use busy signaling */
973 if (host->mrq->data && (cmd == host->mrq->data->stop))
974 mask &= ~SDHCI_DATA_INHIBIT;
975
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300976 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700977 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530978 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100979 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800980 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200981 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800982 tasklet_schedule(&host->finish_tasklet);
983 return;
984 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700985 timeout--;
986 mdelay(1);
987 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800988
989 mod_timer(&host->timer, jiffies + 10 * HZ);
990
991 host->cmd = cmd;
992
Andrei Warkentina3c77782011-04-11 16:13:42 -0500993 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300995 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996
Andrei Warkentine89d4562011-05-23 15:06:37 -0500997 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700998
Pierre Ossmand129bce2006-03-24 03:18:17 -0800999 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301000 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001001 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001002 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001003 tasklet_schedule(&host->finish_tasklet);
1004 return;
1005 }
1006
1007 if (!(cmd->flags & MMC_RSP_PRESENT))
1008 flags = SDHCI_CMD_RESP_NONE;
1009 else if (cmd->flags & MMC_RSP_136)
1010 flags = SDHCI_CMD_RESP_LONG;
1011 else if (cmd->flags & MMC_RSP_BUSY)
1012 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1013 else
1014 flags = SDHCI_CMD_RESP_SHORT;
1015
1016 if (cmd->flags & MMC_RSP_CRC)
1017 flags |= SDHCI_CMD_CRC;
1018 if (cmd->flags & MMC_RSP_OPCODE)
1019 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301020
1021 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301022 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1023 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001024 flags |= SDHCI_CMD_DATA;
1025
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001026 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001027}
1028
1029static void sdhci_finish_command(struct sdhci_host *host)
1030{
1031 int i;
1032
1033 BUG_ON(host->cmd == NULL);
1034
1035 if (host->cmd->flags & MMC_RSP_PRESENT) {
1036 if (host->cmd->flags & MMC_RSP_136) {
1037 /* CRC is stripped so we need to do some shifting. */
1038 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001039 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001040 SDHCI_RESPONSE + (3-i)*4) << 8;
1041 if (i != 3)
1042 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001043 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001044 SDHCI_RESPONSE + (3-i)*4-1);
1045 }
1046 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001047 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001048 }
1049 }
1050
Pierre Ossman17b04292007-07-22 22:18:46 +02001051 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001052
Andrei Warkentine89d4562011-05-23 15:06:37 -05001053 /* Finished CMD23, now send actual command. */
1054 if (host->cmd == host->mrq->sbc) {
1055 host->cmd = NULL;
1056 sdhci_send_command(host, host->mrq->cmd);
1057 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001058
Andrei Warkentine89d4562011-05-23 15:06:37 -05001059 /* Processed actual command. */
1060 if (host->data && host->data_early)
1061 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001062
Andrei Warkentine89d4562011-05-23 15:06:37 -05001063 if (!host->cmd->data)
1064 tasklet_schedule(&host->finish_tasklet);
1065
1066 host->cmd = NULL;
1067 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001068}
1069
1070static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1071{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301072 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001073 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301074 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001075 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001076
Todd Poynor30832ab2011-12-27 15:48:46 +02001077 if (clock && clock == host->clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001078 return;
1079
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001080 host->mmc->actual_clock = 0;
1081
Anton Vorontsov81146342009-03-17 00:13:59 +03001082 if (host->ops->set_clock) {
1083 host->ops->set_clock(host, clock);
1084 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1085 return;
1086 }
1087
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001088 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001089
1090 if (clock == 0)
1091 goto out;
1092
Zhangfei Gao85105c52010-08-06 07:10:01 +08001093 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301094 /*
1095 * Check if the Host Controller supports Programmable Clock
1096 * Mode.
1097 */
1098 if (host->clk_mul) {
1099 u16 ctrl;
1100
1101 /*
1102 * We need to figure out whether the Host Driver needs
1103 * to select Programmable Clock Mode, or the value can
1104 * be set automatically by the Host Controller based on
1105 * the Preset Value registers.
1106 */
1107 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1108 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1109 for (div = 1; div <= 1024; div++) {
1110 if (((host->max_clk * host->clk_mul) /
1111 div) <= clock)
1112 break;
1113 }
1114 /*
1115 * Set Programmable Clock Mode in the Clock
1116 * Control register.
1117 */
1118 clk = SDHCI_PROG_CLOCK_MODE;
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001119 real_div = div;
1120 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301121 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001122 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301123 } else {
1124 /* Version 3.00 divisors must be a multiple of 2. */
1125 if (host->max_clk <= clock)
1126 div = 1;
1127 else {
1128 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1129 div += 2) {
1130 if ((host->max_clk / div) <= clock)
1131 break;
1132 }
1133 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001134 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301135 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001136 }
1137 } else {
1138 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001139 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001140 if ((host->max_clk / div) <= clock)
1141 break;
1142 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001143 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301144 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001145 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001146
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001147 if (real_div)
1148 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1149
Arindam Nathc3ed3872011-05-05 12:19:06 +05301150 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001151 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1152 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001153 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001154 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001155
Chris Ball27f6cb12009-09-22 16:45:31 -07001156 /* Wait max 20 ms */
1157 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001158 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001159 & SDHCI_CLOCK_INT_STABLE)) {
1160 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301161 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001162 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001163 sdhci_dumpregs(host);
1164 return;
1165 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001166 timeout--;
1167 mdelay(1);
1168 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001169
1170 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001171 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001172
1173out:
1174 host->clock = clock;
1175}
1176
Adrian Hunterceb61432011-12-27 15:48:41 +02001177static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001178{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001179 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001180
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001181 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001182 switch (1 << power) {
1183 case MMC_VDD_165_195:
1184 pwr = SDHCI_POWER_180;
1185 break;
1186 case MMC_VDD_29_30:
1187 case MMC_VDD_30_31:
1188 pwr = SDHCI_POWER_300;
1189 break;
1190 case MMC_VDD_32_33:
1191 case MMC_VDD_33_34:
1192 pwr = SDHCI_POWER_330;
1193 break;
1194 default:
1195 BUG();
1196 }
1197 }
1198
1199 if (host->pwr == pwr)
Adrian Hunterceb61432011-12-27 15:48:41 +02001200 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001201
Pierre Ossmanae628902009-05-03 20:45:03 +02001202 host->pwr = pwr;
1203
1204 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001205 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterceb61432011-12-27 15:48:41 +02001206 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001207 }
1208
1209 /*
1210 * Spec says that we should clear the power reg before setting
1211 * a new value. Some controllers don't seem to like this though.
1212 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001213 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001214 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001215
Andres Salomone08c1692008-07-04 10:00:03 -07001216 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001217 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001218 * and set turn on power at the same time, so set the voltage first.
1219 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001220 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001221 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1222
1223 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001224
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001225 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001226
1227 /*
1228 * Some controllers need an extra 10ms delay of 10ms before they
1229 * can apply clock after applying power
1230 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001231 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001232 mdelay(10);
Adrian Hunterceb61432011-12-27 15:48:41 +02001233
1234 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001235}
1236
Pierre Ossmand129bce2006-03-24 03:18:17 -08001237/*****************************************************************************\
1238 * *
1239 * MMC callbacks *
1240 * *
1241\*****************************************************************************/
1242
1243static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1244{
1245 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001246 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001247 unsigned long flags;
1248
1249 host = mmc_priv(mmc);
1250
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001251 sdhci_runtime_pm_get(host);
1252
Pierre Ossmand129bce2006-03-24 03:18:17 -08001253 spin_lock_irqsave(&host->lock, flags);
1254
1255 WARN_ON(host->mrq != NULL);
1256
Pierre Ossmanf9134312008-12-21 17:01:48 +01001257#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001258 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001259#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001260
1261 /*
1262 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1263 * requests if Auto-CMD12 is enabled.
1264 */
1265 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001266 if (mrq->stop) {
1267 mrq->data->stop = NULL;
1268 mrq->stop = NULL;
1269 }
1270 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001271
1272 host->mrq = mrq;
1273
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001274 /* If polling, assume that the card is always present. */
1275 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1276 present = true;
1277 else
1278 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1279 SDHCI_CARD_PRESENT;
1280
1281 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001282 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001283 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301284 } else {
1285 u32 present_state;
1286
1287 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1288 /*
1289 * Check if the re-tuning timer has already expired and there
1290 * is no on-going data transfer. If so, we need to execute
1291 * tuning procedure before sending command.
1292 */
1293 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1294 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1295 spin_unlock_irqrestore(&host->lock, flags);
Girish K S069c9f12012-01-06 09:56:39 +05301296 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301297 spin_lock_irqsave(&host->lock, flags);
1298
1299 /* Restore original mmc_request structure */
1300 host->mrq = mrq;
1301 }
1302
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001303 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001304 sdhci_send_command(host, mrq->sbc);
1305 else
1306 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301307 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001308
Pierre Ossman5f25a662006-10-04 02:15:39 -07001309 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001310 spin_unlock_irqrestore(&host->lock, flags);
1311}
1312
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001313static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001314{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001315 unsigned long flags;
Adrian Hunterceb61432011-12-27 15:48:41 +02001316 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001317 u8 ctrl;
1318
Pierre Ossmand129bce2006-03-24 03:18:17 -08001319 spin_lock_irqsave(&host->lock, flags);
1320
Adrian Hunterceb61432011-12-27 15:48:41 +02001321 if (host->flags & SDHCI_DEVICE_DEAD) {
1322 spin_unlock_irqrestore(&host->lock, flags);
1323 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1324 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1325 return;
1326 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001327
Pierre Ossmand129bce2006-03-24 03:18:17 -08001328 /*
1329 * Reset the chip on each power off.
1330 * Should clear out any weird states.
1331 */
1332 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001333 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001334 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001335 }
1336
1337 sdhci_set_clock(host, ios->clock);
1338
1339 if (ios->power_mode == MMC_POWER_OFF)
Adrian Hunterceb61432011-12-27 15:48:41 +02001340 vdd_bit = sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001341 else
Adrian Hunterceb61432011-12-27 15:48:41 +02001342 vdd_bit = sdhci_set_power(host, ios->vdd);
1343
1344 if (host->vmmc && vdd_bit != -1) {
1345 spin_unlock_irqrestore(&host->lock, flags);
1346 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1347 spin_lock_irqsave(&host->lock, flags);
1348 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001349
Philip Rakity643a81f2010-09-23 08:24:32 -07001350 if (host->ops->platform_send_init_74_clocks)
1351 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1352
Philip Rakity15ec4462010-11-19 16:48:39 -05001353 /*
1354 * If your platform has 8-bit width support but is not a v3 controller,
1355 * or if it requires special setup code, you should implement that in
1356 * platform_8bit_width().
1357 */
1358 if (host->ops->platform_8bit_width)
1359 host->ops->platform_8bit_width(host, ios->bus_width);
1360 else {
1361 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1362 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1363 ctrl &= ~SDHCI_CTRL_4BITBUS;
1364 if (host->version >= SDHCI_SPEC_300)
1365 ctrl |= SDHCI_CTRL_8BITBUS;
1366 } else {
1367 if (host->version >= SDHCI_SPEC_300)
1368 ctrl &= ~SDHCI_CTRL_8BITBUS;
1369 if (ios->bus_width == MMC_BUS_WIDTH_4)
1370 ctrl |= SDHCI_CTRL_4BITBUS;
1371 else
1372 ctrl &= ~SDHCI_CTRL_4BITBUS;
1373 }
1374 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1375 }
1376
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001377 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001378
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001379 if ((ios->timing == MMC_TIMING_SD_HS ||
1380 ios->timing == MMC_TIMING_MMC_HS)
1381 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001382 ctrl |= SDHCI_CTRL_HISPD;
1383 else
1384 ctrl &= ~SDHCI_CTRL_HISPD;
1385
Arindam Nathd6d50a12011-05-05 12:18:59 +05301386 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301387 u16 clk, ctrl_2;
1388 unsigned int clock;
1389
1390 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301391 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1392 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301393 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1394 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001395 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301396 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301397
1398 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1399 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301400 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301401 /*
1402 * We only need to set Driver Strength if the
1403 * preset value enable is not set.
1404 */
1405 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1406 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1407 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1408 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1409 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1410
1411 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301412 } else {
1413 /*
1414 * According to SDHC Spec v3.00, if the Preset Value
1415 * Enable in the Host Control 2 register is set, we
1416 * need to reset SD Clock Enable before changing High
1417 * Speed Enable to avoid generating clock gliches.
1418 */
Arindam Nath758535c2011-05-05 12:19:00 +05301419
1420 /* Reset SD Clock Enable */
1421 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1422 clk &= ~SDHCI_CLOCK_CARD_EN;
1423 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1424
1425 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1426
1427 /* Re-enable SD Clock */
1428 clock = host->clock;
1429 host->clock = 0;
1430 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301431 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301432
Arindam Nath49c468f2011-05-05 12:19:01 +05301433
1434 /* Reset SD Clock Enable */
1435 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1436 clk &= ~SDHCI_CLOCK_CARD_EN;
1437 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1438
Philip Rakity6322cdd2011-05-13 11:17:15 +05301439 if (host->ops->set_uhs_signaling)
1440 host->ops->set_uhs_signaling(host, ios->timing);
1441 else {
1442 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1443 /* Select Bus Speed Mode for host */
1444 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S069c9f12012-01-06 09:56:39 +05301445 if (ios->timing == MMC_TIMING_MMC_HS200)
1446 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1447 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301448 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1449 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1450 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1451 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1452 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1453 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1454 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1455 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1456 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1457 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1458 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301459
1460 /* Re-enable SD Clock */
1461 clock = host->clock;
1462 host->clock = 0;
1463 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301464 } else
1465 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301466
Leandro Dorileob8352262007-07-25 23:47:04 +02001467 /*
1468 * Some (ENE) controllers go apeshit on some ios operation,
1469 * signalling timeout and CRC errors even on CMD0. Resetting
1470 * it on each ios seems to solve the problem.
1471 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001472 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001473 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1474
Pierre Ossman5f25a662006-10-04 02:15:39 -07001475 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001476 spin_unlock_irqrestore(&host->lock, flags);
1477}
1478
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001479static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1480{
1481 struct sdhci_host *host = mmc_priv(mmc);
1482
1483 sdhci_runtime_pm_get(host);
1484 sdhci_do_set_ios(host, ios);
1485 sdhci_runtime_pm_put(host);
1486}
1487
1488static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001489{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001490 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001491 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001492
Pierre Ossmand129bce2006-03-24 03:18:17 -08001493 spin_lock_irqsave(&host->lock, flags);
1494
Pierre Ossman1e728592008-04-16 19:13:13 +02001495 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001496 is_readonly = 0;
1497 else if (host->ops->get_ro)
1498 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001499 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001500 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1501 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001502
1503 spin_unlock_irqrestore(&host->lock, flags);
1504
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001505 /* This quirk needs to be replaced by a callback-function later */
1506 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1507 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001508}
1509
Takashi Iwai82b0e232011-04-21 20:26:38 +02001510#define SAMPLE_COUNT 5
1511
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001512static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001513{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001514 int i, ro_count;
1515
Takashi Iwai82b0e232011-04-21 20:26:38 +02001516 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001517 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001518
1519 ro_count = 0;
1520 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001521 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001522 if (++ro_count > SAMPLE_COUNT / 2)
1523 return 1;
1524 }
1525 msleep(30);
1526 }
1527 return 0;
1528}
1529
Adrian Hunter20758b62011-08-29 16:42:12 +03001530static void sdhci_hw_reset(struct mmc_host *mmc)
1531{
1532 struct sdhci_host *host = mmc_priv(mmc);
1533
1534 if (host->ops && host->ops->hw_reset)
1535 host->ops->hw_reset(host);
1536}
1537
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001538static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001539{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001540 struct sdhci_host *host = mmc_priv(mmc);
1541 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001542
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001543 sdhci_runtime_pm_get(host);
1544 ret = sdhci_do_get_ro(host);
1545 sdhci_runtime_pm_put(host);
1546 return ret;
1547}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001548
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001549static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1550{
Pierre Ossman1e728592008-04-16 19:13:13 +02001551 if (host->flags & SDHCI_DEVICE_DEAD)
1552 goto out;
1553
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001554 if (enable)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001555 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1556 else
1557 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1558
1559 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1560 if (host->runtime_suspended)
1561 goto out;
1562
1563 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001564 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1565 else
1566 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001567out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001568 mmiowb();
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001569}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001570
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001571static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1572{
1573 struct sdhci_host *host = mmc_priv(mmc);
1574 unsigned long flags;
1575
1576 spin_lock_irqsave(&host->lock, flags);
1577 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001578 spin_unlock_irqrestore(&host->lock, flags);
1579}
1580
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001581static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1582 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301583{
Arindam Nathf2119df2011-05-05 12:18:57 +05301584 u8 pwr;
1585 u16 clk, ctrl;
1586 u32 present_state;
1587
Arindam Nathf2119df2011-05-05 12:18:57 +05301588 /*
1589 * Signal Voltage Switching is only applicable for Host Controllers
1590 * v3.00 and above.
1591 */
1592 if (host->version < SDHCI_SPEC_300)
1593 return 0;
1594
1595 /*
1596 * We first check whether the request is to set signalling voltage
1597 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1598 */
1599 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1600 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1601 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1602 ctrl &= ~SDHCI_CTRL_VDD_180;
1603 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1604
1605 /* Wait for 5ms */
1606 usleep_range(5000, 5500);
1607
1608 /* 3.3V regulator output should be stable within 5 ms */
1609 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1610 if (!(ctrl & SDHCI_CTRL_VDD_180))
1611 return 0;
1612 else {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301613 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301614 "signalling voltage failed\n");
1615 return -EIO;
1616 }
1617 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1618 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1619 /* Stop SDCLK */
1620 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1621 clk &= ~SDHCI_CLOCK_CARD_EN;
1622 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1623
1624 /* Check whether DAT[3:0] is 0000 */
1625 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1626 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1627 SDHCI_DATA_LVL_SHIFT)) {
1628 /*
1629 * Enable 1.8V Signal Enable in the Host Control2
1630 * register
1631 */
1632 ctrl |= SDHCI_CTRL_VDD_180;
1633 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1634
1635 /* Wait for 5ms */
1636 usleep_range(5000, 5500);
1637
1638 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1639 if (ctrl & SDHCI_CTRL_VDD_180) {
1640 /* Provide SDCLK again and wait for 1ms*/
1641 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1642 clk |= SDHCI_CLOCK_CARD_EN;
1643 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1644 usleep_range(1000, 1500);
1645
1646 /*
1647 * If DAT[3:0] level is 1111b, then the card
1648 * was successfully switched to 1.8V signaling.
1649 */
1650 present_state = sdhci_readl(host,
1651 SDHCI_PRESENT_STATE);
1652 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1653 SDHCI_DATA_LVL_MASK)
1654 return 0;
1655 }
1656 }
1657
1658 /*
1659 * If we are here, that means the switch to 1.8V signaling
1660 * failed. We power cycle the card, and retry initialization
1661 * sequence by setting S18R to 0.
1662 */
1663 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1664 pwr &= ~SDHCI_POWER_ON;
1665 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1666
1667 /* Wait for 1ms as per the spec */
1668 usleep_range(1000, 1500);
1669 pwr |= SDHCI_POWER_ON;
1670 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1671
Girish K Sa3c76eb2011-10-11 11:44:09 +05301672 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301673 "voltage failed, retrying with S18R set to 0\n");
1674 return -EAGAIN;
1675 } else
1676 /* No signal voltage switch required */
1677 return 0;
1678}
1679
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001680static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1681 struct mmc_ios *ios)
1682{
1683 struct sdhci_host *host = mmc_priv(mmc);
1684 int err;
1685
1686 if (host->version < SDHCI_SPEC_300)
1687 return 0;
1688 sdhci_runtime_pm_get(host);
1689 err = sdhci_do_start_signal_voltage_switch(host, ios);
1690 sdhci_runtime_pm_put(host);
1691 return err;
1692}
1693
Girish K S069c9f12012-01-06 09:56:39 +05301694static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301695{
1696 struct sdhci_host *host;
1697 u16 ctrl;
1698 u32 ier;
1699 int tuning_loop_counter = MAX_TUNING_LOOP;
1700 unsigned long timeout;
1701 int err = 0;
Girish K S069c9f12012-01-06 09:56:39 +05301702 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301703
1704 host = mmc_priv(mmc);
1705
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001706 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301707 disable_irq(host->irq);
1708 spin_lock(&host->lock);
1709
1710 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1711
1712 /*
Girish K S069c9f12012-01-06 09:56:39 +05301713 * The Host Controller needs tuning only in case of SDR104 mode
1714 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301715 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301716 * If the Host Controller supports the HS200 mode then the
1717 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301718 */
Girish K S069c9f12012-01-06 09:56:39 +05301719 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1720 (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1721 host->flags & SDHCI_HS200_NEEDS_TUNING))
1722 requires_tuning_nonuhs = true;
1723
Arindam Nathb513ea22011-05-05 12:19:04 +05301724 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S069c9f12012-01-06 09:56:39 +05301725 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301726 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1727 else {
1728 spin_unlock(&host->lock);
1729 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001730 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301731 return 0;
1732 }
1733
1734 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1735
1736 /*
1737 * As per the Host Controller spec v3.00, tuning command
1738 * generates Buffer Read Ready interrupt, so enable that.
1739 *
1740 * Note: The spec clearly says that when tuning sequence
1741 * is being performed, the controller does not generate
1742 * interrupts other than Buffer Read Ready interrupt. But
1743 * to make sure we don't hit a controller bug, we _only_
1744 * enable Buffer Read Ready interrupt here.
1745 */
1746 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1747 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1748
1749 /*
1750 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1751 * of loops reaches 40 times or a timeout of 150ms occurs.
1752 */
1753 timeout = 150;
1754 do {
1755 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001756 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301757
1758 if (!tuning_loop_counter && !timeout)
1759 break;
1760
Girish K S069c9f12012-01-06 09:56:39 +05301761 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301762 cmd.arg = 0;
1763 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1764 cmd.retries = 0;
1765 cmd.data = NULL;
1766 cmd.error = 0;
1767
1768 mrq.cmd = &cmd;
1769 host->mrq = &mrq;
1770
1771 /*
1772 * In response to CMD19, the card sends 64 bytes of tuning
1773 * block to the Host Controller. So we set the block size
1774 * to 64 here.
1775 */
Girish K S069c9f12012-01-06 09:56:39 +05301776 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1777 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1778 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1779 SDHCI_BLOCK_SIZE);
1780 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1781 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1782 SDHCI_BLOCK_SIZE);
1783 } else {
1784 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1785 SDHCI_BLOCK_SIZE);
1786 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301787
1788 /*
1789 * The tuning block is sent by the card to the host controller.
1790 * So we set the TRNS_READ bit in the Transfer Mode register.
1791 * This also takes care of setting DMA Enable and Multi Block
1792 * Select in the same register to 0.
1793 */
1794 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1795
1796 sdhci_send_command(host, &cmd);
1797
1798 host->cmd = NULL;
1799 host->mrq = NULL;
1800
1801 spin_unlock(&host->lock);
1802 enable_irq(host->irq);
1803
1804 /* Wait for Buffer Read Ready interrupt */
1805 wait_event_interruptible_timeout(host->buf_ready_int,
1806 (host->tuning_done == 1),
1807 msecs_to_jiffies(50));
1808 disable_irq(host->irq);
1809 spin_lock(&host->lock);
1810
1811 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301812 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301813 "Buffer Read Ready interrupt during tuning "
1814 "procedure, falling back to fixed sampling "
1815 "clock\n");
1816 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1817 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1818 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1819 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1820
1821 err = -EIO;
1822 goto out;
1823 }
1824
1825 host->tuning_done = 0;
1826
1827 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1828 tuning_loop_counter--;
1829 timeout--;
1830 mdelay(1);
1831 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1832
1833 /*
1834 * The Host Driver has exhausted the maximum number of loops allowed,
1835 * so use fixed sampling frequency.
1836 */
1837 if (!tuning_loop_counter || !timeout) {
1838 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1839 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1840 } else {
1841 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301842 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301843 " failed, falling back to fixed sampling"
1844 " clock\n");
1845 err = -EIO;
1846 }
1847 }
1848
1849out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301850 /*
1851 * If this is the very first time we are here, we start the retuning
1852 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1853 * flag won't be set, we check this condition before actually starting
1854 * the timer.
1855 */
1856 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1857 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
1858 mod_timer(&host->tuning_timer, jiffies +
1859 host->tuning_count * HZ);
1860 /* Tuning mode 1 limits the maximum data length to 4MB */
1861 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1862 } else {
1863 host->flags &= ~SDHCI_NEEDS_RETUNING;
1864 /* Reload the new initial value for timer */
1865 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1866 mod_timer(&host->tuning_timer, jiffies +
1867 host->tuning_count * HZ);
1868 }
1869
1870 /*
1871 * In case tuning fails, host controllers which support re-tuning can
1872 * try tuning again at a later time, when the re-tuning timer expires.
1873 * So for these controllers, we return 0. Since there might be other
1874 * controllers who do not have this capability, we return error for
1875 * them.
1876 */
1877 if (err && host->tuning_count &&
1878 host->tuning_mode == SDHCI_TUNING_MODE_1)
1879 err = 0;
1880
Arindam Nathb513ea22011-05-05 12:19:04 +05301881 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1882 spin_unlock(&host->lock);
1883 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001884 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301885
1886 return err;
1887}
1888
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001889static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301890{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301891 u16 ctrl;
1892 unsigned long flags;
1893
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301894 /* Host Controller v3.00 defines preset value registers */
1895 if (host->version < SDHCI_SPEC_300)
1896 return;
1897
1898 spin_lock_irqsave(&host->lock, flags);
1899
1900 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1901
1902 /*
1903 * We only enable or disable Preset Value if they are not already
1904 * enabled or disabled respectively. Otherwise, we bail out.
1905 */
1906 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1907 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1908 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001909 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301910 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1911 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1912 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001913 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301914 }
1915
1916 spin_unlock_irqrestore(&host->lock, flags);
1917}
1918
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001919static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1920{
1921 struct sdhci_host *host = mmc_priv(mmc);
1922
1923 sdhci_runtime_pm_get(host);
1924 sdhci_do_enable_preset_value(host, enable);
1925 sdhci_runtime_pm_put(host);
1926}
1927
David Brownellab7aefd2006-11-12 17:55:30 -08001928static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001929 .request = sdhci_request,
1930 .set_ios = sdhci_set_ios,
1931 .get_ro = sdhci_get_ro,
Adrian Hunter20758b62011-08-29 16:42:12 +03001932 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001933 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05301934 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05301935 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301936 .enable_preset_value = sdhci_enable_preset_value,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001937};
1938
1939/*****************************************************************************\
1940 * *
1941 * Tasklets *
1942 * *
1943\*****************************************************************************/
1944
1945static void sdhci_tasklet_card(unsigned long param)
1946{
1947 struct sdhci_host *host;
1948 unsigned long flags;
1949
1950 host = (struct sdhci_host*)param;
1951
1952 spin_lock_irqsave(&host->lock, flags);
1953
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001954 /* Check host->mrq first in case we are runtime suspended */
1955 if (host->mrq &&
1956 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301957 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001958 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301959 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001960 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001961
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001962 sdhci_reset(host, SDHCI_RESET_CMD);
1963 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001964
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001965 host->mrq->cmd->error = -ENOMEDIUM;
1966 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001967 }
1968
1969 spin_unlock_irqrestore(&host->lock, flags);
1970
Pierre Ossman04cf5852008-08-18 22:18:14 +02001971 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001972}
1973
1974static void sdhci_tasklet_finish(unsigned long param)
1975{
1976 struct sdhci_host *host;
1977 unsigned long flags;
1978 struct mmc_request *mrq;
1979
1980 host = (struct sdhci_host*)param;
1981
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001982 spin_lock_irqsave(&host->lock, flags);
1983
Chris Ball0c9c99a2011-04-27 17:35:31 -04001984 /*
1985 * If this tasklet gets rescheduled while running, it will
1986 * be run again afterwards but without any active request.
1987 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001988 if (!host->mrq) {
1989 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04001990 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001991 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001992
1993 del_timer(&host->timer);
1994
1995 mrq = host->mrq;
1996
Pierre Ossmand129bce2006-03-24 03:18:17 -08001997 /*
1998 * The controller needs a reset of internal state machines
1999 * upon error conditions.
2000 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002001 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002002 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002003 (mrq->data && (mrq->data->error ||
2004 (mrq->data->stop && mrq->data->stop->error))) ||
2005 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002006
2007 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002008 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002009 unsigned int clock;
2010
2011 /* This is to force an update */
2012 clock = host->clock;
2013 host->clock = 0;
2014 sdhci_set_clock(host, clock);
2015 }
2016
2017 /* Spec says we should do both at the same time, but Ricoh
2018 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002019 sdhci_reset(host, SDHCI_RESET_CMD);
2020 sdhci_reset(host, SDHCI_RESET_DATA);
2021 }
2022
2023 host->mrq = NULL;
2024 host->cmd = NULL;
2025 host->data = NULL;
2026
Pierre Ossmanf9134312008-12-21 17:01:48 +01002027#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002028 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002029#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002030
Pierre Ossman5f25a662006-10-04 02:15:39 -07002031 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002032 spin_unlock_irqrestore(&host->lock, flags);
2033
2034 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002035 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002036}
2037
2038static void sdhci_timeout_timer(unsigned long data)
2039{
2040 struct sdhci_host *host;
2041 unsigned long flags;
2042
2043 host = (struct sdhci_host*)data;
2044
2045 spin_lock_irqsave(&host->lock, flags);
2046
2047 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302048 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002049 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002050 sdhci_dumpregs(host);
2051
2052 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002053 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002054 sdhci_finish_data(host);
2055 } else {
2056 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002057 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002058 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002059 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002060
2061 tasklet_schedule(&host->finish_tasklet);
2062 }
2063 }
2064
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
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302069static void sdhci_tuning_timer(unsigned long data)
2070{
2071 struct sdhci_host *host;
2072 unsigned long flags;
2073
2074 host = (struct sdhci_host *)data;
2075
2076 spin_lock_irqsave(&host->lock, flags);
2077
2078 host->flags |= SDHCI_NEEDS_RETUNING;
2079
2080 spin_unlock_irqrestore(&host->lock, flags);
2081}
2082
Pierre Ossmand129bce2006-03-24 03:18:17 -08002083/*****************************************************************************\
2084 * *
2085 * Interrupt handling *
2086 * *
2087\*****************************************************************************/
2088
2089static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2090{
2091 BUG_ON(intmask == 0);
2092
2093 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302094 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002095 "though no command operation was in progress.\n",
2096 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002097 sdhci_dumpregs(host);
2098 return;
2099 }
2100
Pierre Ossman43b58b32007-07-25 23:15:27 +02002101 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002102 host->cmd->error = -ETIMEDOUT;
2103 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2104 SDHCI_INT_INDEX))
2105 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002106
Pierre Ossmane8095172008-07-25 01:09:08 +02002107 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002108 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002109 return;
2110 }
2111
2112 /*
2113 * The host can send and interrupt when the busy state has
2114 * ended, allowing us to wait without wasting CPU cycles.
2115 * Unfortunately this is overloaded on the "data complete"
2116 * interrupt, so we need to take some care when handling
2117 * it.
2118 *
2119 * Note: The 1.0 specification is a bit ambiguous about this
2120 * feature so there might be some problems with older
2121 * controllers.
2122 */
2123 if (host->cmd->flags & MMC_RSP_BUSY) {
2124 if (host->cmd->data)
2125 DBG("Cannot wait for busy signal when also "
2126 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002127 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002128 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002129
2130 /* The controller does not support the end-of-busy IRQ,
2131 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002132 }
2133
2134 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002135 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002136}
2137
George G. Davis0957c332010-02-18 12:32:12 -05002138#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002139static void sdhci_show_adma_error(struct sdhci_host *host)
2140{
2141 const char *name = mmc_hostname(host->mmc);
2142 u8 *desc = host->adma_desc;
2143 __le32 *dma;
2144 __le16 *len;
2145 u8 attr;
2146
2147 sdhci_dumpregs(host);
2148
2149 while (true) {
2150 dma = (__le32 *)(desc + 4);
2151 len = (__le16 *)(desc + 2);
2152 attr = *desc;
2153
2154 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2155 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2156
2157 desc += 8;
2158
2159 if (attr & 2)
2160 break;
2161 }
2162}
2163#else
2164static void sdhci_show_adma_error(struct sdhci_host *host) { }
2165#endif
2166
Pierre Ossmand129bce2006-03-24 03:18:17 -08002167static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2168{
Girish K S069c9f12012-01-06 09:56:39 +05302169 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002170 BUG_ON(intmask == 0);
2171
Arindam Nathb513ea22011-05-05 12:19:04 +05302172 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2173 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302174 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2175 if (command == MMC_SEND_TUNING_BLOCK ||
2176 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302177 host->tuning_done = 1;
2178 wake_up(&host->buf_ready_int);
2179 return;
2180 }
2181 }
2182
Pierre Ossmand129bce2006-03-24 03:18:17 -08002183 if (!host->data) {
2184 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002185 * The "data complete" interrupt is also used to
2186 * indicate that a busy state has ended. See comment
2187 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002188 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002189 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2190 if (intmask & SDHCI_INT_DATA_END) {
2191 sdhci_finish_command(host);
2192 return;
2193 }
2194 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002195
Girish K Sa3c76eb2011-10-11 11:44:09 +05302196 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002197 "though no data operation was in progress.\n",
2198 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002199 sdhci_dumpregs(host);
2200
2201 return;
2202 }
2203
2204 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002205 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002206 else if (intmask & SDHCI_INT_DATA_END_BIT)
2207 host->data->error = -EILSEQ;
2208 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2209 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2210 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002211 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002212 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302213 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002214 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002215 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002216 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002217
Pierre Ossman17b04292007-07-22 22:18:46 +02002218 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002219 sdhci_finish_data(host);
2220 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002221 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002222 sdhci_transfer_pio(host);
2223
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002224 /*
2225 * We currently don't do anything fancy with DMA
2226 * boundaries, but as we can't disable the feature
2227 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002228 *
2229 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2230 * should return a valid address to continue from, but as
2231 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002232 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002233 if (intmask & SDHCI_INT_DMA_END) {
2234 u32 dmastart, dmanow;
2235 dmastart = sg_dma_address(host->data->sg);
2236 dmanow = dmastart + host->data->bytes_xfered;
2237 /*
2238 * Force update to the next DMA block boundary.
2239 */
2240 dmanow = (dmanow &
2241 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2242 SDHCI_DEFAULT_BOUNDARY_SIZE;
2243 host->data->bytes_xfered = dmanow - dmastart;
2244 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2245 " next 0x%08x\n",
2246 mmc_hostname(host->mmc), dmastart,
2247 host->data->bytes_xfered, dmanow);
2248 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2249 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002250
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002251 if (intmask & SDHCI_INT_DATA_END) {
2252 if (host->cmd) {
2253 /*
2254 * Data managed to finish before the
2255 * command completed. Make sure we do
2256 * things in the proper order.
2257 */
2258 host->data_early = 1;
2259 } else {
2260 sdhci_finish_data(host);
2261 }
2262 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002263 }
2264}
2265
David Howells7d12e782006-10-05 14:55:46 +01002266static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002267{
2268 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002269 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002270 u32 intmask, unexpected = 0;
2271 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002272
2273 spin_lock(&host->lock);
2274
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002275 if (host->runtime_suspended) {
2276 spin_unlock(&host->lock);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302277 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002278 mmc_hostname(host->mmc));
2279 return IRQ_HANDLED;
2280 }
2281
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002282 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002283
Mark Lord62df67a52007-03-06 13:30:13 +01002284 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002285 result = IRQ_NONE;
2286 goto out;
2287 }
2288
Alexander Stein6379b232012-03-14 09:52:10 +01002289again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002290 DBG("*** %s got interrupt: 0x%08x\n",
2291 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002292
Pierre Ossman3192a282006-06-30 02:22:26 -07002293 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Shawn Guod25928d2011-06-21 22:41:48 +08002294 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2295 SDHCI_CARD_PRESENT;
2296
2297 /*
2298 * There is a observation on i.mx esdhc. INSERT bit will be
2299 * immediately set again when it gets cleared, if a card is
2300 * inserted. We have to mask the irq to prevent interrupt
2301 * storm which will freeze the system. And the REMOVE gets
2302 * the same situation.
2303 *
2304 * More testing are needed here to ensure it works for other
2305 * platforms though.
2306 */
2307 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2308 SDHCI_INT_CARD_REMOVE);
2309 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2310 SDHCI_INT_CARD_INSERT);
2311
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002312 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Shawn Guod25928d2011-06-21 22:41:48 +08002313 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2314 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002315 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002316 }
2317
Pierre Ossmand129bce2006-03-24 03:18:17 -08002318 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002319 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2320 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002321 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002322 }
2323
2324 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002325 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2326 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002327 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002328 }
2329
2330 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2331
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002332 intmask &= ~SDHCI_INT_ERROR;
2333
Pierre Ossmand129bce2006-03-24 03:18:17 -08002334 if (intmask & SDHCI_INT_BUS_POWER) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302335 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002336 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002337 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002338 }
2339
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002340 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002341
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002342 if (intmask & SDHCI_INT_CARD_INT)
2343 cardint = 1;
2344
2345 intmask &= ~SDHCI_INT_CARD_INT;
2346
Pierre Ossman3192a282006-06-30 02:22:26 -07002347 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002348 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002349 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002350 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002351
2352 result = IRQ_HANDLED;
2353
Alexander Stein6379b232012-03-14 09:52:10 +01002354 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2355 if (intmask && --max_loops)
2356 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002357out:
2358 spin_unlock(&host->lock);
2359
Alexander Stein6379b232012-03-14 09:52:10 +01002360 if (unexpected) {
2361 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2362 mmc_hostname(host->mmc), unexpected);
2363 sdhci_dumpregs(host);
2364 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002365 /*
2366 * We have to delay this as it calls back into the driver.
2367 */
2368 if (cardint)
2369 mmc_signal_sdio_irq(host->mmc);
2370
Pierre Ossmand129bce2006-03-24 03:18:17 -08002371 return result;
2372}
2373
2374/*****************************************************************************\
2375 * *
2376 * Suspend/resume *
2377 * *
2378\*****************************************************************************/
2379
2380#ifdef CONFIG_PM
2381
Manuel Lauss29495aa2011-11-03 11:09:45 +01002382int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002383{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002384 int ret;
Aaron Lu38a60ea2012-01-04 10:07:43 +08002385 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002386
Chris Balla1b13b42012-02-06 00:43:59 -05002387 if (host->ops->platform_suspend)
2388 host->ops->platform_suspend(host);
2389
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002390 sdhci_disable_card_detection(host);
2391
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302392 /* Disable tuning since we are suspending */
Aaron Lu38a60ea2012-01-04 10:07:43 +08002393 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2394 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2395 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002396 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302397 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302398 }
2399
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002400 ret = mmc_suspend_host(host->mmc);
Aaron Lu38a60ea2012-01-04 10:07:43 +08002401 if (ret) {
2402 if (has_tuning_timer) {
2403 host->flags |= SDHCI_NEEDS_RETUNING;
2404 mod_timer(&host->tuning_timer, jiffies +
2405 host->tuning_count * HZ);
2406 }
2407
2408 sdhci_enable_card_detection(host);
2409
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002410 return ret;
Aaron Lu38a60ea2012-01-04 10:07:43 +08002411 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002412
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002413 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002414
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002415 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002416}
2417
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002418EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002419
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002420int sdhci_resume_host(struct sdhci_host *host)
2421{
2422 int ret;
2423
Richard Röjforsa13abc72009-09-22 16:45:30 -07002424 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002425 if (host->ops->enable_dma)
2426 host->ops->enable_dma(host);
2427 }
2428
2429 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2430 mmc_hostname(host->mmc), host);
2431 if (ret)
2432 return ret;
2433
Adrian Hunter6308d292012-02-07 14:48:54 +02002434 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2435 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2436 /* Card keeps power but host controller does not */
2437 sdhci_init(host, 0);
2438 host->pwr = 0;
2439 host->clock = 0;
2440 sdhci_do_set_ios(host, &host->mmc->ios);
2441 } else {
2442 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2443 mmiowb();
2444 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002445
2446 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002447 sdhci_enable_card_detection(host);
2448
Chris Balla1b13b42012-02-06 00:43:59 -05002449 if (host->ops->platform_resume)
2450 host->ops->platform_resume(host);
2451
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302452 /* Set the re-tuning expiration flag */
2453 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2454 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2455 host->flags |= SDHCI_NEEDS_RETUNING;
2456
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002457 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002458}
2459
2460EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002461
Daniel Drake5f619702010-11-04 22:20:39 +00002462void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2463{
2464 u8 val;
2465 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2466 val |= SDHCI_WAKE_ON_INT;
2467 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2468}
2469
2470EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2471
Pierre Ossmand129bce2006-03-24 03:18:17 -08002472#endif /* CONFIG_PM */
2473
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002474#ifdef CONFIG_PM_RUNTIME
2475
2476static int sdhci_runtime_pm_get(struct sdhci_host *host)
2477{
2478 return pm_runtime_get_sync(host->mmc->parent);
2479}
2480
2481static int sdhci_runtime_pm_put(struct sdhci_host *host)
2482{
2483 pm_runtime_mark_last_busy(host->mmc->parent);
2484 return pm_runtime_put_autosuspend(host->mmc->parent);
2485}
2486
2487int sdhci_runtime_suspend_host(struct sdhci_host *host)
2488{
2489 unsigned long flags;
2490 int ret = 0;
2491
2492 /* Disable tuning since we are suspending */
2493 if (host->version >= SDHCI_SPEC_300 &&
2494 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2495 del_timer_sync(&host->tuning_timer);
2496 host->flags &= ~SDHCI_NEEDS_RETUNING;
2497 }
2498
2499 spin_lock_irqsave(&host->lock, flags);
2500 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2501 spin_unlock_irqrestore(&host->lock, flags);
2502
2503 synchronize_irq(host->irq);
2504
2505 spin_lock_irqsave(&host->lock, flags);
2506 host->runtime_suspended = true;
2507 spin_unlock_irqrestore(&host->lock, flags);
2508
2509 return ret;
2510}
2511EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2512
2513int sdhci_runtime_resume_host(struct sdhci_host *host)
2514{
2515 unsigned long flags;
2516 int ret = 0, host_flags = host->flags;
2517
2518 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2519 if (host->ops->enable_dma)
2520 host->ops->enable_dma(host);
2521 }
2522
2523 sdhci_init(host, 0);
2524
2525 /* Force clock and power re-program */
2526 host->pwr = 0;
2527 host->clock = 0;
2528 sdhci_do_set_ios(host, &host->mmc->ios);
2529
2530 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2531 if (host_flags & SDHCI_PV_ENABLED)
2532 sdhci_do_enable_preset_value(host, true);
2533
2534 /* Set the re-tuning expiration flag */
2535 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2536 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2537 host->flags |= SDHCI_NEEDS_RETUNING;
2538
2539 spin_lock_irqsave(&host->lock, flags);
2540
2541 host->runtime_suspended = false;
2542
2543 /* Enable SDIO IRQ */
2544 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2545 sdhci_enable_sdio_irq_nolock(host, true);
2546
2547 /* Enable Card Detection */
2548 sdhci_enable_card_detection(host);
2549
2550 spin_unlock_irqrestore(&host->lock, flags);
2551
2552 return ret;
2553}
2554EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2555
2556#endif
2557
Pierre Ossmand129bce2006-03-24 03:18:17 -08002558/*****************************************************************************\
2559 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002560 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002561 * *
2562\*****************************************************************************/
2563
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002564struct sdhci_host *sdhci_alloc_host(struct device *dev,
2565 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002566{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002567 struct mmc_host *mmc;
2568 struct sdhci_host *host;
2569
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002570 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002571
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002572 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002573 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002574 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002575
2576 host = mmc_priv(mmc);
2577 host->mmc = mmc;
2578
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002579 return host;
2580}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002581
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002582EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002583
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002584int sdhci_add_host(struct sdhci_host *host)
2585{
2586 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302587 u32 caps[2];
2588 u32 max_current_caps;
2589 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002590 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002591
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002592 WARN_ON(host == NULL);
2593 if (host == NULL)
2594 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002595
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002596 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002597
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002598 if (debug_quirks)
2599 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002600 if (debug_quirks2)
2601 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002602
Pierre Ossmand96649e2006-06-30 02:22:30 -07002603 sdhci_reset(host, SDHCI_RESET_ALL);
2604
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002605 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002606 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2607 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002608 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302609 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002610 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002611 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002612 }
2613
Arindam Nathf2119df2011-05-05 12:18:57 +05302614 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002615 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002616
Arindam Nathf2119df2011-05-05 12:18:57 +05302617 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2618 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2619
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002620 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002621 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302622 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002623 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002624 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002625 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002626
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002627 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002628 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002629 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002630 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002631 }
2632
Arindam Nathf2119df2011-05-05 12:18:57 +05302633 if ((host->version >= SDHCI_SPEC_200) &&
2634 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002635 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002636
2637 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2638 (host->flags & SDHCI_USE_ADMA)) {
2639 DBG("Disabling ADMA as it is marked broken\n");
2640 host->flags &= ~SDHCI_USE_ADMA;
2641 }
2642
Richard Röjforsa13abc72009-09-22 16:45:30 -07002643 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002644 if (host->ops->enable_dma) {
2645 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302646 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002647 "available. Falling back to PIO.\n",
2648 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002649 host->flags &=
2650 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002651 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002652 }
2653 }
2654
Pierre Ossman2134a922008-06-28 18:28:51 +02002655 if (host->flags & SDHCI_USE_ADMA) {
2656 /*
2657 * We need to allocate descriptors for all sg entries
2658 * (128) and potentially one alignment transfer for
2659 * each of those entries.
2660 */
2661 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2662 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2663 if (!host->adma_desc || !host->align_buffer) {
2664 kfree(host->adma_desc);
2665 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302666 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002667 "buffers. Falling back to standard DMA.\n",
2668 mmc_hostname(mmc));
2669 host->flags &= ~SDHCI_USE_ADMA;
2670 }
2671 }
2672
Pierre Ossman76591502008-07-21 00:32:11 +02002673 /*
2674 * If we use DMA, then it's up to the caller to set the DMA
2675 * mask, but PIO does not need the hw shim so we set a new
2676 * mask here in that case.
2677 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002678 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002679 host->dma_mask = DMA_BIT_MASK(64);
2680 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2681 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002682
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002683 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302684 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002685 >> SDHCI_CLOCK_BASE_SHIFT;
2686 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302687 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002688 >> SDHCI_CLOCK_BASE_SHIFT;
2689
Pierre Ossmand129bce2006-03-24 03:18:17 -08002690 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002691 if (host->max_clk == 0 || host->quirks &
2692 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002693 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302694 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002695 "frequency.\n", mmc_hostname(mmc));
2696 return -ENODEV;
2697 }
2698 host->max_clk = host->ops->get_max_clock(host);
2699 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002700
2701 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302702 * In case of Host Controller v3.00, find out whether clock
2703 * multiplier is supported.
2704 */
2705 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2706 SDHCI_CLOCK_MUL_SHIFT;
2707
2708 /*
2709 * In case the value in Clock Multiplier is 0, then programmable
2710 * clock mode is not supported, otherwise the actual clock
2711 * multiplier is one more than the value of Clock Multiplier
2712 * in the Capabilities Register.
2713 */
2714 if (host->clk_mul)
2715 host->clk_mul += 1;
2716
2717 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002718 * Set host parameters.
2719 */
2720 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302721 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002722 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002723 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302724 else if (host->version >= SDHCI_SPEC_300) {
2725 if (host->clk_mul) {
2726 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2727 mmc->f_max = host->max_clk * host->clk_mul;
2728 } else
2729 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2730 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002731 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002732
Andy Shevchenko272308c2011-08-03 18:36:00 +03002733 host->timeout_clk =
2734 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2735 if (host->timeout_clk == 0) {
2736 if (host->ops->get_timeout_clock) {
2737 host->timeout_clk = host->ops->get_timeout_clock(host);
2738 } else if (!(host->quirks &
2739 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302740 pr_err("%s: Hardware doesn't specify timeout clock "
Andy Shevchenko272308c2011-08-03 18:36:00 +03002741 "frequency.\n", mmc_hostname(mmc));
2742 return -ENODEV;
2743 }
2744 }
2745 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2746 host->timeout_clk *= 1000;
2747
2748 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002749 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002750
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002751 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002752
Andrei Warkentine89d4562011-05-23 15:06:37 -05002753 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2754
2755 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2756 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002757
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002758 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002759 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002760 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002761 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002762 host->flags |= SDHCI_AUTO_CMD23;
2763 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2764 } else {
2765 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2766 }
2767
Philip Rakity15ec4462010-11-19 16:48:39 -05002768 /*
2769 * A controller may support 8-bit width, but the board itself
2770 * might not have the pins brought out. Boards that support
2771 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2772 * their platform code before calling sdhci_add_host(), and we
2773 * won't assume 8-bit width for hosts without that CAP.
2774 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002775 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002776 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002777
Arindam Nathf2119df2011-05-05 12:18:57 +05302778 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002779 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002780
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002781 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2782 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002783 mmc->caps |= MMC_CAP_NEEDS_POLL;
2784
Al Cooper4188bba2012-03-16 15:54:17 -04002785 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
2786 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2787 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05302788 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2789
2790 /* SDR104 supports also implies SDR50 support */
2791 if (caps[1] & SDHCI_SUPPORT_SDR104)
2792 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2793 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2794 mmc->caps |= MMC_CAP_UHS_SDR50;
2795
2796 if (caps[1] & SDHCI_SUPPORT_DDR50)
2797 mmc->caps |= MMC_CAP_UHS_DDR50;
2798
Girish K S069c9f12012-01-06 09:56:39 +05302799 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05302800 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2801 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2802
Girish K S069c9f12012-01-06 09:56:39 +05302803 /* Does the host need tuning for HS200? */
2804 if (mmc->caps2 & MMC_CAP2_HS200)
2805 host->flags |= SDHCI_HS200_NEEDS_TUNING;
2806
Arindam Nathd6d50a12011-05-05 12:18:59 +05302807 /* Driver Type(s) (A, C, D) supported by the host */
2808 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2809 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2810 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2811 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2812 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2813 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2814
Girish K Sbec87262011-10-13 12:04:16 +05302815 /*
2816 * If Power Off Notify capability is enabled by the host,
2817 * set notify to short power off notify timeout value.
2818 */
2819 if (mmc->caps2 & MMC_CAP2_POWEROFF_NOTIFY)
2820 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
2821 else
2822 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_NONE;
2823
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302824 /* Initial value for re-tuning timer count */
2825 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2826 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2827
2828 /*
2829 * In case Re-tuning Timer is not disabled, the actual value of
2830 * re-tuning timer will be 2 ^ (n - 1).
2831 */
2832 if (host->tuning_count)
2833 host->tuning_count = 1 << (host->tuning_count - 1);
2834
2835 /* Re-tuning mode supported by the Host Controller */
2836 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2837 SDHCI_RETUNING_MODE_SHIFT;
2838
Takashi Iwai8f230f42010-12-08 10:04:30 +01002839 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07002840
2841 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2842 if (IS_ERR(host->vmmc)) {
2843 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
2844 host->vmmc = NULL;
2845 }
2846
Arindam Nathf2119df2011-05-05 12:18:57 +05302847 /*
2848 * According to SD Host Controller spec v3.00, if the Host System
2849 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2850 * the value is meaningful only if Voltage Support in the Capabilities
2851 * register is set. The actual current value is 4 times the register
2852 * value.
2853 */
2854 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Philip Rakitybad37e12012-05-27 18:36:44 -07002855 if (!max_current_caps && host->vmmc) {
2856 u32 curr = regulator_get_current_limit(host->vmmc);
2857 if (curr > 0) {
2858
2859 /* convert to SDHCI_MAX_CURRENT format */
2860 curr = curr/1000; /* convert to mA */
2861 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
2862
2863 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
2864 max_current_caps =
2865 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
2866 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
2867 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
2868 }
2869 }
Arindam Nathf2119df2011-05-05 12:18:57 +05302870
2871 if (caps[0] & SDHCI_CAN_VDD_330) {
2872 int max_current_330;
2873
Takashi Iwai8f230f42010-12-08 10:04:30 +01002874 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302875
2876 max_current_330 = ((max_current_caps &
2877 SDHCI_MAX_CURRENT_330_MASK) >>
2878 SDHCI_MAX_CURRENT_330_SHIFT) *
2879 SDHCI_MAX_CURRENT_MULTIPLIER;
2880
2881 if (max_current_330 > 150)
2882 mmc->caps |= MMC_CAP_SET_XPC_330;
2883 }
2884 if (caps[0] & SDHCI_CAN_VDD_300) {
2885 int max_current_300;
2886
Takashi Iwai8f230f42010-12-08 10:04:30 +01002887 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302888
2889 max_current_300 = ((max_current_caps &
2890 SDHCI_MAX_CURRENT_300_MASK) >>
2891 SDHCI_MAX_CURRENT_300_SHIFT) *
2892 SDHCI_MAX_CURRENT_MULTIPLIER;
2893
2894 if (max_current_300 > 150)
2895 mmc->caps |= MMC_CAP_SET_XPC_300;
2896 }
2897 if (caps[0] & SDHCI_CAN_VDD_180) {
2898 int max_current_180;
2899
Takashi Iwai8f230f42010-12-08 10:04:30 +01002900 ocr_avail |= MMC_VDD_165_195;
2901
Arindam Nathf2119df2011-05-05 12:18:57 +05302902 max_current_180 = ((max_current_caps &
2903 SDHCI_MAX_CURRENT_180_MASK) >>
2904 SDHCI_MAX_CURRENT_180_SHIFT) *
2905 SDHCI_MAX_CURRENT_MULTIPLIER;
2906
2907 if (max_current_180 > 150)
2908 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05302909
2910 /* Maximum current capabilities of the host at 1.8V */
2911 if (max_current_180 >= 800)
2912 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2913 else if (max_current_180 >= 600)
2914 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2915 else if (max_current_180 >= 400)
2916 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
Philip Rakity0aa67702012-05-27 18:36:33 -07002917 else if (max_current_180 >= 200)
Arindam Nath5371c922011-05-05 12:19:02 +05302918 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05302919 }
2920
Takashi Iwai8f230f42010-12-08 10:04:30 +01002921 mmc->ocr_avail = ocr_avail;
2922 mmc->ocr_avail_sdio = ocr_avail;
2923 if (host->ocr_avail_sdio)
2924 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2925 mmc->ocr_avail_sd = ocr_avail;
2926 if (host->ocr_avail_sd)
2927 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2928 else /* normal SD controllers don't support 1.8V */
2929 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2930 mmc->ocr_avail_mmc = ocr_avail;
2931 if (host->ocr_avail_mmc)
2932 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07002933
2934 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302935 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002936 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002937 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07002938 }
2939
Pierre Ossmand129bce2006-03-24 03:18:17 -08002940 spin_lock_init(&host->lock);
2941
2942 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02002943 * Maximum number of segments. Depends on if the hardware
2944 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002945 */
Pierre Ossman2134a922008-06-28 18:28:51 +02002946 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002947 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07002948 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002949 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02002950 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002951 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002952
2953 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01002954 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01002955 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08002956 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002957 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002958
2959 /*
2960 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02002961 * of bytes. When doing hardware scatter/gather, each entry cannot
2962 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002963 */
Olof Johansson30652aa2011-01-01 18:37:32 -06002964 if (host->flags & SDHCI_USE_ADMA) {
2965 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2966 mmc->max_seg_size = 65535;
2967 else
2968 mmc->max_seg_size = 65536;
2969 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002970 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002971 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002972
2973 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002974 * Maximum block size. This varies from controller to controller and
2975 * is specified in the capabilities register.
2976 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002977 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2978 mmc->max_blk_size = 2;
2979 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05302980 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03002981 SDHCI_MAX_BLOCK_SHIFT;
2982 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302983 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03002984 "assuming 512 bytes\n", mmc_hostname(mmc));
2985 mmc->max_blk_size = 0;
2986 }
2987 }
2988
2989 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002990
2991 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01002992 * Maximum block count.
2993 */
Ben Dooks1388eef2009-06-14 12:40:53 +01002994 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01002995
2996 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002997 * Init tasklets.
2998 */
2999 tasklet_init(&host->card_tasklet,
3000 sdhci_tasklet_card, (unsigned long)host);
3001 tasklet_init(&host->finish_tasklet,
3002 sdhci_tasklet_finish, (unsigned long)host);
3003
Al Viroe4cad1b2006-10-10 22:47:07 +01003004 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003005
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303006 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303007 init_waitqueue_head(&host->buf_ready_int);
3008
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303009 /* Initialize re-tuning timer */
3010 init_timer(&host->tuning_timer);
3011 host->tuning_timer.data = (unsigned long)host;
3012 host->tuning_timer.function = sdhci_tuning_timer;
3013 }
3014
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003015 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003016 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003017 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003018 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003019
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003020 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003021
3022#ifdef CONFIG_MMC_DEBUG
3023 sdhci_dumpregs(host);
3024#endif
3025
Pierre Ossmanf9134312008-12-21 17:01:48 +01003026#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003027 snprintf(host->led_name, sizeof(host->led_name),
3028 "%s::", mmc_hostname(mmc));
3029 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003030 host->led.brightness = LED_OFF;
3031 host->led.default_trigger = mmc_hostname(mmc);
3032 host->led.brightness_set = sdhci_led_control;
3033
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003034 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003035 if (ret)
3036 goto reset;
3037#endif
3038
Pierre Ossman5f25a662006-10-04 02:15:39 -07003039 mmiowb();
3040
Pierre Ossmand129bce2006-03-24 03:18:17 -08003041 mmc_add_host(mmc);
3042
Girish K Sa3c76eb2011-10-11 11:44:09 +05303043 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003044 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003045 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3046 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003047
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003048 sdhci_enable_card_detection(host);
3049
Pierre Ossmand129bce2006-03-24 03:18:17 -08003050 return 0;
3051
Pierre Ossmanf9134312008-12-21 17:01:48 +01003052#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003053reset:
3054 sdhci_reset(host, SDHCI_RESET_ALL);
3055 free_irq(host->irq, host);
3056#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003057untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003058 tasklet_kill(&host->card_tasklet);
3059 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003060
3061 return ret;
3062}
3063
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003064EXPORT_SYMBOL_GPL(sdhci_add_host);
3065
Pierre Ossman1e728592008-04-16 19:13:13 +02003066void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003067{
Pierre Ossman1e728592008-04-16 19:13:13 +02003068 unsigned long flags;
3069
3070 if (dead) {
3071 spin_lock_irqsave(&host->lock, flags);
3072
3073 host->flags |= SDHCI_DEVICE_DEAD;
3074
3075 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303076 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003077 " transfer!\n", mmc_hostname(host->mmc));
3078
3079 host->mrq->cmd->error = -ENOMEDIUM;
3080 tasklet_schedule(&host->finish_tasklet);
3081 }
3082
3083 spin_unlock_irqrestore(&host->lock, flags);
3084 }
3085
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003086 sdhci_disable_card_detection(host);
3087
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003088 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003089
Pierre Ossmanf9134312008-12-21 17:01:48 +01003090#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003091 led_classdev_unregister(&host->led);
3092#endif
3093
Pierre Ossman1e728592008-04-16 19:13:13 +02003094 if (!dead)
3095 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003096
3097 free_irq(host->irq, host);
3098
3099 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303100 if (host->version >= SDHCI_SPEC_300)
3101 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003102
3103 tasklet_kill(&host->card_tasklet);
3104 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003105
Adrian Hunterceb61432011-12-27 15:48:41 +02003106 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003107 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003108
Pierre Ossman2134a922008-06-28 18:28:51 +02003109 kfree(host->adma_desc);
3110 kfree(host->align_buffer);
3111
3112 host->adma_desc = NULL;
3113 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003114}
3115
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003116EXPORT_SYMBOL_GPL(sdhci_remove_host);
3117
3118void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003119{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003120 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003121}
3122
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003123EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003124
3125/*****************************************************************************\
3126 * *
3127 * Driver init/exit *
3128 * *
3129\*****************************************************************************/
3130
3131static int __init sdhci_drv_init(void)
3132{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303133 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003134 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303135 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003136
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003137 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003138}
3139
3140static void __exit sdhci_drv_exit(void)
3141{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003142}
3143
3144module_init(sdhci_drv_init);
3145module_exit(sdhci_drv_exit);
3146
Pierre Ossmandf673b22006-06-30 02:22:31 -07003147module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003148module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003149
Pierre Ossman32710e82009-04-08 20:14:54 +02003150MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003151MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003152MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003153
Pierre Ossmandf673b22006-06-30 02:22:31 -07003154MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003155MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");