blob: 3efea7706c641e64f1e14c3068cc8827592eb39f [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter50accb92011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080030
Pierre Ossmand129bce2006-03-24 03:18:17 -080031#include "sdhci.h"
32
33#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080034
Pierre Ossmand129bce2006-03-24 03:18:17 -080035#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010036 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmanf9134312008-12-21 17:01:48 +010038#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
39 defined(CONFIG_MMC_SDHCI_MODULE))
40#define SDHCI_USE_LEDS_CLASS
41#endif
42
Arindam Nathb513ea22011-05-05 12:19:04 +053043#define MAX_TUNING_LOOP 40
44
Pierre Ossmandf673b22006-06-30 02:22:31 -070045static unsigned int debug_quirks = 0;
Adrian Hunter50accb92011-10-03 15:33:34 +030046static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070047
Pierre Ossmand129bce2006-03-24 03:18:17 -080048static void sdhci_finish_data(struct sdhci_host *);
49
50static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
51static void sdhci_finish_command(struct sdhci_host *);
Girish K S2cd06dc2012-01-06 09:56:39 +053052static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053053static void sdhci_tuning_timer(unsigned long data);
Pierre Ossmand129bce2006-03-24 03:18:17 -080054
Adrian Hunter50accb92011-10-03 15:33:34 +030055#ifdef CONFIG_PM_RUNTIME
56static int sdhci_runtime_pm_get(struct sdhci_host *host);
57static int sdhci_runtime_pm_put(struct sdhci_host *host);
58#else
59static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
60{
61 return 0;
62}
63static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
64{
65 return 0;
66}
67#endif
68
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053069static void sdhci_dump_state(struct sdhci_host *host)
70{
71 struct mmc_host *mmc = host->mmc;
72
73 pr_info("%s: clk: %d clk-gated: %d claimer: %s pwr: %d\n",
74 mmc_hostname(mmc), host->clock, mmc->clk_gated,
75 mmc->claimer->comm, host->pwr);
76 pr_info("%s: rpmstatus[pltfm](runtime-suspend:usage_count:disable_depth)(%d:%d:%d)\n",
77 mmc_hostname(mmc), mmc->parent->power.runtime_status,
78 atomic_read(&mmc->parent->power.usage_count),
79 mmc->parent->power.disable_depth);
80}
81
Pierre Ossmand129bce2006-03-24 03:18:17 -080082static void sdhci_dumpregs(struct sdhci_host *host)
83{
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053084 pr_info(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070085 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080086
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053087 pr_info(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030088 sdhci_readl(host, SDHCI_DMA_ADDRESS),
89 sdhci_readw(host, SDHCI_HOST_VERSION));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053090 pr_info(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030091 sdhci_readw(host, SDHCI_BLOCK_SIZE),
92 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053093 pr_info(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030094 sdhci_readl(host, SDHCI_ARGUMENT),
95 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053096 pr_info(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030097 sdhci_readl(host, SDHCI_PRESENT_STATE),
98 sdhci_readb(host, SDHCI_HOST_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053099 pr_info(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300100 sdhci_readb(host, SDHCI_POWER_CONTROL),
101 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530102 pr_info(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300103 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
104 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530105 pr_info(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
107 sdhci_readl(host, SDHCI_INT_STATUS));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530108 pr_info(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300109 sdhci_readl(host, SDHCI_INT_ENABLE),
110 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530111 pr_info(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300112 sdhci_readw(host, SDHCI_ACMD12_ERR),
113 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530114 pr_info(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300115 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500116 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530117 pr_info(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500118 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300119 sdhci_readl(host, SDHCI_MAX_CURRENT));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530120 pr_info(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530121 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800122
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100123 if (host->flags & SDHCI_USE_ADMA)
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530124 pr_info(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100125 readl(host->ioaddr + SDHCI_ADMA_ERROR),
126 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
127
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530128 sdhci_dump_state(host);
129 pr_info(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800130}
131
132/*****************************************************************************\
133 * *
134 * Low level functions *
135 * *
136\*****************************************************************************/
137
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300138static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
139{
140 u32 ier;
141
142 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
143 ier &= ~clear;
144 ier |= set;
145 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
146 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
147}
148
149static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
150{
151 sdhci_clear_set_irqs(host, 0, irqs);
152}
153
154static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
155{
156 sdhci_clear_set_irqs(host, irqs, 0);
157}
158
159static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
160{
Sahitya Tummalaca422112013-02-22 12:15:54 +0530161 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300162
Adrian Hunterc79396c2011-12-27 15:48:42 +0200163 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100164 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300165 return;
166
Sahitya Tummalaca422112013-02-22 12:15:54 +0530167 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
168 SDHCI_CARD_PRESENT;
169 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
Adrian Hunter50accb92011-10-03 15:33:34 +0300170
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300171 if (enable)
172 sdhci_unmask_irqs(host, irqs);
173 else
174 sdhci_mask_irqs(host, irqs);
175}
176
177static void sdhci_enable_card_detection(struct sdhci_host *host)
178{
179 sdhci_set_card_detection(host, true);
180}
181
182static void sdhci_disable_card_detection(struct sdhci_host *host)
183{
184 sdhci_set_card_detection(host, false);
185}
186
Pierre Ossmand129bce2006-03-24 03:18:17 -0800187static void sdhci_reset(struct sdhci_host *host, u8 mask)
188{
Pierre Ossmane16514d2006-06-30 02:22:24 -0700189 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300190 u32 uninitialized_var(ier);
Pierre Ossmane16514d2006-06-30 02:22:24 -0700191
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100192 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300193 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700194 SDHCI_CARD_PRESENT))
195 return;
196 }
197
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300198 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
199 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
200
Philip Rakity393c1a32011-01-21 11:26:40 -0800201 if (host->ops->platform_reset_enter)
202 host->ops->platform_reset_enter(host, mask);
203
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300204 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800205
Pierre Ossmane16514d2006-06-30 02:22:24 -0700206 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800207 host->clock = 0;
208
Pierre Ossmane16514d2006-06-30 02:22:24 -0700209 /* Wait max 100 ms */
210 timeout = 100;
211
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530212 if (host->ops->check_power_status && host->pwr &&
213 (mask & SDHCI_RESET_ALL))
Sahitya Tummala179e7382013-03-20 19:24:01 +0530214 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530215
Pierre Ossmane16514d2006-06-30 02:22:24 -0700216 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300217 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d2006-06-30 02:22:24 -0700218 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +0530219 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d2006-06-30 02:22:24 -0700220 mmc_hostname(host->mmc), (int)mask);
221 sdhci_dumpregs(host);
222 return;
223 }
224 timeout--;
225 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300227
Philip Rakity393c1a32011-01-21 11:26:40 -0800228 if (host->ops->platform_reset_exit)
229 host->ops->platform_reset_exit(host, mask);
230
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300231 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
232 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Sahitya Tummalaca422112013-02-22 12:15:54 +0530233
234 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
235 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
236 host->ops->enable_dma(host);
237 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800238}
239
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800240static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
241
242static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800243{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800244 if (soft)
245 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
246 else
247 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800248
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300249 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
250 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700251 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
252 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300253 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800254
255 if (soft) {
256 /* force clock reconfiguration */
257 host->clock = 0;
258 sdhci_set_ios(host->mmc, &host->mmc->ios);
259 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300260}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300262static void sdhci_reinit(struct sdhci_host *host)
263{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800264 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300265 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800266}
267
268static void sdhci_activate_led(struct sdhci_host *host)
269{
270 u8 ctrl;
271
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300272 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300274 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800275}
276
277static void sdhci_deactivate_led(struct sdhci_host *host)
278{
279 u8 ctrl;
280
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300281 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800282 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300283 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800284}
285
Pierre Ossmanf9134312008-12-21 17:01:48 +0100286#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100287static void sdhci_led_control(struct led_classdev *led,
288 enum led_brightness brightness)
289{
290 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
291 unsigned long flags;
292
293 spin_lock_irqsave(&host->lock, flags);
294
Adrian Hunter50accb92011-10-03 15:33:34 +0300295 if (host->runtime_suspended)
296 goto out;
297
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100298 if (brightness == LED_OFF)
299 sdhci_deactivate_led(host);
300 else
301 sdhci_activate_led(host);
Adrian Hunter50accb92011-10-03 15:33:34 +0300302out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100303 spin_unlock_irqrestore(&host->lock, flags);
304}
305#endif
306
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307/*****************************************************************************\
308 * *
309 * Core functions *
310 * *
311\*****************************************************************************/
312
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800314{
Pierre Ossman76591502008-07-21 00:32:11 +0200315 unsigned long flags;
316 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700317 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200318 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800319
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100320 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800321
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100322 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200323 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800324
Pierre Ossman76591502008-07-21 00:32:11 +0200325 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800326
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100327 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200328 if (!sg_miter_next(&host->sg_miter))
329 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800330
Pierre Ossman76591502008-07-21 00:32:11 +0200331 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800332
Pierre Ossman76591502008-07-21 00:32:11 +0200333 blksize -= len;
334 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200335
Pierre Ossman76591502008-07-21 00:32:11 +0200336 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800337
Pierre Ossman76591502008-07-21 00:32:11 +0200338 while (len) {
339 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300340 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200341 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800342 }
Pierre Ossman76591502008-07-21 00:32:11 +0200343
344 *buf = scratch & 0xFF;
345
346 buf++;
347 scratch >>= 8;
348 chunk--;
349 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800350 }
351 }
Pierre Ossman76591502008-07-21 00:32:11 +0200352
353 sg_miter_stop(&host->sg_miter);
354
355 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100356}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800357
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358static void sdhci_write_block_pio(struct sdhci_host *host)
359{
Pierre Ossman76591502008-07-21 00:32:11 +0200360 unsigned long flags;
361 size_t blksize, len, chunk;
362 u32 scratch;
363 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100364
365 DBG("PIO writing\n");
366
367 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200368 chunk = 0;
369 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100370
Pierre Ossman76591502008-07-21 00:32:11 +0200371 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100372
373 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200374 if (!sg_miter_next(&host->sg_miter))
375 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100376
Pierre Ossman76591502008-07-21 00:32:11 +0200377 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200378
Pierre Ossman76591502008-07-21 00:32:11 +0200379 blksize -= len;
380 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381
Pierre Ossman76591502008-07-21 00:32:11 +0200382 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100383
Pierre Ossman76591502008-07-21 00:32:11 +0200384 while (len) {
385 scratch |= (u32)*buf << (chunk * 8);
386
387 buf++;
388 chunk++;
389 len--;
390
391 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300392 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200393 chunk = 0;
394 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100395 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100396 }
397 }
Pierre Ossman76591502008-07-21 00:32:11 +0200398
399 sg_miter_stop(&host->sg_miter);
400
401 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100402}
403
404static void sdhci_transfer_pio(struct sdhci_host *host)
405{
406 u32 mask;
407
408 BUG_ON(!host->data);
409
Pierre Ossman76591502008-07-21 00:32:11 +0200410 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100411 return;
412
413 if (host->data->flags & MMC_DATA_READ)
414 mask = SDHCI_DATA_AVAILABLE;
415 else
416 mask = SDHCI_SPACE_AVAILABLE;
417
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200418 /*
419 * Some controllers (JMicron JMB38x) mess up the buffer bits
420 * for transfers < 4 bytes. As long as it is just one block,
421 * we can ignore the bits.
422 */
423 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
424 (host->data->blocks == 1))
425 mask = ~0;
426
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300427 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300428 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
429 udelay(100);
430
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100431 if (host->data->flags & MMC_DATA_READ)
432 sdhci_read_block_pio(host);
433 else
434 sdhci_write_block_pio(host);
435
Pierre Ossman76591502008-07-21 00:32:11 +0200436 host->blocks--;
437 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100438 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100439 }
440
441 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800442}
443
Pierre Ossman2134a922008-06-28 18:28:51 +0200444static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
445{
446 local_irq_save(*flags);
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800447 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200448}
449
450static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
451{
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800452 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200453 local_irq_restore(*flags);
454}
455
Ben Dooks118cd172010-03-05 13:43:26 -0800456static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
457{
Ben Dooks9e506f32010-03-05 13:43:29 -0800458 __le32 *dataddr = (__le32 __force *)(desc + 4);
459 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800460
Ben Dooks9e506f32010-03-05 13:43:29 -0800461 /* SDHCI specification says ADMA descriptors should be 4 byte
462 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800463
Ben Dooks9e506f32010-03-05 13:43:29 -0800464 cmdlen[0] = cpu_to_le16(cmd);
465 cmdlen[1] = cpu_to_le16(len);
466
467 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800468}
469
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800470static int sdhci_pre_dma_transfer(struct sdhci_host *host,
471 struct mmc_data *data,
472 struct sdhci_next *next)
473{
474 int sg_count;
475
476 if (!next && data->host_cookie &&
477 data->host_cookie != host->next_data.cookie) {
478 printk(KERN_WARNING "[%s] invalid cookie: data->host_cookie %d"
479 " host->next_data.cookie %d\n",
480 __func__, data->host_cookie, host->next_data.cookie);
481 data->host_cookie = 0;
482 }
483
484 /* Check if next job is already prepared */
485 if (next ||
486 (!next && data->host_cookie != host->next_data.cookie)) {
487 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg,
488 data->sg_len,
489 (data->flags & MMC_DATA_WRITE) ?
490 DMA_TO_DEVICE : DMA_FROM_DEVICE);
491 } else {
492 sg_count = host->next_data.sg_count;
493 host->next_data.sg_count = 0;
494 }
495
496 if (sg_count == 0)
497 return -EINVAL;
498
499 if (next) {
500 next->sg_count = sg_count;
501 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
502 } else
503 host->sg_count = sg_count;
504
505 return sg_count;
506}
507
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200508static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200509 struct mmc_data *data)
510{
511 int direction;
512
513 u8 *desc;
514 u8 *align;
515 dma_addr_t addr;
516 dma_addr_t align_addr;
517 int len, offset;
518
519 struct scatterlist *sg;
520 int i;
521 char *buffer;
522 unsigned long flags;
523
524 /*
525 * The spec does not specify endianness of descriptor table.
526 * We currently guess that it is LE.
527 */
528
529 if (data->flags & MMC_DATA_READ)
530 direction = DMA_FROM_DEVICE;
531 else
532 direction = DMA_TO_DEVICE;
533
534 /*
535 * The ADMA descriptor table is mapped further down as we
536 * need to fill it with data first.
537 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200538 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530539 host->align_buffer,
540 host->align_buf_sz,
541 direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700542 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200543 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200544 BUG_ON(host->align_addr & 0x3);
545
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800546 host->sg_count = sdhci_pre_dma_transfer(host, data, NULL);
547 if (host->sg_count < 0)
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200548 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200549
550 desc = host->adma_desc;
551 align = host->align_buffer;
552
553 align_addr = host->align_addr;
554
555 for_each_sg(data->sg, sg, host->sg_count, i) {
556 addr = sg_dma_address(sg);
557 len = sg_dma_len(sg);
558
559 /*
560 * The SDHCI specification states that ADMA
561 * addresses must be 32-bit aligned. If they
562 * aren't, then we use a bounce buffer for
563 * the (up to three) bytes that screw up the
564 * alignment.
565 */
566 offset = (4 - (addr & 0x3)) & 0x3;
567 if (offset) {
568 if (data->flags & MMC_DATA_WRITE) {
569 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200570 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200571 memcpy(align, buffer, offset);
572 sdhci_kunmap_atomic(buffer, &flags);
573 }
574
Ben Dooks118cd172010-03-05 13:43:26 -0800575 /* tran, valid */
576 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200577
578 BUG_ON(offset > 65536);
579
Pierre Ossman2134a922008-06-28 18:28:51 +0200580 align += 4;
581 align_addr += 4;
582
583 desc += 8;
584
585 addr += offset;
586 len -= offset;
587 }
588
Pierre Ossman2134a922008-06-28 18:28:51 +0200589 BUG_ON(len > 65536);
590
Ben Dooks118cd172010-03-05 13:43:26 -0800591 /* tran, valid */
592 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200593 desc += 8;
594
595 /*
596 * If this triggers then we have a calculation bug
597 * somewhere. :/
598 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530599 WARN_ON((desc - host->adma_desc) > host->adma_desc_sz);
600
Pierre Ossman2134a922008-06-28 18:28:51 +0200601 }
602
Thomas Abraham70764a92010-05-26 14:42:04 -0700603 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
604 /*
605 * Mark the last descriptor as the terminating descriptor
606 */
607 if (desc != host->adma_desc) {
608 desc -= 8;
609 desc[0] |= 0x2; /* end */
610 }
611 } else {
612 /*
613 * Add a terminating entry.
614 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200615
Thomas Abraham70764a92010-05-26 14:42:04 -0700616 /* nop, end, valid */
617 sdhci_set_adma_desc(desc, 0, 0, 0x3);
618 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200619
620 /*
621 * Resync align buffer as we might have changed it.
622 */
623 if (data->flags & MMC_DATA_WRITE) {
624 dma_sync_single_for_device(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530625 host->align_addr,
626 host->align_buf_sz,
627 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200628 }
629
630 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530631 host->adma_desc,
632 host->adma_desc_sz,
633 DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200634 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200635 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200636 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200637
638 return 0;
639
640unmap_entries:
641 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
642 data->sg_len, direction);
643unmap_align:
644 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530645 host->align_buf_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200646fail:
647 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200648}
649
650static void sdhci_adma_table_post(struct sdhci_host *host,
651 struct mmc_data *data)
652{
653 int direction;
654
655 struct scatterlist *sg;
656 int i, size;
657 u8 *align;
658 char *buffer;
659 unsigned long flags;
660
661 if (data->flags & MMC_DATA_READ)
662 direction = DMA_FROM_DEVICE;
663 else
664 direction = DMA_TO_DEVICE;
665
666 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530667 host->adma_desc_sz, DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200668
669 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530670 host->align_buf_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200671
672 if (data->flags & MMC_DATA_READ) {
673 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
674 data->sg_len, direction);
675
676 align = host->align_buffer;
677
678 for_each_sg(data->sg, sg, host->sg_count, i) {
679 if (sg_dma_address(sg) & 0x3) {
680 size = 4 - (sg_dma_address(sg) & 0x3);
681
682 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200683 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200684 memcpy(buffer, align, size);
685 sdhci_kunmap_atomic(buffer, &flags);
686
687 align += 4;
688 }
689 }
690 }
691
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800692 if (!data->host_cookie)
693 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
694 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200695}
696
Andrei Warkentina3c77782011-04-11 16:13:42 -0500697static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800698{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700699 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500700 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700701 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800702
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200703 /*
704 * If the host controller provides us with an incorrect timeout
705 * value, just skip the check and use 0xE. The hardware may take
706 * longer to time out, but that's much better than having a too-short
707 * timeout value.
708 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200709 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200710 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200711
Andrei Warkentina3c77782011-04-11 16:13:42 -0500712 /* Unspecified timeout, assume max */
713 if (!data && !cmd->cmd_timeout_ms)
714 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800715
Andrei Warkentina3c77782011-04-11 16:13:42 -0500716 /* timeout in us */
717 if (!data)
718 target_timeout = cmd->cmd_timeout_ms * 1000;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530719 else {
720 target_timeout = data->timeout_ns / 1000;
721 if (host->clock)
722 target_timeout += data->timeout_clks / host->clock;
723 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700724
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700725 /*
726 * Figure out needed cycles.
727 * We do this in steps in order to fit inside a 32 bit int.
728 * The first step is the minimum timeout, which will have a
729 * minimum resolution of 6 bits:
730 * (1) 2^13*1000 > 2^22,
731 * (2) host->timeout_clk < 2^16
732 * =>
733 * (1) / (2) > 2^6
734 */
735 count = 0;
736 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
737 while (current_timeout < target_timeout) {
738 count++;
739 current_timeout <<= 1;
740 if (count >= 0xF)
741 break;
742 }
743
Sahitya Tummala4d12d0b2013-04-12 11:59:25 +0530744 if (!(host->quirks2 & SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT)) {
745 if (count >= 0xF) {
746 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
747 mmc_hostname(host->mmc), count, cmd->opcode);
748 count = 0xE;
749 }
Sahitya Tummalaca422112013-02-22 12:15:54 +0530750 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700751
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200752 return count;
753}
754
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300755static void sdhci_set_transfer_irqs(struct sdhci_host *host)
756{
757 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
758 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
759
760 if (host->flags & SDHCI_REQ_USE_DMA)
761 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
762 else
763 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
764}
765
Andrei Warkentina3c77782011-04-11 16:13:42 -0500766static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200767{
768 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200769 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500770 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200771 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200772
773 WARN_ON(host->data);
774
Andrei Warkentina3c77782011-04-11 16:13:42 -0500775 if (data || (cmd->flags & MMC_RSP_BUSY)) {
776 count = sdhci_calc_timeout(host, cmd);
777 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
778 }
779
780 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200781 return;
782
783 /* Sanity checks */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530784 BUG_ON(data->blksz * data->blocks > host->mmc->max_req_size);
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200785 BUG_ON(data->blksz > host->mmc->max_blk_size);
786 BUG_ON(data->blocks > 65535);
787
788 host->data = data;
789 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400790 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200791
Richard Röjforsa13abc72009-09-22 16:45:30 -0700792 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100793 host->flags |= SDHCI_REQ_USE_DMA;
794
Pierre Ossman2134a922008-06-28 18:28:51 +0200795 /*
796 * FIXME: This doesn't account for merging when mapping the
797 * scatterlist.
798 */
799 if (host->flags & SDHCI_REQ_USE_DMA) {
800 int broken, i;
801 struct scatterlist *sg;
802
803 broken = 0;
804 if (host->flags & SDHCI_USE_ADMA) {
805 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
806 broken = 1;
807 } else {
808 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
809 broken = 1;
810 }
811
812 if (unlikely(broken)) {
813 for_each_sg(data->sg, sg, data->sg_len, i) {
814 if (sg->length & 0x3) {
815 DBG("Reverting to PIO because of "
816 "transfer size (%d)\n",
817 sg->length);
818 host->flags &= ~SDHCI_REQ_USE_DMA;
819 break;
820 }
821 }
822 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100823 }
824
825 /*
826 * The assumption here being that alignment is the same after
827 * translation to device address space.
828 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200829 if (host->flags & SDHCI_REQ_USE_DMA) {
830 int broken, i;
831 struct scatterlist *sg;
832
833 broken = 0;
834 if (host->flags & SDHCI_USE_ADMA) {
835 /*
836 * As we use 3 byte chunks to work around
837 * alignment problems, we need to check this
838 * quirk.
839 */
840 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
841 broken = 1;
842 } else {
843 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
844 broken = 1;
845 }
846
847 if (unlikely(broken)) {
848 for_each_sg(data->sg, sg, data->sg_len, i) {
849 if (sg->offset & 0x3) {
850 DBG("Reverting to PIO because of "
851 "bad alignment\n");
852 host->flags &= ~SDHCI_REQ_USE_DMA;
853 break;
854 }
855 }
856 }
857 }
858
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200859 if (host->flags & SDHCI_REQ_USE_DMA) {
860 if (host->flags & SDHCI_USE_ADMA) {
861 ret = sdhci_adma_table_pre(host, data);
862 if (ret) {
863 /*
864 * This only happens when someone fed
865 * us an invalid request.
866 */
867 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200868 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200869 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300870 sdhci_writel(host, host->adma_addr,
871 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200872 }
873 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300874 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200875
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800876 sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300877 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200878 /*
879 * This only happens when someone fed
880 * us an invalid request.
881 */
882 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200883 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200884 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200885 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300886 sdhci_writel(host, sg_dma_address(data->sg),
887 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200888 }
889 }
890 }
891
Pierre Ossman2134a922008-06-28 18:28:51 +0200892 /*
893 * Always adjust the DMA selection as some controllers
894 * (e.g. JMicron) can't do PIO properly when the selection
895 * is ADMA.
896 */
897 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300898 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200899 ctrl &= ~SDHCI_CTRL_DMA_MASK;
900 if ((host->flags & SDHCI_REQ_USE_DMA) &&
901 (host->flags & SDHCI_USE_ADMA))
902 ctrl |= SDHCI_CTRL_ADMA32;
903 else
904 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300905 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100906 }
907
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200908 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200909 int flags;
910
911 flags = SG_MITER_ATOMIC;
912 if (host->data->flags & MMC_DATA_READ)
913 flags |= SG_MITER_TO_SG;
914 else
915 flags |= SG_MITER_FROM_SG;
916 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200917 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800918 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700919
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300920 sdhci_set_transfer_irqs(host);
921
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400922 /* Set the DMA boundary value and block size */
923 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
924 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300925 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700926}
927
928static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500929 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700930{
931 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500932 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700933
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700934 if (data == NULL)
935 return;
936
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200937 WARN_ON(!host->data);
938
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700939 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500940 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
941 mode |= SDHCI_TRNS_MULTI;
942 /*
943 * If we are sending CMD23, CMD12 never gets sent
944 * on successful completion (so no Auto-CMD12).
945 */
946 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
947 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500948 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
949 mode |= SDHCI_TRNS_AUTO_CMD23;
950 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
951 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700952 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500953
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530954 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700955 mode |= SDHCI_TRNS_READ;
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530956 if (host->ops->toggle_cdr)
957 host->ops->toggle_cdr(host, true);
958 }
959 if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE))
960 host->ops->toggle_cdr(host, false);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100961 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700962 mode |= SDHCI_TRNS_DMA;
963
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300964 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800965}
966
967static void sdhci_finish_data(struct sdhci_host *host)
968{
969 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800970
971 BUG_ON(!host->data);
972
973 data = host->data;
974 host->data = NULL;
975
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100976 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200977 if (host->flags & SDHCI_USE_ADMA)
978 sdhci_adma_table_post(host, data);
979 else {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800980 if (!data->host_cookie)
981 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
982 data->sg_len,
983 (data->flags & MMC_DATA_READ) ?
984 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200985 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800986 }
987
988 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200989 * The specification states that the block count register must
990 * be updated, but it does not specify at what point in the
991 * data flow. That makes the register entirely useless to read
992 * back so we have to assume that nothing made it to the card
993 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200995 if (data->error)
996 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200998 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800999
Andrei Warkentine89d4562011-05-23 15:06:37 -05001000 /*
1001 * Need to send CMD12 if -
1002 * a) open-ended multiblock transfer (no CMD23)
1003 * b) error in multiblock transfer
1004 */
1005 if (data->stop &&
1006 (data->error ||
1007 !host->mrq->sbc)) {
1008
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009 /*
1010 * The controller needs a reset of internal state machines
1011 * upon error conditions.
1012 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001013 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014 sdhci_reset(host, SDHCI_RESET_CMD);
1015 sdhci_reset(host, SDHCI_RESET_DATA);
1016 }
1017
1018 sdhci_send_command(host, data->stop);
1019 } else
1020 tasklet_schedule(&host->finish_tasklet);
1021}
1022
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301023#define SDHCI_REQUEST_TIMEOUT 10 /* Default request timeout in seconds */
1024
Pierre Ossmand129bce2006-03-24 03:18:17 -08001025static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
1026{
1027 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001028 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001029 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030
1031 WARN_ON(host->cmd);
1032
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001034 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001035
1036 mask = SDHCI_CMD_INHIBIT;
1037 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1038 mask |= SDHCI_DATA_INHIBIT;
1039
1040 /* We shouldn't wait for data inihibit for stop commands, even
1041 though they might use busy signaling */
1042 if (host->mrq->data && (cmd == host->mrq->data->stop))
1043 mask &= ~SDHCI_DATA_INHIBIT;
1044
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001045 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001046 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301047 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001048 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001049 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001050 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001051 tasklet_schedule(&host->finish_tasklet);
1052 return;
1053 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001054 timeout--;
1055 mdelay(1);
1056 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301058 mod_timer(&host->timer, jiffies + SDHCI_REQUEST_TIMEOUT * HZ);
1059
1060 if (cmd->cmd_timeout_ms > SDHCI_REQUEST_TIMEOUT * MSEC_PER_SEC)
1061 mod_timer(&host->timer, jiffies +
1062 (msecs_to_jiffies(cmd->cmd_timeout_ms * 2)));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001063
1064 host->cmd = cmd;
1065
Andrei Warkentina3c77782011-04-11 16:13:42 -05001066 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001067
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001068 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001069
Andrei Warkentine89d4562011-05-23 15:06:37 -05001070 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001071
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301073 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001074 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001075 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001076 tasklet_schedule(&host->finish_tasklet);
1077 return;
1078 }
1079
1080 if (!(cmd->flags & MMC_RSP_PRESENT))
1081 flags = SDHCI_CMD_RESP_NONE;
1082 else if (cmd->flags & MMC_RSP_136)
1083 flags = SDHCI_CMD_RESP_LONG;
1084 else if (cmd->flags & MMC_RSP_BUSY)
1085 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1086 else
1087 flags = SDHCI_CMD_RESP_SHORT;
1088
1089 if (cmd->flags & MMC_RSP_CRC)
1090 flags |= SDHCI_CMD_CRC;
1091 if (cmd->flags & MMC_RSP_OPCODE)
1092 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301093
1094 /* CMD19 is special in that the Data Present Select should be set */
Girish K S2cd06dc2012-01-06 09:56:39 +05301095 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1096 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001097 flags |= SDHCI_CMD_DATA;
1098
Sahitya Tummala48b458e2013-04-08 12:53:44 +05301099 if (cmd->data)
1100 host->data_start_time = ktime_get();
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001101 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001102}
1103
1104static void sdhci_finish_command(struct sdhci_host *host)
1105{
1106 int i;
1107
1108 BUG_ON(host->cmd == NULL);
1109
1110 if (host->cmd->flags & MMC_RSP_PRESENT) {
1111 if (host->cmd->flags & MMC_RSP_136) {
1112 /* CRC is stripped so we need to do some shifting. */
1113 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001114 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001115 SDHCI_RESPONSE + (3-i)*4) << 8;
1116 if (i != 3)
1117 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001118 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001119 SDHCI_RESPONSE + (3-i)*4-1);
1120 }
1121 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001122 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001123 }
1124 }
1125
Pierre Ossman17b04292007-07-22 22:18:46 +02001126 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001127
Andrei Warkentine89d4562011-05-23 15:06:37 -05001128 /* Finished CMD23, now send actual command. */
1129 if (host->cmd == host->mrq->sbc) {
1130 host->cmd = NULL;
1131 sdhci_send_command(host, host->mrq->cmd);
1132 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001133
Andrei Warkentine89d4562011-05-23 15:06:37 -05001134 /* Processed actual command. */
1135 if (host->data && host->data_early)
1136 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001137
Andrei Warkentine89d4562011-05-23 15:06:37 -05001138 if (!host->cmd->data)
1139 tasklet_schedule(&host->finish_tasklet);
1140
1141 host->cmd = NULL;
1142 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001143}
1144
1145static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1146{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301147 int div = 0; /* Initialized for compiler warning */
Sahitya Tummalaca422112013-02-22 12:15:54 +05301148 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301149 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001150 unsigned long timeout;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301151 unsigned long flags;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001152
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301153 spin_lock_irqsave(&host->lock, flags);
Todd Poynor30832ab2011-12-27 15:48:46 +02001154 if (clock && clock == host->clock)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301155 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001156
Sahitya Tummalaca422112013-02-22 12:15:54 +05301157 host->mmc->actual_clock = 0;
1158
Anton Vorontsov81146342009-03-17 00:13:59 +03001159 if (host->ops->set_clock) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301160 spin_unlock_irqrestore(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001161 host->ops->set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301162 spin_lock_irqsave(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001163 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301164 goto ret;
Anton Vorontsov81146342009-03-17 00:13:59 +03001165 }
1166
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301167 if (host->clock)
1168 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001169
1170 if (clock == 0)
1171 goto out;
1172
Zhangfei Gao85105c52010-08-06 07:10:01 +08001173 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301174 /*
1175 * Check if the Host Controller supports Programmable Clock
1176 * Mode.
1177 */
1178 if (host->clk_mul) {
1179 u16 ctrl;
1180
1181 /*
1182 * We need to figure out whether the Host Driver needs
1183 * to select Programmable Clock Mode, or the value can
1184 * be set automatically by the Host Controller based on
1185 * the Preset Value registers.
1186 */
1187 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1188 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1189 for (div = 1; div <= 1024; div++) {
1190 if (((host->max_clk * host->clk_mul) /
1191 div) <= clock)
1192 break;
1193 }
1194 /*
1195 * Set Programmable Clock Mode in the Clock
1196 * Control register.
1197 */
1198 clk = SDHCI_PROG_CLOCK_MODE;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301199 real_div = div;
1200 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301201 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001202 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301203 } else {
1204 /* Version 3.00 divisors must be a multiple of 2. */
1205 if (host->max_clk <= clock)
1206 div = 1;
1207 else {
1208 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1209 div += 2) {
1210 if ((host->max_clk / div) <= clock)
1211 break;
1212 }
1213 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301214 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301215 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001216 }
1217 } else {
1218 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001219 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001220 if ((host->max_clk / div) <= clock)
1221 break;
1222 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301223 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301224 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001225 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001226
Sahitya Tummalaca422112013-02-22 12:15:54 +05301227 if (real_div)
1228 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1229
Sahitya Tummala00240122013-02-28 19:50:51 +05301230 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
1231 div = 0;
1232
Arindam Nathc3ed3872011-05-05 12:19:06 +05301233 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001234 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1235 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001236 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001237 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001238
Chris Ball27f6cb12009-09-22 16:45:31 -07001239 /* Wait max 20 ms */
1240 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001241 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001242 & SDHCI_CLOCK_INT_STABLE)) {
1243 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301244 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001245 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001246 sdhci_dumpregs(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301247 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001248 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001249 timeout--;
1250 mdelay(1);
1251 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001252
1253 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001254 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001255
1256out:
1257 host->clock = clock;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301258ret:
1259 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001260}
1261
Sahitya Tummalaca422112013-02-22 12:15:54 +05301262static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001263{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001264 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001265
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001266 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001267 switch (1 << power) {
1268 case MMC_VDD_165_195:
1269 pwr = SDHCI_POWER_180;
1270 break;
1271 case MMC_VDD_29_30:
1272 case MMC_VDD_30_31:
1273 pwr = SDHCI_POWER_300;
1274 break;
1275 case MMC_VDD_32_33:
1276 case MMC_VDD_33_34:
1277 pwr = SDHCI_POWER_330;
1278 break;
1279 default:
1280 BUG();
1281 }
1282 }
1283
1284 if (host->pwr == pwr)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301285 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001286
Pierre Ossmanae628902009-05-03 20:45:03 +02001287 host->pwr = pwr;
1288
1289 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001290 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301291 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301292 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301293 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001294 }
1295
1296 /*
1297 * Spec says that we should clear the power reg before setting
1298 * a new value. Some controllers don't seem to like this though.
1299 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301300 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001301 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301302 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301303 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301304 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001305
Andres Salomone08c1692008-07-04 10:00:03 -07001306 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001307 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001308 * and set turn on power at the same time, so set the voltage first.
1309 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301310 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001311 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301312 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301313 host->ops->check_power_status(host, REQ_BUS_ON);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301314 }
Pierre Ossmanae628902009-05-03 20:45:03 +02001315
1316 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001317
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001318 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301319 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301320 host->ops->check_power_status(host, REQ_BUS_ON);
Harald Welte557b0692009-06-18 16:53:38 +02001321
1322 /*
1323 * Some controllers need an extra 10ms delay of 10ms before they
1324 * can apply clock after applying power
1325 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001326 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001327 mdelay(10);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301328
1329 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001330}
1331
Pierre Ossmand129bce2006-03-24 03:18:17 -08001332/*****************************************************************************\
1333 * *
1334 * MMC callbacks *
1335 * *
1336\*****************************************************************************/
1337
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301338static int sdhci_enable(struct mmc_host *mmc)
1339{
1340 struct sdhci_host *host = mmc_priv(mmc);
1341
1342 if (host->cpu_dma_latency_us)
1343 pm_qos_update_request(&host->pm_qos_req_dma,
1344 host->cpu_dma_latency_us);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301345 if (host->ops->platform_bus_voting)
1346 host->ops->platform_bus_voting(host, 1);
1347
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301348 return 0;
1349}
1350
1351static int sdhci_disable(struct mmc_host *mmc)
1352{
1353 struct sdhci_host *host = mmc_priv(mmc);
1354
1355 if (host->cpu_dma_latency_us)
1356 pm_qos_update_request(&host->pm_qos_req_dma,
1357 PM_QOS_DEFAULT_VALUE);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301358 if (host->ops->platform_bus_voting)
1359 host->ops->platform_bus_voting(host, 0);
1360
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301361 return 0;
1362}
1363
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001364static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1365 bool is_first_req)
1366{
1367 struct sdhci_host *host = mmc_priv(mmc);
1368
1369 if (mrq->data->host_cookie) {
1370 mrq->data->host_cookie = 0;
1371 return;
1372 }
1373
1374 if (host->flags & SDHCI_REQ_USE_DMA)
1375 if (sdhci_pre_dma_transfer(host, mrq->data, &host->next_data) < 0)
1376 mrq->data->host_cookie = 0;
1377}
1378
1379static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1380 int err)
1381{
1382 struct sdhci_host *host = mmc_priv(mmc);
1383 struct mmc_data *data = mrq->data;
1384
1385 if (host->flags & SDHCI_REQ_USE_DMA) {
1386 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1387 (data->flags & MMC_DATA_WRITE) ?
1388 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1389 data->host_cookie = 0;
1390 }
1391}
1392
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301393static bool sdhci_check_state(struct sdhci_host *host)
1394{
1395 struct mmc_host *mmc = host->mmc;
1396
1397 if (!host->clock || !host->pwr ||
1398 pm_runtime_suspended(mmc->parent))
1399 return true;
1400 else
1401 return false;
1402}
1403
Pierre Ossmand129bce2006-03-24 03:18:17 -08001404static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1405{
1406 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001407 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001408 unsigned long flags;
1409
1410 host = mmc_priv(mmc);
1411
Adrian Hunter50accb92011-10-03 15:33:34 +03001412 sdhci_runtime_pm_get(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301413 if (sdhci_check_state(host)) {
1414 sdhci_dump_state(host);
1415 WARN(1, "sdhci in bad state");
1416 mrq->cmd->error = -EIO;
1417 if (mrq->data)
1418 mrq->data->error = -EIO;
1419 tasklet_schedule(&host->finish_tasklet);
1420 return;
1421 }
Adrian Hunter50accb92011-10-03 15:33:34 +03001422
Pierre Ossmand129bce2006-03-24 03:18:17 -08001423 spin_lock_irqsave(&host->lock, flags);
1424
1425 WARN_ON(host->mrq != NULL);
1426
Pierre Ossmanf9134312008-12-21 17:01:48 +01001427#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001428 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001429#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001430
1431 /*
1432 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1433 * requests if Auto-CMD12 is enabled.
1434 */
1435 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001436 if (mrq->stop) {
1437 mrq->data->stop = NULL;
1438 mrq->stop = NULL;
1439 }
1440 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001441
1442 host->mrq = mrq;
1443
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001444 /* If polling, assume that the card is always present. */
1445 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1446 present = true;
1447 else
1448 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1449 SDHCI_CARD_PRESENT;
1450
1451 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001452 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001453 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301454 } else {
1455 u32 present_state;
1456
1457 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1458 /*
1459 * Check if the re-tuning timer has already expired and there
1460 * is no on-going data transfer. If so, we need to execute
1461 * tuning procedure before sending command.
1462 */
1463 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1464 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1465 spin_unlock_irqrestore(&host->lock, flags);
Girish K S2cd06dc2012-01-06 09:56:39 +05301466 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301467 spin_lock_irqsave(&host->lock, flags);
1468
1469 /* Restore original mmc_request structure */
1470 host->mrq = mrq;
1471 }
1472
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001473 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001474 sdhci_send_command(host, mrq->sbc);
1475 else
1476 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301477 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001478
Pierre Ossman5f25a662006-10-04 02:15:39 -07001479 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001480 spin_unlock_irqrestore(&host->lock, flags);
1481}
1482
Adrian Hunter50accb92011-10-03 15:33:34 +03001483static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001484{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001485 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301486 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001487 u8 ctrl;
1488
Sahitya Tummalaca422112013-02-22 12:15:54 +05301489 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301490 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1491 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1492 return;
1493 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001494
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301495 if (ios->clock)
1496 sdhci_set_clock(host, ios->clock);
1497
1498 spin_lock_irqsave(&host->lock, flags);
1499 if (!host->clock) {
1500 spin_unlock_irqrestore(&host->lock, flags);
1501 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001502 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301503 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001504
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301505 if (ios->power_mode & (MMC_POWER_UP | MMC_POWER_ON))
Sahitya Tummalaca422112013-02-22 12:15:54 +05301506 vdd_bit = sdhci_set_power(host, ios->vdd);
1507
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301508 if (host->vmmc && vdd_bit != -1)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301509 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301510
1511 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001512
Philip Rakity643a81f2010-09-23 08:24:32 -07001513 if (host->ops->platform_send_init_74_clocks)
1514 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1515
Philip Rakity15ec4462010-11-19 16:48:39 -05001516 /*
1517 * If your platform has 8-bit width support but is not a v3 controller,
1518 * or if it requires special setup code, you should implement that in
1519 * platform_8bit_width().
1520 */
1521 if (host->ops->platform_8bit_width)
1522 host->ops->platform_8bit_width(host, ios->bus_width);
1523 else {
1524 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1525 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1526 ctrl &= ~SDHCI_CTRL_4BITBUS;
1527 if (host->version >= SDHCI_SPEC_300)
1528 ctrl |= SDHCI_CTRL_8BITBUS;
1529 } else {
1530 if (host->version >= SDHCI_SPEC_300)
1531 ctrl &= ~SDHCI_CTRL_8BITBUS;
1532 if (ios->bus_width == MMC_BUS_WIDTH_4)
1533 ctrl |= SDHCI_CTRL_4BITBUS;
1534 else
1535 ctrl &= ~SDHCI_CTRL_4BITBUS;
1536 }
1537 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1538 }
1539
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001540 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001541
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001542 if ((ios->timing == MMC_TIMING_SD_HS ||
1543 ios->timing == MMC_TIMING_MMC_HS)
1544 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001545 ctrl |= SDHCI_CTRL_HISPD;
1546 else
1547 ctrl &= ~SDHCI_CTRL_HISPD;
1548
Arindam Nathd6d50a12011-05-05 12:18:59 +05301549 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301550 u16 clk, ctrl_2;
1551 unsigned int clock;
1552
1553 /* In case of UHS-I modes, set High Speed Enable */
Girish K S2cd06dc2012-01-06 09:56:39 +05301554 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1555 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301556 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1557 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001558 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301559 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301560
1561 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1562 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301563 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301564 /*
1565 * We only need to set Driver Strength if the
1566 * preset value enable is not set.
1567 */
1568 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1569 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1570 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1571 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1572 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1573
1574 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301575 } else {
1576 /*
1577 * According to SDHC Spec v3.00, if the Preset Value
1578 * Enable in the Host Control 2 register is set, we
1579 * need to reset SD Clock Enable before changing High
1580 * Speed Enable to avoid generating clock gliches.
1581 */
Arindam Nath758535c2011-05-05 12:19:00 +05301582
1583 /* Reset SD Clock Enable */
1584 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1585 clk &= ~SDHCI_CLOCK_CARD_EN;
1586 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1587
1588 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1589
1590 /* Re-enable SD Clock */
1591 clock = host->clock;
1592 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301593 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301594 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301595 spin_lock_irqsave(&host->lock, flags);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301596 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301597
Arindam Nath49c468f2011-05-05 12:19:01 +05301598 /* Reset SD Clock Enable */
1599 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1600 clk &= ~SDHCI_CLOCK_CARD_EN;
1601 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1602
Philip Rakity6322cdd2011-05-13 11:17:15 +05301603 if (host->ops->set_uhs_signaling)
1604 host->ops->set_uhs_signaling(host, ios->timing);
1605 else {
1606 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1607 /* Select Bus Speed Mode for host */
1608 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S2cd06dc2012-01-06 09:56:39 +05301609 if (ios->timing == MMC_TIMING_MMC_HS200)
1610 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1611 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301612 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1613 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1614 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1615 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1616 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1617 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1618 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1619 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1620 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1621 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1622 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301623
1624 /* Re-enable SD Clock */
1625 clock = host->clock;
1626 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301627 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath49c468f2011-05-05 12:19:01 +05301628 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301629 spin_lock_irqsave(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301630 } else
1631 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301632
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301633 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02001634 /*
1635 * Some (ENE) controllers go apeshit on some ios operation,
1636 * signalling timeout and CRC errors even on CMD0. Resetting
1637 * it on each ios seems to solve the problem.
1638 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001639 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001640 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1641
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301642 /*
1643 * Reset the chip on each power off.
1644 * Should clear out any weird states.
1645 */
1646 if (ios->power_mode == MMC_POWER_OFF) {
1647 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1648 sdhci_reinit(host);
1649 vdd_bit = sdhci_set_power(host, -1);
1650 if (host->vmmc && vdd_bit != -1)
1651 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1652 }
1653 if (!ios->clock)
1654 sdhci_set_clock(host, ios->clock);
1655
Pierre Ossman5f25a662006-10-04 02:15:39 -07001656 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001657}
1658
Adrian Hunter50accb92011-10-03 15:33:34 +03001659static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1660{
1661 struct sdhci_host *host = mmc_priv(mmc);
1662
1663 sdhci_runtime_pm_get(host);
1664 sdhci_do_set_ios(host, ios);
1665 sdhci_runtime_pm_put(host);
1666}
1667
1668static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001669{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001670 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001671 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001672
Pierre Ossmand129bce2006-03-24 03:18:17 -08001673 spin_lock_irqsave(&host->lock, flags);
1674
Pierre Ossman1e728592008-04-16 19:13:13 +02001675 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001676 is_readonly = 0;
1677 else if (host->ops->get_ro)
1678 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001679 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001680 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1681 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001682
1683 spin_unlock_irqrestore(&host->lock, flags);
1684
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001685 /* This quirk needs to be replaced by a callback-function later */
1686 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1687 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001688}
1689
Takashi Iwai82b0e232011-04-21 20:26:38 +02001690#define SAMPLE_COUNT 5
1691
Adrian Hunter50accb92011-10-03 15:33:34 +03001692static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001693{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001694 int i, ro_count;
1695
Takashi Iwai82b0e232011-04-21 20:26:38 +02001696 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001697 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001698
1699 ro_count = 0;
1700 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001701 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001702 if (++ro_count > SAMPLE_COUNT / 2)
1703 return 1;
1704 }
1705 msleep(30);
1706 }
1707 return 0;
1708}
1709
Adrian Hunter50accb92011-10-03 15:33:34 +03001710static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001711{
Adrian Hunter50accb92011-10-03 15:33:34 +03001712 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001713
Adrian Hunter50accb92011-10-03 15:33:34 +03001714 if (host->ops && host->ops->hw_reset)
1715 host->ops->hw_reset(host);
1716}
Adrian Hunter20758b62011-08-29 16:42:12 +03001717
Adrian Hunter50accb92011-10-03 15:33:34 +03001718static int sdhci_get_ro(struct mmc_host *mmc)
1719{
1720 struct sdhci_host *host = mmc_priv(mmc);
1721 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001722
Adrian Hunter50accb92011-10-03 15:33:34 +03001723 sdhci_runtime_pm_get(host);
1724 ret = sdhci_do_get_ro(host);
1725 sdhci_runtime_pm_put(host);
1726 return ret;
1727}
1728
1729static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1730{
Pierre Ossman1e728592008-04-16 19:13:13 +02001731 if (host->flags & SDHCI_DEVICE_DEAD)
1732 goto out;
1733
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001734 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001735 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1736 else
1737 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1738
1739 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1740 if (host->runtime_suspended)
1741 goto out;
1742
1743 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001744 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1745 else
1746 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001747out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001748 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001749}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001750
Adrian Hunter50accb92011-10-03 15:33:34 +03001751static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1752{
1753 struct sdhci_host *host = mmc_priv(mmc);
1754 unsigned long flags;
1755
1756 spin_lock_irqsave(&host->lock, flags);
1757 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001758 spin_unlock_irqrestore(&host->lock, flags);
1759}
1760
Adrian Hunter50accb92011-10-03 15:33:34 +03001761static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1762 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301763{
Arindam Nathf2119df2011-05-05 12:18:57 +05301764 u8 pwr;
1765 u16 clk, ctrl;
1766 u32 present_state;
1767
Arindam Nathf2119df2011-05-05 12:18:57 +05301768 /*
1769 * Signal Voltage Switching is only applicable for Host Controllers
1770 * v3.00 and above.
1771 */
1772 if (host->version < SDHCI_SPEC_300)
1773 return 0;
1774
1775 /*
1776 * We first check whether the request is to set signalling voltage
1777 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1778 */
1779 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1780 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1781 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1782 ctrl &= ~SDHCI_CTRL_VDD_180;
1783 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301784 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301785 host->ops->check_power_status(host, REQ_IO_HIGH);
Arindam Nathf2119df2011-05-05 12:18:57 +05301786
1787 /* Wait for 5ms */
1788 usleep_range(5000, 5500);
1789
1790 /* 3.3V regulator output should be stable within 5 ms */
1791 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1792 if (!(ctrl & SDHCI_CTRL_VDD_180))
1793 return 0;
1794 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301795 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301796 "signalling voltage failed\n");
1797 return -EIO;
1798 }
1799 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1800 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1801 /* Stop SDCLK */
1802 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1803 clk &= ~SDHCI_CLOCK_CARD_EN;
1804 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1805
1806 /* Check whether DAT[3:0] is 0000 */
1807 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1808 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1809 SDHCI_DATA_LVL_SHIFT)) {
1810 /*
1811 * Enable 1.8V Signal Enable in the Host Control2
1812 * register
1813 */
1814 ctrl |= SDHCI_CTRL_VDD_180;
1815 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301816 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301817 host->ops->check_power_status(host, REQ_IO_LOW);
Arindam Nathf2119df2011-05-05 12:18:57 +05301818
1819 /* Wait for 5ms */
1820 usleep_range(5000, 5500);
1821
1822 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1823 if (ctrl & SDHCI_CTRL_VDD_180) {
1824 /* Provide SDCLK again and wait for 1ms*/
1825 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1826 clk |= SDHCI_CLOCK_CARD_EN;
1827 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1828 usleep_range(1000, 1500);
1829
1830 /*
1831 * If DAT[3:0] level is 1111b, then the card
1832 * was successfully switched to 1.8V signaling.
1833 */
1834 present_state = sdhci_readl(host,
1835 SDHCI_PRESENT_STATE);
1836 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1837 SDHCI_DATA_LVL_MASK)
1838 return 0;
1839 }
1840 }
1841
1842 /*
1843 * If we are here, that means the switch to 1.8V signaling
1844 * failed. We power cycle the card, and retry initialization
1845 * sequence by setting S18R to 0.
1846 */
1847 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1848 pwr &= ~SDHCI_POWER_ON;
1849 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301850 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301851 host->ops->check_power_status(host, REQ_BUS_OFF);
Arindam Nathf2119df2011-05-05 12:18:57 +05301852
1853 /* Wait for 1ms as per the spec */
1854 usleep_range(1000, 1500);
1855 pwr |= SDHCI_POWER_ON;
1856 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301857 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301858 host->ops->check_power_status(host, REQ_BUS_ON);
Arindam Nathf2119df2011-05-05 12:18:57 +05301859
Sahitya Tummalaca422112013-02-22 12:15:54 +05301860 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301861 "voltage failed, retrying with S18R set to 0\n");
1862 return -EAGAIN;
1863 } else
1864 /* No signal voltage switch required */
1865 return 0;
1866}
1867
Adrian Hunter50accb92011-10-03 15:33:34 +03001868static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1869 struct mmc_ios *ios)
1870{
1871 struct sdhci_host *host = mmc_priv(mmc);
1872 int err;
1873
1874 if (host->version < SDHCI_SPEC_300)
1875 return 0;
1876 sdhci_runtime_pm_get(host);
1877 err = sdhci_do_start_signal_voltage_switch(host, ios);
1878 sdhci_runtime_pm_put(host);
1879 return err;
1880}
1881
Girish K S2cd06dc2012-01-06 09:56:39 +05301882static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301883{
1884 struct sdhci_host *host;
1885 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05301886 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05301887 int tuning_loop_counter = MAX_TUNING_LOOP;
1888 unsigned long timeout;
1889 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05301890 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301891
1892 host = mmc_priv(mmc);
1893
Adrian Hunter50accb92011-10-03 15:33:34 +03001894 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301895 disable_irq(host->irq);
1896 spin_lock(&host->lock);
1897
1898 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1899
1900 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05301901 * The Host Controller needs tuning only in case of SDR104 mode
1902 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301903 * Capabilities register.
Girish K S2cd06dc2012-01-06 09:56:39 +05301904 * If the Host Controller supports the HS200 mode then the
1905 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301906 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08001907 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1908 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
1909 (host->flags & SDHCI_HS200_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05301910 requires_tuning_nonuhs = true;
1911
Arindam Nathb513ea22011-05-05 12:19:04 +05301912 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301913 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301914 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1915 else {
1916 spin_unlock(&host->lock);
1917 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03001918 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301919 return 0;
1920 }
1921
Asutosh Das8ddd3482013-01-04 11:45:46 +05301922 if (host->ops->execute_tuning) {
1923 spin_unlock(&host->lock);
1924 enable_irq(host->irq);
1925 host->ops->execute_tuning(host, opcode);
1926 disable_irq(host->irq);
1927 spin_lock(&host->lock);
1928 goto out;
1929 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301930 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1931
1932 /*
1933 * As per the Host Controller spec v3.00, tuning command
1934 * generates Buffer Read Ready interrupt, so enable that.
1935 *
1936 * Note: The spec clearly says that when tuning sequence
1937 * is being performed, the controller does not generate
1938 * interrupts other than Buffer Read Ready interrupt. But
1939 * to make sure we don't hit a controller bug, we _only_
1940 * enable Buffer Read Ready interrupt here.
1941 */
1942 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1943 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1944
1945 /*
1946 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1947 * of loops reaches 40 times or a timeout of 150ms occurs.
1948 */
1949 timeout = 150;
1950 do {
1951 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03001952 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301953
1954 if (!tuning_loop_counter && !timeout)
1955 break;
1956
Girish K S2cd06dc2012-01-06 09:56:39 +05301957 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301958 cmd.arg = 0;
1959 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1960 cmd.retries = 0;
1961 cmd.data = NULL;
1962 cmd.error = 0;
1963
1964 mrq.cmd = &cmd;
1965 host->mrq = &mrq;
1966
1967 /*
1968 * In response to CMD19, the card sends 64 bytes of tuning
1969 * block to the Host Controller. So we set the block size
1970 * to 64 here.
1971 */
Girish K S2cd06dc2012-01-06 09:56:39 +05301972 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1973 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1974 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1975 SDHCI_BLOCK_SIZE);
1976 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1977 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1978 SDHCI_BLOCK_SIZE);
1979 } else {
1980 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1981 SDHCI_BLOCK_SIZE);
1982 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301983
1984 /*
1985 * The tuning block is sent by the card to the host controller.
1986 * So we set the TRNS_READ bit in the Transfer Mode register.
1987 * This also takes care of setting DMA Enable and Multi Block
1988 * Select in the same register to 0.
1989 */
1990 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1991
1992 sdhci_send_command(host, &cmd);
1993
1994 host->cmd = NULL;
1995 host->mrq = NULL;
1996
1997 spin_unlock(&host->lock);
1998 enable_irq(host->irq);
1999
2000 /* Wait for Buffer Read Ready interrupt */
2001 wait_event_interruptible_timeout(host->buf_ready_int,
2002 (host->tuning_done == 1),
2003 msecs_to_jiffies(50));
2004 disable_irq(host->irq);
2005 spin_lock(&host->lock);
2006
2007 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302008 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05302009 "Buffer Read Ready interrupt during tuning "
2010 "procedure, falling back to fixed sampling "
2011 "clock\n");
2012 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2013 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2014 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2015 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2016
2017 err = -EIO;
2018 goto out;
2019 }
2020
2021 host->tuning_done = 0;
2022
2023 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2024 tuning_loop_counter--;
2025 timeout--;
2026 mdelay(1);
2027 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2028
2029 /*
2030 * The Host Driver has exhausted the maximum number of loops allowed,
2031 * so use fixed sampling frequency.
2032 */
2033 if (!tuning_loop_counter || !timeout) {
2034 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2035 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2036 } else {
2037 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302038 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05302039 " failed, falling back to fixed sampling"
2040 " clock\n");
2041 err = -EIO;
2042 }
2043 }
2044
2045out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302046 /*
2047 * If this is the very first time we are here, we start the retuning
2048 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2049 * flag won't be set, we check this condition before actually starting
2050 * the timer.
2051 */
2052 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2053 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2054 mod_timer(&host->tuning_timer, jiffies +
2055 host->tuning_count * HZ);
2056 /* Tuning mode 1 limits the maximum data length to 4MB */
2057 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2058 } else {
2059 host->flags &= ~SDHCI_NEEDS_RETUNING;
2060 /* Reload the new initial value for timer */
2061 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2062 mod_timer(&host->tuning_timer, jiffies +
2063 host->tuning_count * HZ);
2064 }
2065
2066 /*
2067 * In case tuning fails, host controllers which support re-tuning can
2068 * try tuning again at a later time, when the re-tuning timer expires.
2069 * So for these controllers, we return 0. Since there might be other
2070 * controllers who do not have this capability, we return error for
2071 * them.
2072 */
2073 if (err && host->tuning_count &&
2074 host->tuning_mode == SDHCI_TUNING_MODE_1)
2075 err = 0;
2076
Arindam Nathb513ea22011-05-05 12:19:04 +05302077 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
2078 spin_unlock(&host->lock);
2079 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002080 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302081
2082 return err;
2083}
2084
Adrian Hunter50accb92011-10-03 15:33:34 +03002085static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302086{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302087 u16 ctrl;
2088 unsigned long flags;
2089
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302090 /* Host Controller v3.00 defines preset value registers */
2091 if (host->version < SDHCI_SPEC_300)
2092 return;
2093
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302094 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_PRESET_VALUE)
2095 return;
2096
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302097 spin_lock_irqsave(&host->lock, flags);
2098
2099 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2100
2101 /*
2102 * We only enable or disable Preset Value if they are not already
2103 * enabled or disabled respectively. Otherwise, we bail out.
2104 */
2105 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2106 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2107 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002108 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302109 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2110 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2111 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002112 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302113 }
2114
2115 spin_unlock_irqrestore(&host->lock, flags);
2116}
2117
Adrian Hunter50accb92011-10-03 15:33:34 +03002118static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
2119{
2120 struct sdhci_host *host = mmc_priv(mmc);
2121
2122 sdhci_runtime_pm_get(host);
2123 sdhci_do_enable_preset_value(host, enable);
2124 sdhci_runtime_pm_put(host);
2125}
2126
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002127static int sdhci_stop_request(struct mmc_host *mmc)
2128{
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002129 struct sdhci_host *host = mmc_priv(mmc);
2130 unsigned long flags;
2131 struct mmc_data *data;
2132
2133 spin_lock_irqsave(&host->lock, flags);
2134 if (!host->mrq || !host->data)
2135 goto out;
2136
2137 data = host->data;
2138
2139 if (host->ops->disable_data_xfer)
2140 host->ops->disable_data_xfer(host);
2141
2142 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2143
2144 if (host->flags & SDHCI_REQ_USE_DMA) {
2145 if (host->flags & SDHCI_USE_ADMA) {
2146 sdhci_adma_table_post(host, data);
2147 } else {
2148 if (!data->host_cookie)
2149 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
2150 data->sg_len,
2151 (data->flags & MMC_DATA_READ) ?
2152 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2153 }
2154 }
2155 del_timer(&host->timer);
2156 host->mrq = NULL;
2157 host->cmd = NULL;
2158 host->data = NULL;
2159out:
2160 spin_unlock_irqrestore(&host->lock, flags);
2161 return 0;
2162}
2163
2164static unsigned int sdhci_get_xfer_remain(struct mmc_host *mmc)
2165{
2166 struct sdhci_host *host = mmc_priv(mmc);
2167 u32 present_state = 0;
2168
2169 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
2170
2171 return present_state & SDHCI_DOING_WRITE;
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002172}
2173
David Brownellab7aefd2006-11-12 17:55:30 -08002174static const struct mmc_host_ops sdhci_ops = {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002175 .pre_req = sdhci_pre_req,
2176 .post_req = sdhci_post_req,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002177 .request = sdhci_request,
2178 .set_ios = sdhci_set_ios,
2179 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03002180 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002181 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302182 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302183 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302184 .enable_preset_value = sdhci_enable_preset_value,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302185 .enable = sdhci_enable,
2186 .disable = sdhci_disable,
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002187 .stop_request = sdhci_stop_request,
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002188 .get_xfer_remain = sdhci_get_xfer_remain,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002189};
2190
2191/*****************************************************************************\
2192 * *
2193 * Tasklets *
2194 * *
2195\*****************************************************************************/
2196
2197static void sdhci_tasklet_card(unsigned long param)
2198{
2199 struct sdhci_host *host;
2200 unsigned long flags;
2201
2202 host = (struct sdhci_host*)param;
2203
2204 spin_lock_irqsave(&host->lock, flags);
2205
Adrian Hunter50accb92011-10-03 15:33:34 +03002206 /* Check host->mrq first in case we are runtime suspended */
2207 if (host->mrq &&
2208 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302209 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002210 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05302211 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002212 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002213
Adrian Hunter50accb92011-10-03 15:33:34 +03002214 sdhci_reset(host, SDHCI_RESET_CMD);
2215 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002216
Adrian Hunter50accb92011-10-03 15:33:34 +03002217 host->mrq->cmd->error = -ENOMEDIUM;
2218 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002219 }
2220
2221 spin_unlock_irqrestore(&host->lock, flags);
2222
Pierre Ossman04cf5852008-08-18 22:18:14 +02002223 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002224}
2225
2226static void sdhci_tasklet_finish(unsigned long param)
2227{
2228 struct sdhci_host *host;
2229 unsigned long flags;
2230 struct mmc_request *mrq;
2231
2232 host = (struct sdhci_host*)param;
2233
Adrian Hunter50accb92011-10-03 15:33:34 +03002234 spin_lock_irqsave(&host->lock, flags);
2235
Chris Ball0c9c99a2011-04-27 17:35:31 -04002236 /*
2237 * If this tasklet gets rescheduled while running, it will
2238 * be run again afterwards but without any active request.
2239 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002240 if (!host->mrq) {
2241 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002242 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002243 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002244
2245 del_timer(&host->timer);
2246
2247 mrq = host->mrq;
2248
Pierre Ossmand129bce2006-03-24 03:18:17 -08002249 /*
2250 * The controller needs a reset of internal state machines
2251 * upon error conditions.
2252 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002253 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002254 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002255 (mrq->data && (mrq->data->error ||
2256 (mrq->data->stop && mrq->data->stop->error))) ||
2257 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002258
2259 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002260 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002261 unsigned int clock;
2262
2263 /* This is to force an update */
2264 clock = host->clock;
2265 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302266 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002267 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302268 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002269 }
2270
2271 /* Spec says we should do both at the same time, but Ricoh
2272 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002273 sdhci_reset(host, SDHCI_RESET_CMD);
2274 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002275 } else {
2276 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2277 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002278 }
2279
2280 host->mrq = NULL;
2281 host->cmd = NULL;
2282 host->data = NULL;
2283
Pierre Ossmanf9134312008-12-21 17:01:48 +01002284#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002285 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002286#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002287
Pierre Ossman5f25a662006-10-04 02:15:39 -07002288 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002289 spin_unlock_irqrestore(&host->lock, flags);
2290
2291 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002292 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002293}
2294
2295static void sdhci_timeout_timer(unsigned long data)
2296{
2297 struct sdhci_host *host;
2298 unsigned long flags;
2299
2300 host = (struct sdhci_host*)data;
2301
2302 spin_lock_irqsave(&host->lock, flags);
2303
2304 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302305 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002306 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002307 sdhci_dumpregs(host);
2308
2309 if (host->data) {
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302310 pr_info("%s: bytes to transfer: %d transferred: %d\n",
2311 mmc_hostname(host->mmc),
2312 (host->data->blksz * host->data->blocks),
2313 (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) *
2314 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossman17b04292007-07-22 22:18:46 +02002315 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002316 sdhci_finish_data(host);
2317 } else {
2318 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002319 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002320 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002321 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002322
2323 tasklet_schedule(&host->finish_tasklet);
2324 }
2325 }
2326
Pierre Ossman5f25a662006-10-04 02:15:39 -07002327 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002328 spin_unlock_irqrestore(&host->lock, flags);
2329}
2330
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302331static void sdhci_tuning_timer(unsigned long data)
2332{
2333 struct sdhci_host *host;
2334 unsigned long flags;
2335
2336 host = (struct sdhci_host *)data;
2337
2338 spin_lock_irqsave(&host->lock, flags);
2339
2340 host->flags |= SDHCI_NEEDS_RETUNING;
2341
2342 spin_unlock_irqrestore(&host->lock, flags);
2343}
2344
Pierre Ossmand129bce2006-03-24 03:18:17 -08002345/*****************************************************************************\
2346 * *
2347 * Interrupt handling *
2348 * *
2349\*****************************************************************************/
2350
2351static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2352{
2353 BUG_ON(intmask == 0);
2354
2355 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302356 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002357 "though no command operation was in progress.\n",
2358 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002359 sdhci_dumpregs(host);
2360 return;
2361 }
2362
Pierre Ossman43b58b32007-07-25 23:15:27 +02002363 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002364 host->cmd->error = -ETIMEDOUT;
2365 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2366 SDHCI_INT_INDEX))
2367 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002368
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302369 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2370 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2371 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2372 if (intmask & SDHCI_INT_CRC) {
2373 sdhci_reset(host, SDHCI_RESET_CMD);
2374 host->cmd->error = 0;
2375 }
2376 }
2377 }
2378
Pierre Ossmane8095172008-07-25 01:09:08 +02002379 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002380 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002381 return;
2382 }
2383
2384 /*
2385 * The host can send and interrupt when the busy state has
2386 * ended, allowing us to wait without wasting CPU cycles.
2387 * Unfortunately this is overloaded on the "data complete"
2388 * interrupt, so we need to take some care when handling
2389 * it.
2390 *
2391 * Note: The 1.0 specification is a bit ambiguous about this
2392 * feature so there might be some problems with older
2393 * controllers.
2394 */
2395 if (host->cmd->flags & MMC_RSP_BUSY) {
2396 if (host->cmd->data)
2397 DBG("Cannot wait for busy signal when also "
2398 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002399 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002400 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002401
2402 /* The controller does not support the end-of-busy IRQ,
2403 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002404 }
2405
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302406 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2407 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2408 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2409 if (intmask & SDHCI_INT_CRC) {
2410 sdhci_finish_command(host);
2411 return;
2412 }
2413 }
2414 }
2415
Pierre Ossmane8095172008-07-25 01:09:08 +02002416 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002417 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002418}
2419
Ben Dooks6882a8c2009-06-14 13:52:38 +01002420static void sdhci_show_adma_error(struct sdhci_host *host)
2421{
2422 const char *name = mmc_hostname(host->mmc);
2423 u8 *desc = host->adma_desc;
2424 __le32 *dma;
2425 __le16 *len;
2426 u8 attr;
2427
2428 sdhci_dumpregs(host);
2429
2430 while (true) {
2431 dma = (__le32 *)(desc + 4);
2432 len = (__le16 *)(desc + 2);
2433 attr = *desc;
2434
Sahitya Tummala419b6c82013-04-12 12:28:29 +05302435 pr_info("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
Ben Dooks6882a8c2009-06-14 13:52:38 +01002436 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2437
2438 desc += 8;
2439
2440 if (attr & 2)
2441 break;
2442 }
2443}
Ben Dooks6882a8c2009-06-14 13:52:38 +01002444
Pierre Ossmand129bce2006-03-24 03:18:17 -08002445static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2446{
Girish K S2cd06dc2012-01-06 09:56:39 +05302447 u32 command;
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302448 bool pr_msg = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002449 BUG_ON(intmask == 0);
2450
Arindam Nathb513ea22011-05-05 12:19:04 +05302451 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2452 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302453 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2454 if (command == MMC_SEND_TUNING_BLOCK ||
2455 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302456 host->tuning_done = 1;
2457 wake_up(&host->buf_ready_int);
2458 return;
2459 }
2460 }
2461
Pierre Ossmand129bce2006-03-24 03:18:17 -08002462 if (!host->data) {
2463 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002464 * The "data complete" interrupt is also used to
2465 * indicate that a busy state has ended. See comment
2466 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002467 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002468 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2469 if (intmask & SDHCI_INT_DATA_END) {
2470 sdhci_finish_command(host);
2471 return;
2472 }
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302473 if (host->quirks2 &
2474 SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD)
2475 return;
Pierre Ossmane8095172008-07-25 01:09:08 +02002476 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002477
Sahitya Tummalaca422112013-02-22 12:15:54 +05302478 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002479 "though no data operation was in progress.\n",
2480 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002481 sdhci_dumpregs(host);
2482
2483 return;
2484 }
2485
2486 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002487 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002488 else if (intmask & SDHCI_INT_DATA_END_BIT)
2489 host->data->error = -EILSEQ;
2490 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2491 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2492 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002493 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002494 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302495 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002496 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002497 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002498 }
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302499 if (host->data->error) {
2500 if ((intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT)) &&
2501 (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING)) {
2502 command = SDHCI_GET_CMD(sdhci_readw(host,
2503 SDHCI_COMMAND));
2504 if ((command != MMC_SEND_TUNING_BLOCK_HS200) &&
2505 (command != MMC_SEND_TUNING_BLOCK))
2506 pr_msg = true;
2507 } else {
2508 pr_msg = true;
2509 }
2510 if (pr_msg) {
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302511 pr_err("%s: data txfr (0x%08x) error: %d after %lld ms\n",
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302512 mmc_hostname(host->mmc), intmask,
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302513 host->data->error, ktime_to_ms(ktime_sub(
2514 ktime_get(), host->data_start_time)));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302515 sdhci_dumpregs(host);
2516 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002517 sdhci_finish_data(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302518 } else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002519 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002520 sdhci_transfer_pio(host);
2521
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002522 /*
2523 * We currently don't do anything fancy with DMA
2524 * boundaries, but as we can't disable the feature
2525 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002526 *
2527 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2528 * should return a valid address to continue from, but as
2529 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002530 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002531 if (intmask & SDHCI_INT_DMA_END) {
2532 u32 dmastart, dmanow;
2533 dmastart = sg_dma_address(host->data->sg);
2534 dmanow = dmastart + host->data->bytes_xfered;
2535 /*
2536 * Force update to the next DMA block boundary.
2537 */
2538 dmanow = (dmanow &
2539 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2540 SDHCI_DEFAULT_BOUNDARY_SIZE;
2541 host->data->bytes_xfered = dmanow - dmastart;
2542 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2543 " next 0x%08x\n",
2544 mmc_hostname(host->mmc), dmastart,
2545 host->data->bytes_xfered, dmanow);
2546 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2547 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002548
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002549 if (intmask & SDHCI_INT_DATA_END) {
2550 if (host->cmd) {
2551 /*
2552 * Data managed to finish before the
2553 * command completed. Make sure we do
2554 * things in the proper order.
2555 */
2556 host->data_early = 1;
2557 } else {
2558 sdhci_finish_data(host);
2559 }
2560 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002561 }
2562}
2563
David Howells7d12e782006-10-05 14:55:46 +01002564static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002565{
2566 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002567 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002568 u32 intmask, unexpected = 0;
2569 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002570
2571 spin_lock(&host->lock);
2572
Adrian Hunter50accb92011-10-03 15:33:34 +03002573 if (host->runtime_suspended) {
2574 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302575 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002576 mmc_hostname(host->mmc));
2577 return IRQ_HANDLED;
2578 }
2579
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002580 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002581
Mark Lord62df67a2007-03-06 13:30:13 +01002582 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002583 result = IRQ_NONE;
2584 goto out;
2585 }
2586
Alexander Stein6379b232012-03-14 09:52:10 +01002587again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002588 DBG("*** %s got interrupt: 0x%08x\n",
2589 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002590
Pierre Ossman3192a282006-06-30 02:22:26 -07002591 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302592 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2593 SDHCI_CARD_PRESENT;
2594
2595 /*
2596 * There is a observation on i.mx esdhc. INSERT bit will be
2597 * immediately set again when it gets cleared, if a card is
2598 * inserted. We have to mask the irq to prevent interrupt
2599 * storm which will freeze the system. And the REMOVE gets
2600 * the same situation.
2601 *
2602 * More testing are needed here to ensure it works for other
2603 * platforms though.
2604 */
2605 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2606 SDHCI_INT_CARD_REMOVE);
2607 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2608 SDHCI_INT_CARD_INSERT);
2609
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002610 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302611 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2612 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002613 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002614 }
2615
Pierre Ossmand129bce2006-03-24 03:18:17 -08002616 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002617 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2618 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002619 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2620 (host->clock <= 400000))
2621 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002622 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002623 }
2624
2625 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002626 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2627 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002628 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2629 (host->clock <= 400000))
2630 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002631 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002632 }
2633
2634 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2635
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002636 intmask &= ~SDHCI_INT_ERROR;
2637
Pierre Ossmand129bce2006-03-24 03:18:17 -08002638 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302639 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002640 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002641 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002642 }
2643
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002644 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002645
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002646 if (intmask & SDHCI_INT_CARD_INT)
2647 cardint = 1;
2648
2649 intmask &= ~SDHCI_INT_CARD_INT;
2650
Pierre Ossman3192a282006-06-30 02:22:26 -07002651 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002652 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002653 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002654 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002655
2656 result = IRQ_HANDLED;
2657
Alexander Stein6379b232012-03-14 09:52:10 +01002658 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2659 if (intmask && --max_loops)
2660 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002661out:
2662 spin_unlock(&host->lock);
2663
Alexander Stein6379b232012-03-14 09:52:10 +01002664 if (unexpected) {
2665 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2666 mmc_hostname(host->mmc), unexpected);
2667 sdhci_dumpregs(host);
2668 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002669 /*
2670 * We have to delay this as it calls back into the driver.
2671 */
2672 if (cardint)
2673 mmc_signal_sdio_irq(host->mmc);
2674
Pierre Ossmand129bce2006-03-24 03:18:17 -08002675 return result;
2676}
2677
2678/*****************************************************************************\
2679 * *
2680 * Suspend/resume *
2681 * *
2682\*****************************************************************************/
2683
2684#ifdef CONFIG_PM
2685
Manuel Laussd72faa62011-11-03 11:09:45 +01002686int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002687{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002688 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302689 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002690
Chris Balla1b13b42012-02-06 00:43:59 -05002691 if (host->ops->platform_suspend)
2692 host->ops->platform_suspend(host);
2693
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002694 sdhci_disable_card_detection(host);
2695
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302696 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302697 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2698 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2699 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002700 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302701 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302702 }
2703
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002704 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302705 if (ret) {
2706 if (has_tuning_timer) {
2707 host->flags |= SDHCI_NEEDS_RETUNING;
2708 mod_timer(&host->tuning_timer, jiffies +
2709 host->tuning_count * HZ);
2710 }
2711
2712 sdhci_enable_card_detection(host);
2713
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002714 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302715 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002716
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002717 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002718
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002719 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002720}
2721
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002722EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002723
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002724int sdhci_resume_host(struct sdhci_host *host)
2725{
2726 int ret;
2727
Richard Röjforsa13abc72009-09-22 16:45:30 -07002728 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002729 if (host->ops->enable_dma)
2730 host->ops->enable_dma(host);
2731 }
2732
2733 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2734 mmc_hostname(host->mmc), host);
2735 if (ret)
2736 return ret;
2737
Adrian Hunter6308d292012-02-07 14:48:54 +02002738 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2739 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2740 /* Card keeps power but host controller does not */
2741 sdhci_init(host, 0);
2742 host->pwr = 0;
2743 host->clock = 0;
2744 sdhci_do_set_ios(host, &host->mmc->ios);
2745 } else {
2746 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2747 mmiowb();
2748 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002749
2750 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002751 sdhci_enable_card_detection(host);
2752
Chris Balla1b13b42012-02-06 00:43:59 -05002753 if (host->ops->platform_resume)
2754 host->ops->platform_resume(host);
2755
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302756 /* Set the re-tuning expiration flag */
2757 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2758 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2759 host->flags |= SDHCI_NEEDS_RETUNING;
2760
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002761 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002762}
2763
2764EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002765
Daniel Drake5f619702010-11-04 22:20:39 +00002766void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2767{
2768 u8 val;
2769 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2770 val |= SDHCI_WAKE_ON_INT;
2771 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2772}
2773
2774EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2775
Pierre Ossmand129bce2006-03-24 03:18:17 -08002776#endif /* CONFIG_PM */
2777
Adrian Hunter50accb92011-10-03 15:33:34 +03002778#ifdef CONFIG_PM_RUNTIME
2779
2780static int sdhci_runtime_pm_get(struct sdhci_host *host)
2781{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302782 if (!mmc_use_core_runtime_pm(host->mmc))
2783 return pm_runtime_get_sync(host->mmc->parent);
2784 else
2785 return 0;
Adrian Hunter50accb92011-10-03 15:33:34 +03002786}
2787
2788static int sdhci_runtime_pm_put(struct sdhci_host *host)
2789{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302790 if (!mmc_use_core_runtime_pm(host->mmc)) {
2791 pm_runtime_mark_last_busy(host->mmc->parent);
2792 return pm_runtime_put_autosuspend(host->mmc->parent);
2793 } else {
2794 return 0;
2795 }
Adrian Hunter50accb92011-10-03 15:33:34 +03002796}
2797
2798int sdhci_runtime_suspend_host(struct sdhci_host *host)
2799{
2800 unsigned long flags;
2801 int ret = 0;
2802
2803 /* Disable tuning since we are suspending */
2804 if (host->version >= SDHCI_SPEC_300 &&
2805 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2806 del_timer_sync(&host->tuning_timer);
2807 host->flags &= ~SDHCI_NEEDS_RETUNING;
2808 }
2809
2810 spin_lock_irqsave(&host->lock, flags);
2811 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2812 spin_unlock_irqrestore(&host->lock, flags);
2813
2814 synchronize_irq(host->irq);
2815
2816 spin_lock_irqsave(&host->lock, flags);
2817 host->runtime_suspended = true;
2818 spin_unlock_irqrestore(&host->lock, flags);
2819
2820 return ret;
2821}
2822EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2823
2824int sdhci_runtime_resume_host(struct sdhci_host *host)
2825{
2826 unsigned long flags;
2827 int ret = 0, host_flags = host->flags;
2828
2829 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2830 if (host->ops->enable_dma)
2831 host->ops->enable_dma(host);
2832 }
2833
2834 sdhci_init(host, 0);
2835
2836 /* Force clock and power re-program */
2837 host->pwr = 0;
2838 host->clock = 0;
2839 sdhci_do_set_ios(host, &host->mmc->ios);
2840
2841 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2842 if (host_flags & SDHCI_PV_ENABLED)
2843 sdhci_do_enable_preset_value(host, true);
2844
2845 /* Set the re-tuning expiration flag */
2846 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2847 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2848 host->flags |= SDHCI_NEEDS_RETUNING;
2849
2850 spin_lock_irqsave(&host->lock, flags);
2851
2852 host->runtime_suspended = false;
2853
2854 /* Enable SDIO IRQ */
2855 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2856 sdhci_enable_sdio_irq_nolock(host, true);
2857
2858 /* Enable Card Detection */
2859 sdhci_enable_card_detection(host);
2860
2861 spin_unlock_irqrestore(&host->lock, flags);
2862
2863 return ret;
2864}
2865EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2866
2867#endif
2868
Pierre Ossmand129bce2006-03-24 03:18:17 -08002869/*****************************************************************************\
2870 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002871 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002872 * *
2873\*****************************************************************************/
2874
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002875struct sdhci_host *sdhci_alloc_host(struct device *dev,
2876 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002877{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002878 struct mmc_host *mmc;
2879 struct sdhci_host *host;
2880
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002881 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002882
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002883 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002884 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002885 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002886
2887 host = mmc_priv(mmc);
2888 host->mmc = mmc;
2889
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002890 return host;
2891}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002892
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002893EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002894
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002895int sdhci_add_host(struct sdhci_host *host)
2896{
2897 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302898 u32 caps[2];
2899 u32 max_current_caps;
2900 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002901 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002902
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002903 WARN_ON(host == NULL);
2904 if (host == NULL)
2905 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002906
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002907 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002908
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002909 if (debug_quirks)
2910 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03002911 if (debug_quirks2)
2912 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002913
Pierre Ossmand96649e2006-06-30 02:22:30 -07002914 sdhci_reset(host, SDHCI_RESET_ALL);
2915
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002916 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002917 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2918 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002919 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302920 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002921 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002922 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002923 }
2924
Arindam Nathf2119df2011-05-05 12:18:57 +05302925 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002926 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002927
Arindam Nathf2119df2011-05-05 12:18:57 +05302928 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2929 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2930
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002931 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002932 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302933 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002934 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002935 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002936 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002937
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002938 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002939 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002940 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002941 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002942 }
2943
Arindam Nathf2119df2011-05-05 12:18:57 +05302944 if ((host->version >= SDHCI_SPEC_200) &&
2945 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002946 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002947
2948 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2949 (host->flags & SDHCI_USE_ADMA)) {
2950 DBG("Disabling ADMA as it is marked broken\n");
2951 host->flags &= ~SDHCI_USE_ADMA;
2952 }
2953
Richard Röjforsa13abc72009-09-22 16:45:30 -07002954 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002955 if (host->ops->enable_dma) {
2956 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302957 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002958 "available. Falling back to PIO.\n",
2959 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002960 host->flags &=
2961 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002962 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002963 }
2964 }
2965
Pierre Ossman2134a922008-06-28 18:28:51 +02002966 if (host->flags & SDHCI_USE_ADMA) {
2967 /*
2968 * We need to allocate descriptors for all sg entries
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302969 * (128/max_segments) and potentially one alignment transfer for
Pierre Ossman2134a922008-06-28 18:28:51 +02002970 * each of those entries.
2971 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302972 if (host->ops->get_max_segments)
2973 host->adma_max_desc = host->ops->get_max_segments();
2974 else
2975 host->adma_max_desc = 128;
2976
2977 host->adma_desc_sz = (host->adma_max_desc * 2 + 1) * 4;
2978 host->align_buf_sz = host->adma_max_desc * 4;
2979
2980 pr_debug("%s: %s: dma_desc_size: %d\n",
2981 mmc_hostname(host->mmc), __func__, host->adma_desc_sz);
2982 host->adma_desc = kmalloc(host->adma_desc_sz,
2983 GFP_KERNEL);
2984 host->align_buffer = kmalloc(host->align_buf_sz,
2985 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02002986 if (!host->adma_desc || !host->align_buffer) {
2987 kfree(host->adma_desc);
2988 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302989 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002990 "buffers. Falling back to standard DMA.\n",
2991 mmc_hostname(mmc));
2992 host->flags &= ~SDHCI_USE_ADMA;
2993 }
2994 }
2995
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002996 host->next_data.cookie = 1;
2997
Pierre Ossman76591502008-07-21 00:32:11 +02002998 /*
2999 * If we use DMA, then it's up to the caller to set the DMA
3000 * mask, but PIO does not need the hw shim so we set a new
3001 * mask here in that case.
3002 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003003 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003004 host->dma_mask = DMA_BIT_MASK(64);
3005 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3006 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003007
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003008 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05303009 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003010 >> SDHCI_CLOCK_BASE_SHIFT;
3011 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303012 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003013 >> SDHCI_CLOCK_BASE_SHIFT;
3014
Pierre Ossmand129bce2006-03-24 03:18:17 -08003015 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003016 if (host->max_clk == 0 || host->quirks &
3017 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003018 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303019 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03003020 "frequency.\n", mmc_hostname(mmc));
3021 return -ENODEV;
3022 }
3023 host->max_clk = host->ops->get_max_clock(host);
3024 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003025
3026 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303027 * In case of Host Controller v3.00, find out whether clock
3028 * multiplier is supported.
3029 */
3030 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3031 SDHCI_CLOCK_MUL_SHIFT;
3032
3033 /*
3034 * In case the value in Clock Multiplier is 0, then programmable
3035 * clock mode is not supported, otherwise the actual clock
3036 * multiplier is one more than the value of Clock Multiplier
3037 * in the Capabilities Register.
3038 */
3039 if (host->clk_mul)
3040 host->clk_mul += 1;
3041
3042 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003043 * Set host parameters.
3044 */
3045 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303046 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003047 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003048 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303049 else if (host->version >= SDHCI_SPEC_300) {
3050 if (host->clk_mul) {
3051 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3052 mmc->f_max = host->max_clk * host->clk_mul;
3053 } else
3054 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3055 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003056 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003057
Sahitya Tummalaca422112013-02-22 12:15:54 +05303058 host->timeout_clk =
3059 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
3060 if (host->timeout_clk == 0) {
3061 if (host->ops->get_timeout_clock) {
3062 host->timeout_clk = host->ops->get_timeout_clock(host);
3063 } else if (!(host->quirks &
3064 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3065 pr_err("%s: Hardware doesn't specify timeout clock "
3066 "frequency.\n", mmc_hostname(mmc));
3067 return -ENODEV;
3068 }
3069 }
3070 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3071 host->timeout_clk *= 1000;
3072
3073 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
3074 host->timeout_clk = mmc->f_max / 1000;
3075
Krishna Kondaa20d3362013-04-01 21:01:59 -07003076 if (!(host->quirks2 & SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE))
3077 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Sahitya Tummalaca422112013-02-22 12:15:54 +05303078
Andrei Warkentine89d4562011-05-23 15:06:37 -05003079 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
3080
3081 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3082 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003083
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003084 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003085 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003086 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003087 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003088 host->flags |= SDHCI_AUTO_CMD23;
3089 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3090 } else {
3091 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3092 }
3093
Philip Rakity15ec4462010-11-19 16:48:39 -05003094 /*
3095 * A controller may support 8-bit width, but the board itself
3096 * might not have the pins brought out. Boards that support
3097 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3098 * their platform code before calling sdhci_add_host(), and we
3099 * won't assume 8-bit width for hosts without that CAP.
3100 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003101 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003102 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003103
Arindam Nathf2119df2011-05-05 12:18:57 +05303104 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003105 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003106
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003107 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3108 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003109 mmc->caps |= MMC_CAP_NEEDS_POLL;
3110
Al Cooper4188bba2012-03-16 15:54:17 -04003111 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3112 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3113 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303114 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3115
3116 /* SDR104 supports also implies SDR50 support */
3117 if (caps[1] & SDHCI_SUPPORT_SDR104)
3118 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3119 else if (caps[1] & SDHCI_SUPPORT_SDR50)
3120 mmc->caps |= MMC_CAP_UHS_SDR50;
3121
3122 if (caps[1] & SDHCI_SUPPORT_DDR50)
3123 mmc->caps |= MMC_CAP_UHS_DDR50;
3124
Girish K S2cd06dc2012-01-06 09:56:39 +05303125 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303126 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3127 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3128
Girish K S2cd06dc2012-01-06 09:56:39 +05303129 /* Does the host need tuning for HS200? */
3130 if (mmc->caps2 & MMC_CAP2_HS200)
3131 host->flags |= SDHCI_HS200_NEEDS_TUNING;
3132
Arindam Nathd6d50a12011-05-05 12:18:59 +05303133 /* Driver Type(s) (A, C, D) supported by the host */
3134 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3135 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3136 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3137 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3138 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3139 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3140
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02003141 /* Initial value for re-tuning timer count */
3142 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3143 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3144
3145 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303146 * In case Re-tuning Timer is not disabled, the actual value of
3147 * re-tuning timer will be 2 ^ (n - 1).
3148 */
3149 if (host->tuning_count)
3150 host->tuning_count = 1 << (host->tuning_count - 1);
3151
3152 /* Re-tuning mode supported by the Host Controller */
3153 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3154 SDHCI_RETUNING_MODE_SHIFT;
3155
Takashi Iwai8f230f42010-12-08 10:04:30 +01003156 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05303157 /*
3158 * According to SD Host Controller spec v3.00, if the Host System
3159 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3160 * the value is meaningful only if Voltage Support in the Capabilities
3161 * register is set. The actual current value is 4 times the register
3162 * value.
3163 */
3164 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3165
3166 if (caps[0] & SDHCI_CAN_VDD_330) {
3167 int max_current_330;
3168
Takashi Iwai8f230f42010-12-08 10:04:30 +01003169 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303170
3171 max_current_330 = ((max_current_caps &
3172 SDHCI_MAX_CURRENT_330_MASK) >>
3173 SDHCI_MAX_CURRENT_330_SHIFT) *
3174 SDHCI_MAX_CURRENT_MULTIPLIER;
3175
3176 if (max_current_330 > 150)
3177 mmc->caps |= MMC_CAP_SET_XPC_330;
3178 }
3179 if (caps[0] & SDHCI_CAN_VDD_300) {
3180 int max_current_300;
3181
Takashi Iwai8f230f42010-12-08 10:04:30 +01003182 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303183
3184 max_current_300 = ((max_current_caps &
3185 SDHCI_MAX_CURRENT_300_MASK) >>
3186 SDHCI_MAX_CURRENT_300_SHIFT) *
3187 SDHCI_MAX_CURRENT_MULTIPLIER;
3188
3189 if (max_current_300 > 150)
3190 mmc->caps |= MMC_CAP_SET_XPC_300;
3191 }
3192 if (caps[0] & SDHCI_CAN_VDD_180) {
3193 int max_current_180;
3194
Takashi Iwai8f230f42010-12-08 10:04:30 +01003195 ocr_avail |= MMC_VDD_165_195;
3196
Arindam Nathf2119df2011-05-05 12:18:57 +05303197 max_current_180 = ((max_current_caps &
3198 SDHCI_MAX_CURRENT_180_MASK) >>
3199 SDHCI_MAX_CURRENT_180_SHIFT) *
3200 SDHCI_MAX_CURRENT_MULTIPLIER;
3201
3202 if (max_current_180 > 150)
3203 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05303204
3205 /* Maximum current capabilities of the host at 1.8V */
3206 if (max_current_180 >= 800)
3207 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
3208 else if (max_current_180 >= 600)
3209 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
3210 else if (max_current_180 >= 400)
3211 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
3212 else
3213 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05303214 }
3215
Takashi Iwai8f230f42010-12-08 10:04:30 +01003216 mmc->ocr_avail = ocr_avail;
3217 mmc->ocr_avail_sdio = ocr_avail;
3218 if (host->ocr_avail_sdio)
3219 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3220 mmc->ocr_avail_sd = ocr_avail;
3221 if (host->ocr_avail_sd)
3222 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3223 else /* normal SD controllers don't support 1.8V */
3224 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3225 mmc->ocr_avail_mmc = ocr_avail;
3226 if (host->ocr_avail_mmc)
3227 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003228
3229 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303230 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003231 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003232 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003233 }
3234
Pierre Ossmand129bce2006-03-24 03:18:17 -08003235 spin_lock_init(&host->lock);
3236
3237 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003238 * Maximum number of segments. Depends on if the hardware
3239 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003240 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003241 if (host->flags & SDHCI_USE_ADMA)
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303242 mmc->max_segs = host->adma_max_desc;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003243 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003244 mmc->max_segs = 1;
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303245 else/* PIO */
3246 mmc->max_segs = host->adma_max_desc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003247
3248 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003249 * Maximum number of sectors in one transfer. Limited by DMA boundary
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303250 * size (512KiB), unless specified by platform specific driver. Each
3251 * descriptor can transfer a maximum of 64KB.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003252 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303253 if (host->ops->get_max_segments)
3254 mmc->max_req_size = (host->adma_max_desc * 65536);
3255 else
3256 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003257
3258 /*
3259 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003260 * of bytes. When doing hardware scatter/gather, each entry cannot
3261 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003262 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003263 if (host->flags & SDHCI_USE_ADMA) {
3264 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3265 mmc->max_seg_size = 65535;
3266 else
3267 mmc->max_seg_size = 65536;
3268 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003269 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003270 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003271
3272 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003273 * Maximum block size. This varies from controller to controller and
3274 * is specified in the capabilities register.
3275 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003276 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3277 mmc->max_blk_size = 2;
3278 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303279 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003280 SDHCI_MAX_BLOCK_SHIFT;
3281 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303282 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003283 "assuming 512 bytes\n", mmc_hostname(mmc));
3284 mmc->max_blk_size = 0;
3285 }
3286 }
3287
3288 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003289
3290 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003291 * Maximum block count.
3292 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003293 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003294
3295 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003296 * Init tasklets.
3297 */
3298 tasklet_init(&host->card_tasklet,
3299 sdhci_tasklet_card, (unsigned long)host);
3300 tasklet_init(&host->finish_tasklet,
3301 sdhci_tasklet_finish, (unsigned long)host);
3302
Al Viroe4cad1b2006-10-10 22:47:07 +01003303 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003304
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303305 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303306 init_waitqueue_head(&host->buf_ready_int);
3307
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303308 /* Initialize re-tuning timer */
3309 init_timer(&host->tuning_timer);
3310 host->tuning_timer.data = (unsigned long)host;
3311 host->tuning_timer.function = sdhci_tuning_timer;
3312 }
3313
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003314 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003315 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003316 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003317 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003318
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003319 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3320 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303321 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003322 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003323 }
3324
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003325 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003326
3327#ifdef CONFIG_MMC_DEBUG
3328 sdhci_dumpregs(host);
3329#endif
3330
Pierre Ossmanf9134312008-12-21 17:01:48 +01003331#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003332 snprintf(host->led_name, sizeof(host->led_name),
3333 "%s::", mmc_hostname(mmc));
3334 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003335 host->led.brightness = LED_OFF;
3336 host->led.default_trigger = mmc_hostname(mmc);
3337 host->led.brightness_set = sdhci_led_control;
3338
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003339 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003340 if (ret)
3341 goto reset;
3342#endif
3343
Pierre Ossman5f25a662006-10-04 02:15:39 -07003344 mmiowb();
3345
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303346 if (host->cpu_dma_latency_us)
3347 pm_qos_add_request(&host->pm_qos_req_dma,
3348 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003349 mmc_add_host(mmc);
3350
Sahitya Tummalaca422112013-02-22 12:15:54 +05303351 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003352 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003353 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3354 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003355
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003356 sdhci_enable_card_detection(host);
3357
Pierre Ossmand129bce2006-03-24 03:18:17 -08003358 return 0;
3359
Pierre Ossmanf9134312008-12-21 17:01:48 +01003360#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003361reset:
3362 sdhci_reset(host, SDHCI_RESET_ALL);
3363 free_irq(host->irq, host);
3364#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003365untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003366 tasklet_kill(&host->card_tasklet);
3367 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003368
3369 return ret;
3370}
3371
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003372EXPORT_SYMBOL_GPL(sdhci_add_host);
3373
Pierre Ossman1e728592008-04-16 19:13:13 +02003374void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003375{
Pierre Ossman1e728592008-04-16 19:13:13 +02003376 unsigned long flags;
3377
3378 if (dead) {
3379 spin_lock_irqsave(&host->lock, flags);
3380
3381 host->flags |= SDHCI_DEVICE_DEAD;
3382
3383 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303384 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003385 " transfer!\n", mmc_hostname(host->mmc));
3386
3387 host->mrq->cmd->error = -ENOMEDIUM;
3388 tasklet_schedule(&host->finish_tasklet);
3389 }
3390
3391 spin_unlock_irqrestore(&host->lock, flags);
3392 }
3393
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003394 sdhci_disable_card_detection(host);
3395
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303396 if (host->cpu_dma_latency_us)
3397 pm_qos_remove_request(&host->pm_qos_req_dma);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003398 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003399
Pierre Ossmanf9134312008-12-21 17:01:48 +01003400#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003401 led_classdev_unregister(&host->led);
3402#endif
3403
Pierre Ossman1e728592008-04-16 19:13:13 +02003404 if (!dead)
3405 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003406
3407 free_irq(host->irq, host);
3408
3409 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303410 if (host->version >= SDHCI_SPEC_300)
3411 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003412
3413 tasklet_kill(&host->card_tasklet);
3414 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003415
Sahitya Tummalaca422112013-02-22 12:15:54 +05303416 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003417 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003418
Pierre Ossman2134a922008-06-28 18:28:51 +02003419 kfree(host->adma_desc);
3420 kfree(host->align_buffer);
3421
3422 host->adma_desc = NULL;
3423 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003424}
3425
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003426EXPORT_SYMBOL_GPL(sdhci_remove_host);
3427
3428void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003429{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003430 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003431}
3432
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003433EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003434
3435/*****************************************************************************\
3436 * *
3437 * Driver init/exit *
3438 * *
3439\*****************************************************************************/
3440
3441static int __init sdhci_drv_init(void)
3442{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303443 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003444 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303445 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003446
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003447 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003448}
3449
3450static void __exit sdhci_drv_exit(void)
3451{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003452}
3453
3454module_init(sdhci_drv_init);
3455module_exit(sdhci_drv_exit);
3456
Pierre Ossmandf673b22006-06-30 02:22:31 -07003457module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003458module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003459
Pierre Ossman32710e82009-04-08 20:14:54 +02003460MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003461MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003462MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003463
Pierre Ossmandf673b22006-06-30 02:22:31 -07003464MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003465MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");