blob: 904c255079e1b588e21fe5493cb8b4994b654e71 [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 Tummalaca422112013-02-22 12:15:54 +0530744 if (count >= 0xF) {
Chris Ball34ca2092012-06-01 10:39:45 -0400745 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
746 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700747 count = 0xE;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530748 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700749
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200750 return count;
751}
752
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300753static void sdhci_set_transfer_irqs(struct sdhci_host *host)
754{
755 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
756 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
757
758 if (host->flags & SDHCI_REQ_USE_DMA)
759 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
760 else
761 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
762}
763
Andrei Warkentina3c77782011-04-11 16:13:42 -0500764static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200765{
766 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200767 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500768 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200769 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200770
771 WARN_ON(host->data);
772
Andrei Warkentina3c77782011-04-11 16:13:42 -0500773 if (data || (cmd->flags & MMC_RSP_BUSY)) {
774 count = sdhci_calc_timeout(host, cmd);
775 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
776 }
777
778 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200779 return;
780
781 /* Sanity checks */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530782 BUG_ON(data->blksz * data->blocks > host->mmc->max_req_size);
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200783 BUG_ON(data->blksz > host->mmc->max_blk_size);
784 BUG_ON(data->blocks > 65535);
785
786 host->data = data;
787 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400788 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200789
Richard Röjforsa13abc72009-09-22 16:45:30 -0700790 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100791 host->flags |= SDHCI_REQ_USE_DMA;
792
Pierre Ossman2134a922008-06-28 18:28:51 +0200793 /*
794 * FIXME: This doesn't account for merging when mapping the
795 * scatterlist.
796 */
797 if (host->flags & SDHCI_REQ_USE_DMA) {
798 int broken, i;
799 struct scatterlist *sg;
800
801 broken = 0;
802 if (host->flags & SDHCI_USE_ADMA) {
803 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
804 broken = 1;
805 } else {
806 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
807 broken = 1;
808 }
809
810 if (unlikely(broken)) {
811 for_each_sg(data->sg, sg, data->sg_len, i) {
812 if (sg->length & 0x3) {
813 DBG("Reverting to PIO because of "
814 "transfer size (%d)\n",
815 sg->length);
816 host->flags &= ~SDHCI_REQ_USE_DMA;
817 break;
818 }
819 }
820 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100821 }
822
823 /*
824 * The assumption here being that alignment is the same after
825 * translation to device address space.
826 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200827 if (host->flags & SDHCI_REQ_USE_DMA) {
828 int broken, i;
829 struct scatterlist *sg;
830
831 broken = 0;
832 if (host->flags & SDHCI_USE_ADMA) {
833 /*
834 * As we use 3 byte chunks to work around
835 * alignment problems, we need to check this
836 * quirk.
837 */
838 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
839 broken = 1;
840 } else {
841 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
842 broken = 1;
843 }
844
845 if (unlikely(broken)) {
846 for_each_sg(data->sg, sg, data->sg_len, i) {
847 if (sg->offset & 0x3) {
848 DBG("Reverting to PIO because of "
849 "bad alignment\n");
850 host->flags &= ~SDHCI_REQ_USE_DMA;
851 break;
852 }
853 }
854 }
855 }
856
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200857 if (host->flags & SDHCI_REQ_USE_DMA) {
858 if (host->flags & SDHCI_USE_ADMA) {
859 ret = sdhci_adma_table_pre(host, data);
860 if (ret) {
861 /*
862 * This only happens when someone fed
863 * us an invalid request.
864 */
865 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200866 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200867 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300868 sdhci_writel(host, host->adma_addr,
869 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200870 }
871 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300872 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200873
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800874 sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300875 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200876 /*
877 * This only happens when someone fed
878 * us an invalid request.
879 */
880 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200881 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200882 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200883 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300884 sdhci_writel(host, sg_dma_address(data->sg),
885 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200886 }
887 }
888 }
889
Pierre Ossman2134a922008-06-28 18:28:51 +0200890 /*
891 * Always adjust the DMA selection as some controllers
892 * (e.g. JMicron) can't do PIO properly when the selection
893 * is ADMA.
894 */
895 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300896 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200897 ctrl &= ~SDHCI_CTRL_DMA_MASK;
898 if ((host->flags & SDHCI_REQ_USE_DMA) &&
899 (host->flags & SDHCI_USE_ADMA))
900 ctrl |= SDHCI_CTRL_ADMA32;
901 else
902 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300903 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100904 }
905
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200906 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200907 int flags;
908
909 flags = SG_MITER_ATOMIC;
910 if (host->data->flags & MMC_DATA_READ)
911 flags |= SG_MITER_TO_SG;
912 else
913 flags |= SG_MITER_FROM_SG;
914 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200915 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800916 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700917
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300918 sdhci_set_transfer_irqs(host);
919
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400920 /* Set the DMA boundary value and block size */
921 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
922 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300923 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700924}
925
926static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500927 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700928{
929 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500930 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700931
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700932 if (data == NULL)
933 return;
934
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200935 WARN_ON(!host->data);
936
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700937 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500938 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
939 mode |= SDHCI_TRNS_MULTI;
940 /*
941 * If we are sending CMD23, CMD12 never gets sent
942 * on successful completion (so no Auto-CMD12).
943 */
944 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
945 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500946 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
947 mode |= SDHCI_TRNS_AUTO_CMD23;
948 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
949 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700950 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500951
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530952 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700953 mode |= SDHCI_TRNS_READ;
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530954 if (host->ops->toggle_cdr)
955 host->ops->toggle_cdr(host, true);
956 }
957 if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE))
958 host->ops->toggle_cdr(host, false);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100959 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700960 mode |= SDHCI_TRNS_DMA;
961
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300962 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800963}
964
965static void sdhci_finish_data(struct sdhci_host *host)
966{
967 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800968
969 BUG_ON(!host->data);
970
971 data = host->data;
972 host->data = NULL;
973
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100974 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200975 if (host->flags & SDHCI_USE_ADMA)
976 sdhci_adma_table_post(host, data);
977 else {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800978 if (!data->host_cookie)
979 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
980 data->sg_len,
981 (data->flags & MMC_DATA_READ) ?
982 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200983 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800984 }
985
986 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200987 * The specification states that the block count register must
988 * be updated, but it does not specify at what point in the
989 * data flow. That makes the register entirely useless to read
990 * back so we have to assume that nothing made it to the card
991 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800992 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200993 if (data->error)
994 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800995 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200996 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997
Andrei Warkentine89d4562011-05-23 15:06:37 -0500998 /*
999 * Need to send CMD12 if -
1000 * a) open-ended multiblock transfer (no CMD23)
1001 * b) error in multiblock transfer
1002 */
1003 if (data->stop &&
1004 (data->error ||
1005 !host->mrq->sbc)) {
1006
Pierre Ossmand129bce2006-03-24 03:18:17 -08001007 /*
1008 * The controller needs a reset of internal state machines
1009 * upon error conditions.
1010 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001011 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012 sdhci_reset(host, SDHCI_RESET_CMD);
1013 sdhci_reset(host, SDHCI_RESET_DATA);
1014 }
1015
1016 sdhci_send_command(host, data->stop);
1017 } else
1018 tasklet_schedule(&host->finish_tasklet);
1019}
1020
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301021#define SDHCI_REQUEST_TIMEOUT 10 /* Default request timeout in seconds */
1022
Pierre Ossmand129bce2006-03-24 03:18:17 -08001023static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
1024{
1025 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001026 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001027 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001028
1029 WARN_ON(host->cmd);
1030
Pierre Ossmand129bce2006-03-24 03:18:17 -08001031 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001032 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001033
1034 mask = SDHCI_CMD_INHIBIT;
1035 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1036 mask |= SDHCI_DATA_INHIBIT;
1037
1038 /* We shouldn't wait for data inihibit for stop commands, even
1039 though they might use busy signaling */
1040 if (host->mrq->data && (cmd == host->mrq->data->stop))
1041 mask &= ~SDHCI_DATA_INHIBIT;
1042
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001043 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001044 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301045 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001046 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001048 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001049 tasklet_schedule(&host->finish_tasklet);
1050 return;
1051 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001052 timeout--;
1053 mdelay(1);
1054 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001055
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301056 mod_timer(&host->timer, jiffies + SDHCI_REQUEST_TIMEOUT * HZ);
1057
1058 if (cmd->cmd_timeout_ms > SDHCI_REQUEST_TIMEOUT * MSEC_PER_SEC)
1059 mod_timer(&host->timer, jiffies +
1060 (msecs_to_jiffies(cmd->cmd_timeout_ms * 2)));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001061
1062 host->cmd = cmd;
1063
Andrei Warkentina3c77782011-04-11 16:13:42 -05001064 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001065
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001066 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001067
Andrei Warkentine89d4562011-05-23 15:06:37 -05001068 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001069
Pierre Ossmand129bce2006-03-24 03:18:17 -08001070 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301071 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001073 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001074 tasklet_schedule(&host->finish_tasklet);
1075 return;
1076 }
1077
1078 if (!(cmd->flags & MMC_RSP_PRESENT))
1079 flags = SDHCI_CMD_RESP_NONE;
1080 else if (cmd->flags & MMC_RSP_136)
1081 flags = SDHCI_CMD_RESP_LONG;
1082 else if (cmd->flags & MMC_RSP_BUSY)
1083 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1084 else
1085 flags = SDHCI_CMD_RESP_SHORT;
1086
1087 if (cmd->flags & MMC_RSP_CRC)
1088 flags |= SDHCI_CMD_CRC;
1089 if (cmd->flags & MMC_RSP_OPCODE)
1090 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301091
1092 /* CMD19 is special in that the Data Present Select should be set */
Girish K S2cd06dc2012-01-06 09:56:39 +05301093 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1094 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001095 flags |= SDHCI_CMD_DATA;
1096
Sahitya Tummala48b458e2013-04-08 12:53:44 +05301097 if (cmd->data)
1098 host->data_start_time = ktime_get();
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001099 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001100}
1101
1102static void sdhci_finish_command(struct sdhci_host *host)
1103{
1104 int i;
1105
1106 BUG_ON(host->cmd == NULL);
1107
1108 if (host->cmd->flags & MMC_RSP_PRESENT) {
1109 if (host->cmd->flags & MMC_RSP_136) {
1110 /* CRC is stripped so we need to do some shifting. */
1111 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001112 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001113 SDHCI_RESPONSE + (3-i)*4) << 8;
1114 if (i != 3)
1115 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001116 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001117 SDHCI_RESPONSE + (3-i)*4-1);
1118 }
1119 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001120 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001121 }
1122 }
1123
Pierre Ossman17b04292007-07-22 22:18:46 +02001124 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001125
Andrei Warkentine89d4562011-05-23 15:06:37 -05001126 /* Finished CMD23, now send actual command. */
1127 if (host->cmd == host->mrq->sbc) {
1128 host->cmd = NULL;
1129 sdhci_send_command(host, host->mrq->cmd);
1130 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001131
Andrei Warkentine89d4562011-05-23 15:06:37 -05001132 /* Processed actual command. */
1133 if (host->data && host->data_early)
1134 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001135
Andrei Warkentine89d4562011-05-23 15:06:37 -05001136 if (!host->cmd->data)
1137 tasklet_schedule(&host->finish_tasklet);
1138
1139 host->cmd = NULL;
1140 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001141}
1142
1143static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1144{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301145 int div = 0; /* Initialized for compiler warning */
Sahitya Tummalaca422112013-02-22 12:15:54 +05301146 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301147 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001148 unsigned long timeout;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301149 unsigned long flags;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001150
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301151 spin_lock_irqsave(&host->lock, flags);
Todd Poynor30832ab2011-12-27 15:48:46 +02001152 if (clock && clock == host->clock)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301153 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001154
Sahitya Tummalaca422112013-02-22 12:15:54 +05301155 host->mmc->actual_clock = 0;
1156
Anton Vorontsov81146342009-03-17 00:13:59 +03001157 if (host->ops->set_clock) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301158 spin_unlock_irqrestore(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001159 host->ops->set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301160 spin_lock_irqsave(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001161 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301162 goto ret;
Anton Vorontsov81146342009-03-17 00:13:59 +03001163 }
1164
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301165 if (host->clock)
1166 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001167
1168 if (clock == 0)
1169 goto out;
1170
Zhangfei Gao85105c52010-08-06 07:10:01 +08001171 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301172 /*
1173 * Check if the Host Controller supports Programmable Clock
1174 * Mode.
1175 */
1176 if (host->clk_mul) {
1177 u16 ctrl;
1178
1179 /*
1180 * We need to figure out whether the Host Driver needs
1181 * to select Programmable Clock Mode, or the value can
1182 * be set automatically by the Host Controller based on
1183 * the Preset Value registers.
1184 */
1185 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1186 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1187 for (div = 1; div <= 1024; div++) {
1188 if (((host->max_clk * host->clk_mul) /
1189 div) <= clock)
1190 break;
1191 }
1192 /*
1193 * Set Programmable Clock Mode in the Clock
1194 * Control register.
1195 */
1196 clk = SDHCI_PROG_CLOCK_MODE;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301197 real_div = div;
1198 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301199 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001200 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301201 } else {
1202 /* Version 3.00 divisors must be a multiple of 2. */
1203 if (host->max_clk <= clock)
1204 div = 1;
1205 else {
1206 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1207 div += 2) {
1208 if ((host->max_clk / div) <= clock)
1209 break;
1210 }
1211 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301212 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301213 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001214 }
1215 } else {
1216 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001217 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001218 if ((host->max_clk / div) <= clock)
1219 break;
1220 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301221 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301222 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001223 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001224
Sahitya Tummalaca422112013-02-22 12:15:54 +05301225 if (real_div)
1226 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1227
Sahitya Tummala00240122013-02-28 19:50:51 +05301228 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
1229 div = 0;
1230
Arindam Nathc3ed3872011-05-05 12:19:06 +05301231 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001232 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1233 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001234 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001235 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001236
Chris Ball27f6cb12009-09-22 16:45:31 -07001237 /* Wait max 20 ms */
1238 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001239 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001240 & SDHCI_CLOCK_INT_STABLE)) {
1241 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301242 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001243 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001244 sdhci_dumpregs(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301245 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001246 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001247 timeout--;
1248 mdelay(1);
1249 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001250
1251 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001252 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001253
1254out:
1255 host->clock = clock;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301256ret:
1257 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001258}
1259
Sahitya Tummalaca422112013-02-22 12:15:54 +05301260static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001261{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001262 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001263
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001264 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001265 switch (1 << power) {
1266 case MMC_VDD_165_195:
1267 pwr = SDHCI_POWER_180;
1268 break;
1269 case MMC_VDD_29_30:
1270 case MMC_VDD_30_31:
1271 pwr = SDHCI_POWER_300;
1272 break;
1273 case MMC_VDD_32_33:
1274 case MMC_VDD_33_34:
1275 pwr = SDHCI_POWER_330;
1276 break;
1277 default:
1278 BUG();
1279 }
1280 }
1281
1282 if (host->pwr == pwr)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301283 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001284
Pierre Ossmanae628902009-05-03 20:45:03 +02001285 host->pwr = pwr;
1286
1287 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001288 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301289 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301290 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301291 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001292 }
1293
1294 /*
1295 * Spec says that we should clear the power reg before setting
1296 * a new value. Some controllers don't seem to like this though.
1297 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301298 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001299 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301300 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301301 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301302 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001303
Andres Salomone08c1692008-07-04 10:00:03 -07001304 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001305 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001306 * and set turn on power at the same time, so set the voltage first.
1307 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301308 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001309 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301310 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301311 host->ops->check_power_status(host, REQ_BUS_ON);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301312 }
Pierre Ossmanae628902009-05-03 20:45:03 +02001313
1314 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001315
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001316 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301317 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301318 host->ops->check_power_status(host, REQ_BUS_ON);
Harald Welte557b0692009-06-18 16:53:38 +02001319
1320 /*
1321 * Some controllers need an extra 10ms delay of 10ms before they
1322 * can apply clock after applying power
1323 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001324 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001325 mdelay(10);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301326
1327 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001328}
1329
Pierre Ossmand129bce2006-03-24 03:18:17 -08001330/*****************************************************************************\
1331 * *
1332 * MMC callbacks *
1333 * *
1334\*****************************************************************************/
1335
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301336static int sdhci_enable(struct mmc_host *mmc)
1337{
1338 struct sdhci_host *host = mmc_priv(mmc);
1339
1340 if (host->cpu_dma_latency_us)
1341 pm_qos_update_request(&host->pm_qos_req_dma,
1342 host->cpu_dma_latency_us);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301343 if (host->ops->platform_bus_voting)
1344 host->ops->platform_bus_voting(host, 1);
1345
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301346 return 0;
1347}
1348
1349static int sdhci_disable(struct mmc_host *mmc)
1350{
1351 struct sdhci_host *host = mmc_priv(mmc);
1352
1353 if (host->cpu_dma_latency_us)
1354 pm_qos_update_request(&host->pm_qos_req_dma,
1355 PM_QOS_DEFAULT_VALUE);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301356 if (host->ops->platform_bus_voting)
1357 host->ops->platform_bus_voting(host, 0);
1358
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301359 return 0;
1360}
1361
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001362static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1363 bool is_first_req)
1364{
1365 struct sdhci_host *host = mmc_priv(mmc);
1366
1367 if (mrq->data->host_cookie) {
1368 mrq->data->host_cookie = 0;
1369 return;
1370 }
1371
1372 if (host->flags & SDHCI_REQ_USE_DMA)
1373 if (sdhci_pre_dma_transfer(host, mrq->data, &host->next_data) < 0)
1374 mrq->data->host_cookie = 0;
1375}
1376
1377static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1378 int err)
1379{
1380 struct sdhci_host *host = mmc_priv(mmc);
1381 struct mmc_data *data = mrq->data;
1382
1383 if (host->flags & SDHCI_REQ_USE_DMA) {
1384 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1385 (data->flags & MMC_DATA_WRITE) ?
1386 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1387 data->host_cookie = 0;
1388 }
1389}
1390
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301391static bool sdhci_check_state(struct sdhci_host *host)
1392{
1393 struct mmc_host *mmc = host->mmc;
1394
1395 if (!host->clock || !host->pwr ||
1396 pm_runtime_suspended(mmc->parent))
1397 return true;
1398 else
1399 return false;
1400}
1401
Pierre Ossmand129bce2006-03-24 03:18:17 -08001402static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1403{
1404 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001405 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001406 unsigned long flags;
1407
1408 host = mmc_priv(mmc);
1409
Adrian Hunter50accb92011-10-03 15:33:34 +03001410 sdhci_runtime_pm_get(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301411 if (sdhci_check_state(host)) {
1412 sdhci_dump_state(host);
1413 WARN(1, "sdhci in bad state");
1414 mrq->cmd->error = -EIO;
1415 if (mrq->data)
1416 mrq->data->error = -EIO;
1417 tasklet_schedule(&host->finish_tasklet);
1418 return;
1419 }
Adrian Hunter50accb92011-10-03 15:33:34 +03001420
Pierre Ossmand129bce2006-03-24 03:18:17 -08001421 spin_lock_irqsave(&host->lock, flags);
1422
1423 WARN_ON(host->mrq != NULL);
1424
Pierre Ossmanf9134312008-12-21 17:01:48 +01001425#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001426 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001427#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001428
1429 /*
1430 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1431 * requests if Auto-CMD12 is enabled.
1432 */
1433 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001434 if (mrq->stop) {
1435 mrq->data->stop = NULL;
1436 mrq->stop = NULL;
1437 }
1438 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001439
1440 host->mrq = mrq;
1441
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001442 /* If polling, assume that the card is always present. */
1443 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1444 present = true;
1445 else
1446 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1447 SDHCI_CARD_PRESENT;
1448
1449 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001450 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001451 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301452 } else {
1453 u32 present_state;
1454
1455 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1456 /*
1457 * Check if the re-tuning timer has already expired and there
1458 * is no on-going data transfer. If so, we need to execute
1459 * tuning procedure before sending command.
1460 */
1461 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1462 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1463 spin_unlock_irqrestore(&host->lock, flags);
Girish K S2cd06dc2012-01-06 09:56:39 +05301464 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301465 spin_lock_irqsave(&host->lock, flags);
1466
1467 /* Restore original mmc_request structure */
1468 host->mrq = mrq;
1469 }
1470
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001471 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001472 sdhci_send_command(host, mrq->sbc);
1473 else
1474 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301475 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001476
Pierre Ossman5f25a662006-10-04 02:15:39 -07001477 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001478 spin_unlock_irqrestore(&host->lock, flags);
1479}
1480
Adrian Hunter50accb92011-10-03 15:33:34 +03001481static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001482{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001483 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301484 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001485 u8 ctrl;
1486
Sahitya Tummalaca422112013-02-22 12:15:54 +05301487 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301488 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1489 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1490 return;
1491 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001492
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301493 if (ios->clock)
1494 sdhci_set_clock(host, ios->clock);
1495
1496 spin_lock_irqsave(&host->lock, flags);
1497 if (!host->clock) {
1498 spin_unlock_irqrestore(&host->lock, flags);
1499 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001500 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301501 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001502
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301503 if (ios->power_mode & (MMC_POWER_UP | MMC_POWER_ON))
Sahitya Tummalaca422112013-02-22 12:15:54 +05301504 vdd_bit = sdhci_set_power(host, ios->vdd);
1505
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301506 if (host->vmmc && vdd_bit != -1)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301507 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301508
1509 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001510
Philip Rakity643a81f2010-09-23 08:24:32 -07001511 if (host->ops->platform_send_init_74_clocks)
1512 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1513
Philip Rakity15ec4462010-11-19 16:48:39 -05001514 /*
1515 * If your platform has 8-bit width support but is not a v3 controller,
1516 * or if it requires special setup code, you should implement that in
1517 * platform_8bit_width().
1518 */
1519 if (host->ops->platform_8bit_width)
1520 host->ops->platform_8bit_width(host, ios->bus_width);
1521 else {
1522 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1523 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1524 ctrl &= ~SDHCI_CTRL_4BITBUS;
1525 if (host->version >= SDHCI_SPEC_300)
1526 ctrl |= SDHCI_CTRL_8BITBUS;
1527 } else {
1528 if (host->version >= SDHCI_SPEC_300)
1529 ctrl &= ~SDHCI_CTRL_8BITBUS;
1530 if (ios->bus_width == MMC_BUS_WIDTH_4)
1531 ctrl |= SDHCI_CTRL_4BITBUS;
1532 else
1533 ctrl &= ~SDHCI_CTRL_4BITBUS;
1534 }
1535 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1536 }
1537
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001538 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001539
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001540 if ((ios->timing == MMC_TIMING_SD_HS ||
1541 ios->timing == MMC_TIMING_MMC_HS)
1542 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001543 ctrl |= SDHCI_CTRL_HISPD;
1544 else
1545 ctrl &= ~SDHCI_CTRL_HISPD;
1546
Arindam Nathd6d50a12011-05-05 12:18:59 +05301547 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301548 u16 clk, ctrl_2;
1549 unsigned int clock;
1550
1551 /* In case of UHS-I modes, set High Speed Enable */
Girish K S2cd06dc2012-01-06 09:56:39 +05301552 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1553 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301554 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1555 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001556 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301557 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301558
1559 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1560 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301561 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301562 /*
1563 * We only need to set Driver Strength if the
1564 * preset value enable is not set.
1565 */
1566 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1567 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1568 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1569 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1570 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1571
1572 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301573 } else {
1574 /*
1575 * According to SDHC Spec v3.00, if the Preset Value
1576 * Enable in the Host Control 2 register is set, we
1577 * need to reset SD Clock Enable before changing High
1578 * Speed Enable to avoid generating clock gliches.
1579 */
Arindam Nath758535c2011-05-05 12:19:00 +05301580
1581 /* Reset SD Clock Enable */
1582 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1583 clk &= ~SDHCI_CLOCK_CARD_EN;
1584 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1585
1586 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1587
1588 /* Re-enable SD Clock */
1589 clock = host->clock;
1590 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301591 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301592 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301593 spin_lock_irqsave(&host->lock, flags);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301594 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301595
Arindam Nath49c468f2011-05-05 12:19:01 +05301596 /* Reset SD Clock Enable */
1597 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1598 clk &= ~SDHCI_CLOCK_CARD_EN;
1599 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1600
Philip Rakity6322cdd2011-05-13 11:17:15 +05301601 if (host->ops->set_uhs_signaling)
1602 host->ops->set_uhs_signaling(host, ios->timing);
1603 else {
1604 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1605 /* Select Bus Speed Mode for host */
1606 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S2cd06dc2012-01-06 09:56:39 +05301607 if (ios->timing == MMC_TIMING_MMC_HS200)
1608 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1609 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301610 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1611 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1612 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1613 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1614 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1615 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1616 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1617 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1618 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1619 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1620 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301621
1622 /* Re-enable SD Clock */
1623 clock = host->clock;
1624 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301625 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath49c468f2011-05-05 12:19:01 +05301626 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301627 spin_lock_irqsave(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301628 } else
1629 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301630
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301631 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02001632 /*
1633 * Some (ENE) controllers go apeshit on some ios operation,
1634 * signalling timeout and CRC errors even on CMD0. Resetting
1635 * it on each ios seems to solve the problem.
1636 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001637 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001638 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1639
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301640 /*
1641 * Reset the chip on each power off.
1642 * Should clear out any weird states.
1643 */
1644 if (ios->power_mode == MMC_POWER_OFF) {
1645 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1646 sdhci_reinit(host);
1647 vdd_bit = sdhci_set_power(host, -1);
1648 if (host->vmmc && vdd_bit != -1)
1649 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1650 }
1651 if (!ios->clock)
1652 sdhci_set_clock(host, ios->clock);
1653
Pierre Ossman5f25a662006-10-04 02:15:39 -07001654 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001655}
1656
Adrian Hunter50accb92011-10-03 15:33:34 +03001657static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1658{
1659 struct sdhci_host *host = mmc_priv(mmc);
1660
1661 sdhci_runtime_pm_get(host);
1662 sdhci_do_set_ios(host, ios);
1663 sdhci_runtime_pm_put(host);
1664}
1665
1666static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001667{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001668 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001669 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001670
Pierre Ossmand129bce2006-03-24 03:18:17 -08001671 spin_lock_irqsave(&host->lock, flags);
1672
Pierre Ossman1e728592008-04-16 19:13:13 +02001673 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001674 is_readonly = 0;
1675 else if (host->ops->get_ro)
1676 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001677 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001678 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1679 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001680
1681 spin_unlock_irqrestore(&host->lock, flags);
1682
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001683 /* This quirk needs to be replaced by a callback-function later */
1684 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1685 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001686}
1687
Takashi Iwai82b0e232011-04-21 20:26:38 +02001688#define SAMPLE_COUNT 5
1689
Adrian Hunter50accb92011-10-03 15:33:34 +03001690static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001691{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001692 int i, ro_count;
1693
Takashi Iwai82b0e232011-04-21 20:26:38 +02001694 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001695 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001696
1697 ro_count = 0;
1698 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001699 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001700 if (++ro_count > SAMPLE_COUNT / 2)
1701 return 1;
1702 }
1703 msleep(30);
1704 }
1705 return 0;
1706}
1707
Adrian Hunter50accb92011-10-03 15:33:34 +03001708static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001709{
Adrian Hunter50accb92011-10-03 15:33:34 +03001710 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001711
Adrian Hunter50accb92011-10-03 15:33:34 +03001712 if (host->ops && host->ops->hw_reset)
1713 host->ops->hw_reset(host);
1714}
Adrian Hunter20758b62011-08-29 16:42:12 +03001715
Adrian Hunter50accb92011-10-03 15:33:34 +03001716static int sdhci_get_ro(struct mmc_host *mmc)
1717{
1718 struct sdhci_host *host = mmc_priv(mmc);
1719 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001720
Adrian Hunter50accb92011-10-03 15:33:34 +03001721 sdhci_runtime_pm_get(host);
1722 ret = sdhci_do_get_ro(host);
1723 sdhci_runtime_pm_put(host);
1724 return ret;
1725}
1726
1727static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1728{
Pierre Ossman1e728592008-04-16 19:13:13 +02001729 if (host->flags & SDHCI_DEVICE_DEAD)
1730 goto out;
1731
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001732 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001733 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1734 else
1735 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1736
1737 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1738 if (host->runtime_suspended)
1739 goto out;
1740
1741 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001742 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1743 else
1744 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001745out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001746 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001747}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001748
Adrian Hunter50accb92011-10-03 15:33:34 +03001749static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1750{
1751 struct sdhci_host *host = mmc_priv(mmc);
1752 unsigned long flags;
1753
1754 spin_lock_irqsave(&host->lock, flags);
1755 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001756 spin_unlock_irqrestore(&host->lock, flags);
1757}
1758
Adrian Hunter50accb92011-10-03 15:33:34 +03001759static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1760 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301761{
Arindam Nathf2119df2011-05-05 12:18:57 +05301762 u8 pwr;
1763 u16 clk, ctrl;
1764 u32 present_state;
1765
Arindam Nathf2119df2011-05-05 12:18:57 +05301766 /*
1767 * Signal Voltage Switching is only applicable for Host Controllers
1768 * v3.00 and above.
1769 */
1770 if (host->version < SDHCI_SPEC_300)
1771 return 0;
1772
1773 /*
1774 * We first check whether the request is to set signalling voltage
1775 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1776 */
1777 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1778 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1779 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1780 ctrl &= ~SDHCI_CTRL_VDD_180;
1781 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301782 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301783 host->ops->check_power_status(host, REQ_IO_HIGH);
Arindam Nathf2119df2011-05-05 12:18:57 +05301784
1785 /* Wait for 5ms */
1786 usleep_range(5000, 5500);
1787
1788 /* 3.3V regulator output should be stable within 5 ms */
1789 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1790 if (!(ctrl & SDHCI_CTRL_VDD_180))
1791 return 0;
1792 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301793 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301794 "signalling voltage failed\n");
1795 return -EIO;
1796 }
1797 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1798 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1799 /* Stop SDCLK */
1800 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1801 clk &= ~SDHCI_CLOCK_CARD_EN;
1802 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1803
1804 /* Check whether DAT[3:0] is 0000 */
1805 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1806 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1807 SDHCI_DATA_LVL_SHIFT)) {
1808 /*
1809 * Enable 1.8V Signal Enable in the Host Control2
1810 * register
1811 */
1812 ctrl |= SDHCI_CTRL_VDD_180;
1813 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301814 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301815 host->ops->check_power_status(host, REQ_IO_LOW);
Arindam Nathf2119df2011-05-05 12:18:57 +05301816
1817 /* Wait for 5ms */
1818 usleep_range(5000, 5500);
1819
1820 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1821 if (ctrl & SDHCI_CTRL_VDD_180) {
1822 /* Provide SDCLK again and wait for 1ms*/
1823 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1824 clk |= SDHCI_CLOCK_CARD_EN;
1825 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1826 usleep_range(1000, 1500);
1827
1828 /*
1829 * If DAT[3:0] level is 1111b, then the card
1830 * was successfully switched to 1.8V signaling.
1831 */
1832 present_state = sdhci_readl(host,
1833 SDHCI_PRESENT_STATE);
1834 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1835 SDHCI_DATA_LVL_MASK)
1836 return 0;
1837 }
1838 }
1839
1840 /*
1841 * If we are here, that means the switch to 1.8V signaling
1842 * failed. We power cycle the card, and retry initialization
1843 * sequence by setting S18R to 0.
1844 */
1845 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1846 pwr &= ~SDHCI_POWER_ON;
1847 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301848 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301849 host->ops->check_power_status(host, REQ_BUS_OFF);
Arindam Nathf2119df2011-05-05 12:18:57 +05301850
1851 /* Wait for 1ms as per the spec */
1852 usleep_range(1000, 1500);
1853 pwr |= SDHCI_POWER_ON;
1854 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301855 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301856 host->ops->check_power_status(host, REQ_BUS_ON);
Arindam Nathf2119df2011-05-05 12:18:57 +05301857
Sahitya Tummalaca422112013-02-22 12:15:54 +05301858 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301859 "voltage failed, retrying with S18R set to 0\n");
1860 return -EAGAIN;
1861 } else
1862 /* No signal voltage switch required */
1863 return 0;
1864}
1865
Adrian Hunter50accb92011-10-03 15:33:34 +03001866static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1867 struct mmc_ios *ios)
1868{
1869 struct sdhci_host *host = mmc_priv(mmc);
1870 int err;
1871
1872 if (host->version < SDHCI_SPEC_300)
1873 return 0;
1874 sdhci_runtime_pm_get(host);
1875 err = sdhci_do_start_signal_voltage_switch(host, ios);
1876 sdhci_runtime_pm_put(host);
1877 return err;
1878}
1879
Girish K S2cd06dc2012-01-06 09:56:39 +05301880static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301881{
1882 struct sdhci_host *host;
1883 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05301884 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05301885 int tuning_loop_counter = MAX_TUNING_LOOP;
1886 unsigned long timeout;
1887 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05301888 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301889
1890 host = mmc_priv(mmc);
1891
Adrian Hunter50accb92011-10-03 15:33:34 +03001892 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301893 disable_irq(host->irq);
1894 spin_lock(&host->lock);
1895
1896 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1897
1898 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05301899 * The Host Controller needs tuning only in case of SDR104 mode
1900 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301901 * Capabilities register.
Girish K S2cd06dc2012-01-06 09:56:39 +05301902 * If the Host Controller supports the HS200 mode then the
1903 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301904 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08001905 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1906 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
1907 (host->flags & SDHCI_HS200_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05301908 requires_tuning_nonuhs = true;
1909
Arindam Nathb513ea22011-05-05 12:19:04 +05301910 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301911 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301912 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1913 else {
1914 spin_unlock(&host->lock);
1915 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03001916 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301917 return 0;
1918 }
1919
Asutosh Das8ddd3482013-01-04 11:45:46 +05301920 if (host->ops->execute_tuning) {
1921 spin_unlock(&host->lock);
1922 enable_irq(host->irq);
1923 host->ops->execute_tuning(host, opcode);
1924 disable_irq(host->irq);
1925 spin_lock(&host->lock);
1926 goto out;
1927 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301928 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1929
1930 /*
1931 * As per the Host Controller spec v3.00, tuning command
1932 * generates Buffer Read Ready interrupt, so enable that.
1933 *
1934 * Note: The spec clearly says that when tuning sequence
1935 * is being performed, the controller does not generate
1936 * interrupts other than Buffer Read Ready interrupt. But
1937 * to make sure we don't hit a controller bug, we _only_
1938 * enable Buffer Read Ready interrupt here.
1939 */
1940 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1941 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1942
1943 /*
1944 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1945 * of loops reaches 40 times or a timeout of 150ms occurs.
1946 */
1947 timeout = 150;
1948 do {
1949 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03001950 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301951
1952 if (!tuning_loop_counter && !timeout)
1953 break;
1954
Girish K S2cd06dc2012-01-06 09:56:39 +05301955 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301956 cmd.arg = 0;
1957 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1958 cmd.retries = 0;
1959 cmd.data = NULL;
1960 cmd.error = 0;
1961
1962 mrq.cmd = &cmd;
1963 host->mrq = &mrq;
1964
1965 /*
1966 * In response to CMD19, the card sends 64 bytes of tuning
1967 * block to the Host Controller. So we set the block size
1968 * to 64 here.
1969 */
Girish K S2cd06dc2012-01-06 09:56:39 +05301970 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1971 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1972 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1973 SDHCI_BLOCK_SIZE);
1974 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1975 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1976 SDHCI_BLOCK_SIZE);
1977 } else {
1978 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1979 SDHCI_BLOCK_SIZE);
1980 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301981
1982 /*
1983 * The tuning block is sent by the card to the host controller.
1984 * So we set the TRNS_READ bit in the Transfer Mode register.
1985 * This also takes care of setting DMA Enable and Multi Block
1986 * Select in the same register to 0.
1987 */
1988 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1989
1990 sdhci_send_command(host, &cmd);
1991
1992 host->cmd = NULL;
1993 host->mrq = NULL;
1994
1995 spin_unlock(&host->lock);
1996 enable_irq(host->irq);
1997
1998 /* Wait for Buffer Read Ready interrupt */
1999 wait_event_interruptible_timeout(host->buf_ready_int,
2000 (host->tuning_done == 1),
2001 msecs_to_jiffies(50));
2002 disable_irq(host->irq);
2003 spin_lock(&host->lock);
2004
2005 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302006 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05302007 "Buffer Read Ready interrupt during tuning "
2008 "procedure, falling back to fixed sampling "
2009 "clock\n");
2010 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2011 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2012 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2013 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2014
2015 err = -EIO;
2016 goto out;
2017 }
2018
2019 host->tuning_done = 0;
2020
2021 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2022 tuning_loop_counter--;
2023 timeout--;
2024 mdelay(1);
2025 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2026
2027 /*
2028 * The Host Driver has exhausted the maximum number of loops allowed,
2029 * so use fixed sampling frequency.
2030 */
2031 if (!tuning_loop_counter || !timeout) {
2032 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2033 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2034 } else {
2035 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302036 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05302037 " failed, falling back to fixed sampling"
2038 " clock\n");
2039 err = -EIO;
2040 }
2041 }
2042
2043out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302044 /*
2045 * If this is the very first time we are here, we start the retuning
2046 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2047 * flag won't be set, we check this condition before actually starting
2048 * the timer.
2049 */
2050 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2051 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2052 mod_timer(&host->tuning_timer, jiffies +
2053 host->tuning_count * HZ);
2054 /* Tuning mode 1 limits the maximum data length to 4MB */
2055 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2056 } else {
2057 host->flags &= ~SDHCI_NEEDS_RETUNING;
2058 /* Reload the new initial value for timer */
2059 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2060 mod_timer(&host->tuning_timer, jiffies +
2061 host->tuning_count * HZ);
2062 }
2063
2064 /*
2065 * In case tuning fails, host controllers which support re-tuning can
2066 * try tuning again at a later time, when the re-tuning timer expires.
2067 * So for these controllers, we return 0. Since there might be other
2068 * controllers who do not have this capability, we return error for
2069 * them.
2070 */
2071 if (err && host->tuning_count &&
2072 host->tuning_mode == SDHCI_TUNING_MODE_1)
2073 err = 0;
2074
Arindam Nathb513ea22011-05-05 12:19:04 +05302075 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
2076 spin_unlock(&host->lock);
2077 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002078 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302079
2080 return err;
2081}
2082
Adrian Hunter50accb92011-10-03 15:33:34 +03002083static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302084{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302085 u16 ctrl;
2086 unsigned long flags;
2087
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302088 /* Host Controller v3.00 defines preset value registers */
2089 if (host->version < SDHCI_SPEC_300)
2090 return;
2091
2092 spin_lock_irqsave(&host->lock, flags);
2093
2094 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2095
2096 /*
2097 * We only enable or disable Preset Value if they are not already
2098 * enabled or disabled respectively. Otherwise, we bail out.
2099 */
2100 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2101 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2102 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002103 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302104 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2105 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2106 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002107 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302108 }
2109
2110 spin_unlock_irqrestore(&host->lock, flags);
2111}
2112
Adrian Hunter50accb92011-10-03 15:33:34 +03002113static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
2114{
2115 struct sdhci_host *host = mmc_priv(mmc);
2116
2117 sdhci_runtime_pm_get(host);
2118 sdhci_do_enable_preset_value(host, enable);
2119 sdhci_runtime_pm_put(host);
2120}
2121
David Brownellab7aefd2006-11-12 17:55:30 -08002122static const struct mmc_host_ops sdhci_ops = {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002123 .pre_req = sdhci_pre_req,
2124 .post_req = sdhci_post_req,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002125 .request = sdhci_request,
2126 .set_ios = sdhci_set_ios,
2127 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03002128 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002129 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302130 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302131 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302132 .enable_preset_value = sdhci_enable_preset_value,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302133 .enable = sdhci_enable,
2134 .disable = sdhci_disable,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002135};
2136
2137/*****************************************************************************\
2138 * *
2139 * Tasklets *
2140 * *
2141\*****************************************************************************/
2142
2143static void sdhci_tasklet_card(unsigned long param)
2144{
2145 struct sdhci_host *host;
2146 unsigned long flags;
2147
2148 host = (struct sdhci_host*)param;
2149
2150 spin_lock_irqsave(&host->lock, flags);
2151
Adrian Hunter50accb92011-10-03 15:33:34 +03002152 /* Check host->mrq first in case we are runtime suspended */
2153 if (host->mrq &&
2154 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302155 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002156 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05302157 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002158 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002159
Adrian Hunter50accb92011-10-03 15:33:34 +03002160 sdhci_reset(host, SDHCI_RESET_CMD);
2161 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002162
Adrian Hunter50accb92011-10-03 15:33:34 +03002163 host->mrq->cmd->error = -ENOMEDIUM;
2164 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002165 }
2166
2167 spin_unlock_irqrestore(&host->lock, flags);
2168
Pierre Ossman04cf5852008-08-18 22:18:14 +02002169 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002170}
2171
2172static void sdhci_tasklet_finish(unsigned long param)
2173{
2174 struct sdhci_host *host;
2175 unsigned long flags;
2176 struct mmc_request *mrq;
2177
2178 host = (struct sdhci_host*)param;
2179
Adrian Hunter50accb92011-10-03 15:33:34 +03002180 spin_lock_irqsave(&host->lock, flags);
2181
Chris Ball0c9c99a2011-04-27 17:35:31 -04002182 /*
2183 * If this tasklet gets rescheduled while running, it will
2184 * be run again afterwards but without any active request.
2185 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002186 if (!host->mrq) {
2187 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002188 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002189 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002190
2191 del_timer(&host->timer);
2192
2193 mrq = host->mrq;
2194
Pierre Ossmand129bce2006-03-24 03:18:17 -08002195 /*
2196 * The controller needs a reset of internal state machines
2197 * upon error conditions.
2198 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002199 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002200 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002201 (mrq->data && (mrq->data->error ||
2202 (mrq->data->stop && mrq->data->stop->error))) ||
2203 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002204
2205 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002206 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002207 unsigned int clock;
2208
2209 /* This is to force an update */
2210 clock = host->clock;
2211 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302212 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002213 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302214 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002215 }
2216
2217 /* Spec says we should do both at the same time, but Ricoh
2218 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002219 sdhci_reset(host, SDHCI_RESET_CMD);
2220 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002221 } else {
2222 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2223 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002224 }
2225
2226 host->mrq = NULL;
2227 host->cmd = NULL;
2228 host->data = NULL;
2229
Pierre Ossmanf9134312008-12-21 17:01:48 +01002230#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002231 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002232#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002233
Pierre Ossman5f25a662006-10-04 02:15:39 -07002234 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002235 spin_unlock_irqrestore(&host->lock, flags);
2236
2237 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002238 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002239}
2240
2241static void sdhci_timeout_timer(unsigned long data)
2242{
2243 struct sdhci_host *host;
2244 unsigned long flags;
2245
2246 host = (struct sdhci_host*)data;
2247
2248 spin_lock_irqsave(&host->lock, flags);
2249
2250 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302251 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002252 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002253 sdhci_dumpregs(host);
2254
2255 if (host->data) {
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302256 pr_info("%s: bytes to transfer: %d transferred: %d\n",
2257 mmc_hostname(host->mmc),
2258 (host->data->blksz * host->data->blocks),
2259 (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) *
2260 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossman17b04292007-07-22 22:18:46 +02002261 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002262 sdhci_finish_data(host);
2263 } else {
2264 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002265 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002266 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002267 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002268
2269 tasklet_schedule(&host->finish_tasklet);
2270 }
2271 }
2272
Pierre Ossman5f25a662006-10-04 02:15:39 -07002273 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002274 spin_unlock_irqrestore(&host->lock, flags);
2275}
2276
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302277static void sdhci_tuning_timer(unsigned long data)
2278{
2279 struct sdhci_host *host;
2280 unsigned long flags;
2281
2282 host = (struct sdhci_host *)data;
2283
2284 spin_lock_irqsave(&host->lock, flags);
2285
2286 host->flags |= SDHCI_NEEDS_RETUNING;
2287
2288 spin_unlock_irqrestore(&host->lock, flags);
2289}
2290
Pierre Ossmand129bce2006-03-24 03:18:17 -08002291/*****************************************************************************\
2292 * *
2293 * Interrupt handling *
2294 * *
2295\*****************************************************************************/
2296
2297static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2298{
2299 BUG_ON(intmask == 0);
2300
2301 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302302 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002303 "though no command operation was in progress.\n",
2304 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002305 sdhci_dumpregs(host);
2306 return;
2307 }
2308
Pierre Ossman43b58b32007-07-25 23:15:27 +02002309 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002310 host->cmd->error = -ETIMEDOUT;
2311 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2312 SDHCI_INT_INDEX))
2313 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002314
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302315 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2316 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2317 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2318 if (intmask & SDHCI_INT_CRC) {
2319 sdhci_reset(host, SDHCI_RESET_CMD);
2320 host->cmd->error = 0;
2321 }
2322 }
2323 }
2324
Pierre Ossmane8095172008-07-25 01:09:08 +02002325 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002326 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002327 return;
2328 }
2329
2330 /*
2331 * The host can send and interrupt when the busy state has
2332 * ended, allowing us to wait without wasting CPU cycles.
2333 * Unfortunately this is overloaded on the "data complete"
2334 * interrupt, so we need to take some care when handling
2335 * it.
2336 *
2337 * Note: The 1.0 specification is a bit ambiguous about this
2338 * feature so there might be some problems with older
2339 * controllers.
2340 */
2341 if (host->cmd->flags & MMC_RSP_BUSY) {
2342 if (host->cmd->data)
2343 DBG("Cannot wait for busy signal when also "
2344 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002345 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002346 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002347
2348 /* The controller does not support the end-of-busy IRQ,
2349 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002350 }
2351
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302352 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2353 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2354 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2355 if (intmask & SDHCI_INT_CRC) {
2356 sdhci_finish_command(host);
2357 return;
2358 }
2359 }
2360 }
2361
Pierre Ossmane8095172008-07-25 01:09:08 +02002362 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002363 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002364}
2365
Ben Dooks6882a8c2009-06-14 13:52:38 +01002366static void sdhci_show_adma_error(struct sdhci_host *host)
2367{
2368 const char *name = mmc_hostname(host->mmc);
2369 u8 *desc = host->adma_desc;
2370 __le32 *dma;
2371 __le16 *len;
2372 u8 attr;
2373
2374 sdhci_dumpregs(host);
2375
2376 while (true) {
2377 dma = (__le32 *)(desc + 4);
2378 len = (__le16 *)(desc + 2);
2379 attr = *desc;
2380
Sahitya Tummala419b6c82013-04-12 12:28:29 +05302381 pr_info("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
Ben Dooks6882a8c2009-06-14 13:52:38 +01002382 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2383
2384 desc += 8;
2385
2386 if (attr & 2)
2387 break;
2388 }
2389}
Ben Dooks6882a8c2009-06-14 13:52:38 +01002390
Pierre Ossmand129bce2006-03-24 03:18:17 -08002391static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2392{
Girish K S2cd06dc2012-01-06 09:56:39 +05302393 u32 command;
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302394 bool pr_msg = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002395 BUG_ON(intmask == 0);
2396
Arindam Nathb513ea22011-05-05 12:19:04 +05302397 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2398 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302399 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2400 if (command == MMC_SEND_TUNING_BLOCK ||
2401 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302402 host->tuning_done = 1;
2403 wake_up(&host->buf_ready_int);
2404 return;
2405 }
2406 }
2407
Pierre Ossmand129bce2006-03-24 03:18:17 -08002408 if (!host->data) {
2409 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002410 * The "data complete" interrupt is also used to
2411 * indicate that a busy state has ended. See comment
2412 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002413 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002414 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2415 if (intmask & SDHCI_INT_DATA_END) {
2416 sdhci_finish_command(host);
2417 return;
2418 }
2419 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002420
Sahitya Tummalaca422112013-02-22 12:15:54 +05302421 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002422 "though no data operation was in progress.\n",
2423 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002424 sdhci_dumpregs(host);
2425
2426 return;
2427 }
2428
2429 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002430 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002431 else if (intmask & SDHCI_INT_DATA_END_BIT)
2432 host->data->error = -EILSEQ;
2433 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2434 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2435 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002436 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002437 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302438 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002439 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002440 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002441 }
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302442 if (host->data->error) {
2443 if ((intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT)) &&
2444 (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING)) {
2445 command = SDHCI_GET_CMD(sdhci_readw(host,
2446 SDHCI_COMMAND));
2447 if ((command != MMC_SEND_TUNING_BLOCK_HS200) &&
2448 (command != MMC_SEND_TUNING_BLOCK))
2449 pr_msg = true;
2450 } else {
2451 pr_msg = true;
2452 }
2453 if (pr_msg) {
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302454 pr_err("%s: data txfr (0x%08x) error: %d after %lld ms\n",
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302455 mmc_hostname(host->mmc), intmask,
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302456 host->data->error, ktime_to_ms(ktime_sub(
2457 ktime_get(), host->data_start_time)));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302458 sdhci_dumpregs(host);
2459 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002460 sdhci_finish_data(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302461 } else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002462 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002463 sdhci_transfer_pio(host);
2464
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002465 /*
2466 * We currently don't do anything fancy with DMA
2467 * boundaries, but as we can't disable the feature
2468 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002469 *
2470 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2471 * should return a valid address to continue from, but as
2472 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002473 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002474 if (intmask & SDHCI_INT_DMA_END) {
2475 u32 dmastart, dmanow;
2476 dmastart = sg_dma_address(host->data->sg);
2477 dmanow = dmastart + host->data->bytes_xfered;
2478 /*
2479 * Force update to the next DMA block boundary.
2480 */
2481 dmanow = (dmanow &
2482 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2483 SDHCI_DEFAULT_BOUNDARY_SIZE;
2484 host->data->bytes_xfered = dmanow - dmastart;
2485 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2486 " next 0x%08x\n",
2487 mmc_hostname(host->mmc), dmastart,
2488 host->data->bytes_xfered, dmanow);
2489 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2490 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002491
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002492 if (intmask & SDHCI_INT_DATA_END) {
2493 if (host->cmd) {
2494 /*
2495 * Data managed to finish before the
2496 * command completed. Make sure we do
2497 * things in the proper order.
2498 */
2499 host->data_early = 1;
2500 } else {
2501 sdhci_finish_data(host);
2502 }
2503 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002504 }
2505}
2506
David Howells7d12e782006-10-05 14:55:46 +01002507static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002508{
2509 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002510 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002511 u32 intmask, unexpected = 0;
2512 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002513
2514 spin_lock(&host->lock);
2515
Adrian Hunter50accb92011-10-03 15:33:34 +03002516 if (host->runtime_suspended) {
2517 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302518 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002519 mmc_hostname(host->mmc));
2520 return IRQ_HANDLED;
2521 }
2522
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002523 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002524
Mark Lord62df67a2007-03-06 13:30:13 +01002525 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002526 result = IRQ_NONE;
2527 goto out;
2528 }
2529
Alexander Stein6379b232012-03-14 09:52:10 +01002530again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002531 DBG("*** %s got interrupt: 0x%08x\n",
2532 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002533
Pierre Ossman3192a282006-06-30 02:22:26 -07002534 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302535 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2536 SDHCI_CARD_PRESENT;
2537
2538 /*
2539 * There is a observation on i.mx esdhc. INSERT bit will be
2540 * immediately set again when it gets cleared, if a card is
2541 * inserted. We have to mask the irq to prevent interrupt
2542 * storm which will freeze the system. And the REMOVE gets
2543 * the same situation.
2544 *
2545 * More testing are needed here to ensure it works for other
2546 * platforms though.
2547 */
2548 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2549 SDHCI_INT_CARD_REMOVE);
2550 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2551 SDHCI_INT_CARD_INSERT);
2552
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002553 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302554 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2555 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002556 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002557 }
2558
Pierre Ossmand129bce2006-03-24 03:18:17 -08002559 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002560 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2561 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002562 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2563 (host->clock <= 400000))
2564 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002565 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002566 }
2567
2568 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002569 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2570 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002571 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2572 (host->clock <= 400000))
2573 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002574 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002575 }
2576
2577 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2578
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002579 intmask &= ~SDHCI_INT_ERROR;
2580
Pierre Ossmand129bce2006-03-24 03:18:17 -08002581 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302582 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002583 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002584 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002585 }
2586
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002587 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002588
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002589 if (intmask & SDHCI_INT_CARD_INT)
2590 cardint = 1;
2591
2592 intmask &= ~SDHCI_INT_CARD_INT;
2593
Pierre Ossman3192a282006-06-30 02:22:26 -07002594 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002595 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002596 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002597 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002598
2599 result = IRQ_HANDLED;
2600
Alexander Stein6379b232012-03-14 09:52:10 +01002601 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2602 if (intmask && --max_loops)
2603 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002604out:
2605 spin_unlock(&host->lock);
2606
Alexander Stein6379b232012-03-14 09:52:10 +01002607 if (unexpected) {
2608 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2609 mmc_hostname(host->mmc), unexpected);
2610 sdhci_dumpregs(host);
2611 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002612 /*
2613 * We have to delay this as it calls back into the driver.
2614 */
2615 if (cardint)
2616 mmc_signal_sdio_irq(host->mmc);
2617
Pierre Ossmand129bce2006-03-24 03:18:17 -08002618 return result;
2619}
2620
2621/*****************************************************************************\
2622 * *
2623 * Suspend/resume *
2624 * *
2625\*****************************************************************************/
2626
2627#ifdef CONFIG_PM
2628
Manuel Laussd72faa62011-11-03 11:09:45 +01002629int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002630{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002631 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302632 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002633
Chris Balla1b13b42012-02-06 00:43:59 -05002634 if (host->ops->platform_suspend)
2635 host->ops->platform_suspend(host);
2636
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002637 sdhci_disable_card_detection(host);
2638
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302639 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302640 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2641 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2642 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002643 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302644 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302645 }
2646
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002647 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302648 if (ret) {
2649 if (has_tuning_timer) {
2650 host->flags |= SDHCI_NEEDS_RETUNING;
2651 mod_timer(&host->tuning_timer, jiffies +
2652 host->tuning_count * HZ);
2653 }
2654
2655 sdhci_enable_card_detection(host);
2656
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002657 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302658 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002659
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002660 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002661
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002662 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002663}
2664
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002665EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002666
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002667int sdhci_resume_host(struct sdhci_host *host)
2668{
2669 int ret;
2670
Richard Röjforsa13abc72009-09-22 16:45:30 -07002671 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002672 if (host->ops->enable_dma)
2673 host->ops->enable_dma(host);
2674 }
2675
2676 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2677 mmc_hostname(host->mmc), host);
2678 if (ret)
2679 return ret;
2680
Adrian Hunter6308d292012-02-07 14:48:54 +02002681 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2682 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2683 /* Card keeps power but host controller does not */
2684 sdhci_init(host, 0);
2685 host->pwr = 0;
2686 host->clock = 0;
2687 sdhci_do_set_ios(host, &host->mmc->ios);
2688 } else {
2689 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2690 mmiowb();
2691 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002692
2693 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002694 sdhci_enable_card_detection(host);
2695
Chris Balla1b13b42012-02-06 00:43:59 -05002696 if (host->ops->platform_resume)
2697 host->ops->platform_resume(host);
2698
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302699 /* Set the re-tuning expiration flag */
2700 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2701 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2702 host->flags |= SDHCI_NEEDS_RETUNING;
2703
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002704 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002705}
2706
2707EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002708
Daniel Drake5f619702010-11-04 22:20:39 +00002709void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2710{
2711 u8 val;
2712 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2713 val |= SDHCI_WAKE_ON_INT;
2714 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2715}
2716
2717EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2718
Pierre Ossmand129bce2006-03-24 03:18:17 -08002719#endif /* CONFIG_PM */
2720
Adrian Hunter50accb92011-10-03 15:33:34 +03002721#ifdef CONFIG_PM_RUNTIME
2722
2723static int sdhci_runtime_pm_get(struct sdhci_host *host)
2724{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302725 if (!mmc_use_core_runtime_pm(host->mmc))
2726 return pm_runtime_get_sync(host->mmc->parent);
2727 else
2728 return 0;
Adrian Hunter50accb92011-10-03 15:33:34 +03002729}
2730
2731static int sdhci_runtime_pm_put(struct sdhci_host *host)
2732{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302733 if (!mmc_use_core_runtime_pm(host->mmc)) {
2734 pm_runtime_mark_last_busy(host->mmc->parent);
2735 return pm_runtime_put_autosuspend(host->mmc->parent);
2736 } else {
2737 return 0;
2738 }
Adrian Hunter50accb92011-10-03 15:33:34 +03002739}
2740
2741int sdhci_runtime_suspend_host(struct sdhci_host *host)
2742{
2743 unsigned long flags;
2744 int ret = 0;
2745
2746 /* Disable tuning since we are suspending */
2747 if (host->version >= SDHCI_SPEC_300 &&
2748 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2749 del_timer_sync(&host->tuning_timer);
2750 host->flags &= ~SDHCI_NEEDS_RETUNING;
2751 }
2752
2753 spin_lock_irqsave(&host->lock, flags);
2754 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2755 spin_unlock_irqrestore(&host->lock, flags);
2756
2757 synchronize_irq(host->irq);
2758
2759 spin_lock_irqsave(&host->lock, flags);
2760 host->runtime_suspended = true;
2761 spin_unlock_irqrestore(&host->lock, flags);
2762
2763 return ret;
2764}
2765EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2766
2767int sdhci_runtime_resume_host(struct sdhci_host *host)
2768{
2769 unsigned long flags;
2770 int ret = 0, host_flags = host->flags;
2771
2772 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2773 if (host->ops->enable_dma)
2774 host->ops->enable_dma(host);
2775 }
2776
2777 sdhci_init(host, 0);
2778
2779 /* Force clock and power re-program */
2780 host->pwr = 0;
2781 host->clock = 0;
2782 sdhci_do_set_ios(host, &host->mmc->ios);
2783
2784 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2785 if (host_flags & SDHCI_PV_ENABLED)
2786 sdhci_do_enable_preset_value(host, true);
2787
2788 /* Set the re-tuning expiration flag */
2789 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2790 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2791 host->flags |= SDHCI_NEEDS_RETUNING;
2792
2793 spin_lock_irqsave(&host->lock, flags);
2794
2795 host->runtime_suspended = false;
2796
2797 /* Enable SDIO IRQ */
2798 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2799 sdhci_enable_sdio_irq_nolock(host, true);
2800
2801 /* Enable Card Detection */
2802 sdhci_enable_card_detection(host);
2803
2804 spin_unlock_irqrestore(&host->lock, flags);
2805
2806 return ret;
2807}
2808EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2809
2810#endif
2811
Pierre Ossmand129bce2006-03-24 03:18:17 -08002812/*****************************************************************************\
2813 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002814 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002815 * *
2816\*****************************************************************************/
2817
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002818struct sdhci_host *sdhci_alloc_host(struct device *dev,
2819 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002820{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002821 struct mmc_host *mmc;
2822 struct sdhci_host *host;
2823
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002824 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002825
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002826 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002827 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002828 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002829
2830 host = mmc_priv(mmc);
2831 host->mmc = mmc;
2832
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002833 return host;
2834}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002835
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002836EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002837
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002838int sdhci_add_host(struct sdhci_host *host)
2839{
2840 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302841 u32 caps[2];
2842 u32 max_current_caps;
2843 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002844 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002845
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002846 WARN_ON(host == NULL);
2847 if (host == NULL)
2848 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002849
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002850 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002851
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002852 if (debug_quirks)
2853 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03002854 if (debug_quirks2)
2855 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002856
Pierre Ossmand96649e2006-06-30 02:22:30 -07002857 sdhci_reset(host, SDHCI_RESET_ALL);
2858
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002859 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002860 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2861 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002862 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302863 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002864 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002865 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002866 }
2867
Arindam Nathf2119df2011-05-05 12:18:57 +05302868 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002869 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002870
Arindam Nathf2119df2011-05-05 12:18:57 +05302871 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2872 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2873
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002874 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002875 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302876 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002877 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002878 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002879 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002880
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002881 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002882 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002883 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002884 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002885 }
2886
Arindam Nathf2119df2011-05-05 12:18:57 +05302887 if ((host->version >= SDHCI_SPEC_200) &&
2888 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002889 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002890
2891 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2892 (host->flags & SDHCI_USE_ADMA)) {
2893 DBG("Disabling ADMA as it is marked broken\n");
2894 host->flags &= ~SDHCI_USE_ADMA;
2895 }
2896
Richard Röjforsa13abc72009-09-22 16:45:30 -07002897 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002898 if (host->ops->enable_dma) {
2899 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302900 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002901 "available. Falling back to PIO.\n",
2902 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002903 host->flags &=
2904 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002905 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002906 }
2907 }
2908
Pierre Ossman2134a922008-06-28 18:28:51 +02002909 if (host->flags & SDHCI_USE_ADMA) {
2910 /*
2911 * We need to allocate descriptors for all sg entries
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302912 * (128/max_segments) and potentially one alignment transfer for
Pierre Ossman2134a922008-06-28 18:28:51 +02002913 * each of those entries.
2914 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302915 if (host->ops->get_max_segments)
2916 host->adma_max_desc = host->ops->get_max_segments();
2917 else
2918 host->adma_max_desc = 128;
2919
2920 host->adma_desc_sz = (host->adma_max_desc * 2 + 1) * 4;
2921 host->align_buf_sz = host->adma_max_desc * 4;
2922
2923 pr_debug("%s: %s: dma_desc_size: %d\n",
2924 mmc_hostname(host->mmc), __func__, host->adma_desc_sz);
2925 host->adma_desc = kmalloc(host->adma_desc_sz,
2926 GFP_KERNEL);
2927 host->align_buffer = kmalloc(host->align_buf_sz,
2928 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02002929 if (!host->adma_desc || !host->align_buffer) {
2930 kfree(host->adma_desc);
2931 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302932 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002933 "buffers. Falling back to standard DMA.\n",
2934 mmc_hostname(mmc));
2935 host->flags &= ~SDHCI_USE_ADMA;
2936 }
2937 }
2938
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002939 host->next_data.cookie = 1;
2940
Pierre Ossman76591502008-07-21 00:32:11 +02002941 /*
2942 * If we use DMA, then it's up to the caller to set the DMA
2943 * mask, but PIO does not need the hw shim so we set a new
2944 * mask here in that case.
2945 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002946 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002947 host->dma_mask = DMA_BIT_MASK(64);
2948 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2949 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002950
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002951 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302952 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002953 >> SDHCI_CLOCK_BASE_SHIFT;
2954 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302955 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002956 >> SDHCI_CLOCK_BASE_SHIFT;
2957
Pierre Ossmand129bce2006-03-24 03:18:17 -08002958 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002959 if (host->max_clk == 0 || host->quirks &
2960 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002961 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302962 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002963 "frequency.\n", mmc_hostname(mmc));
2964 return -ENODEV;
2965 }
2966 host->max_clk = host->ops->get_max_clock(host);
2967 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002968
2969 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302970 * In case of Host Controller v3.00, find out whether clock
2971 * multiplier is supported.
2972 */
2973 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2974 SDHCI_CLOCK_MUL_SHIFT;
2975
2976 /*
2977 * In case the value in Clock Multiplier is 0, then programmable
2978 * clock mode is not supported, otherwise the actual clock
2979 * multiplier is one more than the value of Clock Multiplier
2980 * in the Capabilities Register.
2981 */
2982 if (host->clk_mul)
2983 host->clk_mul += 1;
2984
2985 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002986 * Set host parameters.
2987 */
2988 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302989 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002990 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002991 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302992 else if (host->version >= SDHCI_SPEC_300) {
2993 if (host->clk_mul) {
2994 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2995 mmc->f_max = host->max_clk * host->clk_mul;
2996 } else
2997 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2998 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002999 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003000
Sahitya Tummalaca422112013-02-22 12:15:54 +05303001 host->timeout_clk =
3002 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
3003 if (host->timeout_clk == 0) {
3004 if (host->ops->get_timeout_clock) {
3005 host->timeout_clk = host->ops->get_timeout_clock(host);
3006 } else if (!(host->quirks &
3007 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3008 pr_err("%s: Hardware doesn't specify timeout clock "
3009 "frequency.\n", mmc_hostname(mmc));
3010 return -ENODEV;
3011 }
3012 }
3013 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3014 host->timeout_clk *= 1000;
3015
3016 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
3017 host->timeout_clk = mmc->f_max / 1000;
3018
Krishna Kondaa20d3362013-04-01 21:01:59 -07003019 if (!(host->quirks2 & SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE))
3020 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Sahitya Tummalaca422112013-02-22 12:15:54 +05303021
Andrei Warkentine89d4562011-05-23 15:06:37 -05003022 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
3023
3024 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3025 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003026
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003027 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003028 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003029 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003030 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003031 host->flags |= SDHCI_AUTO_CMD23;
3032 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3033 } else {
3034 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3035 }
3036
Philip Rakity15ec4462010-11-19 16:48:39 -05003037 /*
3038 * A controller may support 8-bit width, but the board itself
3039 * might not have the pins brought out. Boards that support
3040 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3041 * their platform code before calling sdhci_add_host(), and we
3042 * won't assume 8-bit width for hosts without that CAP.
3043 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003044 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003045 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003046
Arindam Nathf2119df2011-05-05 12:18:57 +05303047 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003048 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003049
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003050 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3051 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003052 mmc->caps |= MMC_CAP_NEEDS_POLL;
3053
Al Cooper4188bba2012-03-16 15:54:17 -04003054 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3055 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3056 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303057 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3058
3059 /* SDR104 supports also implies SDR50 support */
3060 if (caps[1] & SDHCI_SUPPORT_SDR104)
3061 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3062 else if (caps[1] & SDHCI_SUPPORT_SDR50)
3063 mmc->caps |= MMC_CAP_UHS_SDR50;
3064
3065 if (caps[1] & SDHCI_SUPPORT_DDR50)
3066 mmc->caps |= MMC_CAP_UHS_DDR50;
3067
Girish K S2cd06dc2012-01-06 09:56:39 +05303068 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303069 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3070 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3071
Girish K S2cd06dc2012-01-06 09:56:39 +05303072 /* Does the host need tuning for HS200? */
3073 if (mmc->caps2 & MMC_CAP2_HS200)
3074 host->flags |= SDHCI_HS200_NEEDS_TUNING;
3075
Arindam Nathd6d50a12011-05-05 12:18:59 +05303076 /* Driver Type(s) (A, C, D) supported by the host */
3077 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3078 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3079 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3080 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3081 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3082 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3083
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02003084 /* Initial value for re-tuning timer count */
3085 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3086 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3087
3088 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303089 * In case Re-tuning Timer is not disabled, the actual value of
3090 * re-tuning timer will be 2 ^ (n - 1).
3091 */
3092 if (host->tuning_count)
3093 host->tuning_count = 1 << (host->tuning_count - 1);
3094
3095 /* Re-tuning mode supported by the Host Controller */
3096 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3097 SDHCI_RETUNING_MODE_SHIFT;
3098
Takashi Iwai8f230f42010-12-08 10:04:30 +01003099 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05303100 /*
3101 * According to SD Host Controller spec v3.00, if the Host System
3102 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3103 * the value is meaningful only if Voltage Support in the Capabilities
3104 * register is set. The actual current value is 4 times the register
3105 * value.
3106 */
3107 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3108
3109 if (caps[0] & SDHCI_CAN_VDD_330) {
3110 int max_current_330;
3111
Takashi Iwai8f230f42010-12-08 10:04:30 +01003112 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303113
3114 max_current_330 = ((max_current_caps &
3115 SDHCI_MAX_CURRENT_330_MASK) >>
3116 SDHCI_MAX_CURRENT_330_SHIFT) *
3117 SDHCI_MAX_CURRENT_MULTIPLIER;
3118
3119 if (max_current_330 > 150)
3120 mmc->caps |= MMC_CAP_SET_XPC_330;
3121 }
3122 if (caps[0] & SDHCI_CAN_VDD_300) {
3123 int max_current_300;
3124
Takashi Iwai8f230f42010-12-08 10:04:30 +01003125 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303126
3127 max_current_300 = ((max_current_caps &
3128 SDHCI_MAX_CURRENT_300_MASK) >>
3129 SDHCI_MAX_CURRENT_300_SHIFT) *
3130 SDHCI_MAX_CURRENT_MULTIPLIER;
3131
3132 if (max_current_300 > 150)
3133 mmc->caps |= MMC_CAP_SET_XPC_300;
3134 }
3135 if (caps[0] & SDHCI_CAN_VDD_180) {
3136 int max_current_180;
3137
Takashi Iwai8f230f42010-12-08 10:04:30 +01003138 ocr_avail |= MMC_VDD_165_195;
3139
Arindam Nathf2119df2011-05-05 12:18:57 +05303140 max_current_180 = ((max_current_caps &
3141 SDHCI_MAX_CURRENT_180_MASK) >>
3142 SDHCI_MAX_CURRENT_180_SHIFT) *
3143 SDHCI_MAX_CURRENT_MULTIPLIER;
3144
3145 if (max_current_180 > 150)
3146 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05303147
3148 /* Maximum current capabilities of the host at 1.8V */
3149 if (max_current_180 >= 800)
3150 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
3151 else if (max_current_180 >= 600)
3152 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
3153 else if (max_current_180 >= 400)
3154 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
3155 else
3156 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05303157 }
3158
Takashi Iwai8f230f42010-12-08 10:04:30 +01003159 mmc->ocr_avail = ocr_avail;
3160 mmc->ocr_avail_sdio = ocr_avail;
3161 if (host->ocr_avail_sdio)
3162 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3163 mmc->ocr_avail_sd = ocr_avail;
3164 if (host->ocr_avail_sd)
3165 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3166 else /* normal SD controllers don't support 1.8V */
3167 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3168 mmc->ocr_avail_mmc = ocr_avail;
3169 if (host->ocr_avail_mmc)
3170 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003171
3172 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303173 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003174 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003175 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003176 }
3177
Pierre Ossmand129bce2006-03-24 03:18:17 -08003178 spin_lock_init(&host->lock);
3179
3180 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003181 * Maximum number of segments. Depends on if the hardware
3182 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003183 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003184 if (host->flags & SDHCI_USE_ADMA)
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303185 mmc->max_segs = host->adma_max_desc;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003186 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003187 mmc->max_segs = 1;
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303188 else/* PIO */
3189 mmc->max_segs = host->adma_max_desc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003190
3191 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003192 * Maximum number of sectors in one transfer. Limited by DMA boundary
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303193 * size (512KiB), unless specified by platform specific driver. Each
3194 * descriptor can transfer a maximum of 64KB.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003195 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303196 if (host->ops->get_max_segments)
3197 mmc->max_req_size = (host->adma_max_desc * 65536);
3198 else
3199 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003200
3201 /*
3202 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003203 * of bytes. When doing hardware scatter/gather, each entry cannot
3204 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003205 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003206 if (host->flags & SDHCI_USE_ADMA) {
3207 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3208 mmc->max_seg_size = 65535;
3209 else
3210 mmc->max_seg_size = 65536;
3211 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003212 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003213 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003214
3215 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003216 * Maximum block size. This varies from controller to controller and
3217 * is specified in the capabilities register.
3218 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003219 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3220 mmc->max_blk_size = 2;
3221 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303222 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003223 SDHCI_MAX_BLOCK_SHIFT;
3224 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303225 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003226 "assuming 512 bytes\n", mmc_hostname(mmc));
3227 mmc->max_blk_size = 0;
3228 }
3229 }
3230
3231 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003232
3233 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003234 * Maximum block count.
3235 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003236 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003237
3238 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003239 * Init tasklets.
3240 */
3241 tasklet_init(&host->card_tasklet,
3242 sdhci_tasklet_card, (unsigned long)host);
3243 tasklet_init(&host->finish_tasklet,
3244 sdhci_tasklet_finish, (unsigned long)host);
3245
Al Viroe4cad1b2006-10-10 22:47:07 +01003246 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003247
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303248 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303249 init_waitqueue_head(&host->buf_ready_int);
3250
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303251 /* Initialize re-tuning timer */
3252 init_timer(&host->tuning_timer);
3253 host->tuning_timer.data = (unsigned long)host;
3254 host->tuning_timer.function = sdhci_tuning_timer;
3255 }
3256
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003257 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003258 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003259 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003260 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003261
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003262 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3263 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303264 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003265 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003266 }
3267
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003268 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003269
3270#ifdef CONFIG_MMC_DEBUG
3271 sdhci_dumpregs(host);
3272#endif
3273
Pierre Ossmanf9134312008-12-21 17:01:48 +01003274#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003275 snprintf(host->led_name, sizeof(host->led_name),
3276 "%s::", mmc_hostname(mmc));
3277 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003278 host->led.brightness = LED_OFF;
3279 host->led.default_trigger = mmc_hostname(mmc);
3280 host->led.brightness_set = sdhci_led_control;
3281
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003282 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003283 if (ret)
3284 goto reset;
3285#endif
3286
Pierre Ossman5f25a662006-10-04 02:15:39 -07003287 mmiowb();
3288
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303289 if (host->cpu_dma_latency_us)
3290 pm_qos_add_request(&host->pm_qos_req_dma,
3291 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003292 mmc_add_host(mmc);
3293
Sahitya Tummalaca422112013-02-22 12:15:54 +05303294 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003295 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003296 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3297 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003298
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003299 sdhci_enable_card_detection(host);
3300
Pierre Ossmand129bce2006-03-24 03:18:17 -08003301 return 0;
3302
Pierre Ossmanf9134312008-12-21 17:01:48 +01003303#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003304reset:
3305 sdhci_reset(host, SDHCI_RESET_ALL);
3306 free_irq(host->irq, host);
3307#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003308untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003309 tasklet_kill(&host->card_tasklet);
3310 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003311
3312 return ret;
3313}
3314
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003315EXPORT_SYMBOL_GPL(sdhci_add_host);
3316
Pierre Ossman1e728592008-04-16 19:13:13 +02003317void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003318{
Pierre Ossman1e728592008-04-16 19:13:13 +02003319 unsigned long flags;
3320
3321 if (dead) {
3322 spin_lock_irqsave(&host->lock, flags);
3323
3324 host->flags |= SDHCI_DEVICE_DEAD;
3325
3326 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303327 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003328 " transfer!\n", mmc_hostname(host->mmc));
3329
3330 host->mrq->cmd->error = -ENOMEDIUM;
3331 tasklet_schedule(&host->finish_tasklet);
3332 }
3333
3334 spin_unlock_irqrestore(&host->lock, flags);
3335 }
3336
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003337 sdhci_disable_card_detection(host);
3338
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303339 if (host->cpu_dma_latency_us)
3340 pm_qos_remove_request(&host->pm_qos_req_dma);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003341 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003342
Pierre Ossmanf9134312008-12-21 17:01:48 +01003343#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003344 led_classdev_unregister(&host->led);
3345#endif
3346
Pierre Ossman1e728592008-04-16 19:13:13 +02003347 if (!dead)
3348 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003349
3350 free_irq(host->irq, host);
3351
3352 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303353 if (host->version >= SDHCI_SPEC_300)
3354 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003355
3356 tasklet_kill(&host->card_tasklet);
3357 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003358
Sahitya Tummalaca422112013-02-22 12:15:54 +05303359 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003360 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003361
Pierre Ossman2134a922008-06-28 18:28:51 +02003362 kfree(host->adma_desc);
3363 kfree(host->align_buffer);
3364
3365 host->adma_desc = NULL;
3366 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003367}
3368
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003369EXPORT_SYMBOL_GPL(sdhci_remove_host);
3370
3371void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003372{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003373 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003374}
3375
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003376EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003377
3378/*****************************************************************************\
3379 * *
3380 * Driver init/exit *
3381 * *
3382\*****************************************************************************/
3383
3384static int __init sdhci_drv_init(void)
3385{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303386 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003387 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303388 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003389
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003390 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003391}
3392
3393static void __exit sdhci_drv_exit(void)
3394{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003395}
3396
3397module_init(sdhci_drv_init);
3398module_exit(sdhci_drv_exit);
3399
Pierre Ossmandf673b22006-06-30 02:22:31 -07003400module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003401module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003402
Pierre Ossman32710e82009-04-08 20:14:54 +02003403MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003404MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003405MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003406
Pierre Ossmandf673b22006-06-30 02:22:31 -07003407MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003408MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");