blob: 0549b4aaa93843a42f93c62da1738ab1b291c890 [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter50accb92011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080030
Pierre Ossmand129bce2006-03-24 03:18:17 -080031#include "sdhci.h"
32
33#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080034
Pierre Ossmand129bce2006-03-24 03:18:17 -080035#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010036 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmanf9134312008-12-21 17:01:48 +010038#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
39 defined(CONFIG_MMC_SDHCI_MODULE))
40#define SDHCI_USE_LEDS_CLASS
41#endif
42
Arindam Nathb513ea22011-05-05 12:19:04 +053043#define MAX_TUNING_LOOP 40
44
Pierre Ossmandf673b22006-06-30 02:22:31 -070045static unsigned int debug_quirks = 0;
Adrian Hunter50accb92011-10-03 15:33:34 +030046static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070047
Pierre Ossmand129bce2006-03-24 03:18:17 -080048static void sdhci_finish_data(struct sdhci_host *);
49
50static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
51static void sdhci_finish_command(struct sdhci_host *);
Girish K S2cd06dc2012-01-06 09:56:39 +053052static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053053static void sdhci_tuning_timer(unsigned long data);
Pierre Ossmand129bce2006-03-24 03:18:17 -080054
Adrian Hunter50accb92011-10-03 15:33:34 +030055#ifdef CONFIG_PM_RUNTIME
56static int sdhci_runtime_pm_get(struct sdhci_host *host);
57static int sdhci_runtime_pm_put(struct sdhci_host *host);
58#else
59static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
60{
61 return 0;
62}
63static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
64{
65 return 0;
66}
67#endif
68
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053069static void sdhci_dump_state(struct sdhci_host *host)
70{
71 struct mmc_host *mmc = host->mmc;
72
73 pr_info("%s: clk: %d clk-gated: %d claimer: %s pwr: %d\n",
74 mmc_hostname(mmc), host->clock, mmc->clk_gated,
75 mmc->claimer->comm, host->pwr);
76 pr_info("%s: rpmstatus[pltfm](runtime-suspend:usage_count:disable_depth)(%d:%d:%d)\n",
77 mmc_hostname(mmc), mmc->parent->power.runtime_status,
78 atomic_read(&mmc->parent->power.usage_count),
79 mmc->parent->power.disable_depth);
80}
81
Pierre Ossmand129bce2006-03-24 03:18:17 -080082static void sdhci_dumpregs(struct sdhci_host *host)
83{
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053084 pr_info(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070085 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080086
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053087 pr_info(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030088 sdhci_readl(host, SDHCI_DMA_ADDRESS),
89 sdhci_readw(host, SDHCI_HOST_VERSION));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053090 pr_info(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030091 sdhci_readw(host, SDHCI_BLOCK_SIZE),
92 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053093 pr_info(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030094 sdhci_readl(host, SDHCI_ARGUMENT),
95 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053096 pr_info(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030097 sdhci_readl(host, SDHCI_PRESENT_STATE),
98 sdhci_readb(host, SDHCI_HOST_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053099 pr_info(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300100 sdhci_readb(host, SDHCI_POWER_CONTROL),
101 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530102 pr_info(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300103 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
104 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530105 pr_info(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
107 sdhci_readl(host, SDHCI_INT_STATUS));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530108 pr_info(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300109 sdhci_readl(host, SDHCI_INT_ENABLE),
110 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530111 pr_info(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300112 sdhci_readw(host, SDHCI_ACMD12_ERR),
113 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530114 pr_info(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300115 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500116 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530117 pr_info(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500118 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300119 sdhci_readl(host, SDHCI_MAX_CURRENT));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530120 pr_info(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530121 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800122
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100123 if (host->flags & SDHCI_USE_ADMA)
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530124 pr_info(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100125 readl(host->ioaddr + SDHCI_ADMA_ERROR),
126 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
127
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530128 sdhci_dump_state(host);
129 pr_info(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800130}
131
132/*****************************************************************************\
133 * *
134 * Low level functions *
135 * *
136\*****************************************************************************/
137
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300138static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
139{
140 u32 ier;
141
142 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
143 ier &= ~clear;
144 ier |= set;
145 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
146 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
147}
148
149static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
150{
151 sdhci_clear_set_irqs(host, 0, irqs);
152}
153
154static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
155{
156 sdhci_clear_set_irqs(host, irqs, 0);
157}
158
159static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
160{
Sahitya Tummalaca422112013-02-22 12:15:54 +0530161 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300162
Adrian Hunterc79396c2011-12-27 15:48:42 +0200163 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100164 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300165 return;
166
Sahitya Tummalaca422112013-02-22 12:15:54 +0530167 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
168 SDHCI_CARD_PRESENT;
169 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
Adrian Hunter50accb92011-10-03 15:33:34 +0300170
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300171 if (enable)
172 sdhci_unmask_irqs(host, irqs);
173 else
174 sdhci_mask_irqs(host, irqs);
175}
176
177static void sdhci_enable_card_detection(struct sdhci_host *host)
178{
179 sdhci_set_card_detection(host, true);
180}
181
182static void sdhci_disable_card_detection(struct sdhci_host *host)
183{
184 sdhci_set_card_detection(host, false);
185}
186
Pierre Ossmand129bce2006-03-24 03:18:17 -0800187static void sdhci_reset(struct sdhci_host *host, u8 mask)
188{
Pierre Ossmane16514d2006-06-30 02:22:24 -0700189 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300190 u32 uninitialized_var(ier);
Pierre Ossmane16514d2006-06-30 02:22:24 -0700191
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100192 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300193 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700194 SDHCI_CARD_PRESENT))
195 return;
196 }
197
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300198 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
199 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
200
Philip Rakity393c1a32011-01-21 11:26:40 -0800201 if (host->ops->platform_reset_enter)
202 host->ops->platform_reset_enter(host, mask);
203
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300204 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800205
Pierre Ossmane16514d2006-06-30 02:22:24 -0700206 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800207 host->clock = 0;
208
Pierre Ossmane16514d2006-06-30 02:22:24 -0700209 /* Wait max 100 ms */
210 timeout = 100;
211
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530212 if (host->ops->check_power_status && host->pwr &&
213 (mask & SDHCI_RESET_ALL))
Sahitya Tummala179e7382013-03-20 19:24:01 +0530214 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530215
Pierre Ossmane16514d2006-06-30 02:22:24 -0700216 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300217 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d2006-06-30 02:22:24 -0700218 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +0530219 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d2006-06-30 02:22:24 -0700220 mmc_hostname(host->mmc), (int)mask);
221 sdhci_dumpregs(host);
222 return;
223 }
224 timeout--;
225 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300227
Philip Rakity393c1a32011-01-21 11:26:40 -0800228 if (host->ops->platform_reset_exit)
229 host->ops->platform_reset_exit(host, mask);
230
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300231 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
232 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Sahitya Tummalaca422112013-02-22 12:15:54 +0530233
234 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
235 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
236 host->ops->enable_dma(host);
237 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800238}
239
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800240static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
241
242static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800243{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800244 if (soft)
245 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
246 else
247 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800248
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300249 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
250 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700251 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
252 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300253 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800254
255 if (soft) {
256 /* force clock reconfiguration */
257 host->clock = 0;
258 sdhci_set_ios(host->mmc, &host->mmc->ios);
259 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300260}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300262static void sdhci_reinit(struct sdhci_host *host)
263{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800264 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300265 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800266}
267
268static void sdhci_activate_led(struct sdhci_host *host)
269{
270 u8 ctrl;
271
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300272 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300274 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800275}
276
277static void sdhci_deactivate_led(struct sdhci_host *host)
278{
279 u8 ctrl;
280
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300281 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800282 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300283 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800284}
285
Pierre Ossmanf9134312008-12-21 17:01:48 +0100286#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100287static void sdhci_led_control(struct led_classdev *led,
288 enum led_brightness brightness)
289{
290 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
291 unsigned long flags;
292
293 spin_lock_irqsave(&host->lock, flags);
294
Adrian Hunter50accb92011-10-03 15:33:34 +0300295 if (host->runtime_suspended)
296 goto out;
297
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100298 if (brightness == LED_OFF)
299 sdhci_deactivate_led(host);
300 else
301 sdhci_activate_led(host);
Adrian Hunter50accb92011-10-03 15:33:34 +0300302out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100303 spin_unlock_irqrestore(&host->lock, flags);
304}
305#endif
306
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307/*****************************************************************************\
308 * *
309 * Core functions *
310 * *
311\*****************************************************************************/
312
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800314{
Pierre Ossman76591502008-07-21 00:32:11 +0200315 unsigned long flags;
316 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700317 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200318 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800319
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100320 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800321
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100322 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200323 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800324
Pierre Ossman76591502008-07-21 00:32:11 +0200325 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800326
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100327 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200328 if (!sg_miter_next(&host->sg_miter))
329 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800330
Pierre Ossman76591502008-07-21 00:32:11 +0200331 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800332
Pierre Ossman76591502008-07-21 00:32:11 +0200333 blksize -= len;
334 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200335
Pierre Ossman76591502008-07-21 00:32:11 +0200336 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800337
Pierre Ossman76591502008-07-21 00:32:11 +0200338 while (len) {
339 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300340 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200341 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800342 }
Pierre Ossman76591502008-07-21 00:32:11 +0200343
344 *buf = scratch & 0xFF;
345
346 buf++;
347 scratch >>= 8;
348 chunk--;
349 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800350 }
351 }
Pierre Ossman76591502008-07-21 00:32:11 +0200352
353 sg_miter_stop(&host->sg_miter);
354
355 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100356}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800357
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358static void sdhci_write_block_pio(struct sdhci_host *host)
359{
Pierre Ossman76591502008-07-21 00:32:11 +0200360 unsigned long flags;
361 size_t blksize, len, chunk;
362 u32 scratch;
363 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100364
365 DBG("PIO writing\n");
366
367 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200368 chunk = 0;
369 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100370
Pierre Ossman76591502008-07-21 00:32:11 +0200371 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100372
373 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200374 if (!sg_miter_next(&host->sg_miter))
375 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100376
Pierre Ossman76591502008-07-21 00:32:11 +0200377 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200378
Pierre Ossman76591502008-07-21 00:32:11 +0200379 blksize -= len;
380 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381
Pierre Ossman76591502008-07-21 00:32:11 +0200382 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100383
Pierre Ossman76591502008-07-21 00:32:11 +0200384 while (len) {
385 scratch |= (u32)*buf << (chunk * 8);
386
387 buf++;
388 chunk++;
389 len--;
390
391 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300392 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200393 chunk = 0;
394 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100395 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100396 }
397 }
Pierre Ossman76591502008-07-21 00:32:11 +0200398
399 sg_miter_stop(&host->sg_miter);
400
401 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100402}
403
404static void sdhci_transfer_pio(struct sdhci_host *host)
405{
406 u32 mask;
407
408 BUG_ON(!host->data);
409
Pierre Ossman76591502008-07-21 00:32:11 +0200410 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100411 return;
412
413 if (host->data->flags & MMC_DATA_READ)
414 mask = SDHCI_DATA_AVAILABLE;
415 else
416 mask = SDHCI_SPACE_AVAILABLE;
417
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200418 /*
419 * Some controllers (JMicron JMB38x) mess up the buffer bits
420 * for transfers < 4 bytes. As long as it is just one block,
421 * we can ignore the bits.
422 */
423 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
424 (host->data->blocks == 1))
425 mask = ~0;
426
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300427 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300428 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
429 udelay(100);
430
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100431 if (host->data->flags & MMC_DATA_READ)
432 sdhci_read_block_pio(host);
433 else
434 sdhci_write_block_pio(host);
435
Pierre Ossman76591502008-07-21 00:32:11 +0200436 host->blocks--;
437 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100438 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100439 }
440
441 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800442}
443
Pierre Ossman2134a922008-06-28 18:28:51 +0200444static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
445{
446 local_irq_save(*flags);
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800447 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200448}
449
450static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
451{
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800452 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200453 local_irq_restore(*flags);
454}
455
Ben Dooks118cd172010-03-05 13:43:26 -0800456static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
457{
Ben Dooks9e506f32010-03-05 13:43:29 -0800458 __le32 *dataddr = (__le32 __force *)(desc + 4);
459 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800460
Ben Dooks9e506f32010-03-05 13:43:29 -0800461 /* SDHCI specification says ADMA descriptors should be 4 byte
462 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800463
Ben Dooks9e506f32010-03-05 13:43:29 -0800464 cmdlen[0] = cpu_to_le16(cmd);
465 cmdlen[1] = cpu_to_le16(len);
466
467 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800468}
469
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800470static int sdhci_pre_dma_transfer(struct sdhci_host *host,
471 struct mmc_data *data,
472 struct sdhci_next *next)
473{
474 int sg_count;
475
476 if (!next && data->host_cookie &&
477 data->host_cookie != host->next_data.cookie) {
478 printk(KERN_WARNING "[%s] invalid cookie: data->host_cookie %d"
479 " host->next_data.cookie %d\n",
480 __func__, data->host_cookie, host->next_data.cookie);
481 data->host_cookie = 0;
482 }
483
484 /* Check if next job is already prepared */
485 if (next ||
486 (!next && data->host_cookie != host->next_data.cookie)) {
487 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg,
488 data->sg_len,
489 (data->flags & MMC_DATA_WRITE) ?
490 DMA_TO_DEVICE : DMA_FROM_DEVICE);
491 } else {
492 sg_count = host->next_data.sg_count;
493 host->next_data.sg_count = 0;
494 }
495
496 if (sg_count == 0)
497 return -EINVAL;
498
499 if (next) {
500 next->sg_count = sg_count;
501 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
502 } else
503 host->sg_count = sg_count;
504
505 return sg_count;
506}
507
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200508static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200509 struct mmc_data *data)
510{
511 int direction;
512
513 u8 *desc;
514 u8 *align;
515 dma_addr_t addr;
516 dma_addr_t align_addr;
517 int len, offset;
518
519 struct scatterlist *sg;
520 int i;
521 char *buffer;
522 unsigned long flags;
523
524 /*
525 * The spec does not specify endianness of descriptor table.
526 * We currently guess that it is LE.
527 */
528
529 if (data->flags & MMC_DATA_READ)
530 direction = DMA_FROM_DEVICE;
531 else
532 direction = DMA_TO_DEVICE;
533
534 /*
535 * The ADMA descriptor table is mapped further down as we
536 * need to fill it with data first.
537 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200538 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530539 host->align_buffer,
540 host->align_buf_sz,
541 direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700542 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200543 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200544 BUG_ON(host->align_addr & 0x3);
545
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800546 host->sg_count = sdhci_pre_dma_transfer(host, data, NULL);
547 if (host->sg_count < 0)
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200548 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200549
550 desc = host->adma_desc;
551 align = host->align_buffer;
552
553 align_addr = host->align_addr;
554
555 for_each_sg(data->sg, sg, host->sg_count, i) {
556 addr = sg_dma_address(sg);
557 len = sg_dma_len(sg);
558
559 /*
560 * The SDHCI specification states that ADMA
561 * addresses must be 32-bit aligned. If they
562 * aren't, then we use a bounce buffer for
563 * the (up to three) bytes that screw up the
564 * alignment.
565 */
566 offset = (4 - (addr & 0x3)) & 0x3;
567 if (offset) {
568 if (data->flags & MMC_DATA_WRITE) {
569 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200570 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200571 memcpy(align, buffer, offset);
572 sdhci_kunmap_atomic(buffer, &flags);
573 }
574
Ben Dooks118cd172010-03-05 13:43:26 -0800575 /* tran, valid */
576 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200577
578 BUG_ON(offset > 65536);
579
Pierre Ossman2134a922008-06-28 18:28:51 +0200580 align += 4;
581 align_addr += 4;
582
583 desc += 8;
584
585 addr += offset;
586 len -= offset;
587 }
588
Pierre Ossman2134a922008-06-28 18:28:51 +0200589 BUG_ON(len > 65536);
590
Ben Dooks118cd172010-03-05 13:43:26 -0800591 /* tran, valid */
592 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200593 desc += 8;
594
595 /*
596 * If this triggers then we have a calculation bug
597 * somewhere. :/
598 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530599 WARN_ON((desc - host->adma_desc) > host->adma_desc_sz);
600
Pierre Ossman2134a922008-06-28 18:28:51 +0200601 }
602
Thomas Abraham70764a92010-05-26 14:42:04 -0700603 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
604 /*
605 * Mark the last descriptor as the terminating descriptor
606 */
607 if (desc != host->adma_desc) {
608 desc -= 8;
609 desc[0] |= 0x2; /* end */
610 }
611 } else {
612 /*
613 * Add a terminating entry.
614 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200615
Thomas Abraham70764a92010-05-26 14:42:04 -0700616 /* nop, end, valid */
617 sdhci_set_adma_desc(desc, 0, 0, 0x3);
618 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200619
620 /*
621 * Resync align buffer as we might have changed it.
622 */
623 if (data->flags & MMC_DATA_WRITE) {
624 dma_sync_single_for_device(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530625 host->align_addr,
626 host->align_buf_sz,
627 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200628 }
629
630 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530631 host->adma_desc,
632 host->adma_desc_sz,
633 DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200634 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200635 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200636 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200637
638 return 0;
639
640unmap_entries:
641 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
642 data->sg_len, direction);
643unmap_align:
644 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530645 host->align_buf_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200646fail:
647 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200648}
649
650static void sdhci_adma_table_post(struct sdhci_host *host,
651 struct mmc_data *data)
652{
653 int direction;
654
655 struct scatterlist *sg;
656 int i, size;
657 u8 *align;
658 char *buffer;
659 unsigned long flags;
660
661 if (data->flags & MMC_DATA_READ)
662 direction = DMA_FROM_DEVICE;
663 else
664 direction = DMA_TO_DEVICE;
665
666 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530667 host->adma_desc_sz, DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200668
669 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530670 host->align_buf_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200671
672 if (data->flags & MMC_DATA_READ) {
673 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
674 data->sg_len, direction);
675
676 align = host->align_buffer;
677
678 for_each_sg(data->sg, sg, host->sg_count, i) {
679 if (sg_dma_address(sg) & 0x3) {
680 size = 4 - (sg_dma_address(sg) & 0x3);
681
682 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200683 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200684 memcpy(buffer, align, size);
685 sdhci_kunmap_atomic(buffer, &flags);
686
687 align += 4;
688 }
689 }
690 }
691
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800692 if (!data->host_cookie)
693 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
694 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200695}
696
Andrei Warkentina3c77782011-04-11 16:13:42 -0500697static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800698{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700699 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500700 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700701 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800702
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200703 /*
704 * If the host controller provides us with an incorrect timeout
705 * value, just skip the check and use 0xE. The hardware may take
706 * longer to time out, but that's much better than having a too-short
707 * timeout value.
708 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200709 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200710 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200711
Andrei Warkentina3c77782011-04-11 16:13:42 -0500712 /* Unspecified timeout, assume max */
713 if (!data && !cmd->cmd_timeout_ms)
714 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800715
Andrei Warkentina3c77782011-04-11 16:13:42 -0500716 /* timeout in us */
717 if (!data)
718 target_timeout = cmd->cmd_timeout_ms * 1000;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530719 else {
720 target_timeout = data->timeout_ns / 1000;
721 if (host->clock)
722 target_timeout += data->timeout_clks / host->clock;
723 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700724
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700725 /*
726 * Figure out needed cycles.
727 * We do this in steps in order to fit inside a 32 bit int.
728 * The first step is the minimum timeout, which will have a
729 * minimum resolution of 6 bits:
730 * (1) 2^13*1000 > 2^22,
731 * (2) host->timeout_clk < 2^16
732 * =>
733 * (1) / (2) > 2^6
734 */
735 count = 0;
736 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
737 while (current_timeout < target_timeout) {
738 count++;
739 current_timeout <<= 1;
740 if (count >= 0xF)
741 break;
742 }
743
Sahitya Tummala4d12d0b2013-04-12 11:59:25 +0530744 if (!(host->quirks2 & SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT)) {
745 if (count >= 0xF) {
746 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
747 mmc_hostname(host->mmc), count, cmd->opcode);
748 count = 0xE;
749 }
Sahitya Tummalaca422112013-02-22 12:15:54 +0530750 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700751
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200752 return count;
753}
754
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300755static void sdhci_set_transfer_irqs(struct sdhci_host *host)
756{
757 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
758 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
759
760 if (host->flags & SDHCI_REQ_USE_DMA)
761 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
762 else
763 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
764}
765
Andrei Warkentina3c77782011-04-11 16:13:42 -0500766static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200767{
768 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200769 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500770 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200771 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200772
773 WARN_ON(host->data);
774
Andrei Warkentina3c77782011-04-11 16:13:42 -0500775 if (data || (cmd->flags & MMC_RSP_BUSY)) {
776 count = sdhci_calc_timeout(host, cmd);
777 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
778 }
779
780 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200781 return;
782
783 /* Sanity checks */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530784 BUG_ON(data->blksz * data->blocks > host->mmc->max_req_size);
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200785 BUG_ON(data->blksz > host->mmc->max_blk_size);
786 BUG_ON(data->blocks > 65535);
787
788 host->data = data;
789 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400790 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200791
Richard Röjforsa13abc72009-09-22 16:45:30 -0700792 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100793 host->flags |= SDHCI_REQ_USE_DMA;
794
Pierre Ossman2134a922008-06-28 18:28:51 +0200795 /*
796 * FIXME: This doesn't account for merging when mapping the
797 * scatterlist.
798 */
799 if (host->flags & SDHCI_REQ_USE_DMA) {
800 int broken, i;
801 struct scatterlist *sg;
802
803 broken = 0;
804 if (host->flags & SDHCI_USE_ADMA) {
805 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
806 broken = 1;
807 } else {
808 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
809 broken = 1;
810 }
811
812 if (unlikely(broken)) {
813 for_each_sg(data->sg, sg, data->sg_len, i) {
814 if (sg->length & 0x3) {
815 DBG("Reverting to PIO because of "
816 "transfer size (%d)\n",
817 sg->length);
818 host->flags &= ~SDHCI_REQ_USE_DMA;
819 break;
820 }
821 }
822 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100823 }
824
825 /*
826 * The assumption here being that alignment is the same after
827 * translation to device address space.
828 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200829 if (host->flags & SDHCI_REQ_USE_DMA) {
830 int broken, i;
831 struct scatterlist *sg;
832
833 broken = 0;
834 if (host->flags & SDHCI_USE_ADMA) {
835 /*
836 * As we use 3 byte chunks to work around
837 * alignment problems, we need to check this
838 * quirk.
839 */
840 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
841 broken = 1;
842 } else {
843 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
844 broken = 1;
845 }
846
847 if (unlikely(broken)) {
848 for_each_sg(data->sg, sg, data->sg_len, i) {
849 if (sg->offset & 0x3) {
850 DBG("Reverting to PIO because of "
851 "bad alignment\n");
852 host->flags &= ~SDHCI_REQ_USE_DMA;
853 break;
854 }
855 }
856 }
857 }
858
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200859 if (host->flags & SDHCI_REQ_USE_DMA) {
860 if (host->flags & SDHCI_USE_ADMA) {
861 ret = sdhci_adma_table_pre(host, data);
862 if (ret) {
863 /*
864 * This only happens when someone fed
865 * us an invalid request.
866 */
867 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200868 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200869 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300870 sdhci_writel(host, host->adma_addr,
871 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200872 }
873 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300874 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200875
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800876 sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300877 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200878 /*
879 * This only happens when someone fed
880 * us an invalid request.
881 */
882 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200883 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200884 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200885 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300886 sdhci_writel(host, sg_dma_address(data->sg),
887 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200888 }
889 }
890 }
891
Pierre Ossman2134a922008-06-28 18:28:51 +0200892 /*
893 * Always adjust the DMA selection as some controllers
894 * (e.g. JMicron) can't do PIO properly when the selection
895 * is ADMA.
896 */
897 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300898 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200899 ctrl &= ~SDHCI_CTRL_DMA_MASK;
900 if ((host->flags & SDHCI_REQ_USE_DMA) &&
901 (host->flags & SDHCI_USE_ADMA))
902 ctrl |= SDHCI_CTRL_ADMA32;
903 else
904 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300905 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100906 }
907
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200908 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200909 int flags;
910
911 flags = SG_MITER_ATOMIC;
912 if (host->data->flags & MMC_DATA_READ)
913 flags |= SG_MITER_TO_SG;
914 else
915 flags |= SG_MITER_FROM_SG;
916 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200917 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800918 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700919
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300920 sdhci_set_transfer_irqs(host);
921
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400922 /* Set the DMA boundary value and block size */
923 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
924 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300925 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700926}
927
928static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500929 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700930{
931 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500932 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700933
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700934 if (data == NULL)
935 return;
936
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200937 WARN_ON(!host->data);
938
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700939 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500940 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
941 mode |= SDHCI_TRNS_MULTI;
942 /*
943 * If we are sending CMD23, CMD12 never gets sent
944 * on successful completion (so no Auto-CMD12).
945 */
946 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
947 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500948 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
949 mode |= SDHCI_TRNS_AUTO_CMD23;
950 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
951 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700952 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500953
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530954 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700955 mode |= SDHCI_TRNS_READ;
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530956 if (host->ops->toggle_cdr)
957 host->ops->toggle_cdr(host, true);
958 }
959 if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE))
960 host->ops->toggle_cdr(host, false);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100961 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700962 mode |= SDHCI_TRNS_DMA;
963
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300964 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800965}
966
967static void sdhci_finish_data(struct sdhci_host *host)
968{
969 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800970
971 BUG_ON(!host->data);
972
973 data = host->data;
974 host->data = NULL;
975
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100976 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200977 if (host->flags & SDHCI_USE_ADMA)
978 sdhci_adma_table_post(host, data);
979 else {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800980 if (!data->host_cookie)
981 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
982 data->sg_len,
983 (data->flags & MMC_DATA_READ) ?
984 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200985 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800986 }
987
988 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200989 * The specification states that the block count register must
990 * be updated, but it does not specify at what point in the
991 * data flow. That makes the register entirely useless to read
992 * back so we have to assume that nothing made it to the card
993 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200995 if (data->error)
996 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200998 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800999
Andrei Warkentine89d4562011-05-23 15:06:37 -05001000 /*
1001 * Need to send CMD12 if -
1002 * a) open-ended multiblock transfer (no CMD23)
1003 * b) error in multiblock transfer
1004 */
1005 if (data->stop &&
1006 (data->error ||
1007 !host->mrq->sbc)) {
1008
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009 /*
1010 * The controller needs a reset of internal state machines
1011 * upon error conditions.
1012 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001013 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014 sdhci_reset(host, SDHCI_RESET_CMD);
1015 sdhci_reset(host, SDHCI_RESET_DATA);
1016 }
1017
1018 sdhci_send_command(host, data->stop);
1019 } else
1020 tasklet_schedule(&host->finish_tasklet);
1021}
1022
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301023#define SDHCI_REQUEST_TIMEOUT 10 /* Default request timeout in seconds */
1024
Pierre Ossmand129bce2006-03-24 03:18:17 -08001025static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
1026{
1027 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001028 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001029 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030
1031 WARN_ON(host->cmd);
1032
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001034 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001035
1036 mask = SDHCI_CMD_INHIBIT;
1037 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1038 mask |= SDHCI_DATA_INHIBIT;
1039
1040 /* We shouldn't wait for data inihibit for stop commands, even
1041 though they might use busy signaling */
1042 if (host->mrq->data && (cmd == host->mrq->data->stop))
1043 mask &= ~SDHCI_DATA_INHIBIT;
1044
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001045 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001046 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301047 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001048 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001049 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001050 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001051 tasklet_schedule(&host->finish_tasklet);
1052 return;
1053 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001054 timeout--;
1055 mdelay(1);
1056 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301058 mod_timer(&host->timer, jiffies + SDHCI_REQUEST_TIMEOUT * HZ);
1059
1060 if (cmd->cmd_timeout_ms > SDHCI_REQUEST_TIMEOUT * MSEC_PER_SEC)
1061 mod_timer(&host->timer, jiffies +
1062 (msecs_to_jiffies(cmd->cmd_timeout_ms * 2)));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001063
1064 host->cmd = cmd;
1065
Andrei Warkentina3c77782011-04-11 16:13:42 -05001066 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001067
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001068 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001069
Andrei Warkentine89d4562011-05-23 15:06:37 -05001070 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001071
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301073 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001074 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001075 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001076 tasklet_schedule(&host->finish_tasklet);
1077 return;
1078 }
1079
1080 if (!(cmd->flags & MMC_RSP_PRESENT))
1081 flags = SDHCI_CMD_RESP_NONE;
1082 else if (cmd->flags & MMC_RSP_136)
1083 flags = SDHCI_CMD_RESP_LONG;
1084 else if (cmd->flags & MMC_RSP_BUSY)
1085 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1086 else
1087 flags = SDHCI_CMD_RESP_SHORT;
1088
1089 if (cmd->flags & MMC_RSP_CRC)
1090 flags |= SDHCI_CMD_CRC;
1091 if (cmd->flags & MMC_RSP_OPCODE)
1092 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301093
1094 /* CMD19 is special in that the Data Present Select should be set */
Girish K S2cd06dc2012-01-06 09:56:39 +05301095 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1096 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001097 flags |= SDHCI_CMD_DATA;
1098
Sahitya Tummala48b458e2013-04-08 12:53:44 +05301099 if (cmd->data)
1100 host->data_start_time = ktime_get();
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001101 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001102}
1103
1104static void sdhci_finish_command(struct sdhci_host *host)
1105{
1106 int i;
1107
1108 BUG_ON(host->cmd == NULL);
1109
1110 if (host->cmd->flags & MMC_RSP_PRESENT) {
1111 if (host->cmd->flags & MMC_RSP_136) {
1112 /* CRC is stripped so we need to do some shifting. */
1113 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001114 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001115 SDHCI_RESPONSE + (3-i)*4) << 8;
1116 if (i != 3)
1117 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001118 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001119 SDHCI_RESPONSE + (3-i)*4-1);
1120 }
1121 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001122 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001123 }
1124 }
1125
Pierre Ossman17b04292007-07-22 22:18:46 +02001126 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001127
Andrei Warkentine89d4562011-05-23 15:06:37 -05001128 /* Finished CMD23, now send actual command. */
1129 if (host->cmd == host->mrq->sbc) {
1130 host->cmd = NULL;
1131 sdhci_send_command(host, host->mrq->cmd);
1132 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001133
Andrei Warkentine89d4562011-05-23 15:06:37 -05001134 /* Processed actual command. */
1135 if (host->data && host->data_early)
1136 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001137
Andrei Warkentine89d4562011-05-23 15:06:37 -05001138 if (!host->cmd->data)
1139 tasklet_schedule(&host->finish_tasklet);
1140
1141 host->cmd = NULL;
1142 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001143}
1144
1145static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1146{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301147 int div = 0; /* Initialized for compiler warning */
Sahitya Tummalaca422112013-02-22 12:15:54 +05301148 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301149 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001150 unsigned long timeout;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301151 unsigned long flags;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001152
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301153 spin_lock_irqsave(&host->lock, flags);
Todd Poynor30832ab2011-12-27 15:48:46 +02001154 if (clock && clock == host->clock)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301155 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001156
Sahitya Tummalaca422112013-02-22 12:15:54 +05301157 host->mmc->actual_clock = 0;
1158
Anton Vorontsov81146342009-03-17 00:13:59 +03001159 if (host->ops->set_clock) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301160 spin_unlock_irqrestore(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001161 host->ops->set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301162 spin_lock_irqsave(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001163 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301164 goto ret;
Anton Vorontsov81146342009-03-17 00:13:59 +03001165 }
1166
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301167 if (host->clock)
1168 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001169
1170 if (clock == 0)
1171 goto out;
1172
Zhangfei Gao85105c52010-08-06 07:10:01 +08001173 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301174 /*
1175 * Check if the Host Controller supports Programmable Clock
1176 * Mode.
1177 */
1178 if (host->clk_mul) {
1179 u16 ctrl;
1180
1181 /*
1182 * We need to figure out whether the Host Driver needs
1183 * to select Programmable Clock Mode, or the value can
1184 * be set automatically by the Host Controller based on
1185 * the Preset Value registers.
1186 */
1187 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1188 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1189 for (div = 1; div <= 1024; div++) {
1190 if (((host->max_clk * host->clk_mul) /
1191 div) <= clock)
1192 break;
1193 }
1194 /*
1195 * Set Programmable Clock Mode in the Clock
1196 * Control register.
1197 */
1198 clk = SDHCI_PROG_CLOCK_MODE;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301199 real_div = div;
1200 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301201 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001202 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301203 } else {
1204 /* Version 3.00 divisors must be a multiple of 2. */
1205 if (host->max_clk <= clock)
1206 div = 1;
1207 else {
1208 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1209 div += 2) {
1210 if ((host->max_clk / div) <= clock)
1211 break;
1212 }
1213 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301214 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301215 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001216 }
1217 } else {
1218 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001219 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001220 if ((host->max_clk / div) <= clock)
1221 break;
1222 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301223 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301224 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001225 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001226
Sahitya Tummalaca422112013-02-22 12:15:54 +05301227 if (real_div)
1228 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1229
Sahitya Tummala00240122013-02-28 19:50:51 +05301230 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
1231 div = 0;
1232
Arindam Nathc3ed3872011-05-05 12:19:06 +05301233 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001234 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1235 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001236 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001237 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001238
Chris Ball27f6cb12009-09-22 16:45:31 -07001239 /* Wait max 20 ms */
1240 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001241 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001242 & SDHCI_CLOCK_INT_STABLE)) {
1243 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301244 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001245 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001246 sdhci_dumpregs(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301247 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001248 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001249 timeout--;
1250 mdelay(1);
1251 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001252
1253 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001254 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001255
1256out:
1257 host->clock = clock;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301258ret:
1259 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001260}
1261
Sahitya Tummalaca422112013-02-22 12:15:54 +05301262static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001263{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001264 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001265
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001266 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001267 switch (1 << power) {
1268 case MMC_VDD_165_195:
1269 pwr = SDHCI_POWER_180;
1270 break;
1271 case MMC_VDD_29_30:
1272 case MMC_VDD_30_31:
1273 pwr = SDHCI_POWER_300;
1274 break;
1275 case MMC_VDD_32_33:
1276 case MMC_VDD_33_34:
1277 pwr = SDHCI_POWER_330;
1278 break;
1279 default:
1280 BUG();
1281 }
1282 }
1283
1284 if (host->pwr == pwr)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301285 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001286
Pierre Ossmanae628902009-05-03 20:45:03 +02001287 host->pwr = pwr;
1288
1289 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001290 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301291 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301292 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301293 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001294 }
1295
1296 /*
1297 * Spec says that we should clear the power reg before setting
1298 * a new value. Some controllers don't seem to like this though.
1299 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301300 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001301 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301302 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301303 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301304 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001305
Andres Salomone08c1692008-07-04 10:00:03 -07001306 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001307 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001308 * and set turn on power at the same time, so set the voltage first.
1309 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301310 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001311 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301312 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301313 host->ops->check_power_status(host, REQ_BUS_ON);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301314 }
Pierre Ossmanae628902009-05-03 20:45:03 +02001315
1316 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001317
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001318 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301319 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301320 host->ops->check_power_status(host, REQ_BUS_ON);
Harald Welte557b0692009-06-18 16:53:38 +02001321
1322 /*
1323 * Some controllers need an extra 10ms delay of 10ms before they
1324 * can apply clock after applying power
1325 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001326 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001327 mdelay(10);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301328
1329 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001330}
1331
Pierre Ossmand129bce2006-03-24 03:18:17 -08001332/*****************************************************************************\
1333 * *
1334 * MMC callbacks *
1335 * *
1336\*****************************************************************************/
1337
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301338static int sdhci_enable(struct mmc_host *mmc)
1339{
1340 struct sdhci_host *host = mmc_priv(mmc);
1341
1342 if (host->cpu_dma_latency_us)
1343 pm_qos_update_request(&host->pm_qos_req_dma,
1344 host->cpu_dma_latency_us);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301345 if (host->ops->platform_bus_voting)
1346 host->ops->platform_bus_voting(host, 1);
1347
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301348 return 0;
1349}
1350
1351static int sdhci_disable(struct mmc_host *mmc)
1352{
1353 struct sdhci_host *host = mmc_priv(mmc);
1354
1355 if (host->cpu_dma_latency_us)
1356 pm_qos_update_request(&host->pm_qos_req_dma,
1357 PM_QOS_DEFAULT_VALUE);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301358 if (host->ops->platform_bus_voting)
1359 host->ops->platform_bus_voting(host, 0);
1360
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301361 return 0;
1362}
1363
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001364static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1365 bool is_first_req)
1366{
1367 struct sdhci_host *host = mmc_priv(mmc);
1368
1369 if (mrq->data->host_cookie) {
1370 mrq->data->host_cookie = 0;
1371 return;
1372 }
1373
1374 if (host->flags & SDHCI_REQ_USE_DMA)
1375 if (sdhci_pre_dma_transfer(host, mrq->data, &host->next_data) < 0)
1376 mrq->data->host_cookie = 0;
1377}
1378
1379static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1380 int err)
1381{
1382 struct sdhci_host *host = mmc_priv(mmc);
1383 struct mmc_data *data = mrq->data;
1384
1385 if (host->flags & SDHCI_REQ_USE_DMA) {
1386 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1387 (data->flags & MMC_DATA_WRITE) ?
1388 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1389 data->host_cookie = 0;
1390 }
1391}
1392
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301393static bool sdhci_check_state(struct sdhci_host *host)
1394{
1395 struct mmc_host *mmc = host->mmc;
1396
1397 if (!host->clock || !host->pwr ||
1398 pm_runtime_suspended(mmc->parent))
1399 return true;
1400 else
1401 return false;
1402}
1403
Pierre Ossmand129bce2006-03-24 03:18:17 -08001404static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1405{
1406 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001407 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001408 unsigned long flags;
1409
1410 host = mmc_priv(mmc);
1411
Adrian Hunter50accb92011-10-03 15:33:34 +03001412 sdhci_runtime_pm_get(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301413 if (sdhci_check_state(host)) {
1414 sdhci_dump_state(host);
1415 WARN(1, "sdhci in bad state");
1416 mrq->cmd->error = -EIO;
1417 if (mrq->data)
1418 mrq->data->error = -EIO;
1419 tasklet_schedule(&host->finish_tasklet);
1420 return;
1421 }
Adrian Hunter50accb92011-10-03 15:33:34 +03001422
Pierre Ossmand129bce2006-03-24 03:18:17 -08001423 spin_lock_irqsave(&host->lock, flags);
1424
1425 WARN_ON(host->mrq != NULL);
1426
Pierre Ossmanf9134312008-12-21 17:01:48 +01001427#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001428 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001429#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001430
1431 /*
1432 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1433 * requests if Auto-CMD12 is enabled.
1434 */
1435 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001436 if (mrq->stop) {
1437 mrq->data->stop = NULL;
1438 mrq->stop = NULL;
1439 }
1440 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001441
1442 host->mrq = mrq;
1443
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001444 /* If polling, assume that the card is always present. */
1445 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1446 present = true;
1447 else
1448 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1449 SDHCI_CARD_PRESENT;
1450
1451 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001452 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001453 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301454 } else {
1455 u32 present_state;
1456
1457 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1458 /*
1459 * Check if the re-tuning timer has already expired and there
1460 * is no on-going data transfer. If so, we need to execute
1461 * tuning procedure before sending command.
1462 */
1463 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1464 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1465 spin_unlock_irqrestore(&host->lock, flags);
Girish K S2cd06dc2012-01-06 09:56:39 +05301466 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301467 spin_lock_irqsave(&host->lock, flags);
1468
1469 /* Restore original mmc_request structure */
1470 host->mrq = mrq;
1471 }
1472
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001473 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001474 sdhci_send_command(host, mrq->sbc);
1475 else
1476 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301477 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001478
Pierre Ossman5f25a662006-10-04 02:15:39 -07001479 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001480 spin_unlock_irqrestore(&host->lock, flags);
1481}
1482
Adrian Hunter50accb92011-10-03 15:33:34 +03001483static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001484{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001485 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301486 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001487 u8 ctrl;
1488
Sahitya Tummalaca422112013-02-22 12:15:54 +05301489 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301490 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1491 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1492 return;
1493 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001494
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301495 if (ios->clock)
1496 sdhci_set_clock(host, ios->clock);
1497
1498 spin_lock_irqsave(&host->lock, flags);
1499 if (!host->clock) {
1500 spin_unlock_irqrestore(&host->lock, flags);
1501 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001502 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301503 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001504
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301505 if (ios->power_mode & (MMC_POWER_UP | MMC_POWER_ON))
Sahitya Tummalaca422112013-02-22 12:15:54 +05301506 vdd_bit = sdhci_set_power(host, ios->vdd);
1507
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301508 if (host->vmmc && vdd_bit != -1)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301509 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301510
1511 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001512
Philip Rakity643a81f2010-09-23 08:24:32 -07001513 if (host->ops->platform_send_init_74_clocks)
1514 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1515
Philip Rakity15ec4462010-11-19 16:48:39 -05001516 /*
1517 * If your platform has 8-bit width support but is not a v3 controller,
1518 * or if it requires special setup code, you should implement that in
1519 * platform_8bit_width().
1520 */
1521 if (host->ops->platform_8bit_width)
1522 host->ops->platform_8bit_width(host, ios->bus_width);
1523 else {
1524 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1525 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1526 ctrl &= ~SDHCI_CTRL_4BITBUS;
1527 if (host->version >= SDHCI_SPEC_300)
1528 ctrl |= SDHCI_CTRL_8BITBUS;
1529 } else {
1530 if (host->version >= SDHCI_SPEC_300)
1531 ctrl &= ~SDHCI_CTRL_8BITBUS;
1532 if (ios->bus_width == MMC_BUS_WIDTH_4)
1533 ctrl |= SDHCI_CTRL_4BITBUS;
1534 else
1535 ctrl &= ~SDHCI_CTRL_4BITBUS;
1536 }
1537 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1538 }
1539
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001540 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001541
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001542 if ((ios->timing == MMC_TIMING_SD_HS ||
1543 ios->timing == MMC_TIMING_MMC_HS)
1544 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001545 ctrl |= SDHCI_CTRL_HISPD;
1546 else
1547 ctrl &= ~SDHCI_CTRL_HISPD;
1548
Arindam Nathd6d50a12011-05-05 12:18:59 +05301549 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301550 u16 clk, ctrl_2;
1551 unsigned int clock;
1552
1553 /* In case of UHS-I modes, set High Speed Enable */
Girish K S2cd06dc2012-01-06 09:56:39 +05301554 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1555 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301556 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1557 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001558 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301559 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301560
1561 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1562 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301563 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301564 /*
1565 * We only need to set Driver Strength if the
1566 * preset value enable is not set.
1567 */
1568 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1569 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1570 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1571 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1572 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1573
1574 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301575 } else {
1576 /*
1577 * According to SDHC Spec v3.00, if the Preset Value
1578 * Enable in the Host Control 2 register is set, we
1579 * need to reset SD Clock Enable before changing High
1580 * Speed Enable to avoid generating clock gliches.
1581 */
Arindam Nath758535c2011-05-05 12:19:00 +05301582
1583 /* Reset SD Clock Enable */
1584 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1585 clk &= ~SDHCI_CLOCK_CARD_EN;
1586 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1587
1588 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1589
1590 /* Re-enable SD Clock */
1591 clock = host->clock;
1592 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301593 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301594 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301595 spin_lock_irqsave(&host->lock, flags);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301596 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301597
Arindam Nath49c468f2011-05-05 12:19:01 +05301598 /* Reset SD Clock Enable */
1599 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1600 clk &= ~SDHCI_CLOCK_CARD_EN;
1601 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1602
Philip Rakity6322cdd2011-05-13 11:17:15 +05301603 if (host->ops->set_uhs_signaling)
1604 host->ops->set_uhs_signaling(host, ios->timing);
1605 else {
1606 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1607 /* Select Bus Speed Mode for host */
1608 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S2cd06dc2012-01-06 09:56:39 +05301609 if (ios->timing == MMC_TIMING_MMC_HS200)
1610 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1611 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301612 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1613 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1614 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1615 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1616 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1617 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1618 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1619 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1620 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1621 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1622 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301623
1624 /* Re-enable SD Clock */
1625 clock = host->clock;
1626 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301627 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath49c468f2011-05-05 12:19:01 +05301628 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301629 spin_lock_irqsave(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301630 } else
1631 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301632
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301633 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02001634 /*
1635 * Some (ENE) controllers go apeshit on some ios operation,
1636 * signalling timeout and CRC errors even on CMD0. Resetting
1637 * it on each ios seems to solve the problem.
1638 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001639 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001640 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1641
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301642 /*
1643 * Reset the chip on each power off.
1644 * Should clear out any weird states.
1645 */
1646 if (ios->power_mode == MMC_POWER_OFF) {
1647 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1648 sdhci_reinit(host);
1649 vdd_bit = sdhci_set_power(host, -1);
1650 if (host->vmmc && vdd_bit != -1)
1651 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1652 }
1653 if (!ios->clock)
1654 sdhci_set_clock(host, ios->clock);
1655
Pierre Ossman5f25a662006-10-04 02:15:39 -07001656 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001657}
1658
Adrian Hunter50accb92011-10-03 15:33:34 +03001659static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1660{
1661 struct sdhci_host *host = mmc_priv(mmc);
1662
1663 sdhci_runtime_pm_get(host);
1664 sdhci_do_set_ios(host, ios);
1665 sdhci_runtime_pm_put(host);
1666}
1667
1668static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001669{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001670 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001671 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001672
Pierre Ossmand129bce2006-03-24 03:18:17 -08001673 spin_lock_irqsave(&host->lock, flags);
1674
Pierre Ossman1e728592008-04-16 19:13:13 +02001675 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001676 is_readonly = 0;
1677 else if (host->ops->get_ro)
1678 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001679 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001680 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1681 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001682
1683 spin_unlock_irqrestore(&host->lock, flags);
1684
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001685 /* This quirk needs to be replaced by a callback-function later */
1686 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1687 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001688}
1689
Takashi Iwai82b0e232011-04-21 20:26:38 +02001690#define SAMPLE_COUNT 5
1691
Adrian Hunter50accb92011-10-03 15:33:34 +03001692static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001693{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001694 int i, ro_count;
1695
Takashi Iwai82b0e232011-04-21 20:26:38 +02001696 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001697 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001698
1699 ro_count = 0;
1700 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001701 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001702 if (++ro_count > SAMPLE_COUNT / 2)
1703 return 1;
1704 }
1705 msleep(30);
1706 }
1707 return 0;
1708}
1709
Adrian Hunter50accb92011-10-03 15:33:34 +03001710static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001711{
Adrian Hunter50accb92011-10-03 15:33:34 +03001712 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001713
Adrian Hunter50accb92011-10-03 15:33:34 +03001714 if (host->ops && host->ops->hw_reset)
1715 host->ops->hw_reset(host);
1716}
Adrian Hunter20758b62011-08-29 16:42:12 +03001717
Adrian Hunter50accb92011-10-03 15:33:34 +03001718static int sdhci_get_ro(struct mmc_host *mmc)
1719{
1720 struct sdhci_host *host = mmc_priv(mmc);
1721 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001722
Adrian Hunter50accb92011-10-03 15:33:34 +03001723 sdhci_runtime_pm_get(host);
1724 ret = sdhci_do_get_ro(host);
1725 sdhci_runtime_pm_put(host);
1726 return ret;
1727}
1728
1729static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1730{
Pierre Ossman1e728592008-04-16 19:13:13 +02001731 if (host->flags & SDHCI_DEVICE_DEAD)
1732 goto out;
1733
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001734 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001735 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1736 else
1737 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1738
1739 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1740 if (host->runtime_suspended)
1741 goto out;
1742
1743 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001744 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1745 else
1746 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001747out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001748 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001749}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001750
Adrian Hunter50accb92011-10-03 15:33:34 +03001751static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1752{
1753 struct sdhci_host *host = mmc_priv(mmc);
1754 unsigned long flags;
1755
1756 spin_lock_irqsave(&host->lock, flags);
1757 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001758 spin_unlock_irqrestore(&host->lock, flags);
1759}
1760
Adrian Hunter50accb92011-10-03 15:33:34 +03001761static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1762 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301763{
Arindam Nathf2119df2011-05-05 12:18:57 +05301764 u8 pwr;
1765 u16 clk, ctrl;
1766 u32 present_state;
1767
Arindam Nathf2119df2011-05-05 12:18:57 +05301768 /*
1769 * Signal Voltage Switching is only applicable for Host Controllers
1770 * v3.00 and above.
1771 */
1772 if (host->version < SDHCI_SPEC_300)
1773 return 0;
1774
1775 /*
1776 * We first check whether the request is to set signalling voltage
1777 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1778 */
1779 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1780 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1781 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1782 ctrl &= ~SDHCI_CTRL_VDD_180;
1783 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301784 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301785 host->ops->check_power_status(host, REQ_IO_HIGH);
Arindam Nathf2119df2011-05-05 12:18:57 +05301786
1787 /* Wait for 5ms */
1788 usleep_range(5000, 5500);
1789
1790 /* 3.3V regulator output should be stable within 5 ms */
1791 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1792 if (!(ctrl & SDHCI_CTRL_VDD_180))
1793 return 0;
1794 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301795 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301796 "signalling voltage failed\n");
1797 return -EIO;
1798 }
1799 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1800 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1801 /* Stop SDCLK */
1802 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1803 clk &= ~SDHCI_CLOCK_CARD_EN;
1804 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1805
1806 /* Check whether DAT[3:0] is 0000 */
1807 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1808 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1809 SDHCI_DATA_LVL_SHIFT)) {
1810 /*
1811 * Enable 1.8V Signal Enable in the Host Control2
1812 * register
1813 */
1814 ctrl |= SDHCI_CTRL_VDD_180;
1815 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301816 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301817 host->ops->check_power_status(host, REQ_IO_LOW);
Arindam Nathf2119df2011-05-05 12:18:57 +05301818
1819 /* Wait for 5ms */
1820 usleep_range(5000, 5500);
1821
1822 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1823 if (ctrl & SDHCI_CTRL_VDD_180) {
1824 /* Provide SDCLK again and wait for 1ms*/
1825 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1826 clk |= SDHCI_CLOCK_CARD_EN;
1827 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1828 usleep_range(1000, 1500);
1829
1830 /*
1831 * If DAT[3:0] level is 1111b, then the card
1832 * was successfully switched to 1.8V signaling.
1833 */
1834 present_state = sdhci_readl(host,
1835 SDHCI_PRESENT_STATE);
1836 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1837 SDHCI_DATA_LVL_MASK)
1838 return 0;
1839 }
1840 }
1841
1842 /*
1843 * If we are here, that means the switch to 1.8V signaling
1844 * failed. We power cycle the card, and retry initialization
1845 * sequence by setting S18R to 0.
1846 */
1847 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1848 pwr &= ~SDHCI_POWER_ON;
1849 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301850 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301851 host->ops->check_power_status(host, REQ_BUS_OFF);
Arindam Nathf2119df2011-05-05 12:18:57 +05301852
1853 /* Wait for 1ms as per the spec */
1854 usleep_range(1000, 1500);
1855 pwr |= SDHCI_POWER_ON;
1856 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301857 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301858 host->ops->check_power_status(host, REQ_BUS_ON);
Arindam Nathf2119df2011-05-05 12:18:57 +05301859
Sahitya Tummalaca422112013-02-22 12:15:54 +05301860 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301861 "voltage failed, retrying with S18R set to 0\n");
1862 return -EAGAIN;
1863 } else
1864 /* No signal voltage switch required */
1865 return 0;
1866}
1867
Adrian Hunter50accb92011-10-03 15:33:34 +03001868static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1869 struct mmc_ios *ios)
1870{
1871 struct sdhci_host *host = mmc_priv(mmc);
1872 int err;
1873
1874 if (host->version < SDHCI_SPEC_300)
1875 return 0;
1876 sdhci_runtime_pm_get(host);
1877 err = sdhci_do_start_signal_voltage_switch(host, ios);
1878 sdhci_runtime_pm_put(host);
1879 return err;
1880}
1881
Girish K S2cd06dc2012-01-06 09:56:39 +05301882static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301883{
1884 struct sdhci_host *host;
1885 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05301886 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05301887 int tuning_loop_counter = MAX_TUNING_LOOP;
1888 unsigned long timeout;
1889 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05301890 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301891
1892 host = mmc_priv(mmc);
1893
Adrian Hunter50accb92011-10-03 15:33:34 +03001894 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301895 disable_irq(host->irq);
1896 spin_lock(&host->lock);
1897
1898 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1899
1900 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05301901 * The Host Controller needs tuning only in case of SDR104 mode
1902 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301903 * Capabilities register.
Girish K S2cd06dc2012-01-06 09:56:39 +05301904 * If the Host Controller supports the HS200 mode then the
1905 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301906 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08001907 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1908 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
1909 (host->flags & SDHCI_HS200_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05301910 requires_tuning_nonuhs = true;
1911
Arindam Nathb513ea22011-05-05 12:19:04 +05301912 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301913 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301914 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1915 else {
1916 spin_unlock(&host->lock);
1917 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03001918 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301919 return 0;
1920 }
1921
Asutosh Das8ddd3482013-01-04 11:45:46 +05301922 if (host->ops->execute_tuning) {
1923 spin_unlock(&host->lock);
1924 enable_irq(host->irq);
1925 host->ops->execute_tuning(host, opcode);
1926 disable_irq(host->irq);
1927 spin_lock(&host->lock);
1928 goto out;
1929 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301930 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1931
1932 /*
1933 * As per the Host Controller spec v3.00, tuning command
1934 * generates Buffer Read Ready interrupt, so enable that.
1935 *
1936 * Note: The spec clearly says that when tuning sequence
1937 * is being performed, the controller does not generate
1938 * interrupts other than Buffer Read Ready interrupt. But
1939 * to make sure we don't hit a controller bug, we _only_
1940 * enable Buffer Read Ready interrupt here.
1941 */
1942 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1943 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1944
1945 /*
1946 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1947 * of loops reaches 40 times or a timeout of 150ms occurs.
1948 */
1949 timeout = 150;
1950 do {
1951 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03001952 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301953
1954 if (!tuning_loop_counter && !timeout)
1955 break;
1956
Girish K S2cd06dc2012-01-06 09:56:39 +05301957 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301958 cmd.arg = 0;
1959 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1960 cmd.retries = 0;
1961 cmd.data = NULL;
1962 cmd.error = 0;
1963
1964 mrq.cmd = &cmd;
1965 host->mrq = &mrq;
1966
1967 /*
1968 * In response to CMD19, the card sends 64 bytes of tuning
1969 * block to the Host Controller. So we set the block size
1970 * to 64 here.
1971 */
Girish K S2cd06dc2012-01-06 09:56:39 +05301972 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1973 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1974 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1975 SDHCI_BLOCK_SIZE);
1976 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1977 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1978 SDHCI_BLOCK_SIZE);
1979 } else {
1980 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1981 SDHCI_BLOCK_SIZE);
1982 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301983
1984 /*
1985 * The tuning block is sent by the card to the host controller.
1986 * So we set the TRNS_READ bit in the Transfer Mode register.
1987 * This also takes care of setting DMA Enable and Multi Block
1988 * Select in the same register to 0.
1989 */
1990 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1991
1992 sdhci_send_command(host, &cmd);
1993
1994 host->cmd = NULL;
1995 host->mrq = NULL;
1996
1997 spin_unlock(&host->lock);
1998 enable_irq(host->irq);
1999
2000 /* Wait for Buffer Read Ready interrupt */
2001 wait_event_interruptible_timeout(host->buf_ready_int,
2002 (host->tuning_done == 1),
2003 msecs_to_jiffies(50));
2004 disable_irq(host->irq);
2005 spin_lock(&host->lock);
2006
2007 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302008 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05302009 "Buffer Read Ready interrupt during tuning "
2010 "procedure, falling back to fixed sampling "
2011 "clock\n");
2012 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2013 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2014 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2015 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2016
2017 err = -EIO;
2018 goto out;
2019 }
2020
2021 host->tuning_done = 0;
2022
2023 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2024 tuning_loop_counter--;
2025 timeout--;
2026 mdelay(1);
2027 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2028
2029 /*
2030 * The Host Driver has exhausted the maximum number of loops allowed,
2031 * so use fixed sampling frequency.
2032 */
2033 if (!tuning_loop_counter || !timeout) {
2034 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2035 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2036 } else {
2037 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302038 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05302039 " failed, falling back to fixed sampling"
2040 " clock\n");
2041 err = -EIO;
2042 }
2043 }
2044
2045out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302046 /*
2047 * If this is the very first time we are here, we start the retuning
2048 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2049 * flag won't be set, we check this condition before actually starting
2050 * the timer.
2051 */
2052 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2053 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2054 mod_timer(&host->tuning_timer, jiffies +
2055 host->tuning_count * HZ);
2056 /* Tuning mode 1 limits the maximum data length to 4MB */
2057 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2058 } else {
2059 host->flags &= ~SDHCI_NEEDS_RETUNING;
2060 /* Reload the new initial value for timer */
2061 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2062 mod_timer(&host->tuning_timer, jiffies +
2063 host->tuning_count * HZ);
2064 }
2065
2066 /*
2067 * In case tuning fails, host controllers which support re-tuning can
2068 * try tuning again at a later time, when the re-tuning timer expires.
2069 * So for these controllers, we return 0. Since there might be other
2070 * controllers who do not have this capability, we return error for
2071 * them.
2072 */
2073 if (err && host->tuning_count &&
2074 host->tuning_mode == SDHCI_TUNING_MODE_1)
2075 err = 0;
2076
Arindam Nathb513ea22011-05-05 12:19:04 +05302077 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
2078 spin_unlock(&host->lock);
2079 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002080 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302081
2082 return err;
2083}
2084
Adrian Hunter50accb92011-10-03 15:33:34 +03002085static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302086{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302087 u16 ctrl;
2088 unsigned long flags;
2089
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302090 /* Host Controller v3.00 defines preset value registers */
2091 if (host->version < SDHCI_SPEC_300)
2092 return;
2093
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302094 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_PRESET_VALUE)
2095 return;
2096
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302097 spin_lock_irqsave(&host->lock, flags);
2098
2099 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2100
2101 /*
2102 * We only enable or disable Preset Value if they are not already
2103 * enabled or disabled respectively. Otherwise, we bail out.
2104 */
2105 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2106 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2107 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002108 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302109 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2110 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2111 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002112 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302113 }
2114
2115 spin_unlock_irqrestore(&host->lock, flags);
2116}
2117
Adrian Hunter50accb92011-10-03 15:33:34 +03002118static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
2119{
2120 struct sdhci_host *host = mmc_priv(mmc);
2121
2122 sdhci_runtime_pm_get(host);
2123 sdhci_do_enable_preset_value(host, enable);
2124 sdhci_runtime_pm_put(host);
2125}
2126
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002127static int sdhci_stop_request(struct mmc_host *mmc)
2128{
2129 return -ENOSYS;
2130}
2131
David Brownellab7aefd2006-11-12 17:55:30 -08002132static const struct mmc_host_ops sdhci_ops = {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002133 .pre_req = sdhci_pre_req,
2134 .post_req = sdhci_post_req,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002135 .request = sdhci_request,
2136 .set_ios = sdhci_set_ios,
2137 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03002138 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002139 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302140 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302141 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302142 .enable_preset_value = sdhci_enable_preset_value,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302143 .enable = sdhci_enable,
2144 .disable = sdhci_disable,
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002145 .stop_request = sdhci_stop_request,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002146};
2147
2148/*****************************************************************************\
2149 * *
2150 * Tasklets *
2151 * *
2152\*****************************************************************************/
2153
2154static void sdhci_tasklet_card(unsigned long param)
2155{
2156 struct sdhci_host *host;
2157 unsigned long flags;
2158
2159 host = (struct sdhci_host*)param;
2160
2161 spin_lock_irqsave(&host->lock, flags);
2162
Adrian Hunter50accb92011-10-03 15:33:34 +03002163 /* Check host->mrq first in case we are runtime suspended */
2164 if (host->mrq &&
2165 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302166 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002167 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05302168 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002169 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002170
Adrian Hunter50accb92011-10-03 15:33:34 +03002171 sdhci_reset(host, SDHCI_RESET_CMD);
2172 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002173
Adrian Hunter50accb92011-10-03 15:33:34 +03002174 host->mrq->cmd->error = -ENOMEDIUM;
2175 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002176 }
2177
2178 spin_unlock_irqrestore(&host->lock, flags);
2179
Pierre Ossman04cf5852008-08-18 22:18:14 +02002180 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002181}
2182
2183static void sdhci_tasklet_finish(unsigned long param)
2184{
2185 struct sdhci_host *host;
2186 unsigned long flags;
2187 struct mmc_request *mrq;
2188
2189 host = (struct sdhci_host*)param;
2190
Adrian Hunter50accb92011-10-03 15:33:34 +03002191 spin_lock_irqsave(&host->lock, flags);
2192
Chris Ball0c9c99a2011-04-27 17:35:31 -04002193 /*
2194 * If this tasklet gets rescheduled while running, it will
2195 * be run again afterwards but without any active request.
2196 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002197 if (!host->mrq) {
2198 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002199 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002200 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002201
2202 del_timer(&host->timer);
2203
2204 mrq = host->mrq;
2205
Pierre Ossmand129bce2006-03-24 03:18:17 -08002206 /*
2207 * The controller needs a reset of internal state machines
2208 * upon error conditions.
2209 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002210 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002211 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002212 (mrq->data && (mrq->data->error ||
2213 (mrq->data->stop && mrq->data->stop->error))) ||
2214 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002215
2216 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002217 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002218 unsigned int clock;
2219
2220 /* This is to force an update */
2221 clock = host->clock;
2222 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302223 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002224 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302225 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002226 }
2227
2228 /* Spec says we should do both at the same time, but Ricoh
2229 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002230 sdhci_reset(host, SDHCI_RESET_CMD);
2231 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002232 } else {
2233 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2234 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002235 }
2236
2237 host->mrq = NULL;
2238 host->cmd = NULL;
2239 host->data = NULL;
2240
Pierre Ossmanf9134312008-12-21 17:01:48 +01002241#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002242 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002243#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002244
Pierre Ossman5f25a662006-10-04 02:15:39 -07002245 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002246 spin_unlock_irqrestore(&host->lock, flags);
2247
2248 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002249 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002250}
2251
2252static void sdhci_timeout_timer(unsigned long data)
2253{
2254 struct sdhci_host *host;
2255 unsigned long flags;
2256
2257 host = (struct sdhci_host*)data;
2258
2259 spin_lock_irqsave(&host->lock, flags);
2260
2261 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302262 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002263 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002264 sdhci_dumpregs(host);
2265
2266 if (host->data) {
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302267 pr_info("%s: bytes to transfer: %d transferred: %d\n",
2268 mmc_hostname(host->mmc),
2269 (host->data->blksz * host->data->blocks),
2270 (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) *
2271 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossman17b04292007-07-22 22:18:46 +02002272 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002273 sdhci_finish_data(host);
2274 } else {
2275 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002276 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002277 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002278 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002279
2280 tasklet_schedule(&host->finish_tasklet);
2281 }
2282 }
2283
Pierre Ossman5f25a662006-10-04 02:15:39 -07002284 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002285 spin_unlock_irqrestore(&host->lock, flags);
2286}
2287
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302288static void sdhci_tuning_timer(unsigned long data)
2289{
2290 struct sdhci_host *host;
2291 unsigned long flags;
2292
2293 host = (struct sdhci_host *)data;
2294
2295 spin_lock_irqsave(&host->lock, flags);
2296
2297 host->flags |= SDHCI_NEEDS_RETUNING;
2298
2299 spin_unlock_irqrestore(&host->lock, flags);
2300}
2301
Pierre Ossmand129bce2006-03-24 03:18:17 -08002302/*****************************************************************************\
2303 * *
2304 * Interrupt handling *
2305 * *
2306\*****************************************************************************/
2307
2308static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2309{
2310 BUG_ON(intmask == 0);
2311
2312 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302313 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002314 "though no command operation was in progress.\n",
2315 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002316 sdhci_dumpregs(host);
2317 return;
2318 }
2319
Pierre Ossman43b58b32007-07-25 23:15:27 +02002320 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002321 host->cmd->error = -ETIMEDOUT;
2322 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2323 SDHCI_INT_INDEX))
2324 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002325
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302326 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2327 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2328 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2329 if (intmask & SDHCI_INT_CRC) {
2330 sdhci_reset(host, SDHCI_RESET_CMD);
2331 host->cmd->error = 0;
2332 }
2333 }
2334 }
2335
Pierre Ossmane8095172008-07-25 01:09:08 +02002336 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002337 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002338 return;
2339 }
2340
2341 /*
2342 * The host can send and interrupt when the busy state has
2343 * ended, allowing us to wait without wasting CPU cycles.
2344 * Unfortunately this is overloaded on the "data complete"
2345 * interrupt, so we need to take some care when handling
2346 * it.
2347 *
2348 * Note: The 1.0 specification is a bit ambiguous about this
2349 * feature so there might be some problems with older
2350 * controllers.
2351 */
2352 if (host->cmd->flags & MMC_RSP_BUSY) {
2353 if (host->cmd->data)
2354 DBG("Cannot wait for busy signal when also "
2355 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002356 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002357 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002358
2359 /* The controller does not support the end-of-busy IRQ,
2360 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002361 }
2362
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302363 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2364 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2365 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2366 if (intmask & SDHCI_INT_CRC) {
2367 sdhci_finish_command(host);
2368 return;
2369 }
2370 }
2371 }
2372
Pierre Ossmane8095172008-07-25 01:09:08 +02002373 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002374 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002375}
2376
Ben Dooks6882a8c2009-06-14 13:52:38 +01002377static void sdhci_show_adma_error(struct sdhci_host *host)
2378{
2379 const char *name = mmc_hostname(host->mmc);
2380 u8 *desc = host->adma_desc;
2381 __le32 *dma;
2382 __le16 *len;
2383 u8 attr;
2384
2385 sdhci_dumpregs(host);
2386
2387 while (true) {
2388 dma = (__le32 *)(desc + 4);
2389 len = (__le16 *)(desc + 2);
2390 attr = *desc;
2391
Sahitya Tummala419b6c82013-04-12 12:28:29 +05302392 pr_info("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
Ben Dooks6882a8c2009-06-14 13:52:38 +01002393 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2394
2395 desc += 8;
2396
2397 if (attr & 2)
2398 break;
2399 }
2400}
Ben Dooks6882a8c2009-06-14 13:52:38 +01002401
Pierre Ossmand129bce2006-03-24 03:18:17 -08002402static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2403{
Girish K S2cd06dc2012-01-06 09:56:39 +05302404 u32 command;
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302405 bool pr_msg = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002406 BUG_ON(intmask == 0);
2407
Arindam Nathb513ea22011-05-05 12:19:04 +05302408 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2409 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302410 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2411 if (command == MMC_SEND_TUNING_BLOCK ||
2412 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302413 host->tuning_done = 1;
2414 wake_up(&host->buf_ready_int);
2415 return;
2416 }
2417 }
2418
Pierre Ossmand129bce2006-03-24 03:18:17 -08002419 if (!host->data) {
2420 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002421 * The "data complete" interrupt is also used to
2422 * indicate that a busy state has ended. See comment
2423 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002424 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002425 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2426 if (intmask & SDHCI_INT_DATA_END) {
2427 sdhci_finish_command(host);
2428 return;
2429 }
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302430 if (host->quirks2 &
2431 SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD)
2432 return;
Pierre Ossmane8095172008-07-25 01:09:08 +02002433 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002434
Sahitya Tummalaca422112013-02-22 12:15:54 +05302435 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002436 "though no data operation was in progress.\n",
2437 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002438 sdhci_dumpregs(host);
2439
2440 return;
2441 }
2442
2443 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002444 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002445 else if (intmask & SDHCI_INT_DATA_END_BIT)
2446 host->data->error = -EILSEQ;
2447 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2448 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2449 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002450 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002451 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302452 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002453 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002454 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002455 }
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302456 if (host->data->error) {
2457 if ((intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT)) &&
2458 (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING)) {
2459 command = SDHCI_GET_CMD(sdhci_readw(host,
2460 SDHCI_COMMAND));
2461 if ((command != MMC_SEND_TUNING_BLOCK_HS200) &&
2462 (command != MMC_SEND_TUNING_BLOCK))
2463 pr_msg = true;
2464 } else {
2465 pr_msg = true;
2466 }
2467 if (pr_msg) {
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302468 pr_err("%s: data txfr (0x%08x) error: %d after %lld ms\n",
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302469 mmc_hostname(host->mmc), intmask,
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302470 host->data->error, ktime_to_ms(ktime_sub(
2471 ktime_get(), host->data_start_time)));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302472 sdhci_dumpregs(host);
2473 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002474 sdhci_finish_data(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302475 } else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002476 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002477 sdhci_transfer_pio(host);
2478
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002479 /*
2480 * We currently don't do anything fancy with DMA
2481 * boundaries, but as we can't disable the feature
2482 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002483 *
2484 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2485 * should return a valid address to continue from, but as
2486 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002487 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002488 if (intmask & SDHCI_INT_DMA_END) {
2489 u32 dmastart, dmanow;
2490 dmastart = sg_dma_address(host->data->sg);
2491 dmanow = dmastart + host->data->bytes_xfered;
2492 /*
2493 * Force update to the next DMA block boundary.
2494 */
2495 dmanow = (dmanow &
2496 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2497 SDHCI_DEFAULT_BOUNDARY_SIZE;
2498 host->data->bytes_xfered = dmanow - dmastart;
2499 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2500 " next 0x%08x\n",
2501 mmc_hostname(host->mmc), dmastart,
2502 host->data->bytes_xfered, dmanow);
2503 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2504 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002505
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002506 if (intmask & SDHCI_INT_DATA_END) {
2507 if (host->cmd) {
2508 /*
2509 * Data managed to finish before the
2510 * command completed. Make sure we do
2511 * things in the proper order.
2512 */
2513 host->data_early = 1;
2514 } else {
2515 sdhci_finish_data(host);
2516 }
2517 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002518 }
2519}
2520
David Howells7d12e782006-10-05 14:55:46 +01002521static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002522{
2523 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002524 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002525 u32 intmask, unexpected = 0;
2526 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002527
2528 spin_lock(&host->lock);
2529
Adrian Hunter50accb92011-10-03 15:33:34 +03002530 if (host->runtime_suspended) {
2531 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302532 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002533 mmc_hostname(host->mmc));
2534 return IRQ_HANDLED;
2535 }
2536
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002537 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002538
Mark Lord62df67a2007-03-06 13:30:13 +01002539 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002540 result = IRQ_NONE;
2541 goto out;
2542 }
2543
Alexander Stein6379b232012-03-14 09:52:10 +01002544again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002545 DBG("*** %s got interrupt: 0x%08x\n",
2546 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002547
Pierre Ossman3192a282006-06-30 02:22:26 -07002548 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302549 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2550 SDHCI_CARD_PRESENT;
2551
2552 /*
2553 * There is a observation on i.mx esdhc. INSERT bit will be
2554 * immediately set again when it gets cleared, if a card is
2555 * inserted. We have to mask the irq to prevent interrupt
2556 * storm which will freeze the system. And the REMOVE gets
2557 * the same situation.
2558 *
2559 * More testing are needed here to ensure it works for other
2560 * platforms though.
2561 */
2562 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2563 SDHCI_INT_CARD_REMOVE);
2564 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2565 SDHCI_INT_CARD_INSERT);
2566
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002567 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302568 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2569 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002570 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002571 }
2572
Pierre Ossmand129bce2006-03-24 03:18:17 -08002573 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002574 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2575 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002576 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2577 (host->clock <= 400000))
2578 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002579 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002580 }
2581
2582 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002583 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2584 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002585 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2586 (host->clock <= 400000))
2587 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002588 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002589 }
2590
2591 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2592
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002593 intmask &= ~SDHCI_INT_ERROR;
2594
Pierre Ossmand129bce2006-03-24 03:18:17 -08002595 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302596 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002597 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002598 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002599 }
2600
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002601 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002602
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002603 if (intmask & SDHCI_INT_CARD_INT)
2604 cardint = 1;
2605
2606 intmask &= ~SDHCI_INT_CARD_INT;
2607
Pierre Ossman3192a282006-06-30 02:22:26 -07002608 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002609 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002610 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002611 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002612
2613 result = IRQ_HANDLED;
2614
Alexander Stein6379b232012-03-14 09:52:10 +01002615 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2616 if (intmask && --max_loops)
2617 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002618out:
2619 spin_unlock(&host->lock);
2620
Alexander Stein6379b232012-03-14 09:52:10 +01002621 if (unexpected) {
2622 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2623 mmc_hostname(host->mmc), unexpected);
2624 sdhci_dumpregs(host);
2625 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002626 /*
2627 * We have to delay this as it calls back into the driver.
2628 */
2629 if (cardint)
2630 mmc_signal_sdio_irq(host->mmc);
2631
Pierre Ossmand129bce2006-03-24 03:18:17 -08002632 return result;
2633}
2634
2635/*****************************************************************************\
2636 * *
2637 * Suspend/resume *
2638 * *
2639\*****************************************************************************/
2640
2641#ifdef CONFIG_PM
2642
Manuel Laussd72faa62011-11-03 11:09:45 +01002643int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002644{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002645 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302646 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002647
Chris Balla1b13b42012-02-06 00:43:59 -05002648 if (host->ops->platform_suspend)
2649 host->ops->platform_suspend(host);
2650
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002651 sdhci_disable_card_detection(host);
2652
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302653 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302654 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2655 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2656 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002657 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302658 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302659 }
2660
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002661 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302662 if (ret) {
2663 if (has_tuning_timer) {
2664 host->flags |= SDHCI_NEEDS_RETUNING;
2665 mod_timer(&host->tuning_timer, jiffies +
2666 host->tuning_count * HZ);
2667 }
2668
2669 sdhci_enable_card_detection(host);
2670
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002671 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302672 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002673
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002674 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002675
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002676 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002677}
2678
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002679EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002680
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002681int sdhci_resume_host(struct sdhci_host *host)
2682{
2683 int ret;
2684
Richard Röjforsa13abc72009-09-22 16:45:30 -07002685 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002686 if (host->ops->enable_dma)
2687 host->ops->enable_dma(host);
2688 }
2689
2690 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2691 mmc_hostname(host->mmc), host);
2692 if (ret)
2693 return ret;
2694
Adrian Hunter6308d292012-02-07 14:48:54 +02002695 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2696 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2697 /* Card keeps power but host controller does not */
2698 sdhci_init(host, 0);
2699 host->pwr = 0;
2700 host->clock = 0;
2701 sdhci_do_set_ios(host, &host->mmc->ios);
2702 } else {
2703 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2704 mmiowb();
2705 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002706
2707 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002708 sdhci_enable_card_detection(host);
2709
Chris Balla1b13b42012-02-06 00:43:59 -05002710 if (host->ops->platform_resume)
2711 host->ops->platform_resume(host);
2712
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302713 /* Set the re-tuning expiration flag */
2714 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2715 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2716 host->flags |= SDHCI_NEEDS_RETUNING;
2717
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002718 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002719}
2720
2721EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002722
Daniel Drake5f619702010-11-04 22:20:39 +00002723void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2724{
2725 u8 val;
2726 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2727 val |= SDHCI_WAKE_ON_INT;
2728 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2729}
2730
2731EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2732
Pierre Ossmand129bce2006-03-24 03:18:17 -08002733#endif /* CONFIG_PM */
2734
Adrian Hunter50accb92011-10-03 15:33:34 +03002735#ifdef CONFIG_PM_RUNTIME
2736
2737static int sdhci_runtime_pm_get(struct sdhci_host *host)
2738{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302739 if (!mmc_use_core_runtime_pm(host->mmc))
2740 return pm_runtime_get_sync(host->mmc->parent);
2741 else
2742 return 0;
Adrian Hunter50accb92011-10-03 15:33:34 +03002743}
2744
2745static int sdhci_runtime_pm_put(struct sdhci_host *host)
2746{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302747 if (!mmc_use_core_runtime_pm(host->mmc)) {
2748 pm_runtime_mark_last_busy(host->mmc->parent);
2749 return pm_runtime_put_autosuspend(host->mmc->parent);
2750 } else {
2751 return 0;
2752 }
Adrian Hunter50accb92011-10-03 15:33:34 +03002753}
2754
2755int sdhci_runtime_suspend_host(struct sdhci_host *host)
2756{
2757 unsigned long flags;
2758 int ret = 0;
2759
2760 /* Disable tuning since we are suspending */
2761 if (host->version >= SDHCI_SPEC_300 &&
2762 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2763 del_timer_sync(&host->tuning_timer);
2764 host->flags &= ~SDHCI_NEEDS_RETUNING;
2765 }
2766
2767 spin_lock_irqsave(&host->lock, flags);
2768 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2769 spin_unlock_irqrestore(&host->lock, flags);
2770
2771 synchronize_irq(host->irq);
2772
2773 spin_lock_irqsave(&host->lock, flags);
2774 host->runtime_suspended = true;
2775 spin_unlock_irqrestore(&host->lock, flags);
2776
2777 return ret;
2778}
2779EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2780
2781int sdhci_runtime_resume_host(struct sdhci_host *host)
2782{
2783 unsigned long flags;
2784 int ret = 0, host_flags = host->flags;
2785
2786 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2787 if (host->ops->enable_dma)
2788 host->ops->enable_dma(host);
2789 }
2790
2791 sdhci_init(host, 0);
2792
2793 /* Force clock and power re-program */
2794 host->pwr = 0;
2795 host->clock = 0;
2796 sdhci_do_set_ios(host, &host->mmc->ios);
2797
2798 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2799 if (host_flags & SDHCI_PV_ENABLED)
2800 sdhci_do_enable_preset_value(host, true);
2801
2802 /* Set the re-tuning expiration flag */
2803 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2804 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2805 host->flags |= SDHCI_NEEDS_RETUNING;
2806
2807 spin_lock_irqsave(&host->lock, flags);
2808
2809 host->runtime_suspended = false;
2810
2811 /* Enable SDIO IRQ */
2812 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2813 sdhci_enable_sdio_irq_nolock(host, true);
2814
2815 /* Enable Card Detection */
2816 sdhci_enable_card_detection(host);
2817
2818 spin_unlock_irqrestore(&host->lock, flags);
2819
2820 return ret;
2821}
2822EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2823
2824#endif
2825
Pierre Ossmand129bce2006-03-24 03:18:17 -08002826/*****************************************************************************\
2827 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002828 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002829 * *
2830\*****************************************************************************/
2831
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002832struct sdhci_host *sdhci_alloc_host(struct device *dev,
2833 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002834{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002835 struct mmc_host *mmc;
2836 struct sdhci_host *host;
2837
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002838 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002839
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002840 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002841 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002842 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002843
2844 host = mmc_priv(mmc);
2845 host->mmc = mmc;
2846
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002847 return host;
2848}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002849
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002850EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002851
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002852int sdhci_add_host(struct sdhci_host *host)
2853{
2854 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302855 u32 caps[2];
2856 u32 max_current_caps;
2857 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002858 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002859
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002860 WARN_ON(host == NULL);
2861 if (host == NULL)
2862 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002863
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002864 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002865
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002866 if (debug_quirks)
2867 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03002868 if (debug_quirks2)
2869 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002870
Pierre Ossmand96649e2006-06-30 02:22:30 -07002871 sdhci_reset(host, SDHCI_RESET_ALL);
2872
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002873 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002874 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2875 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002876 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302877 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002878 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002879 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002880 }
2881
Arindam Nathf2119df2011-05-05 12:18:57 +05302882 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002883 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002884
Arindam Nathf2119df2011-05-05 12:18:57 +05302885 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2886 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2887
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002888 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002889 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302890 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002891 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002892 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002893 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002894
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002895 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002896 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002897 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002898 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002899 }
2900
Arindam Nathf2119df2011-05-05 12:18:57 +05302901 if ((host->version >= SDHCI_SPEC_200) &&
2902 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002903 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002904
2905 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2906 (host->flags & SDHCI_USE_ADMA)) {
2907 DBG("Disabling ADMA as it is marked broken\n");
2908 host->flags &= ~SDHCI_USE_ADMA;
2909 }
2910
Richard Röjforsa13abc72009-09-22 16:45:30 -07002911 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002912 if (host->ops->enable_dma) {
2913 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302914 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002915 "available. Falling back to PIO.\n",
2916 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002917 host->flags &=
2918 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002919 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002920 }
2921 }
2922
Pierre Ossman2134a922008-06-28 18:28:51 +02002923 if (host->flags & SDHCI_USE_ADMA) {
2924 /*
2925 * We need to allocate descriptors for all sg entries
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302926 * (128/max_segments) and potentially one alignment transfer for
Pierre Ossman2134a922008-06-28 18:28:51 +02002927 * each of those entries.
2928 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302929 if (host->ops->get_max_segments)
2930 host->adma_max_desc = host->ops->get_max_segments();
2931 else
2932 host->adma_max_desc = 128;
2933
2934 host->adma_desc_sz = (host->adma_max_desc * 2 + 1) * 4;
2935 host->align_buf_sz = host->adma_max_desc * 4;
2936
2937 pr_debug("%s: %s: dma_desc_size: %d\n",
2938 mmc_hostname(host->mmc), __func__, host->adma_desc_sz);
2939 host->adma_desc = kmalloc(host->adma_desc_sz,
2940 GFP_KERNEL);
2941 host->align_buffer = kmalloc(host->align_buf_sz,
2942 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02002943 if (!host->adma_desc || !host->align_buffer) {
2944 kfree(host->adma_desc);
2945 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302946 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002947 "buffers. Falling back to standard DMA.\n",
2948 mmc_hostname(mmc));
2949 host->flags &= ~SDHCI_USE_ADMA;
2950 }
2951 }
2952
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002953 host->next_data.cookie = 1;
2954
Pierre Ossman76591502008-07-21 00:32:11 +02002955 /*
2956 * If we use DMA, then it's up to the caller to set the DMA
2957 * mask, but PIO does not need the hw shim so we set a new
2958 * mask here in that case.
2959 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002960 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002961 host->dma_mask = DMA_BIT_MASK(64);
2962 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2963 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002964
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002965 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302966 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002967 >> SDHCI_CLOCK_BASE_SHIFT;
2968 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302969 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002970 >> SDHCI_CLOCK_BASE_SHIFT;
2971
Pierre Ossmand129bce2006-03-24 03:18:17 -08002972 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002973 if (host->max_clk == 0 || host->quirks &
2974 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002975 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302976 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002977 "frequency.\n", mmc_hostname(mmc));
2978 return -ENODEV;
2979 }
2980 host->max_clk = host->ops->get_max_clock(host);
2981 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002982
2983 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302984 * In case of Host Controller v3.00, find out whether clock
2985 * multiplier is supported.
2986 */
2987 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2988 SDHCI_CLOCK_MUL_SHIFT;
2989
2990 /*
2991 * In case the value in Clock Multiplier is 0, then programmable
2992 * clock mode is not supported, otherwise the actual clock
2993 * multiplier is one more than the value of Clock Multiplier
2994 * in the Capabilities Register.
2995 */
2996 if (host->clk_mul)
2997 host->clk_mul += 1;
2998
2999 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003000 * Set host parameters.
3001 */
3002 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303003 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003004 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003005 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303006 else if (host->version >= SDHCI_SPEC_300) {
3007 if (host->clk_mul) {
3008 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3009 mmc->f_max = host->max_clk * host->clk_mul;
3010 } else
3011 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3012 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003013 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003014
Sahitya Tummalaca422112013-02-22 12:15:54 +05303015 host->timeout_clk =
3016 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
3017 if (host->timeout_clk == 0) {
3018 if (host->ops->get_timeout_clock) {
3019 host->timeout_clk = host->ops->get_timeout_clock(host);
3020 } else if (!(host->quirks &
3021 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3022 pr_err("%s: Hardware doesn't specify timeout clock "
3023 "frequency.\n", mmc_hostname(mmc));
3024 return -ENODEV;
3025 }
3026 }
3027 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3028 host->timeout_clk *= 1000;
3029
3030 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
3031 host->timeout_clk = mmc->f_max / 1000;
3032
Krishna Kondaa20d3362013-04-01 21:01:59 -07003033 if (!(host->quirks2 & SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE))
3034 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Sahitya Tummalaca422112013-02-22 12:15:54 +05303035
Andrei Warkentine89d4562011-05-23 15:06:37 -05003036 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
3037
3038 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3039 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003040
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003041 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003042 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003043 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003044 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003045 host->flags |= SDHCI_AUTO_CMD23;
3046 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3047 } else {
3048 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3049 }
3050
Philip Rakity15ec4462010-11-19 16:48:39 -05003051 /*
3052 * A controller may support 8-bit width, but the board itself
3053 * might not have the pins brought out. Boards that support
3054 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3055 * their platform code before calling sdhci_add_host(), and we
3056 * won't assume 8-bit width for hosts without that CAP.
3057 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003058 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003059 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003060
Arindam Nathf2119df2011-05-05 12:18:57 +05303061 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003062 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003063
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003064 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3065 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003066 mmc->caps |= MMC_CAP_NEEDS_POLL;
3067
Al Cooper4188bba2012-03-16 15:54:17 -04003068 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3069 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3070 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303071 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3072
3073 /* SDR104 supports also implies SDR50 support */
3074 if (caps[1] & SDHCI_SUPPORT_SDR104)
3075 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3076 else if (caps[1] & SDHCI_SUPPORT_SDR50)
3077 mmc->caps |= MMC_CAP_UHS_SDR50;
3078
3079 if (caps[1] & SDHCI_SUPPORT_DDR50)
3080 mmc->caps |= MMC_CAP_UHS_DDR50;
3081
Girish K S2cd06dc2012-01-06 09:56:39 +05303082 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303083 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3084 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3085
Girish K S2cd06dc2012-01-06 09:56:39 +05303086 /* Does the host need tuning for HS200? */
3087 if (mmc->caps2 & MMC_CAP2_HS200)
3088 host->flags |= SDHCI_HS200_NEEDS_TUNING;
3089
Arindam Nathd6d50a12011-05-05 12:18:59 +05303090 /* Driver Type(s) (A, C, D) supported by the host */
3091 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3092 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3093 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3094 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3095 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3096 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3097
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02003098 /* Initial value for re-tuning timer count */
3099 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3100 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3101
3102 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303103 * In case Re-tuning Timer is not disabled, the actual value of
3104 * re-tuning timer will be 2 ^ (n - 1).
3105 */
3106 if (host->tuning_count)
3107 host->tuning_count = 1 << (host->tuning_count - 1);
3108
3109 /* Re-tuning mode supported by the Host Controller */
3110 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3111 SDHCI_RETUNING_MODE_SHIFT;
3112
Takashi Iwai8f230f42010-12-08 10:04:30 +01003113 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05303114 /*
3115 * According to SD Host Controller spec v3.00, if the Host System
3116 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3117 * the value is meaningful only if Voltage Support in the Capabilities
3118 * register is set. The actual current value is 4 times the register
3119 * value.
3120 */
3121 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3122
3123 if (caps[0] & SDHCI_CAN_VDD_330) {
3124 int max_current_330;
3125
Takashi Iwai8f230f42010-12-08 10:04:30 +01003126 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303127
3128 max_current_330 = ((max_current_caps &
3129 SDHCI_MAX_CURRENT_330_MASK) >>
3130 SDHCI_MAX_CURRENT_330_SHIFT) *
3131 SDHCI_MAX_CURRENT_MULTIPLIER;
3132
3133 if (max_current_330 > 150)
3134 mmc->caps |= MMC_CAP_SET_XPC_330;
3135 }
3136 if (caps[0] & SDHCI_CAN_VDD_300) {
3137 int max_current_300;
3138
Takashi Iwai8f230f42010-12-08 10:04:30 +01003139 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303140
3141 max_current_300 = ((max_current_caps &
3142 SDHCI_MAX_CURRENT_300_MASK) >>
3143 SDHCI_MAX_CURRENT_300_SHIFT) *
3144 SDHCI_MAX_CURRENT_MULTIPLIER;
3145
3146 if (max_current_300 > 150)
3147 mmc->caps |= MMC_CAP_SET_XPC_300;
3148 }
3149 if (caps[0] & SDHCI_CAN_VDD_180) {
3150 int max_current_180;
3151
Takashi Iwai8f230f42010-12-08 10:04:30 +01003152 ocr_avail |= MMC_VDD_165_195;
3153
Arindam Nathf2119df2011-05-05 12:18:57 +05303154 max_current_180 = ((max_current_caps &
3155 SDHCI_MAX_CURRENT_180_MASK) >>
3156 SDHCI_MAX_CURRENT_180_SHIFT) *
3157 SDHCI_MAX_CURRENT_MULTIPLIER;
3158
3159 if (max_current_180 > 150)
3160 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05303161
3162 /* Maximum current capabilities of the host at 1.8V */
3163 if (max_current_180 >= 800)
3164 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
3165 else if (max_current_180 >= 600)
3166 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
3167 else if (max_current_180 >= 400)
3168 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
3169 else
3170 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05303171 }
3172
Takashi Iwai8f230f42010-12-08 10:04:30 +01003173 mmc->ocr_avail = ocr_avail;
3174 mmc->ocr_avail_sdio = ocr_avail;
3175 if (host->ocr_avail_sdio)
3176 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3177 mmc->ocr_avail_sd = ocr_avail;
3178 if (host->ocr_avail_sd)
3179 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3180 else /* normal SD controllers don't support 1.8V */
3181 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3182 mmc->ocr_avail_mmc = ocr_avail;
3183 if (host->ocr_avail_mmc)
3184 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003185
3186 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303187 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003188 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003189 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003190 }
3191
Pierre Ossmand129bce2006-03-24 03:18:17 -08003192 spin_lock_init(&host->lock);
3193
3194 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003195 * Maximum number of segments. Depends on if the hardware
3196 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003197 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003198 if (host->flags & SDHCI_USE_ADMA)
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303199 mmc->max_segs = host->adma_max_desc;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003200 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003201 mmc->max_segs = 1;
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303202 else/* PIO */
3203 mmc->max_segs = host->adma_max_desc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003204
3205 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003206 * Maximum number of sectors in one transfer. Limited by DMA boundary
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303207 * size (512KiB), unless specified by platform specific driver. Each
3208 * descriptor can transfer a maximum of 64KB.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003209 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303210 if (host->ops->get_max_segments)
3211 mmc->max_req_size = (host->adma_max_desc * 65536);
3212 else
3213 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003214
3215 /*
3216 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003217 * of bytes. When doing hardware scatter/gather, each entry cannot
3218 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003219 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003220 if (host->flags & SDHCI_USE_ADMA) {
3221 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3222 mmc->max_seg_size = 65535;
3223 else
3224 mmc->max_seg_size = 65536;
3225 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003226 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003227 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003228
3229 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003230 * Maximum block size. This varies from controller to controller and
3231 * is specified in the capabilities register.
3232 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003233 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3234 mmc->max_blk_size = 2;
3235 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303236 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003237 SDHCI_MAX_BLOCK_SHIFT;
3238 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303239 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003240 "assuming 512 bytes\n", mmc_hostname(mmc));
3241 mmc->max_blk_size = 0;
3242 }
3243 }
3244
3245 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003246
3247 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003248 * Maximum block count.
3249 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003250 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003251
3252 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003253 * Init tasklets.
3254 */
3255 tasklet_init(&host->card_tasklet,
3256 sdhci_tasklet_card, (unsigned long)host);
3257 tasklet_init(&host->finish_tasklet,
3258 sdhci_tasklet_finish, (unsigned long)host);
3259
Al Viroe4cad1b2006-10-10 22:47:07 +01003260 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003261
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303262 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303263 init_waitqueue_head(&host->buf_ready_int);
3264
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303265 /* Initialize re-tuning timer */
3266 init_timer(&host->tuning_timer);
3267 host->tuning_timer.data = (unsigned long)host;
3268 host->tuning_timer.function = sdhci_tuning_timer;
3269 }
3270
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003271 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003272 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003273 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003274 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003275
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003276 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3277 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303278 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003279 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003280 }
3281
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003282 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003283
3284#ifdef CONFIG_MMC_DEBUG
3285 sdhci_dumpregs(host);
3286#endif
3287
Pierre Ossmanf9134312008-12-21 17:01:48 +01003288#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003289 snprintf(host->led_name, sizeof(host->led_name),
3290 "%s::", mmc_hostname(mmc));
3291 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003292 host->led.brightness = LED_OFF;
3293 host->led.default_trigger = mmc_hostname(mmc);
3294 host->led.brightness_set = sdhci_led_control;
3295
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003296 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003297 if (ret)
3298 goto reset;
3299#endif
3300
Pierre Ossman5f25a662006-10-04 02:15:39 -07003301 mmiowb();
3302
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303303 if (host->cpu_dma_latency_us)
3304 pm_qos_add_request(&host->pm_qos_req_dma,
3305 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003306 mmc_add_host(mmc);
3307
Sahitya Tummalaca422112013-02-22 12:15:54 +05303308 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003309 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003310 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3311 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003312
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003313 sdhci_enable_card_detection(host);
3314
Pierre Ossmand129bce2006-03-24 03:18:17 -08003315 return 0;
3316
Pierre Ossmanf9134312008-12-21 17:01:48 +01003317#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003318reset:
3319 sdhci_reset(host, SDHCI_RESET_ALL);
3320 free_irq(host->irq, host);
3321#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003322untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003323 tasklet_kill(&host->card_tasklet);
3324 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003325
3326 return ret;
3327}
3328
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003329EXPORT_SYMBOL_GPL(sdhci_add_host);
3330
Pierre Ossman1e728592008-04-16 19:13:13 +02003331void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003332{
Pierre Ossman1e728592008-04-16 19:13:13 +02003333 unsigned long flags;
3334
3335 if (dead) {
3336 spin_lock_irqsave(&host->lock, flags);
3337
3338 host->flags |= SDHCI_DEVICE_DEAD;
3339
3340 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303341 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003342 " transfer!\n", mmc_hostname(host->mmc));
3343
3344 host->mrq->cmd->error = -ENOMEDIUM;
3345 tasklet_schedule(&host->finish_tasklet);
3346 }
3347
3348 spin_unlock_irqrestore(&host->lock, flags);
3349 }
3350
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003351 sdhci_disable_card_detection(host);
3352
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303353 if (host->cpu_dma_latency_us)
3354 pm_qos_remove_request(&host->pm_qos_req_dma);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003355 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003356
Pierre Ossmanf9134312008-12-21 17:01:48 +01003357#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003358 led_classdev_unregister(&host->led);
3359#endif
3360
Pierre Ossman1e728592008-04-16 19:13:13 +02003361 if (!dead)
3362 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003363
3364 free_irq(host->irq, host);
3365
3366 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303367 if (host->version >= SDHCI_SPEC_300)
3368 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003369
3370 tasklet_kill(&host->card_tasklet);
3371 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003372
Sahitya Tummalaca422112013-02-22 12:15:54 +05303373 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003374 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003375
Pierre Ossman2134a922008-06-28 18:28:51 +02003376 kfree(host->adma_desc);
3377 kfree(host->align_buffer);
3378
3379 host->adma_desc = NULL;
3380 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003381}
3382
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003383EXPORT_SYMBOL_GPL(sdhci_remove_host);
3384
3385void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003386{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003387 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003388}
3389
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003390EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003391
3392/*****************************************************************************\
3393 * *
3394 * Driver init/exit *
3395 * *
3396\*****************************************************************************/
3397
3398static int __init sdhci_drv_init(void)
3399{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303400 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003401 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303402 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003403
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003404 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003405}
3406
3407static void __exit sdhci_drv_exit(void)
3408{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003409}
3410
3411module_init(sdhci_drv_init);
3412module_exit(sdhci_drv_exit);
3413
Pierre Ossmandf673b22006-06-30 02:22:31 -07003414module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003415module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003416
Pierre Ossman32710e82009-04-08 20:14:54 +02003417MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003418MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003419MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003420
Pierre Ossmandf673b22006-06-30 02:22:31 -07003421MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003422MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");