blob: 5912790e4d9e9b005bbf0701b8bfb624857983be [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter50accb92011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080030
Pierre Ossmand129bce2006-03-24 03:18:17 -080031#include "sdhci.h"
32
33#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080034
Pierre Ossmand129bce2006-03-24 03:18:17 -080035#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010036 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmanf9134312008-12-21 17:01:48 +010038#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
39 defined(CONFIG_MMC_SDHCI_MODULE))
40#define SDHCI_USE_LEDS_CLASS
41#endif
42
Arindam Nathb513ea22011-05-05 12:19:04 +053043#define MAX_TUNING_LOOP 40
44
Pierre Ossmandf673b22006-06-30 02:22:31 -070045static unsigned int debug_quirks = 0;
Adrian Hunter50accb92011-10-03 15:33:34 +030046static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070047
Pierre Ossmand129bce2006-03-24 03:18:17 -080048static void sdhci_finish_data(struct sdhci_host *);
49
50static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
51static void sdhci_finish_command(struct sdhci_host *);
Girish K S2cd06dc2012-01-06 09:56:39 +053052static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053053static void sdhci_tuning_timer(unsigned long data);
Pierre Ossmand129bce2006-03-24 03:18:17 -080054
Adrian Hunter50accb92011-10-03 15:33:34 +030055#ifdef CONFIG_PM_RUNTIME
56static int sdhci_runtime_pm_get(struct sdhci_host *host);
57static int sdhci_runtime_pm_put(struct sdhci_host *host);
58#else
59static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
60{
61 return 0;
62}
63static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
64{
65 return 0;
66}
67#endif
68
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053069static void sdhci_dump_state(struct sdhci_host *host)
70{
71 struct mmc_host *mmc = host->mmc;
72
73 pr_info("%s: clk: %d clk-gated: %d claimer: %s pwr: %d\n",
74 mmc_hostname(mmc), host->clock, mmc->clk_gated,
75 mmc->claimer->comm, host->pwr);
76 pr_info("%s: rpmstatus[pltfm](runtime-suspend:usage_count:disable_depth)(%d:%d:%d)\n",
77 mmc_hostname(mmc), mmc->parent->power.runtime_status,
78 atomic_read(&mmc->parent->power.usage_count),
79 mmc->parent->power.disable_depth);
80}
81
Pierre Ossmand129bce2006-03-24 03:18:17 -080082static void sdhci_dumpregs(struct sdhci_host *host)
83{
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053084 pr_info(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070085 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080086
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053087 pr_info(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030088 sdhci_readl(host, SDHCI_DMA_ADDRESS),
89 sdhci_readw(host, SDHCI_HOST_VERSION));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053090 pr_info(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030091 sdhci_readw(host, SDHCI_BLOCK_SIZE),
92 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053093 pr_info(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030094 sdhci_readl(host, SDHCI_ARGUMENT),
95 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053096 pr_info(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030097 sdhci_readl(host, SDHCI_PRESENT_STATE),
98 sdhci_readb(host, SDHCI_HOST_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053099 pr_info(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300100 sdhci_readb(host, SDHCI_POWER_CONTROL),
101 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530102 pr_info(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300103 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
104 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530105 pr_info(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
107 sdhci_readl(host, SDHCI_INT_STATUS));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530108 pr_info(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300109 sdhci_readl(host, SDHCI_INT_ENABLE),
110 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530111 pr_info(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300112 sdhci_readw(host, SDHCI_ACMD12_ERR),
113 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530114 pr_info(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300115 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500116 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530117 pr_info(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500118 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300119 sdhci_readl(host, SDHCI_MAX_CURRENT));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530120 pr_info(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530121 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800122
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100123 if (host->flags & SDHCI_USE_ADMA)
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530124 pr_info(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100125 readl(host->ioaddr + SDHCI_ADMA_ERROR),
126 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
127
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530128 sdhci_dump_state(host);
129 pr_info(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800130}
131
132/*****************************************************************************\
133 * *
134 * Low level functions *
135 * *
136\*****************************************************************************/
137
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300138static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
139{
140 u32 ier;
141
142 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
143 ier &= ~clear;
144 ier |= set;
145 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
146 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
147}
148
149static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
150{
151 sdhci_clear_set_irqs(host, 0, irqs);
152}
153
154static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
155{
156 sdhci_clear_set_irqs(host, irqs, 0);
157}
158
159static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
160{
Sahitya Tummalaca422112013-02-22 12:15:54 +0530161 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300162
Adrian Hunterc79396c2011-12-27 15:48:42 +0200163 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100164 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300165 return;
166
Sahitya Tummalaca422112013-02-22 12:15:54 +0530167 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
168 SDHCI_CARD_PRESENT;
169 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
Adrian Hunter50accb92011-10-03 15:33:34 +0300170
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300171 if (enable)
172 sdhci_unmask_irqs(host, irqs);
173 else
174 sdhci_mask_irqs(host, irqs);
175}
176
177static void sdhci_enable_card_detection(struct sdhci_host *host)
178{
179 sdhci_set_card_detection(host, true);
180}
181
182static void sdhci_disable_card_detection(struct sdhci_host *host)
183{
184 sdhci_set_card_detection(host, false);
185}
186
Pierre Ossmand129bce2006-03-24 03:18:17 -0800187static void sdhci_reset(struct sdhci_host *host, u8 mask)
188{
Pierre Ossmane16514d2006-06-30 02:22:24 -0700189 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300190 u32 uninitialized_var(ier);
Pierre Ossmane16514d2006-06-30 02:22:24 -0700191
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100192 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300193 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700194 SDHCI_CARD_PRESENT))
195 return;
196 }
197
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300198 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
199 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
200
Philip Rakity393c1a32011-01-21 11:26:40 -0800201 if (host->ops->platform_reset_enter)
202 host->ops->platform_reset_enter(host, mask);
203
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300204 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800205
Pierre Ossmane16514d2006-06-30 02:22:24 -0700206 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800207 host->clock = 0;
208
Pierre Ossmane16514d2006-06-30 02:22:24 -0700209 /* Wait max 100 ms */
210 timeout = 100;
211
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530212 if (host->ops->check_power_status && host->pwr &&
213 (mask & SDHCI_RESET_ALL))
Sahitya Tummala179e7382013-03-20 19:24:01 +0530214 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530215
Pierre Ossmane16514d2006-06-30 02:22:24 -0700216 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300217 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d2006-06-30 02:22:24 -0700218 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +0530219 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d2006-06-30 02:22:24 -0700220 mmc_hostname(host->mmc), (int)mask);
221 sdhci_dumpregs(host);
222 return;
223 }
224 timeout--;
225 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300227
Philip Rakity393c1a32011-01-21 11:26:40 -0800228 if (host->ops->platform_reset_exit)
229 host->ops->platform_reset_exit(host, mask);
230
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300231 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
232 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Sahitya Tummalaca422112013-02-22 12:15:54 +0530233
234 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
235 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
236 host->ops->enable_dma(host);
237 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800238}
239
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800240static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
241
242static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800243{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800244 if (soft)
245 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
246 else
247 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800248
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300249 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
250 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700251 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
252 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300253 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800254
255 if (soft) {
256 /* force clock reconfiguration */
257 host->clock = 0;
258 sdhci_set_ios(host->mmc, &host->mmc->ios);
259 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300260}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300262static void sdhci_reinit(struct sdhci_host *host)
263{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800264 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300265 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800266}
267
268static void sdhci_activate_led(struct sdhci_host *host)
269{
270 u8 ctrl;
271
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300272 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300274 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800275}
276
277static void sdhci_deactivate_led(struct sdhci_host *host)
278{
279 u8 ctrl;
280
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300281 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800282 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300283 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800284}
285
Pierre Ossmanf9134312008-12-21 17:01:48 +0100286#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100287static void sdhci_led_control(struct led_classdev *led,
288 enum led_brightness brightness)
289{
290 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
291 unsigned long flags;
292
293 spin_lock_irqsave(&host->lock, flags);
294
Adrian Hunter50accb92011-10-03 15:33:34 +0300295 if (host->runtime_suspended)
296 goto out;
297
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100298 if (brightness == LED_OFF)
299 sdhci_deactivate_led(host);
300 else
301 sdhci_activate_led(host);
Adrian Hunter50accb92011-10-03 15:33:34 +0300302out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100303 spin_unlock_irqrestore(&host->lock, flags);
304}
305#endif
306
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307/*****************************************************************************\
308 * *
309 * Core functions *
310 * *
311\*****************************************************************************/
312
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800314{
Pierre Ossman76591502008-07-21 00:32:11 +0200315 unsigned long flags;
316 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700317 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200318 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800319
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100320 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800321
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100322 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200323 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800324
Pierre Ossman76591502008-07-21 00:32:11 +0200325 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800326
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100327 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200328 if (!sg_miter_next(&host->sg_miter))
329 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800330
Pierre Ossman76591502008-07-21 00:32:11 +0200331 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800332
Pierre Ossman76591502008-07-21 00:32:11 +0200333 blksize -= len;
334 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200335
Pierre Ossman76591502008-07-21 00:32:11 +0200336 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800337
Pierre Ossman76591502008-07-21 00:32:11 +0200338 while (len) {
339 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300340 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200341 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800342 }
Pierre Ossman76591502008-07-21 00:32:11 +0200343
344 *buf = scratch & 0xFF;
345
346 buf++;
347 scratch >>= 8;
348 chunk--;
349 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800350 }
351 }
Pierre Ossman76591502008-07-21 00:32:11 +0200352
353 sg_miter_stop(&host->sg_miter);
354
355 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100356}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800357
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358static void sdhci_write_block_pio(struct sdhci_host *host)
359{
Pierre Ossman76591502008-07-21 00:32:11 +0200360 unsigned long flags;
361 size_t blksize, len, chunk;
362 u32 scratch;
363 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100364
365 DBG("PIO writing\n");
366
367 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200368 chunk = 0;
369 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100370
Pierre Ossman76591502008-07-21 00:32:11 +0200371 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100372
373 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200374 if (!sg_miter_next(&host->sg_miter))
375 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100376
Pierre Ossman76591502008-07-21 00:32:11 +0200377 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200378
Pierre Ossman76591502008-07-21 00:32:11 +0200379 blksize -= len;
380 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381
Pierre Ossman76591502008-07-21 00:32:11 +0200382 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100383
Pierre Ossman76591502008-07-21 00:32:11 +0200384 while (len) {
385 scratch |= (u32)*buf << (chunk * 8);
386
387 buf++;
388 chunk++;
389 len--;
390
391 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300392 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200393 chunk = 0;
394 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100395 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100396 }
397 }
Pierre Ossman76591502008-07-21 00:32:11 +0200398
399 sg_miter_stop(&host->sg_miter);
400
401 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100402}
403
404static void sdhci_transfer_pio(struct sdhci_host *host)
405{
406 u32 mask;
407
408 BUG_ON(!host->data);
409
Pierre Ossman76591502008-07-21 00:32:11 +0200410 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100411 return;
412
413 if (host->data->flags & MMC_DATA_READ)
414 mask = SDHCI_DATA_AVAILABLE;
415 else
416 mask = SDHCI_SPACE_AVAILABLE;
417
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200418 /*
419 * Some controllers (JMicron JMB38x) mess up the buffer bits
420 * for transfers < 4 bytes. As long as it is just one block,
421 * we can ignore the bits.
422 */
423 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
424 (host->data->blocks == 1))
425 mask = ~0;
426
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300427 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300428 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
429 udelay(100);
430
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100431 if (host->data->flags & MMC_DATA_READ)
432 sdhci_read_block_pio(host);
433 else
434 sdhci_write_block_pio(host);
435
Pierre Ossman76591502008-07-21 00:32:11 +0200436 host->blocks--;
437 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100438 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100439 }
440
441 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800442}
443
Pierre Ossman2134a922008-06-28 18:28:51 +0200444static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
445{
446 local_irq_save(*flags);
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800447 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200448}
449
450static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
451{
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800452 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200453 local_irq_restore(*flags);
454}
455
Ben Dooks118cd172010-03-05 13:43:26 -0800456static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
457{
Ben Dooks9e506f32010-03-05 13:43:29 -0800458 __le32 *dataddr = (__le32 __force *)(desc + 4);
459 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800460
Ben Dooks9e506f32010-03-05 13:43:29 -0800461 /* SDHCI specification says ADMA descriptors should be 4 byte
462 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800463
Ben Dooks9e506f32010-03-05 13:43:29 -0800464 cmdlen[0] = cpu_to_le16(cmd);
465 cmdlen[1] = cpu_to_le16(len);
466
467 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800468}
469
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800470static int sdhci_pre_dma_transfer(struct sdhci_host *host,
471 struct mmc_data *data,
472 struct sdhci_next *next)
473{
474 int sg_count;
475
476 if (!next && data->host_cookie &&
477 data->host_cookie != host->next_data.cookie) {
478 printk(KERN_WARNING "[%s] invalid cookie: data->host_cookie %d"
479 " host->next_data.cookie %d\n",
480 __func__, data->host_cookie, host->next_data.cookie);
481 data->host_cookie = 0;
482 }
483
484 /* Check if next job is already prepared */
485 if (next ||
486 (!next && data->host_cookie != host->next_data.cookie)) {
487 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg,
488 data->sg_len,
489 (data->flags & MMC_DATA_WRITE) ?
490 DMA_TO_DEVICE : DMA_FROM_DEVICE);
491 } else {
492 sg_count = host->next_data.sg_count;
493 host->next_data.sg_count = 0;
494 }
495
496 if (sg_count == 0)
497 return -EINVAL;
498
499 if (next) {
500 next->sg_count = sg_count;
501 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
502 } else
503 host->sg_count = sg_count;
504
505 return sg_count;
506}
507
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200508static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200509 struct mmc_data *data)
510{
511 int direction;
512
513 u8 *desc;
514 u8 *align;
515 dma_addr_t addr;
516 dma_addr_t align_addr;
517 int len, offset;
518
519 struct scatterlist *sg;
520 int i;
521 char *buffer;
522 unsigned long flags;
523
524 /*
525 * The spec does not specify endianness of descriptor table.
526 * We currently guess that it is LE.
527 */
528
529 if (data->flags & MMC_DATA_READ)
530 direction = DMA_FROM_DEVICE;
531 else
532 direction = DMA_TO_DEVICE;
533
534 /*
535 * The ADMA descriptor table is mapped further down as we
536 * need to fill it with data first.
537 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200538 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530539 host->align_buffer,
540 host->align_buf_sz,
541 direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700542 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200543 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200544 BUG_ON(host->align_addr & 0x3);
545
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800546 host->sg_count = sdhci_pre_dma_transfer(host, data, NULL);
547 if (host->sg_count < 0)
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200548 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200549
550 desc = host->adma_desc;
551 align = host->align_buffer;
552
553 align_addr = host->align_addr;
554
555 for_each_sg(data->sg, sg, host->sg_count, i) {
556 addr = sg_dma_address(sg);
557 len = sg_dma_len(sg);
558
559 /*
560 * The SDHCI specification states that ADMA
561 * addresses must be 32-bit aligned. If they
562 * aren't, then we use a bounce buffer for
563 * the (up to three) bytes that screw up the
564 * alignment.
565 */
566 offset = (4 - (addr & 0x3)) & 0x3;
567 if (offset) {
568 if (data->flags & MMC_DATA_WRITE) {
569 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200570 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200571 memcpy(align, buffer, offset);
572 sdhci_kunmap_atomic(buffer, &flags);
573 }
574
Ben Dooks118cd172010-03-05 13:43:26 -0800575 /* tran, valid */
576 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200577
578 BUG_ON(offset > 65536);
579
Pierre Ossman2134a922008-06-28 18:28:51 +0200580 align += 4;
581 align_addr += 4;
582
583 desc += 8;
584
585 addr += offset;
586 len -= offset;
587 }
588
Pierre Ossman2134a922008-06-28 18:28:51 +0200589 BUG_ON(len > 65536);
590
Ben Dooks118cd172010-03-05 13:43:26 -0800591 /* tran, valid */
592 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200593 desc += 8;
594
595 /*
596 * If this triggers then we have a calculation bug
597 * somewhere. :/
598 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530599 WARN_ON((desc - host->adma_desc) > host->adma_desc_sz);
600
Pierre Ossman2134a922008-06-28 18:28:51 +0200601 }
602
Thomas Abraham70764a92010-05-26 14:42:04 -0700603 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
604 /*
605 * Mark the last descriptor as the terminating descriptor
606 */
607 if (desc != host->adma_desc) {
608 desc -= 8;
609 desc[0] |= 0x2; /* end */
610 }
611 } else {
612 /*
613 * Add a terminating entry.
614 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200615
Thomas Abraham70764a92010-05-26 14:42:04 -0700616 /* nop, end, valid */
617 sdhci_set_adma_desc(desc, 0, 0, 0x3);
618 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200619
620 /*
621 * Resync align buffer as we might have changed it.
622 */
623 if (data->flags & MMC_DATA_WRITE) {
624 dma_sync_single_for_device(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530625 host->align_addr,
626 host->align_buf_sz,
627 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200628 }
629
630 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530631 host->adma_desc,
632 host->adma_desc_sz,
633 DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200634 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200635 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200636 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200637
638 return 0;
639
640unmap_entries:
641 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
642 data->sg_len, direction);
643unmap_align:
644 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530645 host->align_buf_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200646fail:
647 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200648}
649
650static void sdhci_adma_table_post(struct sdhci_host *host,
651 struct mmc_data *data)
652{
653 int direction;
654
655 struct scatterlist *sg;
656 int i, size;
657 u8 *align;
658 char *buffer;
659 unsigned long flags;
660
661 if (data->flags & MMC_DATA_READ)
662 direction = DMA_FROM_DEVICE;
663 else
664 direction = DMA_TO_DEVICE;
665
666 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530667 host->adma_desc_sz, DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200668
669 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530670 host->align_buf_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200671
672 if (data->flags & MMC_DATA_READ) {
673 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
674 data->sg_len, direction);
675
676 align = host->align_buffer;
677
678 for_each_sg(data->sg, sg, host->sg_count, i) {
679 if (sg_dma_address(sg) & 0x3) {
680 size = 4 - (sg_dma_address(sg) & 0x3);
681
682 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200683 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200684 memcpy(buffer, align, size);
685 sdhci_kunmap_atomic(buffer, &flags);
686
687 align += 4;
688 }
689 }
690 }
691
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800692 if (!data->host_cookie)
693 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
694 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200695}
696
Andrei Warkentina3c77782011-04-11 16:13:42 -0500697static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800698{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700699 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500700 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700701 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800702
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200703 /*
704 * If the host controller provides us with an incorrect timeout
705 * value, just skip the check and use 0xE. The hardware may take
706 * longer to time out, but that's much better than having a too-short
707 * timeout value.
708 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200709 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200710 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200711
Andrei Warkentina3c77782011-04-11 16:13:42 -0500712 /* Unspecified timeout, assume max */
713 if (!data && !cmd->cmd_timeout_ms)
714 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800715
Andrei Warkentina3c77782011-04-11 16:13:42 -0500716 /* timeout in us */
717 if (!data)
718 target_timeout = cmd->cmd_timeout_ms * 1000;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530719 else {
720 target_timeout = data->timeout_ns / 1000;
721 if (host->clock)
722 target_timeout += data->timeout_clks / host->clock;
723 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700724
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700725 /*
726 * Figure out needed cycles.
727 * We do this in steps in order to fit inside a 32 bit int.
728 * The first step is the minimum timeout, which will have a
729 * minimum resolution of 6 bits:
730 * (1) 2^13*1000 > 2^22,
731 * (2) host->timeout_clk < 2^16
732 * =>
733 * (1) / (2) > 2^6
734 */
735 count = 0;
736 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
737 while (current_timeout < target_timeout) {
738 count++;
739 current_timeout <<= 1;
740 if (count >= 0xF)
741 break;
742 }
743
Sahitya Tummalaca422112013-02-22 12:15:54 +0530744 if (count >= 0xF) {
Chris Ball34ca2092012-06-01 10:39:45 -0400745 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
746 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700747 count = 0xE;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530748 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700749
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200750 return count;
751}
752
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300753static void sdhci_set_transfer_irqs(struct sdhci_host *host)
754{
755 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
756 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
757
758 if (host->flags & SDHCI_REQ_USE_DMA)
759 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
760 else
761 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
762}
763
Andrei Warkentina3c77782011-04-11 16:13:42 -0500764static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200765{
766 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200767 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500768 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200769 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200770
771 WARN_ON(host->data);
772
Andrei Warkentina3c77782011-04-11 16:13:42 -0500773 if (data || (cmd->flags & MMC_RSP_BUSY)) {
774 count = sdhci_calc_timeout(host, cmd);
775 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
776 }
777
778 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200779 return;
780
781 /* Sanity checks */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530782 BUG_ON(data->blksz * data->blocks > host->mmc->max_req_size);
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200783 BUG_ON(data->blksz > host->mmc->max_blk_size);
784 BUG_ON(data->blocks > 65535);
785
786 host->data = data;
787 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400788 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200789
Richard Röjforsa13abc72009-09-22 16:45:30 -0700790 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100791 host->flags |= SDHCI_REQ_USE_DMA;
792
Pierre Ossman2134a922008-06-28 18:28:51 +0200793 /*
794 * FIXME: This doesn't account for merging when mapping the
795 * scatterlist.
796 */
797 if (host->flags & SDHCI_REQ_USE_DMA) {
798 int broken, i;
799 struct scatterlist *sg;
800
801 broken = 0;
802 if (host->flags & SDHCI_USE_ADMA) {
803 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
804 broken = 1;
805 } else {
806 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
807 broken = 1;
808 }
809
810 if (unlikely(broken)) {
811 for_each_sg(data->sg, sg, data->sg_len, i) {
812 if (sg->length & 0x3) {
813 DBG("Reverting to PIO because of "
814 "transfer size (%d)\n",
815 sg->length);
816 host->flags &= ~SDHCI_REQ_USE_DMA;
817 break;
818 }
819 }
820 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100821 }
822
823 /*
824 * The assumption here being that alignment is the same after
825 * translation to device address space.
826 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200827 if (host->flags & SDHCI_REQ_USE_DMA) {
828 int broken, i;
829 struct scatterlist *sg;
830
831 broken = 0;
832 if (host->flags & SDHCI_USE_ADMA) {
833 /*
834 * As we use 3 byte chunks to work around
835 * alignment problems, we need to check this
836 * quirk.
837 */
838 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
839 broken = 1;
840 } else {
841 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
842 broken = 1;
843 }
844
845 if (unlikely(broken)) {
846 for_each_sg(data->sg, sg, data->sg_len, i) {
847 if (sg->offset & 0x3) {
848 DBG("Reverting to PIO because of "
849 "bad alignment\n");
850 host->flags &= ~SDHCI_REQ_USE_DMA;
851 break;
852 }
853 }
854 }
855 }
856
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200857 if (host->flags & SDHCI_REQ_USE_DMA) {
858 if (host->flags & SDHCI_USE_ADMA) {
859 ret = sdhci_adma_table_pre(host, data);
860 if (ret) {
861 /*
862 * This only happens when someone fed
863 * us an invalid request.
864 */
865 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200866 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200867 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300868 sdhci_writel(host, host->adma_addr,
869 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200870 }
871 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300872 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200873
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800874 sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300875 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200876 /*
877 * This only happens when someone fed
878 * us an invalid request.
879 */
880 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200881 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200882 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200883 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300884 sdhci_writel(host, sg_dma_address(data->sg),
885 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200886 }
887 }
888 }
889
Pierre Ossman2134a922008-06-28 18:28:51 +0200890 /*
891 * Always adjust the DMA selection as some controllers
892 * (e.g. JMicron) can't do PIO properly when the selection
893 * is ADMA.
894 */
895 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300896 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200897 ctrl &= ~SDHCI_CTRL_DMA_MASK;
898 if ((host->flags & SDHCI_REQ_USE_DMA) &&
899 (host->flags & SDHCI_USE_ADMA))
900 ctrl |= SDHCI_CTRL_ADMA32;
901 else
902 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300903 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100904 }
905
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200906 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200907 int flags;
908
909 flags = SG_MITER_ATOMIC;
910 if (host->data->flags & MMC_DATA_READ)
911 flags |= SG_MITER_TO_SG;
912 else
913 flags |= SG_MITER_FROM_SG;
914 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200915 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800916 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700917
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300918 sdhci_set_transfer_irqs(host);
919
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400920 /* Set the DMA boundary value and block size */
921 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
922 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300923 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700924}
925
926static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500927 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700928{
929 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500930 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700931
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700932 if (data == NULL)
933 return;
934
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200935 WARN_ON(!host->data);
936
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700937 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500938 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
939 mode |= SDHCI_TRNS_MULTI;
940 /*
941 * If we are sending CMD23, CMD12 never gets sent
942 * on successful completion (so no Auto-CMD12).
943 */
944 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
945 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500946 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
947 mode |= SDHCI_TRNS_AUTO_CMD23;
948 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
949 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700950 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500951
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530952 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700953 mode |= SDHCI_TRNS_READ;
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530954 if (host->ops->toggle_cdr)
955 host->ops->toggle_cdr(host, true);
956 }
957 if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE))
958 host->ops->toggle_cdr(host, false);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100959 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700960 mode |= SDHCI_TRNS_DMA;
961
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300962 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800963}
964
965static void sdhci_finish_data(struct sdhci_host *host)
966{
967 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800968
969 BUG_ON(!host->data);
970
971 data = host->data;
972 host->data = NULL;
973
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100974 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200975 if (host->flags & SDHCI_USE_ADMA)
976 sdhci_adma_table_post(host, data);
977 else {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800978 if (!data->host_cookie)
979 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
980 data->sg_len,
981 (data->flags & MMC_DATA_READ) ?
982 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200983 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800984 }
985
986 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200987 * The specification states that the block count register must
988 * be updated, but it does not specify at what point in the
989 * data flow. That makes the register entirely useless to read
990 * back so we have to assume that nothing made it to the card
991 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800992 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200993 if (data->error)
994 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800995 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200996 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997
Andrei Warkentine89d4562011-05-23 15:06:37 -0500998 /*
999 * Need to send CMD12 if -
1000 * a) open-ended multiblock transfer (no CMD23)
1001 * b) error in multiblock transfer
1002 */
1003 if (data->stop &&
1004 (data->error ||
1005 !host->mrq->sbc)) {
1006
Pierre Ossmand129bce2006-03-24 03:18:17 -08001007 /*
1008 * The controller needs a reset of internal state machines
1009 * upon error conditions.
1010 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001011 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012 sdhci_reset(host, SDHCI_RESET_CMD);
1013 sdhci_reset(host, SDHCI_RESET_DATA);
1014 }
1015
1016 sdhci_send_command(host, data->stop);
1017 } else
1018 tasklet_schedule(&host->finish_tasklet);
1019}
1020
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301021#define SDHCI_REQUEST_TIMEOUT 10 /* Default request timeout in seconds */
1022
Pierre Ossmand129bce2006-03-24 03:18:17 -08001023static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
1024{
1025 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001026 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001027 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001028
1029 WARN_ON(host->cmd);
1030
Pierre Ossmand129bce2006-03-24 03:18:17 -08001031 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001032 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001033
1034 mask = SDHCI_CMD_INHIBIT;
1035 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1036 mask |= SDHCI_DATA_INHIBIT;
1037
1038 /* We shouldn't wait for data inihibit for stop commands, even
1039 though they might use busy signaling */
1040 if (host->mrq->data && (cmd == host->mrq->data->stop))
1041 mask &= ~SDHCI_DATA_INHIBIT;
1042
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001043 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001044 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301045 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001046 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001048 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001049 tasklet_schedule(&host->finish_tasklet);
1050 return;
1051 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001052 timeout--;
1053 mdelay(1);
1054 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001055
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301056 mod_timer(&host->timer, jiffies + SDHCI_REQUEST_TIMEOUT * HZ);
1057
1058 if (cmd->cmd_timeout_ms > SDHCI_REQUEST_TIMEOUT * MSEC_PER_SEC)
1059 mod_timer(&host->timer, jiffies +
1060 (msecs_to_jiffies(cmd->cmd_timeout_ms * 2)));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001061
1062 host->cmd = cmd;
1063
Andrei Warkentina3c77782011-04-11 16:13:42 -05001064 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001065
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001066 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001067
Andrei Warkentine89d4562011-05-23 15:06:37 -05001068 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001069
Pierre Ossmand129bce2006-03-24 03:18:17 -08001070 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301071 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001073 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001074 tasklet_schedule(&host->finish_tasklet);
1075 return;
1076 }
1077
1078 if (!(cmd->flags & MMC_RSP_PRESENT))
1079 flags = SDHCI_CMD_RESP_NONE;
1080 else if (cmd->flags & MMC_RSP_136)
1081 flags = SDHCI_CMD_RESP_LONG;
1082 else if (cmd->flags & MMC_RSP_BUSY)
1083 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1084 else
1085 flags = SDHCI_CMD_RESP_SHORT;
1086
1087 if (cmd->flags & MMC_RSP_CRC)
1088 flags |= SDHCI_CMD_CRC;
1089 if (cmd->flags & MMC_RSP_OPCODE)
1090 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301091
1092 /* CMD19 is special in that the Data Present Select should be set */
Girish K S2cd06dc2012-01-06 09:56:39 +05301093 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1094 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001095 flags |= SDHCI_CMD_DATA;
1096
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001097 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001098}
1099
1100static void sdhci_finish_command(struct sdhci_host *host)
1101{
1102 int i;
1103
1104 BUG_ON(host->cmd == NULL);
1105
1106 if (host->cmd->flags & MMC_RSP_PRESENT) {
1107 if (host->cmd->flags & MMC_RSP_136) {
1108 /* CRC is stripped so we need to do some shifting. */
1109 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001110 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001111 SDHCI_RESPONSE + (3-i)*4) << 8;
1112 if (i != 3)
1113 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001114 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001115 SDHCI_RESPONSE + (3-i)*4-1);
1116 }
1117 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001118 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001119 }
1120 }
1121
Pierre Ossman17b04292007-07-22 22:18:46 +02001122 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001123
Andrei Warkentine89d4562011-05-23 15:06:37 -05001124 /* Finished CMD23, now send actual command. */
1125 if (host->cmd == host->mrq->sbc) {
1126 host->cmd = NULL;
1127 sdhci_send_command(host, host->mrq->cmd);
1128 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001129
Andrei Warkentine89d4562011-05-23 15:06:37 -05001130 /* Processed actual command. */
1131 if (host->data && host->data_early)
1132 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001133
Andrei Warkentine89d4562011-05-23 15:06:37 -05001134 if (!host->cmd->data)
1135 tasklet_schedule(&host->finish_tasklet);
1136
1137 host->cmd = NULL;
1138 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001139}
1140
1141static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1142{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301143 int div = 0; /* Initialized for compiler warning */
Sahitya Tummalaca422112013-02-22 12:15:54 +05301144 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301145 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001146 unsigned long timeout;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301147 unsigned long flags;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001148
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301149 spin_lock_irqsave(&host->lock, flags);
Todd Poynor30832ab2011-12-27 15:48:46 +02001150 if (clock && clock == host->clock)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301151 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001152
Sahitya Tummalaca422112013-02-22 12:15:54 +05301153 host->mmc->actual_clock = 0;
1154
Anton Vorontsov81146342009-03-17 00:13:59 +03001155 if (host->ops->set_clock) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301156 spin_unlock_irqrestore(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001157 host->ops->set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301158 spin_lock_irqsave(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001159 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301160 goto ret;
Anton Vorontsov81146342009-03-17 00:13:59 +03001161 }
1162
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301163 if (host->clock)
1164 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001165
1166 if (clock == 0)
1167 goto out;
1168
Zhangfei Gao85105c52010-08-06 07:10:01 +08001169 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301170 /*
1171 * Check if the Host Controller supports Programmable Clock
1172 * Mode.
1173 */
1174 if (host->clk_mul) {
1175 u16 ctrl;
1176
1177 /*
1178 * We need to figure out whether the Host Driver needs
1179 * to select Programmable Clock Mode, or the value can
1180 * be set automatically by the Host Controller based on
1181 * the Preset Value registers.
1182 */
1183 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1184 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1185 for (div = 1; div <= 1024; div++) {
1186 if (((host->max_clk * host->clk_mul) /
1187 div) <= clock)
1188 break;
1189 }
1190 /*
1191 * Set Programmable Clock Mode in the Clock
1192 * Control register.
1193 */
1194 clk = SDHCI_PROG_CLOCK_MODE;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301195 real_div = div;
1196 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301197 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001198 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301199 } else {
1200 /* Version 3.00 divisors must be a multiple of 2. */
1201 if (host->max_clk <= clock)
1202 div = 1;
1203 else {
1204 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1205 div += 2) {
1206 if ((host->max_clk / div) <= clock)
1207 break;
1208 }
1209 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301210 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301211 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001212 }
1213 } else {
1214 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001215 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001216 if ((host->max_clk / div) <= clock)
1217 break;
1218 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301219 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301220 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001221 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001222
Sahitya Tummalaca422112013-02-22 12:15:54 +05301223 if (real_div)
1224 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1225
Sahitya Tummala00240122013-02-28 19:50:51 +05301226 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
1227 div = 0;
1228
Arindam Nathc3ed3872011-05-05 12:19:06 +05301229 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001230 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1231 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001232 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001233 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001234
Chris Ball27f6cb12009-09-22 16:45:31 -07001235 /* Wait max 20 ms */
1236 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001237 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001238 & SDHCI_CLOCK_INT_STABLE)) {
1239 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301240 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001241 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001242 sdhci_dumpregs(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301243 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001244 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001245 timeout--;
1246 mdelay(1);
1247 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001248
1249 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001250 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001251
1252out:
1253 host->clock = clock;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301254ret:
1255 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001256}
1257
Sahitya Tummalaca422112013-02-22 12:15:54 +05301258static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001259{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001260 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001261
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001262 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001263 switch (1 << power) {
1264 case MMC_VDD_165_195:
1265 pwr = SDHCI_POWER_180;
1266 break;
1267 case MMC_VDD_29_30:
1268 case MMC_VDD_30_31:
1269 pwr = SDHCI_POWER_300;
1270 break;
1271 case MMC_VDD_32_33:
1272 case MMC_VDD_33_34:
1273 pwr = SDHCI_POWER_330;
1274 break;
1275 default:
1276 BUG();
1277 }
1278 }
1279
1280 if (host->pwr == pwr)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301281 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001282
Pierre Ossmanae628902009-05-03 20:45:03 +02001283 host->pwr = pwr;
1284
1285 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001286 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301287 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301288 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301289 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001290 }
1291
1292 /*
1293 * Spec says that we should clear the power reg before setting
1294 * a new value. Some controllers don't seem to like this though.
1295 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301296 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001297 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301298 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301299 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301300 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001301
Andres Salomone08c1692008-07-04 10:00:03 -07001302 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001303 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001304 * and set turn on power at the same time, so set the voltage first.
1305 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301306 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001307 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301308 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301309 host->ops->check_power_status(host, REQ_BUS_ON);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301310 }
Pierre Ossmanae628902009-05-03 20:45:03 +02001311
1312 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001313
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001314 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301315 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301316 host->ops->check_power_status(host, REQ_BUS_ON);
Harald Welte557b0692009-06-18 16:53:38 +02001317
1318 /*
1319 * Some controllers need an extra 10ms delay of 10ms before they
1320 * can apply clock after applying power
1321 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001322 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001323 mdelay(10);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301324
1325 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001326}
1327
Pierre Ossmand129bce2006-03-24 03:18:17 -08001328/*****************************************************************************\
1329 * *
1330 * MMC callbacks *
1331 * *
1332\*****************************************************************************/
1333
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301334static int sdhci_enable(struct mmc_host *mmc)
1335{
1336 struct sdhci_host *host = mmc_priv(mmc);
1337
1338 if (host->cpu_dma_latency_us)
1339 pm_qos_update_request(&host->pm_qos_req_dma,
1340 host->cpu_dma_latency_us);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301341 if (host->ops->platform_bus_voting)
1342 host->ops->platform_bus_voting(host, 1);
1343
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301344 return 0;
1345}
1346
1347static int sdhci_disable(struct mmc_host *mmc)
1348{
1349 struct sdhci_host *host = mmc_priv(mmc);
1350
1351 if (host->cpu_dma_latency_us)
1352 pm_qos_update_request(&host->pm_qos_req_dma,
1353 PM_QOS_DEFAULT_VALUE);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301354 if (host->ops->platform_bus_voting)
1355 host->ops->platform_bus_voting(host, 0);
1356
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301357 return 0;
1358}
1359
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001360static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1361 bool is_first_req)
1362{
1363 struct sdhci_host *host = mmc_priv(mmc);
1364
1365 if (mrq->data->host_cookie) {
1366 mrq->data->host_cookie = 0;
1367 return;
1368 }
1369
1370 if (host->flags & SDHCI_REQ_USE_DMA)
1371 if (sdhci_pre_dma_transfer(host, mrq->data, &host->next_data) < 0)
1372 mrq->data->host_cookie = 0;
1373}
1374
1375static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1376 int err)
1377{
1378 struct sdhci_host *host = mmc_priv(mmc);
1379 struct mmc_data *data = mrq->data;
1380
1381 if (host->flags & SDHCI_REQ_USE_DMA) {
1382 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1383 (data->flags & MMC_DATA_WRITE) ?
1384 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1385 data->host_cookie = 0;
1386 }
1387}
1388
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301389static bool sdhci_check_state(struct sdhci_host *host)
1390{
1391 struct mmc_host *mmc = host->mmc;
1392
1393 if (!host->clock || !host->pwr ||
1394 pm_runtime_suspended(mmc->parent))
1395 return true;
1396 else
1397 return false;
1398}
1399
Pierre Ossmand129bce2006-03-24 03:18:17 -08001400static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1401{
1402 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001403 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001404 unsigned long flags;
1405
1406 host = mmc_priv(mmc);
1407
Adrian Hunter50accb92011-10-03 15:33:34 +03001408 sdhci_runtime_pm_get(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301409 if (sdhci_check_state(host)) {
1410 sdhci_dump_state(host);
1411 WARN(1, "sdhci in bad state");
1412 mrq->cmd->error = -EIO;
1413 if (mrq->data)
1414 mrq->data->error = -EIO;
1415 tasklet_schedule(&host->finish_tasklet);
1416 return;
1417 }
Adrian Hunter50accb92011-10-03 15:33:34 +03001418
Pierre Ossmand129bce2006-03-24 03:18:17 -08001419 spin_lock_irqsave(&host->lock, flags);
1420
1421 WARN_ON(host->mrq != NULL);
1422
Pierre Ossmanf9134312008-12-21 17:01:48 +01001423#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001424 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001425#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001426
1427 /*
1428 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1429 * requests if Auto-CMD12 is enabled.
1430 */
1431 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001432 if (mrq->stop) {
1433 mrq->data->stop = NULL;
1434 mrq->stop = NULL;
1435 }
1436 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001437
1438 host->mrq = mrq;
1439
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001440 /* If polling, assume that the card is always present. */
1441 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1442 present = true;
1443 else
1444 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1445 SDHCI_CARD_PRESENT;
1446
1447 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001448 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001449 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301450 } else {
1451 u32 present_state;
1452
1453 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1454 /*
1455 * Check if the re-tuning timer has already expired and there
1456 * is no on-going data transfer. If so, we need to execute
1457 * tuning procedure before sending command.
1458 */
1459 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1460 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1461 spin_unlock_irqrestore(&host->lock, flags);
Girish K S2cd06dc2012-01-06 09:56:39 +05301462 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301463 spin_lock_irqsave(&host->lock, flags);
1464
1465 /* Restore original mmc_request structure */
1466 host->mrq = mrq;
1467 }
1468
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001469 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001470 sdhci_send_command(host, mrq->sbc);
1471 else
1472 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301473 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001474
Pierre Ossman5f25a662006-10-04 02:15:39 -07001475 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001476 spin_unlock_irqrestore(&host->lock, flags);
1477}
1478
Adrian Hunter50accb92011-10-03 15:33:34 +03001479static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001480{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001481 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301482 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001483 u8 ctrl;
1484
Sahitya Tummalaca422112013-02-22 12:15:54 +05301485 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301486 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1487 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1488 return;
1489 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001490
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301491 if (ios->clock)
1492 sdhci_set_clock(host, ios->clock);
1493
1494 spin_lock_irqsave(&host->lock, flags);
1495 if (!host->clock) {
1496 spin_unlock_irqrestore(&host->lock, flags);
1497 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001498 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301499 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001500
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301501 if (ios->power_mode & (MMC_POWER_UP | MMC_POWER_ON))
Sahitya Tummalaca422112013-02-22 12:15:54 +05301502 vdd_bit = sdhci_set_power(host, ios->vdd);
1503
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301504 if (host->vmmc && vdd_bit != -1)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301505 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301506
1507 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001508
Philip Rakity643a81f2010-09-23 08:24:32 -07001509 if (host->ops->platform_send_init_74_clocks)
1510 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1511
Philip Rakity15ec4462010-11-19 16:48:39 -05001512 /*
1513 * If your platform has 8-bit width support but is not a v3 controller,
1514 * or if it requires special setup code, you should implement that in
1515 * platform_8bit_width().
1516 */
1517 if (host->ops->platform_8bit_width)
1518 host->ops->platform_8bit_width(host, ios->bus_width);
1519 else {
1520 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1521 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1522 ctrl &= ~SDHCI_CTRL_4BITBUS;
1523 if (host->version >= SDHCI_SPEC_300)
1524 ctrl |= SDHCI_CTRL_8BITBUS;
1525 } else {
1526 if (host->version >= SDHCI_SPEC_300)
1527 ctrl &= ~SDHCI_CTRL_8BITBUS;
1528 if (ios->bus_width == MMC_BUS_WIDTH_4)
1529 ctrl |= SDHCI_CTRL_4BITBUS;
1530 else
1531 ctrl &= ~SDHCI_CTRL_4BITBUS;
1532 }
1533 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1534 }
1535
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001536 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001537
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001538 if ((ios->timing == MMC_TIMING_SD_HS ||
1539 ios->timing == MMC_TIMING_MMC_HS)
1540 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001541 ctrl |= SDHCI_CTRL_HISPD;
1542 else
1543 ctrl &= ~SDHCI_CTRL_HISPD;
1544
Arindam Nathd6d50a12011-05-05 12:18:59 +05301545 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301546 u16 clk, ctrl_2;
1547 unsigned int clock;
1548
1549 /* In case of UHS-I modes, set High Speed Enable */
Girish K S2cd06dc2012-01-06 09:56:39 +05301550 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1551 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301552 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1553 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001554 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301555 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301556
1557 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1558 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301559 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301560 /*
1561 * We only need to set Driver Strength if the
1562 * preset value enable is not set.
1563 */
1564 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1565 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1566 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1567 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1568 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1569
1570 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301571 } else {
1572 /*
1573 * According to SDHC Spec v3.00, if the Preset Value
1574 * Enable in the Host Control 2 register is set, we
1575 * need to reset SD Clock Enable before changing High
1576 * Speed Enable to avoid generating clock gliches.
1577 */
Arindam Nath758535c2011-05-05 12:19:00 +05301578
1579 /* Reset SD Clock Enable */
1580 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1581 clk &= ~SDHCI_CLOCK_CARD_EN;
1582 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1583
1584 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1585
1586 /* Re-enable SD Clock */
1587 clock = host->clock;
1588 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301589 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301590 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301591 spin_lock_irqsave(&host->lock, flags);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301592 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301593
Arindam Nath49c468f2011-05-05 12:19:01 +05301594 /* Reset SD Clock Enable */
1595 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1596 clk &= ~SDHCI_CLOCK_CARD_EN;
1597 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1598
Philip Rakity6322cdd2011-05-13 11:17:15 +05301599 if (host->ops->set_uhs_signaling)
1600 host->ops->set_uhs_signaling(host, ios->timing);
1601 else {
1602 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1603 /* Select Bus Speed Mode for host */
1604 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S2cd06dc2012-01-06 09:56:39 +05301605 if (ios->timing == MMC_TIMING_MMC_HS200)
1606 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1607 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301608 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1609 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1610 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1611 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1612 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1613 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1614 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1615 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1616 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1617 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1618 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301619
1620 /* Re-enable SD Clock */
1621 clock = host->clock;
1622 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301623 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath49c468f2011-05-05 12:19:01 +05301624 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301625 spin_lock_irqsave(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301626 } else
1627 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301628
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301629 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02001630 /*
1631 * Some (ENE) controllers go apeshit on some ios operation,
1632 * signalling timeout and CRC errors even on CMD0. Resetting
1633 * it on each ios seems to solve the problem.
1634 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001635 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001636 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1637
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301638 /*
1639 * Reset the chip on each power off.
1640 * Should clear out any weird states.
1641 */
1642 if (ios->power_mode == MMC_POWER_OFF) {
1643 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1644 sdhci_reinit(host);
1645 vdd_bit = sdhci_set_power(host, -1);
1646 if (host->vmmc && vdd_bit != -1)
1647 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1648 }
1649 if (!ios->clock)
1650 sdhci_set_clock(host, ios->clock);
1651
Pierre Ossman5f25a662006-10-04 02:15:39 -07001652 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001653}
1654
Adrian Hunter50accb92011-10-03 15:33:34 +03001655static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1656{
1657 struct sdhci_host *host = mmc_priv(mmc);
1658
1659 sdhci_runtime_pm_get(host);
1660 sdhci_do_set_ios(host, ios);
1661 sdhci_runtime_pm_put(host);
1662}
1663
1664static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001665{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001666 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001667 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001668
Pierre Ossmand129bce2006-03-24 03:18:17 -08001669 spin_lock_irqsave(&host->lock, flags);
1670
Pierre Ossman1e728592008-04-16 19:13:13 +02001671 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001672 is_readonly = 0;
1673 else if (host->ops->get_ro)
1674 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001675 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001676 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1677 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001678
1679 spin_unlock_irqrestore(&host->lock, flags);
1680
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001681 /* This quirk needs to be replaced by a callback-function later */
1682 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1683 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001684}
1685
Takashi Iwai82b0e232011-04-21 20:26:38 +02001686#define SAMPLE_COUNT 5
1687
Adrian Hunter50accb92011-10-03 15:33:34 +03001688static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001689{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001690 int i, ro_count;
1691
Takashi Iwai82b0e232011-04-21 20:26:38 +02001692 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001693 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001694
1695 ro_count = 0;
1696 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001697 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001698 if (++ro_count > SAMPLE_COUNT / 2)
1699 return 1;
1700 }
1701 msleep(30);
1702 }
1703 return 0;
1704}
1705
Adrian Hunter50accb92011-10-03 15:33:34 +03001706static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001707{
Adrian Hunter50accb92011-10-03 15:33:34 +03001708 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001709
Adrian Hunter50accb92011-10-03 15:33:34 +03001710 if (host->ops && host->ops->hw_reset)
1711 host->ops->hw_reset(host);
1712}
Adrian Hunter20758b62011-08-29 16:42:12 +03001713
Adrian Hunter50accb92011-10-03 15:33:34 +03001714static int sdhci_get_ro(struct mmc_host *mmc)
1715{
1716 struct sdhci_host *host = mmc_priv(mmc);
1717 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001718
Adrian Hunter50accb92011-10-03 15:33:34 +03001719 sdhci_runtime_pm_get(host);
1720 ret = sdhci_do_get_ro(host);
1721 sdhci_runtime_pm_put(host);
1722 return ret;
1723}
1724
1725static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1726{
Pierre Ossman1e728592008-04-16 19:13:13 +02001727 if (host->flags & SDHCI_DEVICE_DEAD)
1728 goto out;
1729
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001730 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001731 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1732 else
1733 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1734
1735 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1736 if (host->runtime_suspended)
1737 goto out;
1738
1739 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001740 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1741 else
1742 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001743out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001744 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001745}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001746
Adrian Hunter50accb92011-10-03 15:33:34 +03001747static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1748{
1749 struct sdhci_host *host = mmc_priv(mmc);
1750 unsigned long flags;
1751
1752 spin_lock_irqsave(&host->lock, flags);
1753 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001754 spin_unlock_irqrestore(&host->lock, flags);
1755}
1756
Adrian Hunter50accb92011-10-03 15:33:34 +03001757static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1758 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301759{
Arindam Nathf2119df2011-05-05 12:18:57 +05301760 u8 pwr;
1761 u16 clk, ctrl;
1762 u32 present_state;
1763
Arindam Nathf2119df2011-05-05 12:18:57 +05301764 /*
1765 * Signal Voltage Switching is only applicable for Host Controllers
1766 * v3.00 and above.
1767 */
1768 if (host->version < SDHCI_SPEC_300)
1769 return 0;
1770
1771 /*
1772 * We first check whether the request is to set signalling voltage
1773 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1774 */
1775 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1776 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1777 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1778 ctrl &= ~SDHCI_CTRL_VDD_180;
1779 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301780 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301781 host->ops->check_power_status(host, REQ_IO_HIGH);
Arindam Nathf2119df2011-05-05 12:18:57 +05301782
1783 /* Wait for 5ms */
1784 usleep_range(5000, 5500);
1785
1786 /* 3.3V regulator output should be stable within 5 ms */
1787 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1788 if (!(ctrl & SDHCI_CTRL_VDD_180))
1789 return 0;
1790 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301791 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301792 "signalling voltage failed\n");
1793 return -EIO;
1794 }
1795 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1796 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1797 /* Stop SDCLK */
1798 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1799 clk &= ~SDHCI_CLOCK_CARD_EN;
1800 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1801
1802 /* Check whether DAT[3:0] is 0000 */
1803 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1804 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1805 SDHCI_DATA_LVL_SHIFT)) {
1806 /*
1807 * Enable 1.8V Signal Enable in the Host Control2
1808 * register
1809 */
1810 ctrl |= SDHCI_CTRL_VDD_180;
1811 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301812 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301813 host->ops->check_power_status(host, REQ_IO_LOW);
Arindam Nathf2119df2011-05-05 12:18:57 +05301814
1815 /* Wait for 5ms */
1816 usleep_range(5000, 5500);
1817
1818 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1819 if (ctrl & SDHCI_CTRL_VDD_180) {
1820 /* Provide SDCLK again and wait for 1ms*/
1821 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1822 clk |= SDHCI_CLOCK_CARD_EN;
1823 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1824 usleep_range(1000, 1500);
1825
1826 /*
1827 * If DAT[3:0] level is 1111b, then the card
1828 * was successfully switched to 1.8V signaling.
1829 */
1830 present_state = sdhci_readl(host,
1831 SDHCI_PRESENT_STATE);
1832 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1833 SDHCI_DATA_LVL_MASK)
1834 return 0;
1835 }
1836 }
1837
1838 /*
1839 * If we are here, that means the switch to 1.8V signaling
1840 * failed. We power cycle the card, and retry initialization
1841 * sequence by setting S18R to 0.
1842 */
1843 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1844 pwr &= ~SDHCI_POWER_ON;
1845 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301846 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301847 host->ops->check_power_status(host, REQ_BUS_OFF);
Arindam Nathf2119df2011-05-05 12:18:57 +05301848
1849 /* Wait for 1ms as per the spec */
1850 usleep_range(1000, 1500);
1851 pwr |= SDHCI_POWER_ON;
1852 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301853 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301854 host->ops->check_power_status(host, REQ_BUS_ON);
Arindam Nathf2119df2011-05-05 12:18:57 +05301855
Sahitya Tummalaca422112013-02-22 12:15:54 +05301856 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301857 "voltage failed, retrying with S18R set to 0\n");
1858 return -EAGAIN;
1859 } else
1860 /* No signal voltage switch required */
1861 return 0;
1862}
1863
Adrian Hunter50accb92011-10-03 15:33:34 +03001864static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1865 struct mmc_ios *ios)
1866{
1867 struct sdhci_host *host = mmc_priv(mmc);
1868 int err;
1869
1870 if (host->version < SDHCI_SPEC_300)
1871 return 0;
1872 sdhci_runtime_pm_get(host);
1873 err = sdhci_do_start_signal_voltage_switch(host, ios);
1874 sdhci_runtime_pm_put(host);
1875 return err;
1876}
1877
Girish K S2cd06dc2012-01-06 09:56:39 +05301878static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301879{
1880 struct sdhci_host *host;
1881 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05301882 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05301883 int tuning_loop_counter = MAX_TUNING_LOOP;
1884 unsigned long timeout;
1885 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05301886 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301887
1888 host = mmc_priv(mmc);
1889
Adrian Hunter50accb92011-10-03 15:33:34 +03001890 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301891 disable_irq(host->irq);
1892 spin_lock(&host->lock);
1893
1894 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1895
1896 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05301897 * The Host Controller needs tuning only in case of SDR104 mode
1898 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301899 * Capabilities register.
Girish K S2cd06dc2012-01-06 09:56:39 +05301900 * If the Host Controller supports the HS200 mode then the
1901 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301902 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08001903 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1904 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
1905 (host->flags & SDHCI_HS200_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05301906 requires_tuning_nonuhs = true;
1907
Arindam Nathb513ea22011-05-05 12:19:04 +05301908 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301909 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301910 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1911 else {
1912 spin_unlock(&host->lock);
1913 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03001914 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301915 return 0;
1916 }
1917
Asutosh Das8ddd3482013-01-04 11:45:46 +05301918 if (host->ops->execute_tuning) {
1919 spin_unlock(&host->lock);
1920 enable_irq(host->irq);
1921 host->ops->execute_tuning(host, opcode);
1922 disable_irq(host->irq);
1923 spin_lock(&host->lock);
1924 goto out;
1925 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301926 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1927
1928 /*
1929 * As per the Host Controller spec v3.00, tuning command
1930 * generates Buffer Read Ready interrupt, so enable that.
1931 *
1932 * Note: The spec clearly says that when tuning sequence
1933 * is being performed, the controller does not generate
1934 * interrupts other than Buffer Read Ready interrupt. But
1935 * to make sure we don't hit a controller bug, we _only_
1936 * enable Buffer Read Ready interrupt here.
1937 */
1938 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1939 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1940
1941 /*
1942 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1943 * of loops reaches 40 times or a timeout of 150ms occurs.
1944 */
1945 timeout = 150;
1946 do {
1947 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03001948 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301949
1950 if (!tuning_loop_counter && !timeout)
1951 break;
1952
Girish K S2cd06dc2012-01-06 09:56:39 +05301953 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301954 cmd.arg = 0;
1955 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1956 cmd.retries = 0;
1957 cmd.data = NULL;
1958 cmd.error = 0;
1959
1960 mrq.cmd = &cmd;
1961 host->mrq = &mrq;
1962
1963 /*
1964 * In response to CMD19, the card sends 64 bytes of tuning
1965 * block to the Host Controller. So we set the block size
1966 * to 64 here.
1967 */
Girish K S2cd06dc2012-01-06 09:56:39 +05301968 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1969 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1970 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1971 SDHCI_BLOCK_SIZE);
1972 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1973 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1974 SDHCI_BLOCK_SIZE);
1975 } else {
1976 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1977 SDHCI_BLOCK_SIZE);
1978 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301979
1980 /*
1981 * The tuning block is sent by the card to the host controller.
1982 * So we set the TRNS_READ bit in the Transfer Mode register.
1983 * This also takes care of setting DMA Enable and Multi Block
1984 * Select in the same register to 0.
1985 */
1986 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1987
1988 sdhci_send_command(host, &cmd);
1989
1990 host->cmd = NULL;
1991 host->mrq = NULL;
1992
1993 spin_unlock(&host->lock);
1994 enable_irq(host->irq);
1995
1996 /* Wait for Buffer Read Ready interrupt */
1997 wait_event_interruptible_timeout(host->buf_ready_int,
1998 (host->tuning_done == 1),
1999 msecs_to_jiffies(50));
2000 disable_irq(host->irq);
2001 spin_lock(&host->lock);
2002
2003 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302004 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05302005 "Buffer Read Ready interrupt during tuning "
2006 "procedure, falling back to fixed sampling "
2007 "clock\n");
2008 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2009 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2010 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2011 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2012
2013 err = -EIO;
2014 goto out;
2015 }
2016
2017 host->tuning_done = 0;
2018
2019 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2020 tuning_loop_counter--;
2021 timeout--;
2022 mdelay(1);
2023 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2024
2025 /*
2026 * The Host Driver has exhausted the maximum number of loops allowed,
2027 * so use fixed sampling frequency.
2028 */
2029 if (!tuning_loop_counter || !timeout) {
2030 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2031 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2032 } else {
2033 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302034 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05302035 " failed, falling back to fixed sampling"
2036 " clock\n");
2037 err = -EIO;
2038 }
2039 }
2040
2041out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302042 /*
2043 * If this is the very first time we are here, we start the retuning
2044 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2045 * flag won't be set, we check this condition before actually starting
2046 * the timer.
2047 */
2048 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2049 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2050 mod_timer(&host->tuning_timer, jiffies +
2051 host->tuning_count * HZ);
2052 /* Tuning mode 1 limits the maximum data length to 4MB */
2053 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2054 } else {
2055 host->flags &= ~SDHCI_NEEDS_RETUNING;
2056 /* Reload the new initial value for timer */
2057 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2058 mod_timer(&host->tuning_timer, jiffies +
2059 host->tuning_count * HZ);
2060 }
2061
2062 /*
2063 * In case tuning fails, host controllers which support re-tuning can
2064 * try tuning again at a later time, when the re-tuning timer expires.
2065 * So for these controllers, we return 0. Since there might be other
2066 * controllers who do not have this capability, we return error for
2067 * them.
2068 */
2069 if (err && host->tuning_count &&
2070 host->tuning_mode == SDHCI_TUNING_MODE_1)
2071 err = 0;
2072
Arindam Nathb513ea22011-05-05 12:19:04 +05302073 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
2074 spin_unlock(&host->lock);
2075 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002076 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302077
2078 return err;
2079}
2080
Adrian Hunter50accb92011-10-03 15:33:34 +03002081static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302082{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302083 u16 ctrl;
2084 unsigned long flags;
2085
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302086 /* Host Controller v3.00 defines preset value registers */
2087 if (host->version < SDHCI_SPEC_300)
2088 return;
2089
2090 spin_lock_irqsave(&host->lock, flags);
2091
2092 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2093
2094 /*
2095 * We only enable or disable Preset Value if they are not already
2096 * enabled or disabled respectively. Otherwise, we bail out.
2097 */
2098 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2099 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2100 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002101 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302102 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2103 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2104 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002105 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302106 }
2107
2108 spin_unlock_irqrestore(&host->lock, flags);
2109}
2110
Adrian Hunter50accb92011-10-03 15:33:34 +03002111static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
2112{
2113 struct sdhci_host *host = mmc_priv(mmc);
2114
2115 sdhci_runtime_pm_get(host);
2116 sdhci_do_enable_preset_value(host, enable);
2117 sdhci_runtime_pm_put(host);
2118}
2119
David Brownellab7aefd2006-11-12 17:55:30 -08002120static const struct mmc_host_ops sdhci_ops = {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002121 .pre_req = sdhci_pre_req,
2122 .post_req = sdhci_post_req,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002123 .request = sdhci_request,
2124 .set_ios = sdhci_set_ios,
2125 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03002126 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002127 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302128 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302129 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302130 .enable_preset_value = sdhci_enable_preset_value,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302131 .enable = sdhci_enable,
2132 .disable = sdhci_disable,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002133};
2134
2135/*****************************************************************************\
2136 * *
2137 * Tasklets *
2138 * *
2139\*****************************************************************************/
2140
2141static void sdhci_tasklet_card(unsigned long param)
2142{
2143 struct sdhci_host *host;
2144 unsigned long flags;
2145
2146 host = (struct sdhci_host*)param;
2147
2148 spin_lock_irqsave(&host->lock, flags);
2149
Adrian Hunter50accb92011-10-03 15:33:34 +03002150 /* Check host->mrq first in case we are runtime suspended */
2151 if (host->mrq &&
2152 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302153 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002154 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05302155 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002156 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002157
Adrian Hunter50accb92011-10-03 15:33:34 +03002158 sdhci_reset(host, SDHCI_RESET_CMD);
2159 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002160
Adrian Hunter50accb92011-10-03 15:33:34 +03002161 host->mrq->cmd->error = -ENOMEDIUM;
2162 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002163 }
2164
2165 spin_unlock_irqrestore(&host->lock, flags);
2166
Pierre Ossman04cf5852008-08-18 22:18:14 +02002167 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002168}
2169
2170static void sdhci_tasklet_finish(unsigned long param)
2171{
2172 struct sdhci_host *host;
2173 unsigned long flags;
2174 struct mmc_request *mrq;
2175
2176 host = (struct sdhci_host*)param;
2177
Adrian Hunter50accb92011-10-03 15:33:34 +03002178 spin_lock_irqsave(&host->lock, flags);
2179
Chris Ball0c9c99a2011-04-27 17:35:31 -04002180 /*
2181 * If this tasklet gets rescheduled while running, it will
2182 * be run again afterwards but without any active request.
2183 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002184 if (!host->mrq) {
2185 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002186 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002187 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002188
2189 del_timer(&host->timer);
2190
2191 mrq = host->mrq;
2192
Pierre Ossmand129bce2006-03-24 03:18:17 -08002193 /*
2194 * The controller needs a reset of internal state machines
2195 * upon error conditions.
2196 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002197 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002198 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002199 (mrq->data && (mrq->data->error ||
2200 (mrq->data->stop && mrq->data->stop->error))) ||
2201 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002202
2203 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002204 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002205 unsigned int clock;
2206
2207 /* This is to force an update */
2208 clock = host->clock;
2209 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302210 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002211 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302212 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002213 }
2214
2215 /* Spec says we should do both at the same time, but Ricoh
2216 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002217 sdhci_reset(host, SDHCI_RESET_CMD);
2218 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002219 } else {
2220 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2221 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002222 }
2223
2224 host->mrq = NULL;
2225 host->cmd = NULL;
2226 host->data = NULL;
2227
Pierre Ossmanf9134312008-12-21 17:01:48 +01002228#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002229 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002230#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002231
Pierre Ossman5f25a662006-10-04 02:15:39 -07002232 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002233 spin_unlock_irqrestore(&host->lock, flags);
2234
2235 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002236 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002237}
2238
2239static void sdhci_timeout_timer(unsigned long data)
2240{
2241 struct sdhci_host *host;
2242 unsigned long flags;
2243
2244 host = (struct sdhci_host*)data;
2245
2246 spin_lock_irqsave(&host->lock, flags);
2247
2248 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302249 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002250 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002251 sdhci_dumpregs(host);
2252
2253 if (host->data) {
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302254 pr_info("%s: bytes to transfer: %d transferred: %d\n",
2255 mmc_hostname(host->mmc),
2256 (host->data->blksz * host->data->blocks),
2257 (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) *
2258 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossman17b04292007-07-22 22:18:46 +02002259 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002260 sdhci_finish_data(host);
2261 } else {
2262 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002263 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002264 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002265 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002266
2267 tasklet_schedule(&host->finish_tasklet);
2268 }
2269 }
2270
Pierre Ossman5f25a662006-10-04 02:15:39 -07002271 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002272 spin_unlock_irqrestore(&host->lock, flags);
2273}
2274
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302275static void sdhci_tuning_timer(unsigned long data)
2276{
2277 struct sdhci_host *host;
2278 unsigned long flags;
2279
2280 host = (struct sdhci_host *)data;
2281
2282 spin_lock_irqsave(&host->lock, flags);
2283
2284 host->flags |= SDHCI_NEEDS_RETUNING;
2285
2286 spin_unlock_irqrestore(&host->lock, flags);
2287}
2288
Pierre Ossmand129bce2006-03-24 03:18:17 -08002289/*****************************************************************************\
2290 * *
2291 * Interrupt handling *
2292 * *
2293\*****************************************************************************/
2294
2295static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2296{
2297 BUG_ON(intmask == 0);
2298
2299 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302300 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002301 "though no command operation was in progress.\n",
2302 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002303 sdhci_dumpregs(host);
2304 return;
2305 }
2306
Pierre Ossman43b58b32007-07-25 23:15:27 +02002307 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002308 host->cmd->error = -ETIMEDOUT;
2309 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2310 SDHCI_INT_INDEX))
2311 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002312
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302313 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2314 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2315 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2316 if (intmask & SDHCI_INT_CRC) {
2317 sdhci_reset(host, SDHCI_RESET_CMD);
2318 host->cmd->error = 0;
2319 }
2320 }
2321 }
2322
Pierre Ossmane8095172008-07-25 01:09:08 +02002323 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002324 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002325 return;
2326 }
2327
2328 /*
2329 * The host can send and interrupt when the busy state has
2330 * ended, allowing us to wait without wasting CPU cycles.
2331 * Unfortunately this is overloaded on the "data complete"
2332 * interrupt, so we need to take some care when handling
2333 * it.
2334 *
2335 * Note: The 1.0 specification is a bit ambiguous about this
2336 * feature so there might be some problems with older
2337 * controllers.
2338 */
2339 if (host->cmd->flags & MMC_RSP_BUSY) {
2340 if (host->cmd->data)
2341 DBG("Cannot wait for busy signal when also "
2342 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002343 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002344 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002345
2346 /* The controller does not support the end-of-busy IRQ,
2347 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002348 }
2349
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302350 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2351 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2352 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2353 if (intmask & SDHCI_INT_CRC) {
2354 sdhci_finish_command(host);
2355 return;
2356 }
2357 }
2358 }
2359
Pierre Ossmane8095172008-07-25 01:09:08 +02002360 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002361 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002362}
2363
George G. Davis0957c332010-02-18 12:32:12 -05002364#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002365static void sdhci_show_adma_error(struct sdhci_host *host)
2366{
2367 const char *name = mmc_hostname(host->mmc);
2368 u8 *desc = host->adma_desc;
2369 __le32 *dma;
2370 __le16 *len;
2371 u8 attr;
2372
2373 sdhci_dumpregs(host);
2374
2375 while (true) {
2376 dma = (__le32 *)(desc + 4);
2377 len = (__le16 *)(desc + 2);
2378 attr = *desc;
2379
2380 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2381 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2382
2383 desc += 8;
2384
2385 if (attr & 2)
2386 break;
2387 }
2388}
2389#else
2390static void sdhci_show_adma_error(struct sdhci_host *host) { }
2391#endif
2392
Pierre Ossmand129bce2006-03-24 03:18:17 -08002393static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2394{
Girish K S2cd06dc2012-01-06 09:56:39 +05302395 u32 command;
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302396 bool pr_msg = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002397 BUG_ON(intmask == 0);
2398
Arindam Nathb513ea22011-05-05 12:19:04 +05302399 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2400 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302401 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2402 if (command == MMC_SEND_TUNING_BLOCK ||
2403 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302404 host->tuning_done = 1;
2405 wake_up(&host->buf_ready_int);
2406 return;
2407 }
2408 }
2409
Pierre Ossmand129bce2006-03-24 03:18:17 -08002410 if (!host->data) {
2411 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002412 * The "data complete" interrupt is also used to
2413 * indicate that a busy state has ended. See comment
2414 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002415 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002416 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2417 if (intmask & SDHCI_INT_DATA_END) {
2418 sdhci_finish_command(host);
2419 return;
2420 }
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302421 if (host->quirks2 &
2422 SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD)
2423 return;
Pierre Ossmane8095172008-07-25 01:09:08 +02002424 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002425
Sahitya Tummalaca422112013-02-22 12:15:54 +05302426 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002427 "though no data operation was in progress.\n",
2428 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002429 sdhci_dumpregs(host);
2430
2431 return;
2432 }
2433
2434 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002435 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002436 else if (intmask & SDHCI_INT_DATA_END_BIT)
2437 host->data->error = -EILSEQ;
2438 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2439 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2440 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002441 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002442 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302443 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002444 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002445 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002446 }
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302447 if (host->data->error) {
2448 if ((intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT)) &&
2449 (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING)) {
2450 command = SDHCI_GET_CMD(sdhci_readw(host,
2451 SDHCI_COMMAND));
2452 if ((command != MMC_SEND_TUNING_BLOCK_HS200) &&
2453 (command != MMC_SEND_TUNING_BLOCK))
2454 pr_msg = true;
2455 } else {
2456 pr_msg = true;
2457 }
2458 if (pr_msg) {
2459 pr_err("%s: data txfr (0x%08x) error: %d\n",
2460 mmc_hostname(host->mmc), intmask,
2461 host->data->error);
2462 sdhci_dumpregs(host);
2463 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002464 sdhci_finish_data(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302465 } else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002466 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002467 sdhci_transfer_pio(host);
2468
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002469 /*
2470 * We currently don't do anything fancy with DMA
2471 * boundaries, but as we can't disable the feature
2472 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002473 *
2474 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2475 * should return a valid address to continue from, but as
2476 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002477 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002478 if (intmask & SDHCI_INT_DMA_END) {
2479 u32 dmastart, dmanow;
2480 dmastart = sg_dma_address(host->data->sg);
2481 dmanow = dmastart + host->data->bytes_xfered;
2482 /*
2483 * Force update to the next DMA block boundary.
2484 */
2485 dmanow = (dmanow &
2486 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2487 SDHCI_DEFAULT_BOUNDARY_SIZE;
2488 host->data->bytes_xfered = dmanow - dmastart;
2489 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2490 " next 0x%08x\n",
2491 mmc_hostname(host->mmc), dmastart,
2492 host->data->bytes_xfered, dmanow);
2493 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2494 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002495
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002496 if (intmask & SDHCI_INT_DATA_END) {
2497 if (host->cmd) {
2498 /*
2499 * Data managed to finish before the
2500 * command completed. Make sure we do
2501 * things in the proper order.
2502 */
2503 host->data_early = 1;
2504 } else {
2505 sdhci_finish_data(host);
2506 }
2507 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002508 }
2509}
2510
David Howells7d12e782006-10-05 14:55:46 +01002511static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002512{
2513 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002514 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002515 u32 intmask, unexpected = 0;
2516 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002517
2518 spin_lock(&host->lock);
2519
Adrian Hunter50accb92011-10-03 15:33:34 +03002520 if (host->runtime_suspended) {
2521 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302522 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002523 mmc_hostname(host->mmc));
2524 return IRQ_HANDLED;
2525 }
2526
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002527 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002528
Mark Lord62df67a2007-03-06 13:30:13 +01002529 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002530 result = IRQ_NONE;
2531 goto out;
2532 }
2533
Alexander Stein6379b232012-03-14 09:52:10 +01002534again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002535 DBG("*** %s got interrupt: 0x%08x\n",
2536 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002537
Pierre Ossman3192a282006-06-30 02:22:26 -07002538 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302539 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2540 SDHCI_CARD_PRESENT;
2541
2542 /*
2543 * There is a observation on i.mx esdhc. INSERT bit will be
2544 * immediately set again when it gets cleared, if a card is
2545 * inserted. We have to mask the irq to prevent interrupt
2546 * storm which will freeze the system. And the REMOVE gets
2547 * the same situation.
2548 *
2549 * More testing are needed here to ensure it works for other
2550 * platforms though.
2551 */
2552 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2553 SDHCI_INT_CARD_REMOVE);
2554 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2555 SDHCI_INT_CARD_INSERT);
2556
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002557 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302558 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2559 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002560 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002561 }
2562
Pierre Ossmand129bce2006-03-24 03:18:17 -08002563 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002564 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2565 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002566 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2567 (host->clock <= 400000))
2568 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002569 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002570 }
2571
2572 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002573 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2574 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002575 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2576 (host->clock <= 400000))
2577 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002578 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002579 }
2580
2581 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2582
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002583 intmask &= ~SDHCI_INT_ERROR;
2584
Pierre Ossmand129bce2006-03-24 03:18:17 -08002585 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302586 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002587 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002588 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002589 }
2590
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002591 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002592
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002593 if (intmask & SDHCI_INT_CARD_INT)
2594 cardint = 1;
2595
2596 intmask &= ~SDHCI_INT_CARD_INT;
2597
Pierre Ossman3192a282006-06-30 02:22:26 -07002598 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002599 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002600 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002601 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002602
2603 result = IRQ_HANDLED;
2604
Alexander Stein6379b232012-03-14 09:52:10 +01002605 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2606 if (intmask && --max_loops)
2607 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002608out:
2609 spin_unlock(&host->lock);
2610
Alexander Stein6379b232012-03-14 09:52:10 +01002611 if (unexpected) {
2612 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2613 mmc_hostname(host->mmc), unexpected);
2614 sdhci_dumpregs(host);
2615 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002616 /*
2617 * We have to delay this as it calls back into the driver.
2618 */
2619 if (cardint)
2620 mmc_signal_sdio_irq(host->mmc);
2621
Pierre Ossmand129bce2006-03-24 03:18:17 -08002622 return result;
2623}
2624
2625/*****************************************************************************\
2626 * *
2627 * Suspend/resume *
2628 * *
2629\*****************************************************************************/
2630
2631#ifdef CONFIG_PM
2632
Manuel Laussd72faa62011-11-03 11:09:45 +01002633int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002634{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002635 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302636 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002637
Chris Balla1b13b42012-02-06 00:43:59 -05002638 if (host->ops->platform_suspend)
2639 host->ops->platform_suspend(host);
2640
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002641 sdhci_disable_card_detection(host);
2642
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302643 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302644 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2645 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2646 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002647 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302648 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302649 }
2650
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002651 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302652 if (ret) {
2653 if (has_tuning_timer) {
2654 host->flags |= SDHCI_NEEDS_RETUNING;
2655 mod_timer(&host->tuning_timer, jiffies +
2656 host->tuning_count * HZ);
2657 }
2658
2659 sdhci_enable_card_detection(host);
2660
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002661 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302662 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002663
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002664 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002665
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002666 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002667}
2668
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002669EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002670
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002671int sdhci_resume_host(struct sdhci_host *host)
2672{
2673 int ret;
2674
Richard Röjforsa13abc72009-09-22 16:45:30 -07002675 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002676 if (host->ops->enable_dma)
2677 host->ops->enable_dma(host);
2678 }
2679
2680 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2681 mmc_hostname(host->mmc), host);
2682 if (ret)
2683 return ret;
2684
Adrian Hunter6308d292012-02-07 14:48:54 +02002685 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2686 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2687 /* Card keeps power but host controller does not */
2688 sdhci_init(host, 0);
2689 host->pwr = 0;
2690 host->clock = 0;
2691 sdhci_do_set_ios(host, &host->mmc->ios);
2692 } else {
2693 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2694 mmiowb();
2695 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002696
2697 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002698 sdhci_enable_card_detection(host);
2699
Chris Balla1b13b42012-02-06 00:43:59 -05002700 if (host->ops->platform_resume)
2701 host->ops->platform_resume(host);
2702
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302703 /* Set the re-tuning expiration flag */
2704 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2705 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2706 host->flags |= SDHCI_NEEDS_RETUNING;
2707
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002708 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002709}
2710
2711EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002712
Daniel Drake5f619702010-11-04 22:20:39 +00002713void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2714{
2715 u8 val;
2716 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2717 val |= SDHCI_WAKE_ON_INT;
2718 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2719}
2720
2721EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2722
Pierre Ossmand129bce2006-03-24 03:18:17 -08002723#endif /* CONFIG_PM */
2724
Adrian Hunter50accb92011-10-03 15:33:34 +03002725#ifdef CONFIG_PM_RUNTIME
2726
2727static int sdhci_runtime_pm_get(struct sdhci_host *host)
2728{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302729 if (!mmc_use_core_runtime_pm(host->mmc))
2730 return pm_runtime_get_sync(host->mmc->parent);
2731 else
2732 return 0;
Adrian Hunter50accb92011-10-03 15:33:34 +03002733}
2734
2735static int sdhci_runtime_pm_put(struct sdhci_host *host)
2736{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302737 if (!mmc_use_core_runtime_pm(host->mmc)) {
2738 pm_runtime_mark_last_busy(host->mmc->parent);
2739 return pm_runtime_put_autosuspend(host->mmc->parent);
2740 } else {
2741 return 0;
2742 }
Adrian Hunter50accb92011-10-03 15:33:34 +03002743}
2744
2745int sdhci_runtime_suspend_host(struct sdhci_host *host)
2746{
2747 unsigned long flags;
2748 int ret = 0;
2749
2750 /* Disable tuning since we are suspending */
2751 if (host->version >= SDHCI_SPEC_300 &&
2752 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2753 del_timer_sync(&host->tuning_timer);
2754 host->flags &= ~SDHCI_NEEDS_RETUNING;
2755 }
2756
2757 spin_lock_irqsave(&host->lock, flags);
2758 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2759 spin_unlock_irqrestore(&host->lock, flags);
2760
2761 synchronize_irq(host->irq);
2762
2763 spin_lock_irqsave(&host->lock, flags);
2764 host->runtime_suspended = true;
2765 spin_unlock_irqrestore(&host->lock, flags);
2766
2767 return ret;
2768}
2769EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2770
2771int sdhci_runtime_resume_host(struct sdhci_host *host)
2772{
2773 unsigned long flags;
2774 int ret = 0, host_flags = host->flags;
2775
2776 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2777 if (host->ops->enable_dma)
2778 host->ops->enable_dma(host);
2779 }
2780
2781 sdhci_init(host, 0);
2782
2783 /* Force clock and power re-program */
2784 host->pwr = 0;
2785 host->clock = 0;
2786 sdhci_do_set_ios(host, &host->mmc->ios);
2787
2788 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2789 if (host_flags & SDHCI_PV_ENABLED)
2790 sdhci_do_enable_preset_value(host, true);
2791
2792 /* Set the re-tuning expiration flag */
2793 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2794 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2795 host->flags |= SDHCI_NEEDS_RETUNING;
2796
2797 spin_lock_irqsave(&host->lock, flags);
2798
2799 host->runtime_suspended = false;
2800
2801 /* Enable SDIO IRQ */
2802 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2803 sdhci_enable_sdio_irq_nolock(host, true);
2804
2805 /* Enable Card Detection */
2806 sdhci_enable_card_detection(host);
2807
2808 spin_unlock_irqrestore(&host->lock, flags);
2809
2810 return ret;
2811}
2812EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2813
2814#endif
2815
Pierre Ossmand129bce2006-03-24 03:18:17 -08002816/*****************************************************************************\
2817 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002818 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002819 * *
2820\*****************************************************************************/
2821
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002822struct sdhci_host *sdhci_alloc_host(struct device *dev,
2823 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002824{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002825 struct mmc_host *mmc;
2826 struct sdhci_host *host;
2827
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002828 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002829
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002830 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002831 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002832 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002833
2834 host = mmc_priv(mmc);
2835 host->mmc = mmc;
2836
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002837 return host;
2838}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002839
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002840EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002841
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002842int sdhci_add_host(struct sdhci_host *host)
2843{
2844 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302845 u32 caps[2];
2846 u32 max_current_caps;
2847 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002848 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002849
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002850 WARN_ON(host == NULL);
2851 if (host == NULL)
2852 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002853
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002854 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002855
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002856 if (debug_quirks)
2857 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03002858 if (debug_quirks2)
2859 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002860
Pierre Ossmand96649e2006-06-30 02:22:30 -07002861 sdhci_reset(host, SDHCI_RESET_ALL);
2862
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002863 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002864 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2865 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002866 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302867 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002868 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002869 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002870 }
2871
Arindam Nathf2119df2011-05-05 12:18:57 +05302872 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002873 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002874
Arindam Nathf2119df2011-05-05 12:18:57 +05302875 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2876 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2877
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002878 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002879 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302880 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002881 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002882 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002883 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002884
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002885 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002886 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002887 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002888 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002889 }
2890
Arindam Nathf2119df2011-05-05 12:18:57 +05302891 if ((host->version >= SDHCI_SPEC_200) &&
2892 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002893 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002894
2895 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2896 (host->flags & SDHCI_USE_ADMA)) {
2897 DBG("Disabling ADMA as it is marked broken\n");
2898 host->flags &= ~SDHCI_USE_ADMA;
2899 }
2900
Richard Röjforsa13abc72009-09-22 16:45:30 -07002901 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002902 if (host->ops->enable_dma) {
2903 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302904 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002905 "available. Falling back to PIO.\n",
2906 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002907 host->flags &=
2908 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002909 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002910 }
2911 }
2912
Pierre Ossman2134a922008-06-28 18:28:51 +02002913 if (host->flags & SDHCI_USE_ADMA) {
2914 /*
2915 * We need to allocate descriptors for all sg entries
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302916 * (128/max_segments) and potentially one alignment transfer for
Pierre Ossman2134a922008-06-28 18:28:51 +02002917 * each of those entries.
2918 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302919 if (host->ops->get_max_segments)
2920 host->adma_max_desc = host->ops->get_max_segments();
2921 else
2922 host->adma_max_desc = 128;
2923
2924 host->adma_desc_sz = (host->adma_max_desc * 2 + 1) * 4;
2925 host->align_buf_sz = host->adma_max_desc * 4;
2926
2927 pr_debug("%s: %s: dma_desc_size: %d\n",
2928 mmc_hostname(host->mmc), __func__, host->adma_desc_sz);
2929 host->adma_desc = kmalloc(host->adma_desc_sz,
2930 GFP_KERNEL);
2931 host->align_buffer = kmalloc(host->align_buf_sz,
2932 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02002933 if (!host->adma_desc || !host->align_buffer) {
2934 kfree(host->adma_desc);
2935 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302936 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002937 "buffers. Falling back to standard DMA.\n",
2938 mmc_hostname(mmc));
2939 host->flags &= ~SDHCI_USE_ADMA;
2940 }
2941 }
2942
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002943 host->next_data.cookie = 1;
2944
Pierre Ossman76591502008-07-21 00:32:11 +02002945 /*
2946 * If we use DMA, then it's up to the caller to set the DMA
2947 * mask, but PIO does not need the hw shim so we set a new
2948 * mask here in that case.
2949 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002950 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002951 host->dma_mask = DMA_BIT_MASK(64);
2952 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2953 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002954
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002955 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302956 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002957 >> SDHCI_CLOCK_BASE_SHIFT;
2958 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302959 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002960 >> SDHCI_CLOCK_BASE_SHIFT;
2961
Pierre Ossmand129bce2006-03-24 03:18:17 -08002962 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002963 if (host->max_clk == 0 || host->quirks &
2964 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002965 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302966 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002967 "frequency.\n", mmc_hostname(mmc));
2968 return -ENODEV;
2969 }
2970 host->max_clk = host->ops->get_max_clock(host);
2971 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002972
2973 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302974 * In case of Host Controller v3.00, find out whether clock
2975 * multiplier is supported.
2976 */
2977 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2978 SDHCI_CLOCK_MUL_SHIFT;
2979
2980 /*
2981 * In case the value in Clock Multiplier is 0, then programmable
2982 * clock mode is not supported, otherwise the actual clock
2983 * multiplier is one more than the value of Clock Multiplier
2984 * in the Capabilities Register.
2985 */
2986 if (host->clk_mul)
2987 host->clk_mul += 1;
2988
2989 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002990 * Set host parameters.
2991 */
2992 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302993 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002994 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002995 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302996 else if (host->version >= SDHCI_SPEC_300) {
2997 if (host->clk_mul) {
2998 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2999 mmc->f_max = host->max_clk * host->clk_mul;
3000 } else
3001 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3002 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003003 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003004
Sahitya Tummalaca422112013-02-22 12:15:54 +05303005 host->timeout_clk =
3006 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
3007 if (host->timeout_clk == 0) {
3008 if (host->ops->get_timeout_clock) {
3009 host->timeout_clk = host->ops->get_timeout_clock(host);
3010 } else if (!(host->quirks &
3011 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3012 pr_err("%s: Hardware doesn't specify timeout clock "
3013 "frequency.\n", mmc_hostname(mmc));
3014 return -ENODEV;
3015 }
3016 }
3017 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3018 host->timeout_clk *= 1000;
3019
3020 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
3021 host->timeout_clk = mmc->f_max / 1000;
3022
Krishna Kondaa20d3362013-04-01 21:01:59 -07003023 if (!(host->quirks2 & SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE))
3024 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Sahitya Tummalaca422112013-02-22 12:15:54 +05303025
Andrei Warkentine89d4562011-05-23 15:06:37 -05003026 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
3027
3028 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3029 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003030
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003031 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003032 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003033 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003034 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003035 host->flags |= SDHCI_AUTO_CMD23;
3036 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3037 } else {
3038 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3039 }
3040
Philip Rakity15ec4462010-11-19 16:48:39 -05003041 /*
3042 * A controller may support 8-bit width, but the board itself
3043 * might not have the pins brought out. Boards that support
3044 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3045 * their platform code before calling sdhci_add_host(), and we
3046 * won't assume 8-bit width for hosts without that CAP.
3047 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003048 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003049 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003050
Arindam Nathf2119df2011-05-05 12:18:57 +05303051 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003052 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003053
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003054 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3055 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003056 mmc->caps |= MMC_CAP_NEEDS_POLL;
3057
Al Cooper4188bba2012-03-16 15:54:17 -04003058 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3059 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3060 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303061 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3062
3063 /* SDR104 supports also implies SDR50 support */
3064 if (caps[1] & SDHCI_SUPPORT_SDR104)
3065 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3066 else if (caps[1] & SDHCI_SUPPORT_SDR50)
3067 mmc->caps |= MMC_CAP_UHS_SDR50;
3068
3069 if (caps[1] & SDHCI_SUPPORT_DDR50)
3070 mmc->caps |= MMC_CAP_UHS_DDR50;
3071
Girish K S2cd06dc2012-01-06 09:56:39 +05303072 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303073 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3074 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3075
Girish K S2cd06dc2012-01-06 09:56:39 +05303076 /* Does the host need tuning for HS200? */
3077 if (mmc->caps2 & MMC_CAP2_HS200)
3078 host->flags |= SDHCI_HS200_NEEDS_TUNING;
3079
Arindam Nathd6d50a12011-05-05 12:18:59 +05303080 /* Driver Type(s) (A, C, D) supported by the host */
3081 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3082 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3083 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3084 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3085 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3086 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3087
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02003088 /* Initial value for re-tuning timer count */
3089 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3090 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3091
3092 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303093 * In case Re-tuning Timer is not disabled, the actual value of
3094 * re-tuning timer will be 2 ^ (n - 1).
3095 */
3096 if (host->tuning_count)
3097 host->tuning_count = 1 << (host->tuning_count - 1);
3098
3099 /* Re-tuning mode supported by the Host Controller */
3100 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3101 SDHCI_RETUNING_MODE_SHIFT;
3102
Takashi Iwai8f230f42010-12-08 10:04:30 +01003103 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05303104 /*
3105 * According to SD Host Controller spec v3.00, if the Host System
3106 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3107 * the value is meaningful only if Voltage Support in the Capabilities
3108 * register is set. The actual current value is 4 times the register
3109 * value.
3110 */
3111 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3112
3113 if (caps[0] & SDHCI_CAN_VDD_330) {
3114 int max_current_330;
3115
Takashi Iwai8f230f42010-12-08 10:04:30 +01003116 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303117
3118 max_current_330 = ((max_current_caps &
3119 SDHCI_MAX_CURRENT_330_MASK) >>
3120 SDHCI_MAX_CURRENT_330_SHIFT) *
3121 SDHCI_MAX_CURRENT_MULTIPLIER;
3122
3123 if (max_current_330 > 150)
3124 mmc->caps |= MMC_CAP_SET_XPC_330;
3125 }
3126 if (caps[0] & SDHCI_CAN_VDD_300) {
3127 int max_current_300;
3128
Takashi Iwai8f230f42010-12-08 10:04:30 +01003129 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303130
3131 max_current_300 = ((max_current_caps &
3132 SDHCI_MAX_CURRENT_300_MASK) >>
3133 SDHCI_MAX_CURRENT_300_SHIFT) *
3134 SDHCI_MAX_CURRENT_MULTIPLIER;
3135
3136 if (max_current_300 > 150)
3137 mmc->caps |= MMC_CAP_SET_XPC_300;
3138 }
3139 if (caps[0] & SDHCI_CAN_VDD_180) {
3140 int max_current_180;
3141
Takashi Iwai8f230f42010-12-08 10:04:30 +01003142 ocr_avail |= MMC_VDD_165_195;
3143
Arindam Nathf2119df2011-05-05 12:18:57 +05303144 max_current_180 = ((max_current_caps &
3145 SDHCI_MAX_CURRENT_180_MASK) >>
3146 SDHCI_MAX_CURRENT_180_SHIFT) *
3147 SDHCI_MAX_CURRENT_MULTIPLIER;
3148
3149 if (max_current_180 > 150)
3150 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05303151
3152 /* Maximum current capabilities of the host at 1.8V */
3153 if (max_current_180 >= 800)
3154 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
3155 else if (max_current_180 >= 600)
3156 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
3157 else if (max_current_180 >= 400)
3158 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
3159 else
3160 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05303161 }
3162
Takashi Iwai8f230f42010-12-08 10:04:30 +01003163 mmc->ocr_avail = ocr_avail;
3164 mmc->ocr_avail_sdio = ocr_avail;
3165 if (host->ocr_avail_sdio)
3166 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3167 mmc->ocr_avail_sd = ocr_avail;
3168 if (host->ocr_avail_sd)
3169 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3170 else /* normal SD controllers don't support 1.8V */
3171 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3172 mmc->ocr_avail_mmc = ocr_avail;
3173 if (host->ocr_avail_mmc)
3174 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003175
3176 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303177 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003178 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003179 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003180 }
3181
Pierre Ossmand129bce2006-03-24 03:18:17 -08003182 spin_lock_init(&host->lock);
3183
3184 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003185 * Maximum number of segments. Depends on if the hardware
3186 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003187 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003188 if (host->flags & SDHCI_USE_ADMA)
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303189 mmc->max_segs = host->adma_max_desc;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003190 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003191 mmc->max_segs = 1;
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303192 else/* PIO */
3193 mmc->max_segs = host->adma_max_desc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003194
3195 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003196 * Maximum number of sectors in one transfer. Limited by DMA boundary
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303197 * size (512KiB), unless specified by platform specific driver. Each
3198 * descriptor can transfer a maximum of 64KB.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003199 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303200 if (host->ops->get_max_segments)
3201 mmc->max_req_size = (host->adma_max_desc * 65536);
3202 else
3203 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003204
3205 /*
3206 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003207 * of bytes. When doing hardware scatter/gather, each entry cannot
3208 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003209 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003210 if (host->flags & SDHCI_USE_ADMA) {
3211 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3212 mmc->max_seg_size = 65535;
3213 else
3214 mmc->max_seg_size = 65536;
3215 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003216 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003217 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003218
3219 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003220 * Maximum block size. This varies from controller to controller and
3221 * is specified in the capabilities register.
3222 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003223 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3224 mmc->max_blk_size = 2;
3225 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303226 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003227 SDHCI_MAX_BLOCK_SHIFT;
3228 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303229 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003230 "assuming 512 bytes\n", mmc_hostname(mmc));
3231 mmc->max_blk_size = 0;
3232 }
3233 }
3234
3235 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003236
3237 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003238 * Maximum block count.
3239 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003240 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003241
3242 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003243 * Init tasklets.
3244 */
3245 tasklet_init(&host->card_tasklet,
3246 sdhci_tasklet_card, (unsigned long)host);
3247 tasklet_init(&host->finish_tasklet,
3248 sdhci_tasklet_finish, (unsigned long)host);
3249
Al Viroe4cad1b2006-10-10 22:47:07 +01003250 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003251
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303252 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303253 init_waitqueue_head(&host->buf_ready_int);
3254
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303255 /* Initialize re-tuning timer */
3256 init_timer(&host->tuning_timer);
3257 host->tuning_timer.data = (unsigned long)host;
3258 host->tuning_timer.function = sdhci_tuning_timer;
3259 }
3260
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003261 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003262 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003263 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003264 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003265
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003266 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3267 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303268 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003269 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003270 }
3271
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003272 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003273
3274#ifdef CONFIG_MMC_DEBUG
3275 sdhci_dumpregs(host);
3276#endif
3277
Pierre Ossmanf9134312008-12-21 17:01:48 +01003278#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003279 snprintf(host->led_name, sizeof(host->led_name),
3280 "%s::", mmc_hostname(mmc));
3281 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003282 host->led.brightness = LED_OFF;
3283 host->led.default_trigger = mmc_hostname(mmc);
3284 host->led.brightness_set = sdhci_led_control;
3285
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003286 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003287 if (ret)
3288 goto reset;
3289#endif
3290
Pierre Ossman5f25a662006-10-04 02:15:39 -07003291 mmiowb();
3292
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303293 if (host->cpu_dma_latency_us)
3294 pm_qos_add_request(&host->pm_qos_req_dma,
3295 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003296 mmc_add_host(mmc);
3297
Sahitya Tummalaca422112013-02-22 12:15:54 +05303298 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003299 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003300 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3301 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003302
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003303 sdhci_enable_card_detection(host);
3304
Pierre Ossmand129bce2006-03-24 03:18:17 -08003305 return 0;
3306
Pierre Ossmanf9134312008-12-21 17:01:48 +01003307#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003308reset:
3309 sdhci_reset(host, SDHCI_RESET_ALL);
3310 free_irq(host->irq, host);
3311#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003312untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003313 tasklet_kill(&host->card_tasklet);
3314 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003315
3316 return ret;
3317}
3318
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003319EXPORT_SYMBOL_GPL(sdhci_add_host);
3320
Pierre Ossman1e728592008-04-16 19:13:13 +02003321void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003322{
Pierre Ossman1e728592008-04-16 19:13:13 +02003323 unsigned long flags;
3324
3325 if (dead) {
3326 spin_lock_irqsave(&host->lock, flags);
3327
3328 host->flags |= SDHCI_DEVICE_DEAD;
3329
3330 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303331 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003332 " transfer!\n", mmc_hostname(host->mmc));
3333
3334 host->mrq->cmd->error = -ENOMEDIUM;
3335 tasklet_schedule(&host->finish_tasklet);
3336 }
3337
3338 spin_unlock_irqrestore(&host->lock, flags);
3339 }
3340
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003341 sdhci_disable_card_detection(host);
3342
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303343 if (host->cpu_dma_latency_us)
3344 pm_qos_remove_request(&host->pm_qos_req_dma);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003345 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003346
Pierre Ossmanf9134312008-12-21 17:01:48 +01003347#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003348 led_classdev_unregister(&host->led);
3349#endif
3350
Pierre Ossman1e728592008-04-16 19:13:13 +02003351 if (!dead)
3352 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003353
3354 free_irq(host->irq, host);
3355
3356 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303357 if (host->version >= SDHCI_SPEC_300)
3358 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003359
3360 tasklet_kill(&host->card_tasklet);
3361 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003362
Sahitya Tummalaca422112013-02-22 12:15:54 +05303363 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003364 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003365
Pierre Ossman2134a922008-06-28 18:28:51 +02003366 kfree(host->adma_desc);
3367 kfree(host->align_buffer);
3368
3369 host->adma_desc = NULL;
3370 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003371}
3372
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003373EXPORT_SYMBOL_GPL(sdhci_remove_host);
3374
3375void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003376{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003377 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003378}
3379
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003380EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003381
3382/*****************************************************************************\
3383 * *
3384 * Driver init/exit *
3385 * *
3386\*****************************************************************************/
3387
3388static int __init sdhci_drv_init(void)
3389{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303390 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003391 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303392 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003393
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003394 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003395}
3396
3397static void __exit sdhci_drv_exit(void)
3398{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003399}
3400
3401module_init(sdhci_drv_init);
3402module_exit(sdhci_drv_exit);
3403
Pierre Ossmandf673b22006-06-30 02:22:31 -07003404module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003405module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003406
Pierre Ossman32710e82009-04-08 20:14:54 +02003407MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003408MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003409MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003410
Pierre Ossmandf673b22006-06-30 02:22:31 -07003411MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003412MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");