blob: 654dbcb5be8f8222db930c2603fb122606edc11b [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);
Sahitya Tummala1b248c42013-05-24 14:08:10 +053054static bool sdhci_check_state(struct sdhci_host *);
Pierre Ossmand129bce2006-03-24 03:18:17 -080055
Adrian Hunter50accb92011-10-03 15:33:34 +030056#ifdef CONFIG_PM_RUNTIME
57static int sdhci_runtime_pm_get(struct sdhci_host *host);
58static int sdhci_runtime_pm_put(struct sdhci_host *host);
59#else
60static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
61{
62 return 0;
63}
64static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
65{
66 return 0;
67}
68#endif
69
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053070static void sdhci_dump_state(struct sdhci_host *host)
71{
72 struct mmc_host *mmc = host->mmc;
73
74 pr_info("%s: clk: %d clk-gated: %d claimer: %s pwr: %d\n",
75 mmc_hostname(mmc), host->clock, mmc->clk_gated,
76 mmc->claimer->comm, host->pwr);
77 pr_info("%s: rpmstatus[pltfm](runtime-suspend:usage_count:disable_depth)(%d:%d:%d)\n",
78 mmc_hostname(mmc), mmc->parent->power.runtime_status,
79 atomic_read(&mmc->parent->power.usage_count),
80 mmc->parent->power.disable_depth);
81}
82
Pierre Ossmand129bce2006-03-24 03:18:17 -080083static void sdhci_dumpregs(struct sdhci_host *host)
84{
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053085 pr_info(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070086 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080087
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053088 pr_info(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030089 sdhci_readl(host, SDHCI_DMA_ADDRESS),
90 sdhci_readw(host, SDHCI_HOST_VERSION));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053091 pr_info(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030092 sdhci_readw(host, SDHCI_BLOCK_SIZE),
93 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053094 pr_info(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030095 sdhci_readl(host, SDHCI_ARGUMENT),
96 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053097 pr_info(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030098 sdhci_readl(host, SDHCI_PRESENT_STATE),
99 sdhci_readb(host, SDHCI_HOST_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530100 pr_info(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300101 sdhci_readb(host, SDHCI_POWER_CONTROL),
102 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530103 pr_info(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300104 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
105 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530106 pr_info(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300107 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
108 sdhci_readl(host, SDHCI_INT_STATUS));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530109 pr_info(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300110 sdhci_readl(host, SDHCI_INT_ENABLE),
111 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530112 pr_info(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300113 sdhci_readw(host, SDHCI_ACMD12_ERR),
114 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530115 pr_info(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300116 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500117 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530118 pr_info(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500119 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300120 sdhci_readl(host, SDHCI_MAX_CURRENT));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530121 pr_info(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530122 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800123
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100124 if (host->flags & SDHCI_USE_ADMA)
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530125 pr_info(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100126 readl(host->ioaddr + SDHCI_ADMA_ERROR),
127 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
128
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530129 sdhci_dump_state(host);
130 pr_info(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800131}
132
133/*****************************************************************************\
134 * *
135 * Low level functions *
136 * *
137\*****************************************************************************/
138
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300139static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
140{
141 u32 ier;
142
143 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
144 ier &= ~clear;
145 ier |= set;
146 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
147 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
148}
149
150static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
151{
152 sdhci_clear_set_irqs(host, 0, irqs);
153}
154
155static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
156{
157 sdhci_clear_set_irqs(host, irqs, 0);
158}
159
160static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
161{
Sahitya Tummalaca422112013-02-22 12:15:54 +0530162 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300163
Adrian Hunterc79396c2011-12-27 15:48:42 +0200164 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100165 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300166 return;
167
Sahitya Tummalaca422112013-02-22 12:15:54 +0530168 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
169 SDHCI_CARD_PRESENT;
170 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
Adrian Hunter50accb92011-10-03 15:33:34 +0300171
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300172 if (enable)
173 sdhci_unmask_irqs(host, irqs);
174 else
175 sdhci_mask_irqs(host, irqs);
176}
177
178static void sdhci_enable_card_detection(struct sdhci_host *host)
179{
180 sdhci_set_card_detection(host, true);
181}
182
183static void sdhci_disable_card_detection(struct sdhci_host *host)
184{
185 sdhci_set_card_detection(host, false);
186}
187
Pierre Ossmand129bce2006-03-24 03:18:17 -0800188static void sdhci_reset(struct sdhci_host *host, u8 mask)
189{
Pierre Ossmane16514d2006-06-30 02:22:24 -0700190 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300191 u32 uninitialized_var(ier);
Pierre Ossmane16514d2006-06-30 02:22:24 -0700192
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100193 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300194 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700195 SDHCI_CARD_PRESENT))
196 return;
197 }
198
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300199 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
200 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
201
Philip Rakity393c1a32011-01-21 11:26:40 -0800202 if (host->ops->platform_reset_enter)
203 host->ops->platform_reset_enter(host, mask);
204
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300205 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800206
Pierre Ossmane16514d2006-06-30 02:22:24 -0700207 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800208 host->clock = 0;
209
Pierre Ossmane16514d2006-06-30 02:22:24 -0700210 /* Wait max 100 ms */
211 timeout = 100;
212
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530213 if (host->ops->check_power_status && host->pwr &&
214 (mask & SDHCI_RESET_ALL))
Sahitya Tummala179e7382013-03-20 19:24:01 +0530215 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530216
Pierre Ossmane16514d2006-06-30 02:22:24 -0700217 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300218 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d2006-06-30 02:22:24 -0700219 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +0530220 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d2006-06-30 02:22:24 -0700221 mmc_hostname(host->mmc), (int)mask);
222 sdhci_dumpregs(host);
223 return;
224 }
225 timeout--;
226 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800227 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300228
Philip Rakity393c1a32011-01-21 11:26:40 -0800229 if (host->ops->platform_reset_exit)
230 host->ops->platform_reset_exit(host, mask);
231
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300232 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
233 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Sahitya Tummalaca422112013-02-22 12:15:54 +0530234
235 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
236 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
237 host->ops->enable_dma(host);
238 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800239}
240
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800241static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
242
243static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800244{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800245 if (soft)
246 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
247 else
248 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800249
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300250 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
251 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700252 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
253 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300254 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800255
256 if (soft) {
257 /* force clock reconfiguration */
258 host->clock = 0;
259 sdhci_set_ios(host->mmc, &host->mmc->ios);
260 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300261}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800262
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300263static void sdhci_reinit(struct sdhci_host *host)
264{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800265 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300266 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800267}
268
269static void sdhci_activate_led(struct sdhci_host *host)
270{
271 u8 ctrl;
272
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300273 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800274 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300275 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800276}
277
278static void sdhci_deactivate_led(struct sdhci_host *host)
279{
280 u8 ctrl;
281
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300282 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800283 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300284 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800285}
286
Pierre Ossmanf9134312008-12-21 17:01:48 +0100287#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100288static void sdhci_led_control(struct led_classdev *led,
289 enum led_brightness brightness)
290{
291 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
292 unsigned long flags;
293
294 spin_lock_irqsave(&host->lock, flags);
295
Sahitya Tummala1b248c42013-05-24 14:08:10 +0530296 if (host->runtime_suspended || sdhci_check_state(host))
Adrian Hunter50accb92011-10-03 15:33:34 +0300297 goto out;
298
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100299 if (brightness == LED_OFF)
300 sdhci_deactivate_led(host);
301 else
302 sdhci_activate_led(host);
Adrian Hunter50accb92011-10-03 15:33:34 +0300303out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100304 spin_unlock_irqrestore(&host->lock, flags);
305}
306#endif
307
Pierre Ossmand129bce2006-03-24 03:18:17 -0800308/*****************************************************************************\
309 * *
310 * Core functions *
311 * *
312\*****************************************************************************/
313
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100314static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800315{
Pierre Ossman76591502008-07-21 00:32:11 +0200316 unsigned long flags;
317 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700318 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200319 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800320
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100321 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800322
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100323 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200324 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800325
Pierre Ossman76591502008-07-21 00:32:11 +0200326 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800327
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100328 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200329 if (!sg_miter_next(&host->sg_miter))
330 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800331
Pierre Ossman76591502008-07-21 00:32:11 +0200332 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800333
Pierre Ossman76591502008-07-21 00:32:11 +0200334 blksize -= len;
335 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200336
Pierre Ossman76591502008-07-21 00:32:11 +0200337 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800338
Pierre Ossman76591502008-07-21 00:32:11 +0200339 while (len) {
340 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300341 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200342 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800343 }
Pierre Ossman76591502008-07-21 00:32:11 +0200344
345 *buf = scratch & 0xFF;
346
347 buf++;
348 scratch >>= 8;
349 chunk--;
350 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800351 }
352 }
Pierre Ossman76591502008-07-21 00:32:11 +0200353
354 sg_miter_stop(&host->sg_miter);
355
356 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100357}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800358
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100359static void sdhci_write_block_pio(struct sdhci_host *host)
360{
Pierre Ossman76591502008-07-21 00:32:11 +0200361 unsigned long flags;
362 size_t blksize, len, chunk;
363 u32 scratch;
364 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100365
366 DBG("PIO writing\n");
367
368 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200369 chunk = 0;
370 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100371
Pierre Ossman76591502008-07-21 00:32:11 +0200372 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100373
374 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200375 if (!sg_miter_next(&host->sg_miter))
376 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100377
Pierre Ossman76591502008-07-21 00:32:11 +0200378 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200379
Pierre Ossman76591502008-07-21 00:32:11 +0200380 blksize -= len;
381 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100382
Pierre Ossman76591502008-07-21 00:32:11 +0200383 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100384
Pierre Ossman76591502008-07-21 00:32:11 +0200385 while (len) {
386 scratch |= (u32)*buf << (chunk * 8);
387
388 buf++;
389 chunk++;
390 len--;
391
392 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300393 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200394 chunk = 0;
395 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100396 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100397 }
398 }
Pierre Ossman76591502008-07-21 00:32:11 +0200399
400 sg_miter_stop(&host->sg_miter);
401
402 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100403}
404
405static void sdhci_transfer_pio(struct sdhci_host *host)
406{
407 u32 mask;
408
409 BUG_ON(!host->data);
410
Pierre Ossman76591502008-07-21 00:32:11 +0200411 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100412 return;
413
414 if (host->data->flags & MMC_DATA_READ)
415 mask = SDHCI_DATA_AVAILABLE;
416 else
417 mask = SDHCI_SPACE_AVAILABLE;
418
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200419 /*
420 * Some controllers (JMicron JMB38x) mess up the buffer bits
421 * for transfers < 4 bytes. As long as it is just one block,
422 * we can ignore the bits.
423 */
424 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
425 (host->data->blocks == 1))
426 mask = ~0;
427
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300428 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300429 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
430 udelay(100);
431
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100432 if (host->data->flags & MMC_DATA_READ)
433 sdhci_read_block_pio(host);
434 else
435 sdhci_write_block_pio(host);
436
Pierre Ossman76591502008-07-21 00:32:11 +0200437 host->blocks--;
438 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100439 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100440 }
441
442 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800443}
444
Pierre Ossman2134a922008-06-28 18:28:51 +0200445static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
446{
447 local_irq_save(*flags);
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800448 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200449}
450
451static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
452{
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800453 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200454 local_irq_restore(*flags);
455}
456
Ben Dooks118cd172010-03-05 13:43:26 -0800457static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
458{
Ben Dooks9e506f32010-03-05 13:43:29 -0800459 __le32 *dataddr = (__le32 __force *)(desc + 4);
460 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800461
Ben Dooks9e506f32010-03-05 13:43:29 -0800462 /* SDHCI specification says ADMA descriptors should be 4 byte
463 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800464
Ben Dooks9e506f32010-03-05 13:43:29 -0800465 cmdlen[0] = cpu_to_le16(cmd);
466 cmdlen[1] = cpu_to_le16(len);
467
468 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800469}
470
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800471static int sdhci_pre_dma_transfer(struct sdhci_host *host,
472 struct mmc_data *data,
473 struct sdhci_next *next)
474{
475 int sg_count;
476
477 if (!next && data->host_cookie &&
478 data->host_cookie != host->next_data.cookie) {
479 printk(KERN_WARNING "[%s] invalid cookie: data->host_cookie %d"
480 " host->next_data.cookie %d\n",
481 __func__, data->host_cookie, host->next_data.cookie);
482 data->host_cookie = 0;
483 }
484
485 /* Check if next job is already prepared */
486 if (next ||
487 (!next && data->host_cookie != host->next_data.cookie)) {
488 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg,
489 data->sg_len,
490 (data->flags & MMC_DATA_WRITE) ?
491 DMA_TO_DEVICE : DMA_FROM_DEVICE);
492 } else {
493 sg_count = host->next_data.sg_count;
494 host->next_data.sg_count = 0;
495 }
496
497 if (sg_count == 0)
498 return -EINVAL;
499
500 if (next) {
501 next->sg_count = sg_count;
502 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
503 } else
504 host->sg_count = sg_count;
505
506 return sg_count;
507}
508
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200509static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200510 struct mmc_data *data)
511{
512 int direction;
513
514 u8 *desc;
515 u8 *align;
516 dma_addr_t addr;
517 dma_addr_t align_addr;
518 int len, offset;
519
520 struct scatterlist *sg;
521 int i;
522 char *buffer;
523 unsigned long flags;
524
525 /*
526 * The spec does not specify endianness of descriptor table.
527 * We currently guess that it is LE.
528 */
529
530 if (data->flags & MMC_DATA_READ)
531 direction = DMA_FROM_DEVICE;
532 else
533 direction = DMA_TO_DEVICE;
534
535 /*
536 * The ADMA descriptor table is mapped further down as we
537 * need to fill it with data first.
538 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200539 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530540 host->align_buffer,
541 host->align_buf_sz,
542 direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700543 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200544 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200545 BUG_ON(host->align_addr & 0x3);
546
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800547 host->sg_count = sdhci_pre_dma_transfer(host, data, NULL);
548 if (host->sg_count < 0)
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200549 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200550
551 desc = host->adma_desc;
552 align = host->align_buffer;
553
554 align_addr = host->align_addr;
555
556 for_each_sg(data->sg, sg, host->sg_count, i) {
557 addr = sg_dma_address(sg);
558 len = sg_dma_len(sg);
559
560 /*
561 * The SDHCI specification states that ADMA
562 * addresses must be 32-bit aligned. If they
563 * aren't, then we use a bounce buffer for
564 * the (up to three) bytes that screw up the
565 * alignment.
566 */
567 offset = (4 - (addr & 0x3)) & 0x3;
568 if (offset) {
569 if (data->flags & MMC_DATA_WRITE) {
570 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200571 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200572 memcpy(align, buffer, offset);
573 sdhci_kunmap_atomic(buffer, &flags);
574 }
575
Ben Dooks118cd172010-03-05 13:43:26 -0800576 /* tran, valid */
577 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200578
579 BUG_ON(offset > 65536);
580
Pierre Ossman2134a922008-06-28 18:28:51 +0200581 align += 4;
582 align_addr += 4;
583
584 desc += 8;
585
586 addr += offset;
587 len -= offset;
588 }
589
Pierre Ossman2134a922008-06-28 18:28:51 +0200590 BUG_ON(len > 65536);
591
Ben Dooks118cd172010-03-05 13:43:26 -0800592 /* tran, valid */
593 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200594 desc += 8;
595
596 /*
597 * If this triggers then we have a calculation bug
598 * somewhere. :/
599 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530600 WARN_ON((desc - host->adma_desc) > host->adma_desc_sz);
601
Pierre Ossman2134a922008-06-28 18:28:51 +0200602 }
603
Thomas Abraham70764a92010-05-26 14:42:04 -0700604 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
605 /*
606 * Mark the last descriptor as the terminating descriptor
607 */
608 if (desc != host->adma_desc) {
609 desc -= 8;
610 desc[0] |= 0x2; /* end */
611 }
612 } else {
613 /*
614 * Add a terminating entry.
615 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200616
Thomas Abraham70764a92010-05-26 14:42:04 -0700617 /* nop, end, valid */
618 sdhci_set_adma_desc(desc, 0, 0, 0x3);
619 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200620
621 /*
622 * Resync align buffer as we might have changed it.
623 */
624 if (data->flags & MMC_DATA_WRITE) {
625 dma_sync_single_for_device(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530626 host->align_addr,
627 host->align_buf_sz,
628 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200629 }
630
631 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530632 host->adma_desc,
633 host->adma_desc_sz,
634 DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200635 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200636 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200637 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200638
639 return 0;
640
641unmap_entries:
642 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
643 data->sg_len, direction);
644unmap_align:
645 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530646 host->align_buf_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200647fail:
648 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200649}
650
651static void sdhci_adma_table_post(struct sdhci_host *host,
652 struct mmc_data *data)
653{
654 int direction;
655
656 struct scatterlist *sg;
657 int i, size;
658 u8 *align;
659 char *buffer;
660 unsigned long flags;
661
662 if (data->flags & MMC_DATA_READ)
663 direction = DMA_FROM_DEVICE;
664 else
665 direction = DMA_TO_DEVICE;
666
667 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530668 host->adma_desc_sz, DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200669
670 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530671 host->align_buf_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200672
673 if (data->flags & MMC_DATA_READ) {
674 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
675 data->sg_len, direction);
676
677 align = host->align_buffer;
678
679 for_each_sg(data->sg, sg, host->sg_count, i) {
680 if (sg_dma_address(sg) & 0x3) {
681 size = 4 - (sg_dma_address(sg) & 0x3);
682
683 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200684 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200685 memcpy(buffer, align, size);
686 sdhci_kunmap_atomic(buffer, &flags);
687
688 align += 4;
689 }
690 }
691 }
692
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800693 if (!data->host_cookie)
694 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
695 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200696}
697
Andrei Warkentina3c77782011-04-11 16:13:42 -0500698static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800699{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700700 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500701 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700702 unsigned target_timeout, current_timeout;
Sahitya Tummalaf667cc12013-06-10 16:32:51 +0530703 u32 curr_clk = 0; /* In KHz */
Pierre Ossmand129bce2006-03-24 03:18:17 -0800704
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200705 /*
706 * If the host controller provides us with an incorrect timeout
707 * value, just skip the check and use 0xE. The hardware may take
708 * longer to time out, but that's much better than having a too-short
709 * timeout value.
710 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200711 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200712 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200713
Andrei Warkentina3c77782011-04-11 16:13:42 -0500714 /* Unspecified timeout, assume max */
715 if (!data && !cmd->cmd_timeout_ms)
716 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800717
Andrei Warkentina3c77782011-04-11 16:13:42 -0500718 /* timeout in us */
719 if (!data)
720 target_timeout = cmd->cmd_timeout_ms * 1000;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530721 else {
722 target_timeout = data->timeout_ns / 1000;
723 if (host->clock)
724 target_timeout += data->timeout_clks / host->clock;
725 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700726
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700727 /*
728 * Figure out needed cycles.
729 * We do this in steps in order to fit inside a 32 bit int.
730 * The first step is the minimum timeout, which will have a
731 * minimum resolution of 6 bits:
732 * (1) 2^13*1000 > 2^22,
733 * (2) host->timeout_clk < 2^16
734 * =>
735 * (1) / (2) > 2^6
736 */
737 count = 0;
Sahitya Tummalaf667cc12013-06-10 16:32:51 +0530738 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK) {
739 curr_clk = host->clock / 1000;
740 if (host->quirks2 & SDHCI_QUIRK2_DIVIDE_TOUT_BY_4)
741 curr_clk /= 4;
742 current_timeout = (1 << 13) * 1000 / curr_clk;
743 } else {
744 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
745 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700746 while (current_timeout < target_timeout) {
747 count++;
748 current_timeout <<= 1;
749 if (count >= 0xF)
750 break;
751 }
752
Sahitya Tummala4d12d0b2013-04-12 11:59:25 +0530753 if (!(host->quirks2 & SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT)) {
754 if (count >= 0xF) {
755 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
756 mmc_hostname(host->mmc), count, cmd->opcode);
757 count = 0xE;
758 }
Sahitya Tummalaca422112013-02-22 12:15:54 +0530759 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700760
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200761 return count;
762}
763
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300764static void sdhci_set_transfer_irqs(struct sdhci_host *host)
765{
766 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
767 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
768
769 if (host->flags & SDHCI_REQ_USE_DMA)
770 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
771 else
772 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
773}
774
Andrei Warkentina3c77782011-04-11 16:13:42 -0500775static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200776{
777 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200778 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500779 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200780 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200781
782 WARN_ON(host->data);
783
Andrei Warkentina3c77782011-04-11 16:13:42 -0500784 if (data || (cmd->flags & MMC_RSP_BUSY)) {
785 count = sdhci_calc_timeout(host, cmd);
786 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
787 }
788
789 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200790 return;
791
792 /* Sanity checks */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530793 BUG_ON(data->blksz * data->blocks > host->mmc->max_req_size);
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200794 BUG_ON(data->blksz > host->mmc->max_blk_size);
795 BUG_ON(data->blocks > 65535);
796
797 host->data = data;
798 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400799 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200800
Richard Röjforsa13abc72009-09-22 16:45:30 -0700801 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100802 host->flags |= SDHCI_REQ_USE_DMA;
803
Pierre Ossman2134a922008-06-28 18:28:51 +0200804 /*
805 * FIXME: This doesn't account for merging when mapping the
806 * scatterlist.
807 */
808 if (host->flags & SDHCI_REQ_USE_DMA) {
809 int broken, i;
810 struct scatterlist *sg;
811
812 broken = 0;
813 if (host->flags & SDHCI_USE_ADMA) {
814 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
815 broken = 1;
816 } else {
817 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
818 broken = 1;
819 }
820
821 if (unlikely(broken)) {
822 for_each_sg(data->sg, sg, data->sg_len, i) {
823 if (sg->length & 0x3) {
824 DBG("Reverting to PIO because of "
825 "transfer size (%d)\n",
826 sg->length);
827 host->flags &= ~SDHCI_REQ_USE_DMA;
828 break;
829 }
830 }
831 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100832 }
833
834 /*
835 * The assumption here being that alignment is the same after
836 * translation to device address space.
837 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200838 if (host->flags & SDHCI_REQ_USE_DMA) {
839 int broken, i;
840 struct scatterlist *sg;
841
842 broken = 0;
843 if (host->flags & SDHCI_USE_ADMA) {
844 /*
845 * As we use 3 byte chunks to work around
846 * alignment problems, we need to check this
847 * quirk.
848 */
849 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
850 broken = 1;
851 } else {
852 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
853 broken = 1;
854 }
855
856 if (unlikely(broken)) {
857 for_each_sg(data->sg, sg, data->sg_len, i) {
858 if (sg->offset & 0x3) {
859 DBG("Reverting to PIO because of "
860 "bad alignment\n");
861 host->flags &= ~SDHCI_REQ_USE_DMA;
862 break;
863 }
864 }
865 }
866 }
867
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200868 if (host->flags & SDHCI_REQ_USE_DMA) {
869 if (host->flags & SDHCI_USE_ADMA) {
870 ret = sdhci_adma_table_pre(host, data);
871 if (ret) {
872 /*
873 * This only happens when someone fed
874 * us an invalid request.
875 */
876 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200877 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200878 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300879 sdhci_writel(host, host->adma_addr,
880 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200881 }
882 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300883 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200884
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800885 sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300886 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200887 /*
888 * This only happens when someone fed
889 * us an invalid request.
890 */
891 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200892 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200893 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200894 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300895 sdhci_writel(host, sg_dma_address(data->sg),
896 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200897 }
898 }
899 }
900
Pierre Ossman2134a922008-06-28 18:28:51 +0200901 /*
902 * Always adjust the DMA selection as some controllers
903 * (e.g. JMicron) can't do PIO properly when the selection
904 * is ADMA.
905 */
906 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300907 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200908 ctrl &= ~SDHCI_CTRL_DMA_MASK;
909 if ((host->flags & SDHCI_REQ_USE_DMA) &&
910 (host->flags & SDHCI_USE_ADMA))
911 ctrl |= SDHCI_CTRL_ADMA32;
912 else
913 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300914 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100915 }
916
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200917 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200918 int flags;
919
920 flags = SG_MITER_ATOMIC;
921 if (host->data->flags & MMC_DATA_READ)
922 flags |= SG_MITER_TO_SG;
923 else
924 flags |= SG_MITER_FROM_SG;
925 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200926 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800927 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700928
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300929 sdhci_set_transfer_irqs(host);
930
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400931 /* Set the DMA boundary value and block size */
932 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
933 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300934 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700935}
936
937static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500938 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700939{
940 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500941 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700942
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700943 if (data == NULL)
944 return;
945
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200946 WARN_ON(!host->data);
947
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700948 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500949 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
950 mode |= SDHCI_TRNS_MULTI;
951 /*
952 * If we are sending CMD23, CMD12 never gets sent
953 * on successful completion (so no Auto-CMD12).
954 */
955 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
956 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500957 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
958 mode |= SDHCI_TRNS_AUTO_CMD23;
959 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
960 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700961 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500962
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530963 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700964 mode |= SDHCI_TRNS_READ;
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530965 if (host->ops->toggle_cdr)
966 host->ops->toggle_cdr(host, true);
967 }
968 if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE))
969 host->ops->toggle_cdr(host, false);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100970 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700971 mode |= SDHCI_TRNS_DMA;
972
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300973 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800974}
975
976static void sdhci_finish_data(struct sdhci_host *host)
977{
978 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800979
980 BUG_ON(!host->data);
981
982 data = host->data;
983 host->data = NULL;
984
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100985 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200986 if (host->flags & SDHCI_USE_ADMA)
987 sdhci_adma_table_post(host, data);
988 else {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800989 if (!data->host_cookie)
990 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
991 data->sg_len,
992 (data->flags & MMC_DATA_READ) ?
993 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200994 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800995 }
996
997 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200998 * The specification states that the block count register must
999 * be updated, but it does not specify at what point in the
1000 * data flow. That makes the register entirely useless to read
1001 * back so we have to assume that nothing made it to the card
1002 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001003 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001004 if (data->error)
1005 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001006 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001007 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001008
Andrei Warkentine89d4562011-05-23 15:06:37 -05001009 /*
1010 * Need to send CMD12 if -
1011 * a) open-ended multiblock transfer (no CMD23)
1012 * b) error in multiblock transfer
1013 */
1014 if (data->stop &&
1015 (data->error ||
1016 !host->mrq->sbc)) {
1017
Pierre Ossmand129bce2006-03-24 03:18:17 -08001018 /*
1019 * The controller needs a reset of internal state machines
1020 * upon error conditions.
1021 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001022 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001023 sdhci_reset(host, SDHCI_RESET_CMD);
1024 sdhci_reset(host, SDHCI_RESET_DATA);
1025 }
1026
1027 sdhci_send_command(host, data->stop);
1028 } else
1029 tasklet_schedule(&host->finish_tasklet);
1030}
1031
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301032#define SDHCI_REQUEST_TIMEOUT 10 /* Default request timeout in seconds */
1033
Pierre Ossmand129bce2006-03-24 03:18:17 -08001034static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
1035{
1036 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001037 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001038 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001039
1040 WARN_ON(host->cmd);
1041
Pierre Ossmand129bce2006-03-24 03:18:17 -08001042 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001043 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001044
1045 mask = SDHCI_CMD_INHIBIT;
1046 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1047 mask |= SDHCI_DATA_INHIBIT;
1048
1049 /* We shouldn't wait for data inihibit for stop commands, even
1050 though they might use busy signaling */
1051 if (host->mrq->data && (cmd == host->mrq->data->stop))
1052 mask &= ~SDHCI_DATA_INHIBIT;
1053
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001054 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001055 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301056 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001057 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001058 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001059 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001060 tasklet_schedule(&host->finish_tasklet);
1061 return;
1062 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001063 timeout--;
1064 mdelay(1);
1065 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001066
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301067 mod_timer(&host->timer, jiffies + SDHCI_REQUEST_TIMEOUT * HZ);
1068
1069 if (cmd->cmd_timeout_ms > SDHCI_REQUEST_TIMEOUT * MSEC_PER_SEC)
1070 mod_timer(&host->timer, jiffies +
1071 (msecs_to_jiffies(cmd->cmd_timeout_ms * 2)));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072
1073 host->cmd = cmd;
1074
Andrei Warkentina3c77782011-04-11 16:13:42 -05001075 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001076
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001077 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001078
Andrei Warkentine89d4562011-05-23 15:06:37 -05001079 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001080
Pierre Ossmand129bce2006-03-24 03:18:17 -08001081 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301082 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001083 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001084 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001085 tasklet_schedule(&host->finish_tasklet);
1086 return;
1087 }
1088
1089 if (!(cmd->flags & MMC_RSP_PRESENT))
1090 flags = SDHCI_CMD_RESP_NONE;
1091 else if (cmd->flags & MMC_RSP_136)
1092 flags = SDHCI_CMD_RESP_LONG;
1093 else if (cmd->flags & MMC_RSP_BUSY)
1094 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1095 else
1096 flags = SDHCI_CMD_RESP_SHORT;
1097
1098 if (cmd->flags & MMC_RSP_CRC)
1099 flags |= SDHCI_CMD_CRC;
1100 if (cmd->flags & MMC_RSP_OPCODE)
1101 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301102
1103 /* CMD19 is special in that the Data Present Select should be set */
Girish K S2cd06dc2012-01-06 09:56:39 +05301104 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1105 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001106 flags |= SDHCI_CMD_DATA;
1107
Sahitya Tummala48b458e2013-04-08 12:53:44 +05301108 if (cmd->data)
1109 host->data_start_time = ktime_get();
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001110 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001111}
1112
1113static void sdhci_finish_command(struct sdhci_host *host)
1114{
1115 int i;
1116
1117 BUG_ON(host->cmd == NULL);
1118
1119 if (host->cmd->flags & MMC_RSP_PRESENT) {
1120 if (host->cmd->flags & MMC_RSP_136) {
1121 /* CRC is stripped so we need to do some shifting. */
1122 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001123 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001124 SDHCI_RESPONSE + (3-i)*4) << 8;
1125 if (i != 3)
1126 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001127 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001128 SDHCI_RESPONSE + (3-i)*4-1);
1129 }
1130 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001131 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001132 }
1133 }
1134
Pierre Ossman17b04292007-07-22 22:18:46 +02001135 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001136
Andrei Warkentine89d4562011-05-23 15:06:37 -05001137 /* Finished CMD23, now send actual command. */
1138 if (host->cmd == host->mrq->sbc) {
1139 host->cmd = NULL;
1140 sdhci_send_command(host, host->mrq->cmd);
1141 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001142
Andrei Warkentine89d4562011-05-23 15:06:37 -05001143 /* Processed actual command. */
1144 if (host->data && host->data_early)
1145 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001146
Andrei Warkentine89d4562011-05-23 15:06:37 -05001147 if (!host->cmd->data)
1148 tasklet_schedule(&host->finish_tasklet);
1149
1150 host->cmd = NULL;
1151 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001152}
1153
1154static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1155{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301156 int div = 0; /* Initialized for compiler warning */
Sahitya Tummalaca422112013-02-22 12:15:54 +05301157 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301158 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001159 unsigned long timeout;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301160 unsigned long flags;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001161
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301162 spin_lock_irqsave(&host->lock, flags);
Todd Poynor30832ab2011-12-27 15:48:46 +02001163 if (clock && clock == host->clock)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301164 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001165
Sahitya Tummalaca422112013-02-22 12:15:54 +05301166 host->mmc->actual_clock = 0;
1167
Anton Vorontsov81146342009-03-17 00:13:59 +03001168 if (host->ops->set_clock) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301169 spin_unlock_irqrestore(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001170 host->ops->set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301171 spin_lock_irqsave(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001172 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301173 goto ret;
Anton Vorontsov81146342009-03-17 00:13:59 +03001174 }
1175
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301176 if (host->clock)
1177 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001178
1179 if (clock == 0)
1180 goto out;
1181
Zhangfei Gao85105c52010-08-06 07:10:01 +08001182 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301183 /*
1184 * Check if the Host Controller supports Programmable Clock
1185 * Mode.
1186 */
1187 if (host->clk_mul) {
1188 u16 ctrl;
1189
1190 /*
1191 * We need to figure out whether the Host Driver needs
1192 * to select Programmable Clock Mode, or the value can
1193 * be set automatically by the Host Controller based on
1194 * the Preset Value registers.
1195 */
1196 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1197 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1198 for (div = 1; div <= 1024; div++) {
1199 if (((host->max_clk * host->clk_mul) /
1200 div) <= clock)
1201 break;
1202 }
1203 /*
1204 * Set Programmable Clock Mode in the Clock
1205 * Control register.
1206 */
1207 clk = SDHCI_PROG_CLOCK_MODE;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301208 real_div = div;
1209 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301210 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001211 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301212 } else {
1213 /* Version 3.00 divisors must be a multiple of 2. */
1214 if (host->max_clk <= clock)
1215 div = 1;
1216 else {
1217 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1218 div += 2) {
1219 if ((host->max_clk / div) <= clock)
1220 break;
1221 }
1222 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301223 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301224 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001225 }
1226 } else {
1227 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001228 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001229 if ((host->max_clk / div) <= clock)
1230 break;
1231 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301232 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301233 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001234 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001235
Sahitya Tummalaca422112013-02-22 12:15:54 +05301236 if (real_div)
1237 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1238
Sahitya Tummala00240122013-02-28 19:50:51 +05301239 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
1240 div = 0;
1241
Arindam Nathc3ed3872011-05-05 12:19:06 +05301242 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001243 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1244 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001245 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001246 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001247
Chris Ball27f6cb12009-09-22 16:45:31 -07001248 /* Wait max 20 ms */
1249 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001250 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001251 & SDHCI_CLOCK_INT_STABLE)) {
1252 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301253 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001254 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001255 sdhci_dumpregs(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301256 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001257 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001258 timeout--;
1259 mdelay(1);
1260 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001261
1262 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001263 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001264
1265out:
1266 host->clock = clock;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301267ret:
1268 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001269}
1270
Sahitya Tummalaca422112013-02-22 12:15:54 +05301271static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001272{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001273 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001274
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001275 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001276 switch (1 << power) {
1277 case MMC_VDD_165_195:
1278 pwr = SDHCI_POWER_180;
1279 break;
1280 case MMC_VDD_29_30:
1281 case MMC_VDD_30_31:
1282 pwr = SDHCI_POWER_300;
1283 break;
1284 case MMC_VDD_32_33:
1285 case MMC_VDD_33_34:
1286 pwr = SDHCI_POWER_330;
1287 break;
1288 default:
1289 BUG();
1290 }
1291 }
1292
1293 if (host->pwr == pwr)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301294 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001295
Pierre Ossmanae628902009-05-03 20:45:03 +02001296 host->pwr = pwr;
1297
1298 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001299 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301300 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301301 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301302 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001303 }
1304
1305 /*
1306 * Spec says that we should clear the power reg before setting
1307 * a new value. Some controllers don't seem to like this though.
1308 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301309 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001310 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301311 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301312 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301313 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001314
Andres Salomone08c1692008-07-04 10:00:03 -07001315 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001316 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001317 * and set turn on power at the same time, so set the voltage first.
1318 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301319 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001320 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301321 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301322 host->ops->check_power_status(host, REQ_BUS_ON);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301323 }
Pierre Ossmanae628902009-05-03 20:45:03 +02001324
1325 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001326
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001327 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301328 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301329 host->ops->check_power_status(host, REQ_BUS_ON);
Harald Welte557b0692009-06-18 16:53:38 +02001330
1331 /*
1332 * Some controllers need an extra 10ms delay of 10ms before they
1333 * can apply clock after applying power
1334 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001335 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001336 mdelay(10);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301337
1338 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001339}
1340
Pierre Ossmand129bce2006-03-24 03:18:17 -08001341/*****************************************************************************\
1342 * *
1343 * MMC callbacks *
1344 * *
1345\*****************************************************************************/
1346
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301347static int sdhci_enable(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 host->cpu_dma_latency_us);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301354 if (host->ops->platform_bus_voting)
1355 host->ops->platform_bus_voting(host, 1);
1356
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301357 return 0;
1358}
1359
1360static int sdhci_disable(struct mmc_host *mmc)
1361{
1362 struct sdhci_host *host = mmc_priv(mmc);
1363
1364 if (host->cpu_dma_latency_us)
1365 pm_qos_update_request(&host->pm_qos_req_dma,
1366 PM_QOS_DEFAULT_VALUE);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301367 if (host->ops->platform_bus_voting)
1368 host->ops->platform_bus_voting(host, 0);
1369
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301370 return 0;
1371}
1372
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001373static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1374 bool is_first_req)
1375{
1376 struct sdhci_host *host = mmc_priv(mmc);
1377
1378 if (mrq->data->host_cookie) {
1379 mrq->data->host_cookie = 0;
1380 return;
1381 }
1382
1383 if (host->flags & SDHCI_REQ_USE_DMA)
1384 if (sdhci_pre_dma_transfer(host, mrq->data, &host->next_data) < 0)
1385 mrq->data->host_cookie = 0;
1386}
1387
1388static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1389 int err)
1390{
1391 struct sdhci_host *host = mmc_priv(mmc);
1392 struct mmc_data *data = mrq->data;
1393
1394 if (host->flags & SDHCI_REQ_USE_DMA) {
1395 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1396 (data->flags & MMC_DATA_WRITE) ?
1397 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1398 data->host_cookie = 0;
1399 }
1400}
1401
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301402static bool sdhci_check_state(struct sdhci_host *host)
1403{
1404 struct mmc_host *mmc = host->mmc;
1405
1406 if (!host->clock || !host->pwr ||
Sahitya Tummala1b248c42013-05-24 14:08:10 +05301407 (mmc_use_core_runtime_pm(mmc) ?
1408 pm_runtime_suspended(mmc->parent) : 0))
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301409 return true;
1410 else
1411 return false;
1412}
1413
Pierre Ossmand129bce2006-03-24 03:18:17 -08001414static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1415{
1416 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001417 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001418 unsigned long flags;
1419
1420 host = mmc_priv(mmc);
1421
Adrian Hunter50accb92011-10-03 15:33:34 +03001422 sdhci_runtime_pm_get(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301423 if (sdhci_check_state(host)) {
1424 sdhci_dump_state(host);
1425 WARN(1, "sdhci in bad state");
1426 mrq->cmd->error = -EIO;
1427 if (mrq->data)
1428 mrq->data->error = -EIO;
1429 tasklet_schedule(&host->finish_tasklet);
1430 return;
1431 }
Adrian Hunter50accb92011-10-03 15:33:34 +03001432
Pierre Ossmand129bce2006-03-24 03:18:17 -08001433 spin_lock_irqsave(&host->lock, flags);
1434
1435 WARN_ON(host->mrq != NULL);
1436
Pierre Ossmanf9134312008-12-21 17:01:48 +01001437#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001438 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001439#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001440
1441 /*
1442 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1443 * requests if Auto-CMD12 is enabled.
1444 */
1445 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001446 if (mrq->stop) {
1447 mrq->data->stop = NULL;
1448 mrq->stop = NULL;
1449 }
1450 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001451
1452 host->mrq = mrq;
1453
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001454 /* If polling, assume that the card is always present. */
1455 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1456 present = true;
1457 else
1458 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1459 SDHCI_CARD_PRESENT;
1460
1461 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001462 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001463 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301464 } else {
1465 u32 present_state;
1466
1467 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1468 /*
1469 * Check if the re-tuning timer has already expired and there
1470 * is no on-going data transfer. If so, we need to execute
1471 * tuning procedure before sending command.
1472 */
1473 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1474 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1475 spin_unlock_irqrestore(&host->lock, flags);
Girish K S2cd06dc2012-01-06 09:56:39 +05301476 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301477 spin_lock_irqsave(&host->lock, flags);
1478
1479 /* Restore original mmc_request structure */
1480 host->mrq = mrq;
1481 }
1482
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001483 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001484 sdhci_send_command(host, mrq->sbc);
1485 else
1486 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301487 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001488
Pierre Ossman5f25a662006-10-04 02:15:39 -07001489 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001490 spin_unlock_irqrestore(&host->lock, flags);
1491}
1492
Adrian Hunter50accb92011-10-03 15:33:34 +03001493static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001494{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001495 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301496 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001497 u8 ctrl;
1498
Sahitya Tummala40474e42013-07-10 14:40:37 +05301499 mutex_lock(&host->ios_mutex);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301500 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301501 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1502 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
Sahitya Tummala40474e42013-07-10 14:40:37 +05301503 mutex_unlock(&host->ios_mutex);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301504 return;
1505 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001506
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301507 if (ios->clock)
1508 sdhci_set_clock(host, ios->clock);
1509
1510 spin_lock_irqsave(&host->lock, flags);
1511 if (!host->clock) {
1512 spin_unlock_irqrestore(&host->lock, flags);
Sahitya Tummala40474e42013-07-10 14:40:37 +05301513 mutex_unlock(&host->ios_mutex);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301514 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001515 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301516 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001517
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301518 if (ios->power_mode & (MMC_POWER_UP | MMC_POWER_ON))
Sahitya Tummalaca422112013-02-22 12:15:54 +05301519 vdd_bit = sdhci_set_power(host, ios->vdd);
1520
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301521 if (host->vmmc && vdd_bit != -1)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301522 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301523
1524 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001525
Philip Rakity643a81f2010-09-23 08:24:32 -07001526 if (host->ops->platform_send_init_74_clocks)
1527 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1528
Philip Rakity15ec4462010-11-19 16:48:39 -05001529 /*
1530 * If your platform has 8-bit width support but is not a v3 controller,
1531 * or if it requires special setup code, you should implement that in
1532 * platform_8bit_width().
1533 */
1534 if (host->ops->platform_8bit_width)
1535 host->ops->platform_8bit_width(host, ios->bus_width);
1536 else {
1537 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1538 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1539 ctrl &= ~SDHCI_CTRL_4BITBUS;
1540 if (host->version >= SDHCI_SPEC_300)
1541 ctrl |= SDHCI_CTRL_8BITBUS;
1542 } else {
1543 if (host->version >= SDHCI_SPEC_300)
1544 ctrl &= ~SDHCI_CTRL_8BITBUS;
1545 if (ios->bus_width == MMC_BUS_WIDTH_4)
1546 ctrl |= SDHCI_CTRL_4BITBUS;
1547 else
1548 ctrl &= ~SDHCI_CTRL_4BITBUS;
1549 }
1550 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1551 }
1552
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001553 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001554
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001555 if ((ios->timing == MMC_TIMING_SD_HS ||
1556 ios->timing == MMC_TIMING_MMC_HS)
1557 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001558 ctrl |= SDHCI_CTRL_HISPD;
1559 else
1560 ctrl &= ~SDHCI_CTRL_HISPD;
1561
Arindam Nathd6d50a12011-05-05 12:18:59 +05301562 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301563 u16 clk, ctrl_2;
1564 unsigned int clock;
1565
1566 /* In case of UHS-I modes, set High Speed Enable */
Girish K S2cd06dc2012-01-06 09:56:39 +05301567 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1568 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301569 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1570 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001571 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301572 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301573
1574 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1575 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301576 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301577 /*
1578 * We only need to set Driver Strength if the
1579 * preset value enable is not set.
1580 */
1581 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1582 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1583 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1584 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1585 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1586
1587 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301588 } else {
1589 /*
1590 * According to SDHC Spec v3.00, if the Preset Value
1591 * Enable in the Host Control 2 register is set, we
1592 * need to reset SD Clock Enable before changing High
1593 * Speed Enable to avoid generating clock gliches.
1594 */
Arindam Nath758535c2011-05-05 12:19:00 +05301595
1596 /* Reset SD Clock Enable */
1597 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1598 clk &= ~SDHCI_CLOCK_CARD_EN;
1599 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1600
1601 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1602
1603 /* Re-enable SD Clock */
1604 clock = host->clock;
1605 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301606 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301607 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301608 spin_lock_irqsave(&host->lock, flags);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301609 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301610
Arindam Nath49c468f2011-05-05 12:19:01 +05301611 /* Reset SD Clock Enable */
1612 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1613 clk &= ~SDHCI_CLOCK_CARD_EN;
1614 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1615
Philip Rakity6322cdd2011-05-13 11:17:15 +05301616 if (host->ops->set_uhs_signaling)
1617 host->ops->set_uhs_signaling(host, ios->timing);
1618 else {
1619 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1620 /* Select Bus Speed Mode for host */
1621 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S2cd06dc2012-01-06 09:56:39 +05301622 if (ios->timing == MMC_TIMING_MMC_HS200)
1623 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1624 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301625 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1626 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1627 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1628 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1629 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1630 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1631 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1632 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1633 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1634 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1635 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301636
1637 /* Re-enable SD Clock */
1638 clock = host->clock;
1639 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301640 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath49c468f2011-05-05 12:19:01 +05301641 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301642 spin_lock_irqsave(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301643 } else
1644 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301645
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301646 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02001647 /*
1648 * Some (ENE) controllers go apeshit on some ios operation,
1649 * signalling timeout and CRC errors even on CMD0. Resetting
1650 * it on each ios seems to solve the problem.
1651 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001652 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001653 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1654
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301655 /*
1656 * Reset the chip on each power off.
1657 * Should clear out any weird states.
1658 */
1659 if (ios->power_mode == MMC_POWER_OFF) {
1660 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1661 sdhci_reinit(host);
1662 vdd_bit = sdhci_set_power(host, -1);
1663 if (host->vmmc && vdd_bit != -1)
1664 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1665 }
1666 if (!ios->clock)
1667 sdhci_set_clock(host, ios->clock);
1668
Pierre Ossman5f25a662006-10-04 02:15:39 -07001669 mmiowb();
Sahitya Tummala40474e42013-07-10 14:40:37 +05301670 mutex_unlock(&host->ios_mutex);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001671}
1672
Adrian Hunter50accb92011-10-03 15:33:34 +03001673static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1674{
1675 struct sdhci_host *host = mmc_priv(mmc);
1676
1677 sdhci_runtime_pm_get(host);
1678 sdhci_do_set_ios(host, ios);
1679 sdhci_runtime_pm_put(host);
1680}
1681
1682static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001683{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001684 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001685 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001686
Pierre Ossmand129bce2006-03-24 03:18:17 -08001687 spin_lock_irqsave(&host->lock, flags);
1688
Pierre Ossman1e728592008-04-16 19:13:13 +02001689 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001690 is_readonly = 0;
1691 else if (host->ops->get_ro)
1692 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001693 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001694 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1695 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001696
1697 spin_unlock_irqrestore(&host->lock, flags);
1698
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001699 /* This quirk needs to be replaced by a callback-function later */
1700 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1701 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001702}
1703
Takashi Iwai82b0e232011-04-21 20:26:38 +02001704#define SAMPLE_COUNT 5
1705
Adrian Hunter50accb92011-10-03 15:33:34 +03001706static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001707{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001708 int i, ro_count;
1709
Takashi Iwai82b0e232011-04-21 20:26:38 +02001710 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001711 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001712
1713 ro_count = 0;
1714 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001715 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001716 if (++ro_count > SAMPLE_COUNT / 2)
1717 return 1;
1718 }
1719 msleep(30);
1720 }
1721 return 0;
1722}
1723
Adrian Hunter50accb92011-10-03 15:33:34 +03001724static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001725{
Adrian Hunter50accb92011-10-03 15:33:34 +03001726 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001727
Adrian Hunter50accb92011-10-03 15:33:34 +03001728 if (host->ops && host->ops->hw_reset)
1729 host->ops->hw_reset(host);
1730}
Adrian Hunter20758b62011-08-29 16:42:12 +03001731
Adrian Hunter50accb92011-10-03 15:33:34 +03001732static int sdhci_get_ro(struct mmc_host *mmc)
1733{
1734 struct sdhci_host *host = mmc_priv(mmc);
1735 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001736
Adrian Hunter50accb92011-10-03 15:33:34 +03001737 sdhci_runtime_pm_get(host);
1738 ret = sdhci_do_get_ro(host);
1739 sdhci_runtime_pm_put(host);
1740 return ret;
1741}
1742
1743static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1744{
Pierre Ossman1e728592008-04-16 19:13:13 +02001745 if (host->flags & SDHCI_DEVICE_DEAD)
1746 goto out;
1747
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001748 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001749 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1750 else
1751 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1752
1753 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1754 if (host->runtime_suspended)
1755 goto out;
1756
1757 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001758 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1759 else
1760 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001761out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001762 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001763}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001764
Adrian Hunter50accb92011-10-03 15:33:34 +03001765static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1766{
1767 struct sdhci_host *host = mmc_priv(mmc);
1768 unsigned long flags;
1769
1770 spin_lock_irqsave(&host->lock, flags);
1771 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001772 spin_unlock_irqrestore(&host->lock, flags);
1773}
1774
Adrian Hunter50accb92011-10-03 15:33:34 +03001775static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1776 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301777{
Arindam Nathf2119df2011-05-05 12:18:57 +05301778 u8 pwr;
1779 u16 clk, ctrl;
1780 u32 present_state;
1781
Arindam Nathf2119df2011-05-05 12:18:57 +05301782 /*
1783 * Signal Voltage Switching is only applicable for Host Controllers
1784 * v3.00 and above.
1785 */
1786 if (host->version < SDHCI_SPEC_300)
1787 return 0;
1788
1789 /*
1790 * We first check whether the request is to set signalling voltage
1791 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1792 */
1793 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1794 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1795 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1796 ctrl &= ~SDHCI_CTRL_VDD_180;
1797 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301798 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301799 host->ops->check_power_status(host, REQ_IO_HIGH);
Arindam Nathf2119df2011-05-05 12:18:57 +05301800
1801 /* Wait for 5ms */
1802 usleep_range(5000, 5500);
1803
1804 /* 3.3V regulator output should be stable within 5 ms */
1805 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1806 if (!(ctrl & SDHCI_CTRL_VDD_180))
1807 return 0;
1808 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301809 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301810 "signalling voltage failed\n");
1811 return -EIO;
1812 }
1813 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1814 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1815 /* Stop SDCLK */
1816 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1817 clk &= ~SDHCI_CLOCK_CARD_EN;
1818 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1819
1820 /* Check whether DAT[3:0] is 0000 */
1821 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1822 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1823 SDHCI_DATA_LVL_SHIFT)) {
1824 /*
1825 * Enable 1.8V Signal Enable in the Host Control2
1826 * register
1827 */
1828 ctrl |= SDHCI_CTRL_VDD_180;
1829 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301830 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301831 host->ops->check_power_status(host, REQ_IO_LOW);
Arindam Nathf2119df2011-05-05 12:18:57 +05301832
1833 /* Wait for 5ms */
1834 usleep_range(5000, 5500);
1835
1836 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1837 if (ctrl & SDHCI_CTRL_VDD_180) {
1838 /* Provide SDCLK again and wait for 1ms*/
1839 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1840 clk |= SDHCI_CLOCK_CARD_EN;
1841 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1842 usleep_range(1000, 1500);
1843
1844 /*
1845 * If DAT[3:0] level is 1111b, then the card
1846 * was successfully switched to 1.8V signaling.
1847 */
1848 present_state = sdhci_readl(host,
1849 SDHCI_PRESENT_STATE);
1850 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1851 SDHCI_DATA_LVL_MASK)
1852 return 0;
1853 }
1854 }
1855
1856 /*
1857 * If we are here, that means the switch to 1.8V signaling
1858 * failed. We power cycle the card, and retry initialization
1859 * sequence by setting S18R to 0.
1860 */
1861 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1862 pwr &= ~SDHCI_POWER_ON;
1863 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301864 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301865 host->ops->check_power_status(host, REQ_BUS_OFF);
Arindam Nathf2119df2011-05-05 12:18:57 +05301866
1867 /* Wait for 1ms as per the spec */
1868 usleep_range(1000, 1500);
1869 pwr |= SDHCI_POWER_ON;
1870 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301871 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301872 host->ops->check_power_status(host, REQ_BUS_ON);
Arindam Nathf2119df2011-05-05 12:18:57 +05301873
Sahitya Tummalaca422112013-02-22 12:15:54 +05301874 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301875 "voltage failed, retrying with S18R set to 0\n");
1876 return -EAGAIN;
1877 } else
1878 /* No signal voltage switch required */
1879 return 0;
1880}
1881
Adrian Hunter50accb92011-10-03 15:33:34 +03001882static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1883 struct mmc_ios *ios)
1884{
1885 struct sdhci_host *host = mmc_priv(mmc);
1886 int err;
1887
1888 if (host->version < SDHCI_SPEC_300)
1889 return 0;
1890 sdhci_runtime_pm_get(host);
1891 err = sdhci_do_start_signal_voltage_switch(host, ios);
1892 sdhci_runtime_pm_put(host);
1893 return err;
1894}
1895
Girish K S2cd06dc2012-01-06 09:56:39 +05301896static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301897{
1898 struct sdhci_host *host;
1899 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05301900 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05301901 int tuning_loop_counter = MAX_TUNING_LOOP;
1902 unsigned long timeout;
1903 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05301904 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301905
1906 host = mmc_priv(mmc);
1907
Adrian Hunter50accb92011-10-03 15:33:34 +03001908 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301909 disable_irq(host->irq);
1910 spin_lock(&host->lock);
1911
1912 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1913
1914 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05301915 * The Host Controller needs tuning only in case of SDR104 mode
1916 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301917 * Capabilities register.
Girish K S2cd06dc2012-01-06 09:56:39 +05301918 * If the Host Controller supports the HS200 mode then the
1919 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301920 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08001921 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1922 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
1923 (host->flags & SDHCI_HS200_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05301924 requires_tuning_nonuhs = true;
1925
Arindam Nathb513ea22011-05-05 12:19:04 +05301926 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301927 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301928 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1929 else {
1930 spin_unlock(&host->lock);
1931 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03001932 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301933 return 0;
1934 }
1935
Asutosh Das8ddd3482013-01-04 11:45:46 +05301936 if (host->ops->execute_tuning) {
1937 spin_unlock(&host->lock);
1938 enable_irq(host->irq);
Sahitya Tummaladda88d62013-06-13 10:36:11 +05301939 err = host->ops->execute_tuning(host, opcode);
Asutosh Das8ddd3482013-01-04 11:45:46 +05301940 disable_irq(host->irq);
1941 spin_lock(&host->lock);
1942 goto out;
1943 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301944 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1945
1946 /*
1947 * As per the Host Controller spec v3.00, tuning command
1948 * generates Buffer Read Ready interrupt, so enable that.
1949 *
1950 * Note: The spec clearly says that when tuning sequence
1951 * is being performed, the controller does not generate
1952 * interrupts other than Buffer Read Ready interrupt. But
1953 * to make sure we don't hit a controller bug, we _only_
1954 * enable Buffer Read Ready interrupt here.
1955 */
1956 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1957 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1958
1959 /*
1960 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1961 * of loops reaches 40 times or a timeout of 150ms occurs.
1962 */
1963 timeout = 150;
1964 do {
1965 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03001966 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301967
1968 if (!tuning_loop_counter && !timeout)
1969 break;
1970
Girish K S2cd06dc2012-01-06 09:56:39 +05301971 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301972 cmd.arg = 0;
1973 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1974 cmd.retries = 0;
1975 cmd.data = NULL;
1976 cmd.error = 0;
1977
1978 mrq.cmd = &cmd;
1979 host->mrq = &mrq;
1980
1981 /*
1982 * In response to CMD19, the card sends 64 bytes of tuning
1983 * block to the Host Controller. So we set the block size
1984 * to 64 here.
1985 */
Girish K S2cd06dc2012-01-06 09:56:39 +05301986 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1987 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1988 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1989 SDHCI_BLOCK_SIZE);
1990 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1991 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1992 SDHCI_BLOCK_SIZE);
1993 } else {
1994 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1995 SDHCI_BLOCK_SIZE);
1996 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301997
1998 /*
1999 * The tuning block is sent by the card to the host controller.
2000 * So we set the TRNS_READ bit in the Transfer Mode register.
2001 * This also takes care of setting DMA Enable and Multi Block
2002 * Select in the same register to 0.
2003 */
2004 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2005
2006 sdhci_send_command(host, &cmd);
2007
2008 host->cmd = NULL;
2009 host->mrq = NULL;
2010
2011 spin_unlock(&host->lock);
2012 enable_irq(host->irq);
2013
2014 /* Wait for Buffer Read Ready interrupt */
2015 wait_event_interruptible_timeout(host->buf_ready_int,
2016 (host->tuning_done == 1),
2017 msecs_to_jiffies(50));
2018 disable_irq(host->irq);
2019 spin_lock(&host->lock);
2020
2021 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302022 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05302023 "Buffer Read Ready interrupt during tuning "
2024 "procedure, falling back to fixed sampling "
2025 "clock\n");
2026 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2027 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2028 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2029 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2030
2031 err = -EIO;
2032 goto out;
2033 }
2034
2035 host->tuning_done = 0;
2036
2037 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2038 tuning_loop_counter--;
2039 timeout--;
2040 mdelay(1);
2041 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2042
2043 /*
2044 * The Host Driver has exhausted the maximum number of loops allowed,
2045 * so use fixed sampling frequency.
2046 */
2047 if (!tuning_loop_counter || !timeout) {
2048 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2049 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2050 } else {
2051 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302052 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05302053 " failed, falling back to fixed sampling"
2054 " clock\n");
2055 err = -EIO;
2056 }
2057 }
2058
2059out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302060 /*
2061 * If this is the very first time we are here, we start the retuning
2062 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2063 * flag won't be set, we check this condition before actually starting
2064 * the timer.
2065 */
2066 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2067 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2068 mod_timer(&host->tuning_timer, jiffies +
2069 host->tuning_count * HZ);
2070 /* Tuning mode 1 limits the maximum data length to 4MB */
2071 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2072 } else {
2073 host->flags &= ~SDHCI_NEEDS_RETUNING;
2074 /* Reload the new initial value for timer */
2075 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2076 mod_timer(&host->tuning_timer, jiffies +
2077 host->tuning_count * HZ);
2078 }
2079
2080 /*
2081 * In case tuning fails, host controllers which support re-tuning can
2082 * try tuning again at a later time, when the re-tuning timer expires.
2083 * So for these controllers, we return 0. Since there might be other
2084 * controllers who do not have this capability, we return error for
2085 * them.
2086 */
2087 if (err && host->tuning_count &&
2088 host->tuning_mode == SDHCI_TUNING_MODE_1)
2089 err = 0;
2090
Arindam Nathb513ea22011-05-05 12:19:04 +05302091 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
2092 spin_unlock(&host->lock);
2093 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002094 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302095
2096 return err;
2097}
2098
Adrian Hunter50accb92011-10-03 15:33:34 +03002099static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302100{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302101 u16 ctrl;
2102 unsigned long flags;
2103
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302104 /* Host Controller v3.00 defines preset value registers */
2105 if (host->version < SDHCI_SPEC_300)
2106 return;
2107
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302108 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_PRESET_VALUE)
2109 return;
2110
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302111 spin_lock_irqsave(&host->lock, flags);
2112
2113 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2114
2115 /*
2116 * We only enable or disable Preset Value if they are not already
2117 * enabled or disabled respectively. Otherwise, we bail out.
2118 */
2119 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2120 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2121 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002122 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302123 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2124 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2125 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002126 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302127 }
2128
2129 spin_unlock_irqrestore(&host->lock, flags);
2130}
2131
Adrian Hunter50accb92011-10-03 15:33:34 +03002132static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
2133{
2134 struct sdhci_host *host = mmc_priv(mmc);
2135
2136 sdhci_runtime_pm_get(host);
2137 sdhci_do_enable_preset_value(host, enable);
2138 sdhci_runtime_pm_put(host);
2139}
2140
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002141static int sdhci_stop_request(struct mmc_host *mmc)
2142{
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002143 struct sdhci_host *host = mmc_priv(mmc);
2144 unsigned long flags;
2145 struct mmc_data *data;
2146
2147 spin_lock_irqsave(&host->lock, flags);
2148 if (!host->mrq || !host->data)
2149 goto out;
2150
2151 data = host->data;
2152
2153 if (host->ops->disable_data_xfer)
2154 host->ops->disable_data_xfer(host);
2155
2156 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2157
2158 if (host->flags & SDHCI_REQ_USE_DMA) {
2159 if (host->flags & SDHCI_USE_ADMA) {
2160 sdhci_adma_table_post(host, data);
2161 } else {
2162 if (!data->host_cookie)
2163 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
2164 data->sg_len,
2165 (data->flags & MMC_DATA_READ) ?
2166 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2167 }
2168 }
2169 del_timer(&host->timer);
2170 host->mrq = NULL;
2171 host->cmd = NULL;
2172 host->data = NULL;
2173out:
2174 spin_unlock_irqrestore(&host->lock, flags);
2175 return 0;
2176}
2177
2178static unsigned int sdhci_get_xfer_remain(struct mmc_host *mmc)
2179{
2180 struct sdhci_host *host = mmc_priv(mmc);
2181 u32 present_state = 0;
2182
2183 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
2184
2185 return present_state & SDHCI_DOING_WRITE;
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002186}
2187
David Brownellab7aefd2006-11-12 17:55:30 -08002188static const struct mmc_host_ops sdhci_ops = {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002189 .pre_req = sdhci_pre_req,
2190 .post_req = sdhci_post_req,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002191 .request = sdhci_request,
2192 .set_ios = sdhci_set_ios,
2193 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03002194 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002195 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302196 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302197 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302198 .enable_preset_value = sdhci_enable_preset_value,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302199 .enable = sdhci_enable,
2200 .disable = sdhci_disable,
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002201 .stop_request = sdhci_stop_request,
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002202 .get_xfer_remain = sdhci_get_xfer_remain,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002203};
2204
2205/*****************************************************************************\
2206 * *
2207 * Tasklets *
2208 * *
2209\*****************************************************************************/
2210
2211static void sdhci_tasklet_card(unsigned long param)
2212{
2213 struct sdhci_host *host;
2214 unsigned long flags;
2215
2216 host = (struct sdhci_host*)param;
2217
2218 spin_lock_irqsave(&host->lock, flags);
2219
Adrian Hunter50accb92011-10-03 15:33:34 +03002220 /* Check host->mrq first in case we are runtime suspended */
2221 if (host->mrq &&
2222 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302223 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002224 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05302225 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002226 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002227
Adrian Hunter50accb92011-10-03 15:33:34 +03002228 sdhci_reset(host, SDHCI_RESET_CMD);
2229 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002230
Adrian Hunter50accb92011-10-03 15:33:34 +03002231 host->mrq->cmd->error = -ENOMEDIUM;
2232 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002233 }
2234
2235 spin_unlock_irqrestore(&host->lock, flags);
2236
Pierre Ossman04cf5852008-08-18 22:18:14 +02002237 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002238}
2239
2240static void sdhci_tasklet_finish(unsigned long param)
2241{
2242 struct sdhci_host *host;
2243 unsigned long flags;
2244 struct mmc_request *mrq;
2245
2246 host = (struct sdhci_host*)param;
2247
Adrian Hunter50accb92011-10-03 15:33:34 +03002248 spin_lock_irqsave(&host->lock, flags);
2249
Chris Ball0c9c99a2011-04-27 17:35:31 -04002250 /*
2251 * If this tasklet gets rescheduled while running, it will
2252 * be run again afterwards but without any active request.
2253 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002254 if (!host->mrq) {
2255 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002256 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002257 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002258
2259 del_timer(&host->timer);
2260
2261 mrq = host->mrq;
2262
Pierre Ossmand129bce2006-03-24 03:18:17 -08002263 /*
2264 * The controller needs a reset of internal state machines
2265 * upon error conditions.
2266 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002267 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002268 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002269 (mrq->data && (mrq->data->error ||
2270 (mrq->data->stop && mrq->data->stop->error))) ||
2271 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002272
2273 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002274 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002275 unsigned int clock;
2276
2277 /* This is to force an update */
2278 clock = host->clock;
2279 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302280 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002281 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302282 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002283 }
2284
2285 /* Spec says we should do both at the same time, but Ricoh
2286 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002287 sdhci_reset(host, SDHCI_RESET_CMD);
2288 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002289 } else {
2290 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2291 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002292 }
2293
2294 host->mrq = NULL;
2295 host->cmd = NULL;
2296 host->data = NULL;
2297
Pierre Ossmanf9134312008-12-21 17:01:48 +01002298#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002299 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002300#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002301
Pierre Ossman5f25a662006-10-04 02:15:39 -07002302 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002303 spin_unlock_irqrestore(&host->lock, flags);
2304
2305 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002306 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002307}
2308
2309static void sdhci_timeout_timer(unsigned long data)
2310{
2311 struct sdhci_host *host;
2312 unsigned long flags;
2313
2314 host = (struct sdhci_host*)data;
2315
2316 spin_lock_irqsave(&host->lock, flags);
2317
2318 if (host->mrq) {
Subhash Jadavani5b254e42013-06-27 18:16:02 +05302319 if (!host->mrq->cmd->ignore_timeout) {
2320 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2321 mmc_hostname(host->mmc));
2322 sdhci_dumpregs(host);
2323 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002324
2325 if (host->data) {
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302326 pr_info("%s: bytes to transfer: %d transferred: %d\n",
2327 mmc_hostname(host->mmc),
2328 (host->data->blksz * host->data->blocks),
2329 (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) *
2330 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossman17b04292007-07-22 22:18:46 +02002331 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002332 sdhci_finish_data(host);
2333 } else {
2334 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002335 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002336 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002337 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002338
2339 tasklet_schedule(&host->finish_tasklet);
2340 }
2341 }
2342
Pierre Ossman5f25a662006-10-04 02:15:39 -07002343 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002344 spin_unlock_irqrestore(&host->lock, flags);
2345}
2346
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302347static void sdhci_tuning_timer(unsigned long data)
2348{
2349 struct sdhci_host *host;
2350 unsigned long flags;
2351
2352 host = (struct sdhci_host *)data;
2353
2354 spin_lock_irqsave(&host->lock, flags);
2355
2356 host->flags |= SDHCI_NEEDS_RETUNING;
2357
2358 spin_unlock_irqrestore(&host->lock, flags);
2359}
2360
Pierre Ossmand129bce2006-03-24 03:18:17 -08002361/*****************************************************************************\
2362 * *
2363 * Interrupt handling *
2364 * *
2365\*****************************************************************************/
2366
2367static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2368{
2369 BUG_ON(intmask == 0);
2370
2371 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302372 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002373 "though no command operation was in progress.\n",
2374 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002375 sdhci_dumpregs(host);
2376 return;
2377 }
2378
Pierre Ossman43b58b32007-07-25 23:15:27 +02002379 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002380 host->cmd->error = -ETIMEDOUT;
2381 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2382 SDHCI_INT_INDEX))
2383 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002384
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302385 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2386 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2387 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2388 if (intmask & SDHCI_INT_CRC) {
2389 sdhci_reset(host, SDHCI_RESET_CMD);
2390 host->cmd->error = 0;
2391 }
2392 }
2393 }
2394
Pierre Ossmane8095172008-07-25 01:09:08 +02002395 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002396 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002397 return;
2398 }
2399
2400 /*
2401 * The host can send and interrupt when the busy state has
2402 * ended, allowing us to wait without wasting CPU cycles.
2403 * Unfortunately this is overloaded on the "data complete"
2404 * interrupt, so we need to take some care when handling
2405 * it.
2406 *
2407 * Note: The 1.0 specification is a bit ambiguous about this
2408 * feature so there might be some problems with older
2409 * controllers.
2410 */
2411 if (host->cmd->flags & MMC_RSP_BUSY) {
2412 if (host->cmd->data)
2413 DBG("Cannot wait for busy signal when also "
2414 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002415 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002416 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002417
2418 /* The controller does not support the end-of-busy IRQ,
2419 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002420 }
2421
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302422 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2423 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2424 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2425 if (intmask & SDHCI_INT_CRC) {
2426 sdhci_finish_command(host);
2427 return;
2428 }
2429 }
2430 }
2431
Pierre Ossmane8095172008-07-25 01:09:08 +02002432 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002433 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002434}
2435
Ben Dooks6882a8c2009-06-14 13:52:38 +01002436static void sdhci_show_adma_error(struct sdhci_host *host)
2437{
2438 const char *name = mmc_hostname(host->mmc);
2439 u8 *desc = host->adma_desc;
2440 __le32 *dma;
2441 __le16 *len;
2442 u8 attr;
2443
2444 sdhci_dumpregs(host);
2445
2446 while (true) {
2447 dma = (__le32 *)(desc + 4);
2448 len = (__le16 *)(desc + 2);
2449 attr = *desc;
2450
Sahitya Tummala419b6c82013-04-12 12:28:29 +05302451 pr_info("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
Ben Dooks6882a8c2009-06-14 13:52:38 +01002452 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2453
2454 desc += 8;
2455
2456 if (attr & 2)
2457 break;
2458 }
2459}
Ben Dooks6882a8c2009-06-14 13:52:38 +01002460
Pierre Ossmand129bce2006-03-24 03:18:17 -08002461static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2462{
Girish K S2cd06dc2012-01-06 09:56:39 +05302463 u32 command;
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302464 bool pr_msg = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002465 BUG_ON(intmask == 0);
2466
Arindam Nathb513ea22011-05-05 12:19:04 +05302467 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2468 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302469 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2470 if (command == MMC_SEND_TUNING_BLOCK ||
2471 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302472 host->tuning_done = 1;
2473 wake_up(&host->buf_ready_int);
2474 return;
2475 }
2476 }
2477
Pierre Ossmand129bce2006-03-24 03:18:17 -08002478 if (!host->data) {
2479 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002480 * The "data complete" interrupt is also used to
2481 * indicate that a busy state has ended. See comment
2482 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002483 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002484 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2485 if (intmask & SDHCI_INT_DATA_END) {
2486 sdhci_finish_command(host);
2487 return;
2488 }
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302489 if (host->quirks2 &
2490 SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD)
2491 return;
Pierre Ossmane8095172008-07-25 01:09:08 +02002492 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002493
Sahitya Tummalaca422112013-02-22 12:15:54 +05302494 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002495 "though no data operation was in progress.\n",
2496 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002497 sdhci_dumpregs(host);
2498
2499 return;
2500 }
2501
2502 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002503 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002504 else if (intmask & SDHCI_INT_DATA_END_BIT)
2505 host->data->error = -EILSEQ;
2506 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2507 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2508 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002509 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002510 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302511 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002512 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002513 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002514 }
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302515 if (host->data->error) {
2516 if ((intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT)) &&
2517 (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING)) {
2518 command = SDHCI_GET_CMD(sdhci_readw(host,
2519 SDHCI_COMMAND));
2520 if ((command != MMC_SEND_TUNING_BLOCK_HS200) &&
2521 (command != MMC_SEND_TUNING_BLOCK))
2522 pr_msg = true;
2523 } else {
2524 pr_msg = true;
2525 }
2526 if (pr_msg) {
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302527 pr_err("%s: data txfr (0x%08x) error: %d after %lld ms\n",
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302528 mmc_hostname(host->mmc), intmask,
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302529 host->data->error, ktime_to_ms(ktime_sub(
2530 ktime_get(), host->data_start_time)));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302531 sdhci_dumpregs(host);
2532 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002533 sdhci_finish_data(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302534 } else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002535 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002536 sdhci_transfer_pio(host);
2537
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002538 /*
2539 * We currently don't do anything fancy with DMA
2540 * boundaries, but as we can't disable the feature
2541 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002542 *
2543 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2544 * should return a valid address to continue from, but as
2545 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002546 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002547 if (intmask & SDHCI_INT_DMA_END) {
2548 u32 dmastart, dmanow;
2549 dmastart = sg_dma_address(host->data->sg);
2550 dmanow = dmastart + host->data->bytes_xfered;
2551 /*
2552 * Force update to the next DMA block boundary.
2553 */
2554 dmanow = (dmanow &
2555 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2556 SDHCI_DEFAULT_BOUNDARY_SIZE;
2557 host->data->bytes_xfered = dmanow - dmastart;
2558 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2559 " next 0x%08x\n",
2560 mmc_hostname(host->mmc), dmastart,
2561 host->data->bytes_xfered, dmanow);
2562 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2563 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002564
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002565 if (intmask & SDHCI_INT_DATA_END) {
2566 if (host->cmd) {
2567 /*
2568 * Data managed to finish before the
2569 * command completed. Make sure we do
2570 * things in the proper order.
2571 */
2572 host->data_early = 1;
2573 } else {
2574 sdhci_finish_data(host);
2575 }
2576 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002577 }
2578}
2579
David Howells7d12e782006-10-05 14:55:46 +01002580static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002581{
2582 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002583 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002584 u32 intmask, unexpected = 0;
2585 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002586
2587 spin_lock(&host->lock);
2588
Adrian Hunter50accb92011-10-03 15:33:34 +03002589 if (host->runtime_suspended) {
2590 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302591 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002592 mmc_hostname(host->mmc));
2593 return IRQ_HANDLED;
2594 }
2595
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002596 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002597
Mark Lord62df67a2007-03-06 13:30:13 +01002598 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002599 result = IRQ_NONE;
2600 goto out;
2601 }
2602
Alexander Stein6379b232012-03-14 09:52:10 +01002603again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002604 DBG("*** %s got interrupt: 0x%08x\n",
2605 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002606
Pierre Ossman3192a282006-06-30 02:22:26 -07002607 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302608 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2609 SDHCI_CARD_PRESENT;
2610
2611 /*
2612 * There is a observation on i.mx esdhc. INSERT bit will be
2613 * immediately set again when it gets cleared, if a card is
2614 * inserted. We have to mask the irq to prevent interrupt
2615 * storm which will freeze the system. And the REMOVE gets
2616 * the same situation.
2617 *
2618 * More testing are needed here to ensure it works for other
2619 * platforms though.
2620 */
2621 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2622 SDHCI_INT_CARD_REMOVE);
2623 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2624 SDHCI_INT_CARD_INSERT);
2625
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002626 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302627 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2628 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002629 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002630 }
2631
Pierre Ossmand129bce2006-03-24 03:18:17 -08002632 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002633 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2634 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002635 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2636 (host->clock <= 400000))
2637 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002638 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002639 }
2640
2641 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002642 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2643 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002644 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2645 (host->clock <= 400000))
2646 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002647 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002648 }
2649
2650 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2651
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002652 intmask &= ~SDHCI_INT_ERROR;
2653
Pierre Ossmand129bce2006-03-24 03:18:17 -08002654 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302655 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002656 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002657 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002658 }
2659
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002660 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002661
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002662 if (intmask & SDHCI_INT_CARD_INT)
2663 cardint = 1;
2664
2665 intmask &= ~SDHCI_INT_CARD_INT;
2666
Pierre Ossman3192a282006-06-30 02:22:26 -07002667 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002668 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002669 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002670 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002671
2672 result = IRQ_HANDLED;
2673
Alexander Stein6379b232012-03-14 09:52:10 +01002674 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2675 if (intmask && --max_loops)
2676 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002677out:
2678 spin_unlock(&host->lock);
2679
Alexander Stein6379b232012-03-14 09:52:10 +01002680 if (unexpected) {
2681 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2682 mmc_hostname(host->mmc), unexpected);
2683 sdhci_dumpregs(host);
2684 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002685 /*
2686 * We have to delay this as it calls back into the driver.
2687 */
2688 if (cardint)
2689 mmc_signal_sdio_irq(host->mmc);
2690
Pierre Ossmand129bce2006-03-24 03:18:17 -08002691 return result;
2692}
2693
2694/*****************************************************************************\
2695 * *
2696 * Suspend/resume *
2697 * *
2698\*****************************************************************************/
2699
2700#ifdef CONFIG_PM
2701
Manuel Laussd72faa62011-11-03 11:09:45 +01002702int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002703{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002704 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302705 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002706
Chris Balla1b13b42012-02-06 00:43:59 -05002707 if (host->ops->platform_suspend)
2708 host->ops->platform_suspend(host);
2709
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002710 sdhci_disable_card_detection(host);
2711
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302712 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302713 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2714 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2715 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002716 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302717 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302718 }
2719
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002720 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302721 if (ret) {
2722 if (has_tuning_timer) {
2723 host->flags |= SDHCI_NEEDS_RETUNING;
2724 mod_timer(&host->tuning_timer, jiffies +
2725 host->tuning_count * HZ);
2726 }
2727
2728 sdhci_enable_card_detection(host);
2729
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002730 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302731 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002732
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002733 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002734
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002735 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002736}
2737
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002738EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002739
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002740int sdhci_resume_host(struct sdhci_host *host)
2741{
2742 int ret;
2743
Richard Röjforsa13abc72009-09-22 16:45:30 -07002744 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002745 if (host->ops->enable_dma)
2746 host->ops->enable_dma(host);
2747 }
2748
2749 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2750 mmc_hostname(host->mmc), host);
2751 if (ret)
2752 return ret;
2753
Adrian Hunter6308d292012-02-07 14:48:54 +02002754 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2755 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2756 /* Card keeps power but host controller does not */
2757 sdhci_init(host, 0);
2758 host->pwr = 0;
2759 host->clock = 0;
2760 sdhci_do_set_ios(host, &host->mmc->ios);
2761 } else {
2762 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2763 mmiowb();
2764 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002765
2766 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002767 sdhci_enable_card_detection(host);
2768
Chris Balla1b13b42012-02-06 00:43:59 -05002769 if (host->ops->platform_resume)
2770 host->ops->platform_resume(host);
2771
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302772 /* Set the re-tuning expiration flag */
2773 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2774 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2775 host->flags |= SDHCI_NEEDS_RETUNING;
2776
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002777 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002778}
2779
2780EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002781
Daniel Drake5f619702010-11-04 22:20:39 +00002782void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2783{
2784 u8 val;
2785 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2786 val |= SDHCI_WAKE_ON_INT;
2787 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2788}
2789
2790EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2791
Pierre Ossmand129bce2006-03-24 03:18:17 -08002792#endif /* CONFIG_PM */
2793
Adrian Hunter50accb92011-10-03 15:33:34 +03002794#ifdef CONFIG_PM_RUNTIME
2795
2796static int sdhci_runtime_pm_get(struct sdhci_host *host)
2797{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302798 if (!mmc_use_core_runtime_pm(host->mmc))
2799 return pm_runtime_get_sync(host->mmc->parent);
2800 else
2801 return 0;
Adrian Hunter50accb92011-10-03 15:33:34 +03002802}
2803
2804static int sdhci_runtime_pm_put(struct sdhci_host *host)
2805{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302806 if (!mmc_use_core_runtime_pm(host->mmc)) {
2807 pm_runtime_mark_last_busy(host->mmc->parent);
2808 return pm_runtime_put_autosuspend(host->mmc->parent);
2809 } else {
2810 return 0;
2811 }
Adrian Hunter50accb92011-10-03 15:33:34 +03002812}
2813
2814int sdhci_runtime_suspend_host(struct sdhci_host *host)
2815{
2816 unsigned long flags;
2817 int ret = 0;
2818
2819 /* Disable tuning since we are suspending */
2820 if (host->version >= SDHCI_SPEC_300 &&
2821 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2822 del_timer_sync(&host->tuning_timer);
2823 host->flags &= ~SDHCI_NEEDS_RETUNING;
2824 }
2825
2826 spin_lock_irqsave(&host->lock, flags);
2827 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2828 spin_unlock_irqrestore(&host->lock, flags);
2829
2830 synchronize_irq(host->irq);
2831
2832 spin_lock_irqsave(&host->lock, flags);
2833 host->runtime_suspended = true;
2834 spin_unlock_irqrestore(&host->lock, flags);
2835
2836 return ret;
2837}
2838EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2839
2840int sdhci_runtime_resume_host(struct sdhci_host *host)
2841{
2842 unsigned long flags;
2843 int ret = 0, host_flags = host->flags;
2844
2845 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2846 if (host->ops->enable_dma)
2847 host->ops->enable_dma(host);
2848 }
2849
2850 sdhci_init(host, 0);
2851
2852 /* Force clock and power re-program */
2853 host->pwr = 0;
2854 host->clock = 0;
2855 sdhci_do_set_ios(host, &host->mmc->ios);
2856
2857 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2858 if (host_flags & SDHCI_PV_ENABLED)
2859 sdhci_do_enable_preset_value(host, true);
2860
2861 /* Set the re-tuning expiration flag */
2862 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2863 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2864 host->flags |= SDHCI_NEEDS_RETUNING;
2865
2866 spin_lock_irqsave(&host->lock, flags);
2867
2868 host->runtime_suspended = false;
2869
2870 /* Enable SDIO IRQ */
2871 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2872 sdhci_enable_sdio_irq_nolock(host, true);
2873
2874 /* Enable Card Detection */
2875 sdhci_enable_card_detection(host);
2876
2877 spin_unlock_irqrestore(&host->lock, flags);
2878
2879 return ret;
2880}
2881EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2882
2883#endif
2884
Pierre Ossmand129bce2006-03-24 03:18:17 -08002885/*****************************************************************************\
2886 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002887 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002888 * *
2889\*****************************************************************************/
2890
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002891struct sdhci_host *sdhci_alloc_host(struct device *dev,
2892 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002893{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002894 struct mmc_host *mmc;
2895 struct sdhci_host *host;
2896
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002897 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002898
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002899 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002900 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002901 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002902
2903 host = mmc_priv(mmc);
2904 host->mmc = mmc;
2905
Sahitya Tummala951c1202013-05-24 08:47:26 +05302906 spin_lock_init(&host->lock);
Sahitya Tummala40474e42013-07-10 14:40:37 +05302907 mutex_init(&host->ios_mutex);
Sahitya Tummala951c1202013-05-24 08:47:26 +05302908
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002909 return host;
2910}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002911
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002912EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002913
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002914int sdhci_add_host(struct sdhci_host *host)
2915{
2916 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302917 u32 caps[2];
2918 u32 max_current_caps;
2919 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002920 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002921
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002922 WARN_ON(host == NULL);
2923 if (host == NULL)
2924 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002925
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002926 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002927
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002928 if (debug_quirks)
2929 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03002930 if (debug_quirks2)
2931 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002932
Pierre Ossmand96649e2006-06-30 02:22:30 -07002933 sdhci_reset(host, SDHCI_RESET_ALL);
2934
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002935 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002936 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2937 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002938 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302939 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002940 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002941 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002942 }
2943
Arindam Nathf2119df2011-05-05 12:18:57 +05302944 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002945 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002946
Arindam Nathf2119df2011-05-05 12:18:57 +05302947 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2948 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2949
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002950 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002951 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302952 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002953 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002954 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002955 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002956
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002957 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002958 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002959 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002960 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002961 }
2962
Arindam Nathf2119df2011-05-05 12:18:57 +05302963 if ((host->version >= SDHCI_SPEC_200) &&
2964 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002965 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002966
2967 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2968 (host->flags & SDHCI_USE_ADMA)) {
2969 DBG("Disabling ADMA as it is marked broken\n");
2970 host->flags &= ~SDHCI_USE_ADMA;
2971 }
2972
Richard Röjforsa13abc72009-09-22 16:45:30 -07002973 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002974 if (host->ops->enable_dma) {
2975 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302976 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002977 "available. Falling back to PIO.\n",
2978 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002979 host->flags &=
2980 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002981 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002982 }
2983 }
2984
Pierre Ossman2134a922008-06-28 18:28:51 +02002985 if (host->flags & SDHCI_USE_ADMA) {
2986 /*
2987 * We need to allocate descriptors for all sg entries
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302988 * (128/max_segments) and potentially one alignment transfer for
Pierre Ossman2134a922008-06-28 18:28:51 +02002989 * each of those entries.
2990 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302991 if (host->ops->get_max_segments)
2992 host->adma_max_desc = host->ops->get_max_segments();
2993 else
2994 host->adma_max_desc = 128;
2995
2996 host->adma_desc_sz = (host->adma_max_desc * 2 + 1) * 4;
2997 host->align_buf_sz = host->adma_max_desc * 4;
2998
2999 pr_debug("%s: %s: dma_desc_size: %d\n",
3000 mmc_hostname(host->mmc), __func__, host->adma_desc_sz);
3001 host->adma_desc = kmalloc(host->adma_desc_sz,
3002 GFP_KERNEL);
3003 host->align_buffer = kmalloc(host->align_buf_sz,
3004 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02003005 if (!host->adma_desc || !host->align_buffer) {
3006 kfree(host->adma_desc);
3007 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05303008 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02003009 "buffers. Falling back to standard DMA.\n",
3010 mmc_hostname(mmc));
3011 host->flags &= ~SDHCI_USE_ADMA;
3012 }
3013 }
3014
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08003015 host->next_data.cookie = 1;
3016
Pierre Ossman76591502008-07-21 00:32:11 +02003017 /*
3018 * If we use DMA, then it's up to the caller to set the DMA
3019 * mask, but PIO does not need the hw shim so we set a new
3020 * mask here in that case.
3021 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003022 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003023 host->dma_mask = DMA_BIT_MASK(64);
3024 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3025 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003026
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003027 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05303028 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003029 >> SDHCI_CLOCK_BASE_SHIFT;
3030 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303031 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003032 >> SDHCI_CLOCK_BASE_SHIFT;
3033
Pierre Ossmand129bce2006-03-24 03:18:17 -08003034 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003035 if (host->max_clk == 0 || host->quirks &
3036 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003037 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303038 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03003039 "frequency.\n", mmc_hostname(mmc));
3040 return -ENODEV;
3041 }
3042 host->max_clk = host->ops->get_max_clock(host);
3043 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003044
3045 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303046 * In case of Host Controller v3.00, find out whether clock
3047 * multiplier is supported.
3048 */
3049 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3050 SDHCI_CLOCK_MUL_SHIFT;
3051
3052 /*
3053 * In case the value in Clock Multiplier is 0, then programmable
3054 * clock mode is not supported, otherwise the actual clock
3055 * multiplier is one more than the value of Clock Multiplier
3056 * in the Capabilities Register.
3057 */
3058 if (host->clk_mul)
3059 host->clk_mul += 1;
3060
3061 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003062 * Set host parameters.
3063 */
3064 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303065 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003066 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003067 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303068 else if (host->version >= SDHCI_SPEC_300) {
3069 if (host->clk_mul) {
3070 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3071 mmc->f_max = host->max_clk * host->clk_mul;
3072 } else
3073 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3074 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003075 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003076
Sahitya Tummalaca422112013-02-22 12:15:54 +05303077 host->timeout_clk =
3078 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
3079 if (host->timeout_clk == 0) {
3080 if (host->ops->get_timeout_clock) {
3081 host->timeout_clk = host->ops->get_timeout_clock(host);
3082 } else if (!(host->quirks &
3083 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3084 pr_err("%s: Hardware doesn't specify timeout clock "
3085 "frequency.\n", mmc_hostname(mmc));
3086 return -ENODEV;
3087 }
3088 }
3089 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3090 host->timeout_clk *= 1000;
3091
3092 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
3093 host->timeout_clk = mmc->f_max / 1000;
3094
Krishna Kondaa20d3362013-04-01 21:01:59 -07003095 if (!(host->quirks2 & SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE))
3096 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Sahitya Tummalaca422112013-02-22 12:15:54 +05303097
Andrei Warkentine89d4562011-05-23 15:06:37 -05003098 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
3099
3100 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3101 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003102
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003103 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003104 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003105 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003106 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003107 host->flags |= SDHCI_AUTO_CMD23;
3108 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3109 } else {
3110 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3111 }
3112
Philip Rakity15ec4462010-11-19 16:48:39 -05003113 /*
3114 * A controller may support 8-bit width, but the board itself
3115 * might not have the pins brought out. Boards that support
3116 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3117 * their platform code before calling sdhci_add_host(), and we
3118 * won't assume 8-bit width for hosts without that CAP.
3119 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003120 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003121 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003122
Arindam Nathf2119df2011-05-05 12:18:57 +05303123 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003124 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003125
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003126 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3127 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003128 mmc->caps |= MMC_CAP_NEEDS_POLL;
3129
Al Cooper4188bba2012-03-16 15:54:17 -04003130 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3131 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3132 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303133 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3134
3135 /* SDR104 supports also implies SDR50 support */
3136 if (caps[1] & SDHCI_SUPPORT_SDR104)
3137 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3138 else if (caps[1] & SDHCI_SUPPORT_SDR50)
3139 mmc->caps |= MMC_CAP_UHS_SDR50;
3140
3141 if (caps[1] & SDHCI_SUPPORT_DDR50)
3142 mmc->caps |= MMC_CAP_UHS_DDR50;
3143
Girish K S2cd06dc2012-01-06 09:56:39 +05303144 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303145 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3146 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3147
Girish K S2cd06dc2012-01-06 09:56:39 +05303148 /* Does the host need tuning for HS200? */
3149 if (mmc->caps2 & MMC_CAP2_HS200)
3150 host->flags |= SDHCI_HS200_NEEDS_TUNING;
3151
Arindam Nathd6d50a12011-05-05 12:18:59 +05303152 /* Driver Type(s) (A, C, D) supported by the host */
3153 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3154 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3155 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3156 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3157 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3158 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3159
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02003160 /* Initial value for re-tuning timer count */
3161 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3162 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3163
3164 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303165 * In case Re-tuning Timer is not disabled, the actual value of
3166 * re-tuning timer will be 2 ^ (n - 1).
3167 */
3168 if (host->tuning_count)
3169 host->tuning_count = 1 << (host->tuning_count - 1);
3170
3171 /* Re-tuning mode supported by the Host Controller */
3172 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3173 SDHCI_RETUNING_MODE_SHIFT;
3174
Takashi Iwai8f230f42010-12-08 10:04:30 +01003175 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05303176 /*
3177 * According to SD Host Controller spec v3.00, if the Host System
3178 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3179 * the value is meaningful only if Voltage Support in the Capabilities
3180 * register is set. The actual current value is 4 times the register
3181 * value.
3182 */
3183 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3184
3185 if (caps[0] & SDHCI_CAN_VDD_330) {
3186 int max_current_330;
3187
Takashi Iwai8f230f42010-12-08 10:04:30 +01003188 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303189
3190 max_current_330 = ((max_current_caps &
3191 SDHCI_MAX_CURRENT_330_MASK) >>
3192 SDHCI_MAX_CURRENT_330_SHIFT) *
3193 SDHCI_MAX_CURRENT_MULTIPLIER;
3194
3195 if (max_current_330 > 150)
3196 mmc->caps |= MMC_CAP_SET_XPC_330;
3197 }
3198 if (caps[0] & SDHCI_CAN_VDD_300) {
3199 int max_current_300;
3200
Takashi Iwai8f230f42010-12-08 10:04:30 +01003201 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303202
3203 max_current_300 = ((max_current_caps &
3204 SDHCI_MAX_CURRENT_300_MASK) >>
3205 SDHCI_MAX_CURRENT_300_SHIFT) *
3206 SDHCI_MAX_CURRENT_MULTIPLIER;
3207
3208 if (max_current_300 > 150)
3209 mmc->caps |= MMC_CAP_SET_XPC_300;
3210 }
3211 if (caps[0] & SDHCI_CAN_VDD_180) {
3212 int max_current_180;
3213
Takashi Iwai8f230f42010-12-08 10:04:30 +01003214 ocr_avail |= MMC_VDD_165_195;
3215
Arindam Nathf2119df2011-05-05 12:18:57 +05303216 max_current_180 = ((max_current_caps &
3217 SDHCI_MAX_CURRENT_180_MASK) >>
3218 SDHCI_MAX_CURRENT_180_SHIFT) *
3219 SDHCI_MAX_CURRENT_MULTIPLIER;
3220
3221 if (max_current_180 > 150)
3222 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05303223
3224 /* Maximum current capabilities of the host at 1.8V */
3225 if (max_current_180 >= 800)
3226 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
3227 else if (max_current_180 >= 600)
3228 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
3229 else if (max_current_180 >= 400)
3230 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
3231 else
3232 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05303233 }
3234
Takashi Iwai8f230f42010-12-08 10:04:30 +01003235 mmc->ocr_avail = ocr_avail;
3236 mmc->ocr_avail_sdio = ocr_avail;
3237 if (host->ocr_avail_sdio)
3238 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3239 mmc->ocr_avail_sd = ocr_avail;
3240 if (host->ocr_avail_sd)
3241 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3242 else /* normal SD controllers don't support 1.8V */
3243 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3244 mmc->ocr_avail_mmc = ocr_avail;
3245 if (host->ocr_avail_mmc)
3246 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003247
3248 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303249 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003250 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003251 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003252 }
3253
Pierre Ossmand129bce2006-03-24 03:18:17 -08003254 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003255 * Maximum number of segments. Depends on if the hardware
3256 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003257 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003258 if (host->flags & SDHCI_USE_ADMA)
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303259 mmc->max_segs = host->adma_max_desc;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003260 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003261 mmc->max_segs = 1;
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303262 else/* PIO */
3263 mmc->max_segs = host->adma_max_desc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003264
3265 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003266 * Maximum number of sectors in one transfer. Limited by DMA boundary
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303267 * size (512KiB), unless specified by platform specific driver. Each
3268 * descriptor can transfer a maximum of 64KB.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003269 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303270 if (host->ops->get_max_segments)
3271 mmc->max_req_size = (host->adma_max_desc * 65536);
3272 else
3273 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003274
3275 /*
3276 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003277 * of bytes. When doing hardware scatter/gather, each entry cannot
3278 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003279 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003280 if (host->flags & SDHCI_USE_ADMA) {
3281 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3282 mmc->max_seg_size = 65535;
3283 else
3284 mmc->max_seg_size = 65536;
3285 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003286 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003287 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003288
3289 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003290 * Maximum block size. This varies from controller to controller and
3291 * is specified in the capabilities register.
3292 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003293 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3294 mmc->max_blk_size = 2;
3295 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303296 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003297 SDHCI_MAX_BLOCK_SHIFT;
3298 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303299 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003300 "assuming 512 bytes\n", mmc_hostname(mmc));
3301 mmc->max_blk_size = 0;
3302 }
3303 }
3304
3305 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003306
3307 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003308 * Maximum block count.
3309 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003310 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003311
3312 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003313 * Init tasklets.
3314 */
3315 tasklet_init(&host->card_tasklet,
3316 sdhci_tasklet_card, (unsigned long)host);
3317 tasklet_init(&host->finish_tasklet,
3318 sdhci_tasklet_finish, (unsigned long)host);
3319
Al Viroe4cad1b2006-10-10 22:47:07 +01003320 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003321
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303322 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303323 init_waitqueue_head(&host->buf_ready_int);
3324
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303325 /* Initialize re-tuning timer */
3326 init_timer(&host->tuning_timer);
3327 host->tuning_timer.data = (unsigned long)host;
3328 host->tuning_timer.function = sdhci_tuning_timer;
3329 }
3330
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003331 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003332 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003333 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003334 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003335
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003336 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3337 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303338 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003339 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003340 }
3341
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003342 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003343
3344#ifdef CONFIG_MMC_DEBUG
3345 sdhci_dumpregs(host);
3346#endif
3347
Pierre Ossmanf9134312008-12-21 17:01:48 +01003348#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003349 snprintf(host->led_name, sizeof(host->led_name),
3350 "%s::", mmc_hostname(mmc));
3351 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003352 host->led.brightness = LED_OFF;
3353 host->led.default_trigger = mmc_hostname(mmc);
3354 host->led.brightness_set = sdhci_led_control;
3355
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003356 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003357 if (ret)
3358 goto reset;
3359#endif
3360
Pierre Ossman5f25a662006-10-04 02:15:39 -07003361 mmiowb();
3362
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303363 if (host->cpu_dma_latency_us)
3364 pm_qos_add_request(&host->pm_qos_req_dma,
3365 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003366 mmc_add_host(mmc);
3367
Sahitya Tummalaca422112013-02-22 12:15:54 +05303368 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003369 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003370 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3371 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003372
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003373 sdhci_enable_card_detection(host);
3374
Pierre Ossmand129bce2006-03-24 03:18:17 -08003375 return 0;
3376
Pierre Ossmanf9134312008-12-21 17:01:48 +01003377#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003378reset:
3379 sdhci_reset(host, SDHCI_RESET_ALL);
3380 free_irq(host->irq, host);
3381#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003382untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003383 tasklet_kill(&host->card_tasklet);
3384 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003385
3386 return ret;
3387}
3388
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003389EXPORT_SYMBOL_GPL(sdhci_add_host);
3390
Pierre Ossman1e728592008-04-16 19:13:13 +02003391void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003392{
Pierre Ossman1e728592008-04-16 19:13:13 +02003393 unsigned long flags;
3394
3395 if (dead) {
3396 spin_lock_irqsave(&host->lock, flags);
3397
3398 host->flags |= SDHCI_DEVICE_DEAD;
3399
3400 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303401 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003402 " transfer!\n", mmc_hostname(host->mmc));
3403
3404 host->mrq->cmd->error = -ENOMEDIUM;
3405 tasklet_schedule(&host->finish_tasklet);
3406 }
3407
3408 spin_unlock_irqrestore(&host->lock, flags);
3409 }
3410
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003411 sdhci_disable_card_detection(host);
3412
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303413 if (host->cpu_dma_latency_us)
3414 pm_qos_remove_request(&host->pm_qos_req_dma);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003415 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003416
Pierre Ossmanf9134312008-12-21 17:01:48 +01003417#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003418 led_classdev_unregister(&host->led);
3419#endif
3420
Pierre Ossman1e728592008-04-16 19:13:13 +02003421 if (!dead)
3422 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003423
3424 free_irq(host->irq, host);
3425
3426 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303427 if (host->version >= SDHCI_SPEC_300)
3428 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003429
3430 tasklet_kill(&host->card_tasklet);
3431 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003432
Sahitya Tummalaca422112013-02-22 12:15:54 +05303433 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003434 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003435
Pierre Ossman2134a922008-06-28 18:28:51 +02003436 kfree(host->adma_desc);
3437 kfree(host->align_buffer);
3438
3439 host->adma_desc = NULL;
3440 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003441}
3442
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003443EXPORT_SYMBOL_GPL(sdhci_remove_host);
3444
3445void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003446{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003447 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003448}
3449
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003450EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003451
3452/*****************************************************************************\
3453 * *
3454 * Driver init/exit *
3455 * *
3456\*****************************************************************************/
3457
3458static int __init sdhci_drv_init(void)
3459{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303460 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003461 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303462 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003463
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003464 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003465}
3466
3467static void __exit sdhci_drv_exit(void)
3468{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003469}
3470
3471module_init(sdhci_drv_init);
3472module_exit(sdhci_drv_exit);
3473
Pierre Ossmandf673b22006-06-30 02:22:31 -07003474module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003475module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003476
Pierre Ossman32710e82009-04-08 20:14:54 +02003477MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003478MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003479MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003480
Pierre Ossmandf673b22006-06-30 02:22:31 -07003481MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003482MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");