blob: 4f9bbad89d5095c9f8eb202342827f9b660df1f2 [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;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800703
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200704 /*
705 * If the host controller provides us with an incorrect timeout
706 * value, just skip the check and use 0xE. The hardware may take
707 * longer to time out, but that's much better than having a too-short
708 * timeout value.
709 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200710 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200711 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200712
Andrei Warkentina3c77782011-04-11 16:13:42 -0500713 /* Unspecified timeout, assume max */
714 if (!data && !cmd->cmd_timeout_ms)
715 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800716
Andrei Warkentina3c77782011-04-11 16:13:42 -0500717 /* timeout in us */
718 if (!data)
719 target_timeout = cmd->cmd_timeout_ms * 1000;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530720 else {
721 target_timeout = data->timeout_ns / 1000;
722 if (host->clock)
723 target_timeout += data->timeout_clks / host->clock;
724 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700725
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700726 /*
727 * Figure out needed cycles.
728 * We do this in steps in order to fit inside a 32 bit int.
729 * The first step is the minimum timeout, which will have a
730 * minimum resolution of 6 bits:
731 * (1) 2^13*1000 > 2^22,
732 * (2) host->timeout_clk < 2^16
733 * =>
734 * (1) / (2) > 2^6
735 */
736 count = 0;
737 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
738 while (current_timeout < target_timeout) {
739 count++;
740 current_timeout <<= 1;
741 if (count >= 0xF)
742 break;
743 }
744
Sahitya Tummala4d12d0b2013-04-12 11:59:25 +0530745 if (!(host->quirks2 & SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT)) {
746 if (count >= 0xF) {
747 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
748 mmc_hostname(host->mmc), count, cmd->opcode);
749 count = 0xE;
750 }
Sahitya Tummalaca422112013-02-22 12:15:54 +0530751 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700752
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200753 return count;
754}
755
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300756static void sdhci_set_transfer_irqs(struct sdhci_host *host)
757{
758 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
759 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
760
761 if (host->flags & SDHCI_REQ_USE_DMA)
762 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
763 else
764 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
765}
766
Andrei Warkentina3c77782011-04-11 16:13:42 -0500767static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200768{
769 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200770 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500771 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200772 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200773
774 WARN_ON(host->data);
775
Andrei Warkentina3c77782011-04-11 16:13:42 -0500776 if (data || (cmd->flags & MMC_RSP_BUSY)) {
777 count = sdhci_calc_timeout(host, cmd);
778 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
779 }
780
781 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200782 return;
783
784 /* Sanity checks */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530785 BUG_ON(data->blksz * data->blocks > host->mmc->max_req_size);
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200786 BUG_ON(data->blksz > host->mmc->max_blk_size);
787 BUG_ON(data->blocks > 65535);
788
789 host->data = data;
790 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400791 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200792
Richard Röjforsa13abc72009-09-22 16:45:30 -0700793 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100794 host->flags |= SDHCI_REQ_USE_DMA;
795
Pierre Ossman2134a922008-06-28 18:28:51 +0200796 /*
797 * FIXME: This doesn't account for merging when mapping the
798 * scatterlist.
799 */
800 if (host->flags & SDHCI_REQ_USE_DMA) {
801 int broken, i;
802 struct scatterlist *sg;
803
804 broken = 0;
805 if (host->flags & SDHCI_USE_ADMA) {
806 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
807 broken = 1;
808 } else {
809 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
810 broken = 1;
811 }
812
813 if (unlikely(broken)) {
814 for_each_sg(data->sg, sg, data->sg_len, i) {
815 if (sg->length & 0x3) {
816 DBG("Reverting to PIO because of "
817 "transfer size (%d)\n",
818 sg->length);
819 host->flags &= ~SDHCI_REQ_USE_DMA;
820 break;
821 }
822 }
823 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100824 }
825
826 /*
827 * The assumption here being that alignment is the same after
828 * translation to device address space.
829 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200830 if (host->flags & SDHCI_REQ_USE_DMA) {
831 int broken, i;
832 struct scatterlist *sg;
833
834 broken = 0;
835 if (host->flags & SDHCI_USE_ADMA) {
836 /*
837 * As we use 3 byte chunks to work around
838 * alignment problems, we need to check this
839 * quirk.
840 */
841 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
842 broken = 1;
843 } else {
844 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
845 broken = 1;
846 }
847
848 if (unlikely(broken)) {
849 for_each_sg(data->sg, sg, data->sg_len, i) {
850 if (sg->offset & 0x3) {
851 DBG("Reverting to PIO because of "
852 "bad alignment\n");
853 host->flags &= ~SDHCI_REQ_USE_DMA;
854 break;
855 }
856 }
857 }
858 }
859
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200860 if (host->flags & SDHCI_REQ_USE_DMA) {
861 if (host->flags & SDHCI_USE_ADMA) {
862 ret = sdhci_adma_table_pre(host, data);
863 if (ret) {
864 /*
865 * This only happens when someone fed
866 * us an invalid request.
867 */
868 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200869 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200870 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300871 sdhci_writel(host, host->adma_addr,
872 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200873 }
874 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300875 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200876
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800877 sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300878 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200879 /*
880 * This only happens when someone fed
881 * us an invalid request.
882 */
883 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200884 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200885 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200886 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300887 sdhci_writel(host, sg_dma_address(data->sg),
888 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200889 }
890 }
891 }
892
Pierre Ossman2134a922008-06-28 18:28:51 +0200893 /*
894 * Always adjust the DMA selection as some controllers
895 * (e.g. JMicron) can't do PIO properly when the selection
896 * is ADMA.
897 */
898 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300899 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200900 ctrl &= ~SDHCI_CTRL_DMA_MASK;
901 if ((host->flags & SDHCI_REQ_USE_DMA) &&
902 (host->flags & SDHCI_USE_ADMA))
903 ctrl |= SDHCI_CTRL_ADMA32;
904 else
905 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300906 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100907 }
908
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200909 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200910 int flags;
911
912 flags = SG_MITER_ATOMIC;
913 if (host->data->flags & MMC_DATA_READ)
914 flags |= SG_MITER_TO_SG;
915 else
916 flags |= SG_MITER_FROM_SG;
917 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200918 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800919 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700920
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300921 sdhci_set_transfer_irqs(host);
922
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400923 /* Set the DMA boundary value and block size */
924 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
925 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300926 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700927}
928
929static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500930 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700931{
932 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500933 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700934
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700935 if (data == NULL)
936 return;
937
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200938 WARN_ON(!host->data);
939
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700940 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500941 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
942 mode |= SDHCI_TRNS_MULTI;
943 /*
944 * If we are sending CMD23, CMD12 never gets sent
945 * on successful completion (so no Auto-CMD12).
946 */
947 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
948 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500949 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
950 mode |= SDHCI_TRNS_AUTO_CMD23;
951 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
952 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700953 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500954
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530955 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700956 mode |= SDHCI_TRNS_READ;
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530957 if (host->ops->toggle_cdr)
958 host->ops->toggle_cdr(host, true);
959 }
960 if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE))
961 host->ops->toggle_cdr(host, false);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100962 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700963 mode |= SDHCI_TRNS_DMA;
964
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300965 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800966}
967
968static void sdhci_finish_data(struct sdhci_host *host)
969{
970 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800971
972 BUG_ON(!host->data);
973
974 data = host->data;
975 host->data = NULL;
976
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100977 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200978 if (host->flags & SDHCI_USE_ADMA)
979 sdhci_adma_table_post(host, data);
980 else {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800981 if (!data->host_cookie)
982 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
983 data->sg_len,
984 (data->flags & MMC_DATA_READ) ?
985 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200986 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800987 }
988
989 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200990 * The specification states that the block count register must
991 * be updated, but it does not specify at what point in the
992 * data flow. That makes the register entirely useless to read
993 * back so we have to assume that nothing made it to the card
994 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800995 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200996 if (data->error)
997 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200999 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001000
Andrei Warkentine89d4562011-05-23 15:06:37 -05001001 /*
1002 * Need to send CMD12 if -
1003 * a) open-ended multiblock transfer (no CMD23)
1004 * b) error in multiblock transfer
1005 */
1006 if (data->stop &&
1007 (data->error ||
1008 !host->mrq->sbc)) {
1009
Pierre Ossmand129bce2006-03-24 03:18:17 -08001010 /*
1011 * The controller needs a reset of internal state machines
1012 * upon error conditions.
1013 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001014 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001015 sdhci_reset(host, SDHCI_RESET_CMD);
1016 sdhci_reset(host, SDHCI_RESET_DATA);
1017 }
1018
1019 sdhci_send_command(host, data->stop);
1020 } else
1021 tasklet_schedule(&host->finish_tasklet);
1022}
1023
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301024#define SDHCI_REQUEST_TIMEOUT 10 /* Default request timeout in seconds */
1025
Pierre Ossmand129bce2006-03-24 03:18:17 -08001026static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
1027{
1028 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001029 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001030 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001031
1032 WARN_ON(host->cmd);
1033
Pierre Ossmand129bce2006-03-24 03:18:17 -08001034 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001035 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001036
1037 mask = SDHCI_CMD_INHIBIT;
1038 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1039 mask |= SDHCI_DATA_INHIBIT;
1040
1041 /* We shouldn't wait for data inihibit for stop commands, even
1042 though they might use busy signaling */
1043 if (host->mrq->data && (cmd == host->mrq->data->stop))
1044 mask &= ~SDHCI_DATA_INHIBIT;
1045
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001046 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001047 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301048 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001049 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001050 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001051 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001052 tasklet_schedule(&host->finish_tasklet);
1053 return;
1054 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001055 timeout--;
1056 mdelay(1);
1057 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001058
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301059 mod_timer(&host->timer, jiffies + SDHCI_REQUEST_TIMEOUT * HZ);
1060
1061 if (cmd->cmd_timeout_ms > SDHCI_REQUEST_TIMEOUT * MSEC_PER_SEC)
1062 mod_timer(&host->timer, jiffies +
1063 (msecs_to_jiffies(cmd->cmd_timeout_ms * 2)));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001064
1065 host->cmd = cmd;
1066
Andrei Warkentina3c77782011-04-11 16:13:42 -05001067 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001068
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001069 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001070
Andrei Warkentine89d4562011-05-23 15:06:37 -05001071 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001072
Pierre Ossmand129bce2006-03-24 03:18:17 -08001073 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301074 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001075 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001076 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001077 tasklet_schedule(&host->finish_tasklet);
1078 return;
1079 }
1080
1081 if (!(cmd->flags & MMC_RSP_PRESENT))
1082 flags = SDHCI_CMD_RESP_NONE;
1083 else if (cmd->flags & MMC_RSP_136)
1084 flags = SDHCI_CMD_RESP_LONG;
1085 else if (cmd->flags & MMC_RSP_BUSY)
1086 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1087 else
1088 flags = SDHCI_CMD_RESP_SHORT;
1089
1090 if (cmd->flags & MMC_RSP_CRC)
1091 flags |= SDHCI_CMD_CRC;
1092 if (cmd->flags & MMC_RSP_OPCODE)
1093 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301094
1095 /* CMD19 is special in that the Data Present Select should be set */
Girish K S2cd06dc2012-01-06 09:56:39 +05301096 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1097 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001098 flags |= SDHCI_CMD_DATA;
1099
Sahitya Tummala48b458e2013-04-08 12:53:44 +05301100 if (cmd->data)
1101 host->data_start_time = ktime_get();
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001102 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001103}
1104
1105static void sdhci_finish_command(struct sdhci_host *host)
1106{
1107 int i;
1108
1109 BUG_ON(host->cmd == NULL);
1110
1111 if (host->cmd->flags & MMC_RSP_PRESENT) {
1112 if (host->cmd->flags & MMC_RSP_136) {
1113 /* CRC is stripped so we need to do some shifting. */
1114 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001115 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001116 SDHCI_RESPONSE + (3-i)*4) << 8;
1117 if (i != 3)
1118 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001119 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001120 SDHCI_RESPONSE + (3-i)*4-1);
1121 }
1122 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001123 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001124 }
1125 }
1126
Pierre Ossman17b04292007-07-22 22:18:46 +02001127 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001128
Andrei Warkentine89d4562011-05-23 15:06:37 -05001129 /* Finished CMD23, now send actual command. */
1130 if (host->cmd == host->mrq->sbc) {
1131 host->cmd = NULL;
1132 sdhci_send_command(host, host->mrq->cmd);
1133 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001134
Andrei Warkentine89d4562011-05-23 15:06:37 -05001135 /* Processed actual command. */
1136 if (host->data && host->data_early)
1137 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001138
Andrei Warkentine89d4562011-05-23 15:06:37 -05001139 if (!host->cmd->data)
1140 tasklet_schedule(&host->finish_tasklet);
1141
1142 host->cmd = NULL;
1143 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001144}
1145
1146static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1147{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301148 int div = 0; /* Initialized for compiler warning */
Sahitya Tummalaca422112013-02-22 12:15:54 +05301149 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301150 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001151 unsigned long timeout;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301152 unsigned long flags;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001153
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301154 spin_lock_irqsave(&host->lock, flags);
Todd Poynor30832ab2011-12-27 15:48:46 +02001155 if (clock && clock == host->clock)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301156 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001157
Sahitya Tummalaca422112013-02-22 12:15:54 +05301158 host->mmc->actual_clock = 0;
1159
Anton Vorontsov81146342009-03-17 00:13:59 +03001160 if (host->ops->set_clock) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301161 spin_unlock_irqrestore(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001162 host->ops->set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301163 spin_lock_irqsave(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001164 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301165 goto ret;
Anton Vorontsov81146342009-03-17 00:13:59 +03001166 }
1167
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301168 if (host->clock)
1169 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001170
1171 if (clock == 0)
1172 goto out;
1173
Zhangfei Gao85105c52010-08-06 07:10:01 +08001174 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301175 /*
1176 * Check if the Host Controller supports Programmable Clock
1177 * Mode.
1178 */
1179 if (host->clk_mul) {
1180 u16 ctrl;
1181
1182 /*
1183 * We need to figure out whether the Host Driver needs
1184 * to select Programmable Clock Mode, or the value can
1185 * be set automatically by the Host Controller based on
1186 * the Preset Value registers.
1187 */
1188 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1189 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1190 for (div = 1; div <= 1024; div++) {
1191 if (((host->max_clk * host->clk_mul) /
1192 div) <= clock)
1193 break;
1194 }
1195 /*
1196 * Set Programmable Clock Mode in the Clock
1197 * Control register.
1198 */
1199 clk = SDHCI_PROG_CLOCK_MODE;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301200 real_div = div;
1201 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301202 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001203 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301204 } else {
1205 /* Version 3.00 divisors must be a multiple of 2. */
1206 if (host->max_clk <= clock)
1207 div = 1;
1208 else {
1209 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1210 div += 2) {
1211 if ((host->max_clk / div) <= clock)
1212 break;
1213 }
1214 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301215 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301216 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001217 }
1218 } else {
1219 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001220 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001221 if ((host->max_clk / div) <= clock)
1222 break;
1223 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301224 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301225 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001226 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001227
Sahitya Tummalaca422112013-02-22 12:15:54 +05301228 if (real_div)
1229 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1230
Sahitya Tummala00240122013-02-28 19:50:51 +05301231 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
1232 div = 0;
1233
Arindam Nathc3ed3872011-05-05 12:19:06 +05301234 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001235 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1236 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001237 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001238 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001239
Chris Ball27f6cb12009-09-22 16:45:31 -07001240 /* Wait max 20 ms */
1241 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001242 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001243 & SDHCI_CLOCK_INT_STABLE)) {
1244 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301245 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001246 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001247 sdhci_dumpregs(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301248 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001249 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001250 timeout--;
1251 mdelay(1);
1252 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001253
1254 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001255 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001256
1257out:
1258 host->clock = clock;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301259ret:
1260 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001261}
1262
Sahitya Tummalaca422112013-02-22 12:15:54 +05301263static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001264{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001265 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001266
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001267 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001268 switch (1 << power) {
1269 case MMC_VDD_165_195:
1270 pwr = SDHCI_POWER_180;
1271 break;
1272 case MMC_VDD_29_30:
1273 case MMC_VDD_30_31:
1274 pwr = SDHCI_POWER_300;
1275 break;
1276 case MMC_VDD_32_33:
1277 case MMC_VDD_33_34:
1278 pwr = SDHCI_POWER_330;
1279 break;
1280 default:
1281 BUG();
1282 }
1283 }
1284
1285 if (host->pwr == pwr)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301286 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001287
Pierre Ossmanae628902009-05-03 20:45:03 +02001288 host->pwr = pwr;
1289
1290 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001291 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301292 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301293 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301294 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001295 }
1296
1297 /*
1298 * Spec says that we should clear the power reg before setting
1299 * a new value. Some controllers don't seem to like this though.
1300 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301301 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001302 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301303 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301304 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301305 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001306
Andres Salomone08c1692008-07-04 10:00:03 -07001307 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001308 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001309 * and set turn on power at the same time, so set the voltage first.
1310 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301311 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001312 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301313 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301314 host->ops->check_power_status(host, REQ_BUS_ON);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301315 }
Pierre Ossmanae628902009-05-03 20:45:03 +02001316
1317 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001318
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001319 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301320 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301321 host->ops->check_power_status(host, REQ_BUS_ON);
Harald Welte557b0692009-06-18 16:53:38 +02001322
1323 /*
1324 * Some controllers need an extra 10ms delay of 10ms before they
1325 * can apply clock after applying power
1326 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001327 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001328 mdelay(10);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301329
1330 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001331}
1332
Pierre Ossmand129bce2006-03-24 03:18:17 -08001333/*****************************************************************************\
1334 * *
1335 * MMC callbacks *
1336 * *
1337\*****************************************************************************/
1338
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301339static int sdhci_enable(struct mmc_host *mmc)
1340{
1341 struct sdhci_host *host = mmc_priv(mmc);
1342
1343 if (host->cpu_dma_latency_us)
1344 pm_qos_update_request(&host->pm_qos_req_dma,
1345 host->cpu_dma_latency_us);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301346 if (host->ops->platform_bus_voting)
1347 host->ops->platform_bus_voting(host, 1);
1348
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301349 return 0;
1350}
1351
1352static int sdhci_disable(struct mmc_host *mmc)
1353{
1354 struct sdhci_host *host = mmc_priv(mmc);
1355
1356 if (host->cpu_dma_latency_us)
1357 pm_qos_update_request(&host->pm_qos_req_dma,
1358 PM_QOS_DEFAULT_VALUE);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301359 if (host->ops->platform_bus_voting)
1360 host->ops->platform_bus_voting(host, 0);
1361
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301362 return 0;
1363}
1364
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001365static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1366 bool is_first_req)
1367{
1368 struct sdhci_host *host = mmc_priv(mmc);
1369
1370 if (mrq->data->host_cookie) {
1371 mrq->data->host_cookie = 0;
1372 return;
1373 }
1374
1375 if (host->flags & SDHCI_REQ_USE_DMA)
1376 if (sdhci_pre_dma_transfer(host, mrq->data, &host->next_data) < 0)
1377 mrq->data->host_cookie = 0;
1378}
1379
1380static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1381 int err)
1382{
1383 struct sdhci_host *host = mmc_priv(mmc);
1384 struct mmc_data *data = mrq->data;
1385
1386 if (host->flags & SDHCI_REQ_USE_DMA) {
1387 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1388 (data->flags & MMC_DATA_WRITE) ?
1389 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1390 data->host_cookie = 0;
1391 }
1392}
1393
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301394static bool sdhci_check_state(struct sdhci_host *host)
1395{
1396 struct mmc_host *mmc = host->mmc;
1397
1398 if (!host->clock || !host->pwr ||
Sahitya Tummala1b248c42013-05-24 14:08:10 +05301399 (mmc_use_core_runtime_pm(mmc) ?
1400 pm_runtime_suspended(mmc->parent) : 0))
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301401 return true;
1402 else
1403 return false;
1404}
1405
Pierre Ossmand129bce2006-03-24 03:18:17 -08001406static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1407{
1408 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001409 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001410 unsigned long flags;
1411
1412 host = mmc_priv(mmc);
1413
Adrian Hunter50accb92011-10-03 15:33:34 +03001414 sdhci_runtime_pm_get(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301415 if (sdhci_check_state(host)) {
1416 sdhci_dump_state(host);
1417 WARN(1, "sdhci in bad state");
1418 mrq->cmd->error = -EIO;
1419 if (mrq->data)
1420 mrq->data->error = -EIO;
1421 tasklet_schedule(&host->finish_tasklet);
1422 return;
1423 }
Adrian Hunter50accb92011-10-03 15:33:34 +03001424
Pierre Ossmand129bce2006-03-24 03:18:17 -08001425 spin_lock_irqsave(&host->lock, flags);
1426
1427 WARN_ON(host->mrq != NULL);
1428
Pierre Ossmanf9134312008-12-21 17:01:48 +01001429#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001430 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001431#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001432
1433 /*
1434 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1435 * requests if Auto-CMD12 is enabled.
1436 */
1437 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001438 if (mrq->stop) {
1439 mrq->data->stop = NULL;
1440 mrq->stop = NULL;
1441 }
1442 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001443
1444 host->mrq = mrq;
1445
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001446 /* If polling, assume that the card is always present. */
1447 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1448 present = true;
1449 else
1450 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1451 SDHCI_CARD_PRESENT;
1452
1453 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001454 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001455 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301456 } else {
1457 u32 present_state;
1458
1459 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1460 /*
1461 * Check if the re-tuning timer has already expired and there
1462 * is no on-going data transfer. If so, we need to execute
1463 * tuning procedure before sending command.
1464 */
1465 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1466 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1467 spin_unlock_irqrestore(&host->lock, flags);
Girish K S2cd06dc2012-01-06 09:56:39 +05301468 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301469 spin_lock_irqsave(&host->lock, flags);
1470
1471 /* Restore original mmc_request structure */
1472 host->mrq = mrq;
1473 }
1474
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001475 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001476 sdhci_send_command(host, mrq->sbc);
1477 else
1478 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301479 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001480
Pierre Ossman5f25a662006-10-04 02:15:39 -07001481 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001482 spin_unlock_irqrestore(&host->lock, flags);
1483}
1484
Adrian Hunter50accb92011-10-03 15:33:34 +03001485static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001486{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001487 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301488 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001489 u8 ctrl;
1490
Sahitya Tummalaca422112013-02-22 12:15:54 +05301491 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301492 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1493 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1494 return;
1495 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001496
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301497 if (ios->clock)
1498 sdhci_set_clock(host, ios->clock);
1499
1500 spin_lock_irqsave(&host->lock, flags);
1501 if (!host->clock) {
1502 spin_unlock_irqrestore(&host->lock, flags);
1503 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001504 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301505 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001506
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301507 if (ios->power_mode & (MMC_POWER_UP | MMC_POWER_ON))
Sahitya Tummalaca422112013-02-22 12:15:54 +05301508 vdd_bit = sdhci_set_power(host, ios->vdd);
1509
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301510 if (host->vmmc && vdd_bit != -1)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301511 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301512
1513 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001514
Philip Rakity643a81f2010-09-23 08:24:32 -07001515 if (host->ops->platform_send_init_74_clocks)
1516 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1517
Philip Rakity15ec4462010-11-19 16:48:39 -05001518 /*
1519 * If your platform has 8-bit width support but is not a v3 controller,
1520 * or if it requires special setup code, you should implement that in
1521 * platform_8bit_width().
1522 */
1523 if (host->ops->platform_8bit_width)
1524 host->ops->platform_8bit_width(host, ios->bus_width);
1525 else {
1526 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1527 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1528 ctrl &= ~SDHCI_CTRL_4BITBUS;
1529 if (host->version >= SDHCI_SPEC_300)
1530 ctrl |= SDHCI_CTRL_8BITBUS;
1531 } else {
1532 if (host->version >= SDHCI_SPEC_300)
1533 ctrl &= ~SDHCI_CTRL_8BITBUS;
1534 if (ios->bus_width == MMC_BUS_WIDTH_4)
1535 ctrl |= SDHCI_CTRL_4BITBUS;
1536 else
1537 ctrl &= ~SDHCI_CTRL_4BITBUS;
1538 }
1539 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1540 }
1541
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001542 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001543
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001544 if ((ios->timing == MMC_TIMING_SD_HS ||
1545 ios->timing == MMC_TIMING_MMC_HS)
1546 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001547 ctrl |= SDHCI_CTRL_HISPD;
1548 else
1549 ctrl &= ~SDHCI_CTRL_HISPD;
1550
Arindam Nathd6d50a12011-05-05 12:18:59 +05301551 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301552 u16 clk, ctrl_2;
1553 unsigned int clock;
1554
1555 /* In case of UHS-I modes, set High Speed Enable */
Girish K S2cd06dc2012-01-06 09:56:39 +05301556 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1557 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301558 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1559 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001560 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301561 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301562
1563 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1564 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301565 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301566 /*
1567 * We only need to set Driver Strength if the
1568 * preset value enable is not set.
1569 */
1570 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1571 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1572 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1573 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1574 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1575
1576 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301577 } else {
1578 /*
1579 * According to SDHC Spec v3.00, if the Preset Value
1580 * Enable in the Host Control 2 register is set, we
1581 * need to reset SD Clock Enable before changing High
1582 * Speed Enable to avoid generating clock gliches.
1583 */
Arindam Nath758535c2011-05-05 12:19:00 +05301584
1585 /* Reset SD Clock Enable */
1586 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1587 clk &= ~SDHCI_CLOCK_CARD_EN;
1588 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1589
1590 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1591
1592 /* Re-enable SD Clock */
1593 clock = host->clock;
1594 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301595 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301596 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301597 spin_lock_irqsave(&host->lock, flags);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301598 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301599
Arindam Nath49c468f2011-05-05 12:19:01 +05301600 /* Reset SD Clock Enable */
1601 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1602 clk &= ~SDHCI_CLOCK_CARD_EN;
1603 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1604
Philip Rakity6322cdd2011-05-13 11:17:15 +05301605 if (host->ops->set_uhs_signaling)
1606 host->ops->set_uhs_signaling(host, ios->timing);
1607 else {
1608 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1609 /* Select Bus Speed Mode for host */
1610 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S2cd06dc2012-01-06 09:56:39 +05301611 if (ios->timing == MMC_TIMING_MMC_HS200)
1612 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1613 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301614 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1615 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1616 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1617 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1618 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1619 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1620 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1621 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1622 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1623 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1624 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301625
1626 /* Re-enable SD Clock */
1627 clock = host->clock;
1628 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301629 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath49c468f2011-05-05 12:19:01 +05301630 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301631 spin_lock_irqsave(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301632 } else
1633 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301634
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301635 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02001636 /*
1637 * Some (ENE) controllers go apeshit on some ios operation,
1638 * signalling timeout and CRC errors even on CMD0. Resetting
1639 * it on each ios seems to solve the problem.
1640 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001641 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001642 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1643
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301644 /*
1645 * Reset the chip on each power off.
1646 * Should clear out any weird states.
1647 */
1648 if (ios->power_mode == MMC_POWER_OFF) {
1649 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1650 sdhci_reinit(host);
1651 vdd_bit = sdhci_set_power(host, -1);
1652 if (host->vmmc && vdd_bit != -1)
1653 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1654 }
1655 if (!ios->clock)
1656 sdhci_set_clock(host, ios->clock);
1657
Pierre Ossman5f25a662006-10-04 02:15:39 -07001658 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001659}
1660
Adrian Hunter50accb92011-10-03 15:33:34 +03001661static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1662{
1663 struct sdhci_host *host = mmc_priv(mmc);
1664
1665 sdhci_runtime_pm_get(host);
1666 sdhci_do_set_ios(host, ios);
1667 sdhci_runtime_pm_put(host);
1668}
1669
1670static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001671{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001672 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001673 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001674
Pierre Ossmand129bce2006-03-24 03:18:17 -08001675 spin_lock_irqsave(&host->lock, flags);
1676
Pierre Ossman1e728592008-04-16 19:13:13 +02001677 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001678 is_readonly = 0;
1679 else if (host->ops->get_ro)
1680 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001681 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001682 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1683 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001684
1685 spin_unlock_irqrestore(&host->lock, flags);
1686
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001687 /* This quirk needs to be replaced by a callback-function later */
1688 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1689 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001690}
1691
Takashi Iwai82b0e232011-04-21 20:26:38 +02001692#define SAMPLE_COUNT 5
1693
Adrian Hunter50accb92011-10-03 15:33:34 +03001694static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001695{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001696 int i, ro_count;
1697
Takashi Iwai82b0e232011-04-21 20:26:38 +02001698 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001699 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001700
1701 ro_count = 0;
1702 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001703 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001704 if (++ro_count > SAMPLE_COUNT / 2)
1705 return 1;
1706 }
1707 msleep(30);
1708 }
1709 return 0;
1710}
1711
Adrian Hunter50accb92011-10-03 15:33:34 +03001712static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001713{
Adrian Hunter50accb92011-10-03 15:33:34 +03001714 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001715
Adrian Hunter50accb92011-10-03 15:33:34 +03001716 if (host->ops && host->ops->hw_reset)
1717 host->ops->hw_reset(host);
1718}
Adrian Hunter20758b62011-08-29 16:42:12 +03001719
Adrian Hunter50accb92011-10-03 15:33:34 +03001720static int sdhci_get_ro(struct mmc_host *mmc)
1721{
1722 struct sdhci_host *host = mmc_priv(mmc);
1723 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001724
Adrian Hunter50accb92011-10-03 15:33:34 +03001725 sdhci_runtime_pm_get(host);
1726 ret = sdhci_do_get_ro(host);
1727 sdhci_runtime_pm_put(host);
1728 return ret;
1729}
1730
1731static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1732{
Pierre Ossman1e728592008-04-16 19:13:13 +02001733 if (host->flags & SDHCI_DEVICE_DEAD)
1734 goto out;
1735
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001736 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001737 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1738 else
1739 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1740
1741 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1742 if (host->runtime_suspended)
1743 goto out;
1744
1745 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001746 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1747 else
1748 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001749out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001750 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001751}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001752
Adrian Hunter50accb92011-10-03 15:33:34 +03001753static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1754{
1755 struct sdhci_host *host = mmc_priv(mmc);
1756 unsigned long flags;
1757
1758 spin_lock_irqsave(&host->lock, flags);
1759 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001760 spin_unlock_irqrestore(&host->lock, flags);
1761}
1762
Adrian Hunter50accb92011-10-03 15:33:34 +03001763static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1764 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301765{
Arindam Nathf2119df2011-05-05 12:18:57 +05301766 u8 pwr;
1767 u16 clk, ctrl;
1768 u32 present_state;
1769
Arindam Nathf2119df2011-05-05 12:18:57 +05301770 /*
1771 * Signal Voltage Switching is only applicable for Host Controllers
1772 * v3.00 and above.
1773 */
1774 if (host->version < SDHCI_SPEC_300)
1775 return 0;
1776
1777 /*
1778 * We first check whether the request is to set signalling voltage
1779 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1780 */
1781 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1782 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1783 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1784 ctrl &= ~SDHCI_CTRL_VDD_180;
1785 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301786 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301787 host->ops->check_power_status(host, REQ_IO_HIGH);
Arindam Nathf2119df2011-05-05 12:18:57 +05301788
1789 /* Wait for 5ms */
1790 usleep_range(5000, 5500);
1791
1792 /* 3.3V regulator output should be stable within 5 ms */
1793 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1794 if (!(ctrl & SDHCI_CTRL_VDD_180))
1795 return 0;
1796 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301797 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301798 "signalling voltage failed\n");
1799 return -EIO;
1800 }
1801 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1802 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1803 /* Stop SDCLK */
1804 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1805 clk &= ~SDHCI_CLOCK_CARD_EN;
1806 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1807
1808 /* Check whether DAT[3:0] is 0000 */
1809 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1810 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1811 SDHCI_DATA_LVL_SHIFT)) {
1812 /*
1813 * Enable 1.8V Signal Enable in the Host Control2
1814 * register
1815 */
1816 ctrl |= SDHCI_CTRL_VDD_180;
1817 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301818 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301819 host->ops->check_power_status(host, REQ_IO_LOW);
Arindam Nathf2119df2011-05-05 12:18:57 +05301820
1821 /* Wait for 5ms */
1822 usleep_range(5000, 5500);
1823
1824 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1825 if (ctrl & SDHCI_CTRL_VDD_180) {
1826 /* Provide SDCLK again and wait for 1ms*/
1827 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1828 clk |= SDHCI_CLOCK_CARD_EN;
1829 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1830 usleep_range(1000, 1500);
1831
1832 /*
1833 * If DAT[3:0] level is 1111b, then the card
1834 * was successfully switched to 1.8V signaling.
1835 */
1836 present_state = sdhci_readl(host,
1837 SDHCI_PRESENT_STATE);
1838 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1839 SDHCI_DATA_LVL_MASK)
1840 return 0;
1841 }
1842 }
1843
1844 /*
1845 * If we are here, that means the switch to 1.8V signaling
1846 * failed. We power cycle the card, and retry initialization
1847 * sequence by setting S18R to 0.
1848 */
1849 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1850 pwr &= ~SDHCI_POWER_ON;
1851 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301852 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301853 host->ops->check_power_status(host, REQ_BUS_OFF);
Arindam Nathf2119df2011-05-05 12:18:57 +05301854
1855 /* Wait for 1ms as per the spec */
1856 usleep_range(1000, 1500);
1857 pwr |= SDHCI_POWER_ON;
1858 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301859 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301860 host->ops->check_power_status(host, REQ_BUS_ON);
Arindam Nathf2119df2011-05-05 12:18:57 +05301861
Sahitya Tummalaca422112013-02-22 12:15:54 +05301862 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301863 "voltage failed, retrying with S18R set to 0\n");
1864 return -EAGAIN;
1865 } else
1866 /* No signal voltage switch required */
1867 return 0;
1868}
1869
Adrian Hunter50accb92011-10-03 15:33:34 +03001870static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1871 struct mmc_ios *ios)
1872{
1873 struct sdhci_host *host = mmc_priv(mmc);
1874 int err;
1875
1876 if (host->version < SDHCI_SPEC_300)
1877 return 0;
1878 sdhci_runtime_pm_get(host);
1879 err = sdhci_do_start_signal_voltage_switch(host, ios);
1880 sdhci_runtime_pm_put(host);
1881 return err;
1882}
1883
Girish K S2cd06dc2012-01-06 09:56:39 +05301884static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301885{
1886 struct sdhci_host *host;
1887 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05301888 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05301889 int tuning_loop_counter = MAX_TUNING_LOOP;
1890 unsigned long timeout;
1891 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05301892 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301893
1894 host = mmc_priv(mmc);
1895
Adrian Hunter50accb92011-10-03 15:33:34 +03001896 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301897 disable_irq(host->irq);
1898 spin_lock(&host->lock);
1899
1900 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1901
1902 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05301903 * The Host Controller needs tuning only in case of SDR104 mode
1904 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301905 * Capabilities register.
Girish K S2cd06dc2012-01-06 09:56:39 +05301906 * If the Host Controller supports the HS200 mode then the
1907 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301908 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08001909 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1910 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
1911 (host->flags & SDHCI_HS200_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05301912 requires_tuning_nonuhs = true;
1913
Arindam Nathb513ea22011-05-05 12:19:04 +05301914 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301915 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301916 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1917 else {
1918 spin_unlock(&host->lock);
1919 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03001920 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301921 return 0;
1922 }
1923
Asutosh Das8ddd3482013-01-04 11:45:46 +05301924 if (host->ops->execute_tuning) {
1925 spin_unlock(&host->lock);
1926 enable_irq(host->irq);
1927 host->ops->execute_tuning(host, opcode);
1928 disable_irq(host->irq);
1929 spin_lock(&host->lock);
1930 goto out;
1931 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301932 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1933
1934 /*
1935 * As per the Host Controller spec v3.00, tuning command
1936 * generates Buffer Read Ready interrupt, so enable that.
1937 *
1938 * Note: The spec clearly says that when tuning sequence
1939 * is being performed, the controller does not generate
1940 * interrupts other than Buffer Read Ready interrupt. But
1941 * to make sure we don't hit a controller bug, we _only_
1942 * enable Buffer Read Ready interrupt here.
1943 */
1944 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1945 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1946
1947 /*
1948 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1949 * of loops reaches 40 times or a timeout of 150ms occurs.
1950 */
1951 timeout = 150;
1952 do {
1953 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03001954 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301955
1956 if (!tuning_loop_counter && !timeout)
1957 break;
1958
Girish K S2cd06dc2012-01-06 09:56:39 +05301959 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301960 cmd.arg = 0;
1961 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1962 cmd.retries = 0;
1963 cmd.data = NULL;
1964 cmd.error = 0;
1965
1966 mrq.cmd = &cmd;
1967 host->mrq = &mrq;
1968
1969 /*
1970 * In response to CMD19, the card sends 64 bytes of tuning
1971 * block to the Host Controller. So we set the block size
1972 * to 64 here.
1973 */
Girish K S2cd06dc2012-01-06 09:56:39 +05301974 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1975 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1976 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1977 SDHCI_BLOCK_SIZE);
1978 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1979 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1980 SDHCI_BLOCK_SIZE);
1981 } else {
1982 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1983 SDHCI_BLOCK_SIZE);
1984 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301985
1986 /*
1987 * The tuning block is sent by the card to the host controller.
1988 * So we set the TRNS_READ bit in the Transfer Mode register.
1989 * This also takes care of setting DMA Enable and Multi Block
1990 * Select in the same register to 0.
1991 */
1992 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1993
1994 sdhci_send_command(host, &cmd);
1995
1996 host->cmd = NULL;
1997 host->mrq = NULL;
1998
1999 spin_unlock(&host->lock);
2000 enable_irq(host->irq);
2001
2002 /* Wait for Buffer Read Ready interrupt */
2003 wait_event_interruptible_timeout(host->buf_ready_int,
2004 (host->tuning_done == 1),
2005 msecs_to_jiffies(50));
2006 disable_irq(host->irq);
2007 spin_lock(&host->lock);
2008
2009 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302010 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05302011 "Buffer Read Ready interrupt during tuning "
2012 "procedure, falling back to fixed sampling "
2013 "clock\n");
2014 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2015 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2016 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2017 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2018
2019 err = -EIO;
2020 goto out;
2021 }
2022
2023 host->tuning_done = 0;
2024
2025 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2026 tuning_loop_counter--;
2027 timeout--;
2028 mdelay(1);
2029 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2030
2031 /*
2032 * The Host Driver has exhausted the maximum number of loops allowed,
2033 * so use fixed sampling frequency.
2034 */
2035 if (!tuning_loop_counter || !timeout) {
2036 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2037 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2038 } else {
2039 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302040 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05302041 " failed, falling back to fixed sampling"
2042 " clock\n");
2043 err = -EIO;
2044 }
2045 }
2046
2047out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302048 /*
2049 * If this is the very first time we are here, we start the retuning
2050 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2051 * flag won't be set, we check this condition before actually starting
2052 * the timer.
2053 */
2054 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2055 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2056 mod_timer(&host->tuning_timer, jiffies +
2057 host->tuning_count * HZ);
2058 /* Tuning mode 1 limits the maximum data length to 4MB */
2059 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2060 } else {
2061 host->flags &= ~SDHCI_NEEDS_RETUNING;
2062 /* Reload the new initial value for timer */
2063 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2064 mod_timer(&host->tuning_timer, jiffies +
2065 host->tuning_count * HZ);
2066 }
2067
2068 /*
2069 * In case tuning fails, host controllers which support re-tuning can
2070 * try tuning again at a later time, when the re-tuning timer expires.
2071 * So for these controllers, we return 0. Since there might be other
2072 * controllers who do not have this capability, we return error for
2073 * them.
2074 */
2075 if (err && host->tuning_count &&
2076 host->tuning_mode == SDHCI_TUNING_MODE_1)
2077 err = 0;
2078
Arindam Nathb513ea22011-05-05 12:19:04 +05302079 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
2080 spin_unlock(&host->lock);
2081 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002082 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302083
2084 return err;
2085}
2086
Adrian Hunter50accb92011-10-03 15:33:34 +03002087static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302088{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302089 u16 ctrl;
2090 unsigned long flags;
2091
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302092 /* Host Controller v3.00 defines preset value registers */
2093 if (host->version < SDHCI_SPEC_300)
2094 return;
2095
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302096 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_PRESET_VALUE)
2097 return;
2098
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302099 spin_lock_irqsave(&host->lock, flags);
2100
2101 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2102
2103 /*
2104 * We only enable or disable Preset Value if they are not already
2105 * enabled or disabled respectively. Otherwise, we bail out.
2106 */
2107 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2108 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2109 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002110 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302111 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2112 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2113 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002114 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302115 }
2116
2117 spin_unlock_irqrestore(&host->lock, flags);
2118}
2119
Adrian Hunter50accb92011-10-03 15:33:34 +03002120static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
2121{
2122 struct sdhci_host *host = mmc_priv(mmc);
2123
2124 sdhci_runtime_pm_get(host);
2125 sdhci_do_enable_preset_value(host, enable);
2126 sdhci_runtime_pm_put(host);
2127}
2128
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002129static int sdhci_stop_request(struct mmc_host *mmc)
2130{
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002131 struct sdhci_host *host = mmc_priv(mmc);
2132 unsigned long flags;
2133 struct mmc_data *data;
2134
2135 spin_lock_irqsave(&host->lock, flags);
2136 if (!host->mrq || !host->data)
2137 goto out;
2138
2139 data = host->data;
2140
2141 if (host->ops->disable_data_xfer)
2142 host->ops->disable_data_xfer(host);
2143
2144 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2145
2146 if (host->flags & SDHCI_REQ_USE_DMA) {
2147 if (host->flags & SDHCI_USE_ADMA) {
2148 sdhci_adma_table_post(host, data);
2149 } else {
2150 if (!data->host_cookie)
2151 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
2152 data->sg_len,
2153 (data->flags & MMC_DATA_READ) ?
2154 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2155 }
2156 }
2157 del_timer(&host->timer);
2158 host->mrq = NULL;
2159 host->cmd = NULL;
2160 host->data = NULL;
2161out:
2162 spin_unlock_irqrestore(&host->lock, flags);
2163 return 0;
2164}
2165
2166static unsigned int sdhci_get_xfer_remain(struct mmc_host *mmc)
2167{
2168 struct sdhci_host *host = mmc_priv(mmc);
2169 u32 present_state = 0;
2170
2171 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
2172
2173 return present_state & SDHCI_DOING_WRITE;
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002174}
2175
David Brownellab7aefd2006-11-12 17:55:30 -08002176static const struct mmc_host_ops sdhci_ops = {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002177 .pre_req = sdhci_pre_req,
2178 .post_req = sdhci_post_req,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002179 .request = sdhci_request,
2180 .set_ios = sdhci_set_ios,
2181 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03002182 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002183 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302184 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302185 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302186 .enable_preset_value = sdhci_enable_preset_value,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302187 .enable = sdhci_enable,
2188 .disable = sdhci_disable,
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002189 .stop_request = sdhci_stop_request,
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002190 .get_xfer_remain = sdhci_get_xfer_remain,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002191};
2192
2193/*****************************************************************************\
2194 * *
2195 * Tasklets *
2196 * *
2197\*****************************************************************************/
2198
2199static void sdhci_tasklet_card(unsigned long param)
2200{
2201 struct sdhci_host *host;
2202 unsigned long flags;
2203
2204 host = (struct sdhci_host*)param;
2205
2206 spin_lock_irqsave(&host->lock, flags);
2207
Adrian Hunter50accb92011-10-03 15:33:34 +03002208 /* Check host->mrq first in case we are runtime suspended */
2209 if (host->mrq &&
2210 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302211 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002212 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05302213 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002214 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002215
Adrian Hunter50accb92011-10-03 15:33:34 +03002216 sdhci_reset(host, SDHCI_RESET_CMD);
2217 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002218
Adrian Hunter50accb92011-10-03 15:33:34 +03002219 host->mrq->cmd->error = -ENOMEDIUM;
2220 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002221 }
2222
2223 spin_unlock_irqrestore(&host->lock, flags);
2224
Pierre Ossman04cf5852008-08-18 22:18:14 +02002225 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002226}
2227
2228static void sdhci_tasklet_finish(unsigned long param)
2229{
2230 struct sdhci_host *host;
2231 unsigned long flags;
2232 struct mmc_request *mrq;
2233
2234 host = (struct sdhci_host*)param;
2235
Adrian Hunter50accb92011-10-03 15:33:34 +03002236 spin_lock_irqsave(&host->lock, flags);
2237
Chris Ball0c9c99a2011-04-27 17:35:31 -04002238 /*
2239 * If this tasklet gets rescheduled while running, it will
2240 * be run again afterwards but without any active request.
2241 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002242 if (!host->mrq) {
2243 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002244 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002245 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002246
2247 del_timer(&host->timer);
2248
2249 mrq = host->mrq;
2250
Pierre Ossmand129bce2006-03-24 03:18:17 -08002251 /*
2252 * The controller needs a reset of internal state machines
2253 * upon error conditions.
2254 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002255 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002256 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002257 (mrq->data && (mrq->data->error ||
2258 (mrq->data->stop && mrq->data->stop->error))) ||
2259 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002260
2261 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002262 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002263 unsigned int clock;
2264
2265 /* This is to force an update */
2266 clock = host->clock;
2267 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302268 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002269 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302270 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002271 }
2272
2273 /* Spec says we should do both at the same time, but Ricoh
2274 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002275 sdhci_reset(host, SDHCI_RESET_CMD);
2276 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002277 } else {
2278 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2279 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002280 }
2281
2282 host->mrq = NULL;
2283 host->cmd = NULL;
2284 host->data = NULL;
2285
Pierre Ossmanf9134312008-12-21 17:01:48 +01002286#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002287 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002288#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002289
Pierre Ossman5f25a662006-10-04 02:15:39 -07002290 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002291 spin_unlock_irqrestore(&host->lock, flags);
2292
2293 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002294 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002295}
2296
2297static void sdhci_timeout_timer(unsigned long data)
2298{
2299 struct sdhci_host *host;
2300 unsigned long flags;
2301
2302 host = (struct sdhci_host*)data;
2303
2304 spin_lock_irqsave(&host->lock, flags);
2305
2306 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302307 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002308 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002309 sdhci_dumpregs(host);
2310
2311 if (host->data) {
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302312 pr_info("%s: bytes to transfer: %d transferred: %d\n",
2313 mmc_hostname(host->mmc),
2314 (host->data->blksz * host->data->blocks),
2315 (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) *
2316 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossman17b04292007-07-22 22:18:46 +02002317 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002318 sdhci_finish_data(host);
2319 } else {
2320 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002321 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002322 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002323 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002324
2325 tasklet_schedule(&host->finish_tasklet);
2326 }
2327 }
2328
Pierre Ossman5f25a662006-10-04 02:15:39 -07002329 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002330 spin_unlock_irqrestore(&host->lock, flags);
2331}
2332
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302333static void sdhci_tuning_timer(unsigned long data)
2334{
2335 struct sdhci_host *host;
2336 unsigned long flags;
2337
2338 host = (struct sdhci_host *)data;
2339
2340 spin_lock_irqsave(&host->lock, flags);
2341
2342 host->flags |= SDHCI_NEEDS_RETUNING;
2343
2344 spin_unlock_irqrestore(&host->lock, flags);
2345}
2346
Pierre Ossmand129bce2006-03-24 03:18:17 -08002347/*****************************************************************************\
2348 * *
2349 * Interrupt handling *
2350 * *
2351\*****************************************************************************/
2352
2353static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2354{
2355 BUG_ON(intmask == 0);
2356
2357 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302358 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002359 "though no command operation was in progress.\n",
2360 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002361 sdhci_dumpregs(host);
2362 return;
2363 }
2364
Pierre Ossman43b58b32007-07-25 23:15:27 +02002365 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002366 host->cmd->error = -ETIMEDOUT;
2367 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2368 SDHCI_INT_INDEX))
2369 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002370
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302371 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2372 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2373 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2374 if (intmask & SDHCI_INT_CRC) {
2375 sdhci_reset(host, SDHCI_RESET_CMD);
2376 host->cmd->error = 0;
2377 }
2378 }
2379 }
2380
Pierre Ossmane8095172008-07-25 01:09:08 +02002381 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002382 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002383 return;
2384 }
2385
2386 /*
2387 * The host can send and interrupt when the busy state has
2388 * ended, allowing us to wait without wasting CPU cycles.
2389 * Unfortunately this is overloaded on the "data complete"
2390 * interrupt, so we need to take some care when handling
2391 * it.
2392 *
2393 * Note: The 1.0 specification is a bit ambiguous about this
2394 * feature so there might be some problems with older
2395 * controllers.
2396 */
2397 if (host->cmd->flags & MMC_RSP_BUSY) {
2398 if (host->cmd->data)
2399 DBG("Cannot wait for busy signal when also "
2400 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002401 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002402 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002403
2404 /* The controller does not support the end-of-busy IRQ,
2405 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002406 }
2407
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302408 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2409 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2410 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2411 if (intmask & SDHCI_INT_CRC) {
2412 sdhci_finish_command(host);
2413 return;
2414 }
2415 }
2416 }
2417
Pierre Ossmane8095172008-07-25 01:09:08 +02002418 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002419 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002420}
2421
Ben Dooks6882a8c2009-06-14 13:52:38 +01002422static void sdhci_show_adma_error(struct sdhci_host *host)
2423{
2424 const char *name = mmc_hostname(host->mmc);
2425 u8 *desc = host->adma_desc;
2426 __le32 *dma;
2427 __le16 *len;
2428 u8 attr;
2429
2430 sdhci_dumpregs(host);
2431
2432 while (true) {
2433 dma = (__le32 *)(desc + 4);
2434 len = (__le16 *)(desc + 2);
2435 attr = *desc;
2436
Sahitya Tummala419b6c82013-04-12 12:28:29 +05302437 pr_info("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
Ben Dooks6882a8c2009-06-14 13:52:38 +01002438 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2439
2440 desc += 8;
2441
2442 if (attr & 2)
2443 break;
2444 }
2445}
Ben Dooks6882a8c2009-06-14 13:52:38 +01002446
Pierre Ossmand129bce2006-03-24 03:18:17 -08002447static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2448{
Girish K S2cd06dc2012-01-06 09:56:39 +05302449 u32 command;
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302450 bool pr_msg = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002451 BUG_ON(intmask == 0);
2452
Arindam Nathb513ea22011-05-05 12:19:04 +05302453 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2454 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302455 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2456 if (command == MMC_SEND_TUNING_BLOCK ||
2457 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302458 host->tuning_done = 1;
2459 wake_up(&host->buf_ready_int);
2460 return;
2461 }
2462 }
2463
Pierre Ossmand129bce2006-03-24 03:18:17 -08002464 if (!host->data) {
2465 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002466 * The "data complete" interrupt is also used to
2467 * indicate that a busy state has ended. See comment
2468 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002469 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002470 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2471 if (intmask & SDHCI_INT_DATA_END) {
2472 sdhci_finish_command(host);
2473 return;
2474 }
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302475 if (host->quirks2 &
2476 SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD)
2477 return;
Pierre Ossmane8095172008-07-25 01:09:08 +02002478 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002479
Sahitya Tummalaca422112013-02-22 12:15:54 +05302480 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002481 "though no data operation was in progress.\n",
2482 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002483 sdhci_dumpregs(host);
2484
2485 return;
2486 }
2487
2488 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002489 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002490 else if (intmask & SDHCI_INT_DATA_END_BIT)
2491 host->data->error = -EILSEQ;
2492 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2493 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2494 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002495 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002496 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302497 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002498 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002499 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002500 }
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302501 if (host->data->error) {
2502 if ((intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT)) &&
2503 (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING)) {
2504 command = SDHCI_GET_CMD(sdhci_readw(host,
2505 SDHCI_COMMAND));
2506 if ((command != MMC_SEND_TUNING_BLOCK_HS200) &&
2507 (command != MMC_SEND_TUNING_BLOCK))
2508 pr_msg = true;
2509 } else {
2510 pr_msg = true;
2511 }
2512 if (pr_msg) {
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302513 pr_err("%s: data txfr (0x%08x) error: %d after %lld ms\n",
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302514 mmc_hostname(host->mmc), intmask,
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302515 host->data->error, ktime_to_ms(ktime_sub(
2516 ktime_get(), host->data_start_time)));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302517 sdhci_dumpregs(host);
2518 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002519 sdhci_finish_data(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302520 } else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002521 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002522 sdhci_transfer_pio(host);
2523
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002524 /*
2525 * We currently don't do anything fancy with DMA
2526 * boundaries, but as we can't disable the feature
2527 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002528 *
2529 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2530 * should return a valid address to continue from, but as
2531 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002532 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002533 if (intmask & SDHCI_INT_DMA_END) {
2534 u32 dmastart, dmanow;
2535 dmastart = sg_dma_address(host->data->sg);
2536 dmanow = dmastart + host->data->bytes_xfered;
2537 /*
2538 * Force update to the next DMA block boundary.
2539 */
2540 dmanow = (dmanow &
2541 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2542 SDHCI_DEFAULT_BOUNDARY_SIZE;
2543 host->data->bytes_xfered = dmanow - dmastart;
2544 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2545 " next 0x%08x\n",
2546 mmc_hostname(host->mmc), dmastart,
2547 host->data->bytes_xfered, dmanow);
2548 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2549 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002550
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002551 if (intmask & SDHCI_INT_DATA_END) {
2552 if (host->cmd) {
2553 /*
2554 * Data managed to finish before the
2555 * command completed. Make sure we do
2556 * things in the proper order.
2557 */
2558 host->data_early = 1;
2559 } else {
2560 sdhci_finish_data(host);
2561 }
2562 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002563 }
2564}
2565
David Howells7d12e782006-10-05 14:55:46 +01002566static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002567{
2568 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002569 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002570 u32 intmask, unexpected = 0;
2571 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002572
2573 spin_lock(&host->lock);
2574
Adrian Hunter50accb92011-10-03 15:33:34 +03002575 if (host->runtime_suspended) {
2576 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302577 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002578 mmc_hostname(host->mmc));
2579 return IRQ_HANDLED;
2580 }
2581
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002582 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002583
Mark Lord62df67a2007-03-06 13:30:13 +01002584 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002585 result = IRQ_NONE;
2586 goto out;
2587 }
2588
Alexander Stein6379b232012-03-14 09:52:10 +01002589again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002590 DBG("*** %s got interrupt: 0x%08x\n",
2591 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002592
Pierre Ossman3192a282006-06-30 02:22:26 -07002593 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302594 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2595 SDHCI_CARD_PRESENT;
2596
2597 /*
2598 * There is a observation on i.mx esdhc. INSERT bit will be
2599 * immediately set again when it gets cleared, if a card is
2600 * inserted. We have to mask the irq to prevent interrupt
2601 * storm which will freeze the system. And the REMOVE gets
2602 * the same situation.
2603 *
2604 * More testing are needed here to ensure it works for other
2605 * platforms though.
2606 */
2607 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2608 SDHCI_INT_CARD_REMOVE);
2609 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2610 SDHCI_INT_CARD_INSERT);
2611
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002612 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302613 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2614 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002615 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002616 }
2617
Pierre Ossmand129bce2006-03-24 03:18:17 -08002618 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002619 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2620 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002621 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2622 (host->clock <= 400000))
2623 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002624 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002625 }
2626
2627 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002628 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2629 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002630 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2631 (host->clock <= 400000))
2632 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002633 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002634 }
2635
2636 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2637
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002638 intmask &= ~SDHCI_INT_ERROR;
2639
Pierre Ossmand129bce2006-03-24 03:18:17 -08002640 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302641 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002642 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002643 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002644 }
2645
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002646 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002647
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002648 if (intmask & SDHCI_INT_CARD_INT)
2649 cardint = 1;
2650
2651 intmask &= ~SDHCI_INT_CARD_INT;
2652
Pierre Ossman3192a282006-06-30 02:22:26 -07002653 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002654 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002655 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002656 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002657
2658 result = IRQ_HANDLED;
2659
Alexander Stein6379b232012-03-14 09:52:10 +01002660 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2661 if (intmask && --max_loops)
2662 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002663out:
2664 spin_unlock(&host->lock);
2665
Alexander Stein6379b232012-03-14 09:52:10 +01002666 if (unexpected) {
2667 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2668 mmc_hostname(host->mmc), unexpected);
2669 sdhci_dumpregs(host);
2670 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002671 /*
2672 * We have to delay this as it calls back into the driver.
2673 */
2674 if (cardint)
2675 mmc_signal_sdio_irq(host->mmc);
2676
Pierre Ossmand129bce2006-03-24 03:18:17 -08002677 return result;
2678}
2679
2680/*****************************************************************************\
2681 * *
2682 * Suspend/resume *
2683 * *
2684\*****************************************************************************/
2685
2686#ifdef CONFIG_PM
2687
Manuel Laussd72faa62011-11-03 11:09:45 +01002688int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002689{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002690 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302691 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002692
Chris Balla1b13b42012-02-06 00:43:59 -05002693 if (host->ops->platform_suspend)
2694 host->ops->platform_suspend(host);
2695
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002696 sdhci_disable_card_detection(host);
2697
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302698 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302699 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2700 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2701 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002702 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302703 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302704 }
2705
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002706 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302707 if (ret) {
2708 if (has_tuning_timer) {
2709 host->flags |= SDHCI_NEEDS_RETUNING;
2710 mod_timer(&host->tuning_timer, jiffies +
2711 host->tuning_count * HZ);
2712 }
2713
2714 sdhci_enable_card_detection(host);
2715
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002716 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302717 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002718
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002719 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002720
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002721 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002722}
2723
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002724EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002725
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002726int sdhci_resume_host(struct sdhci_host *host)
2727{
2728 int ret;
2729
Richard Röjforsa13abc72009-09-22 16:45:30 -07002730 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002731 if (host->ops->enable_dma)
2732 host->ops->enable_dma(host);
2733 }
2734
2735 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2736 mmc_hostname(host->mmc), host);
2737 if (ret)
2738 return ret;
2739
Adrian Hunter6308d292012-02-07 14:48:54 +02002740 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2741 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2742 /* Card keeps power but host controller does not */
2743 sdhci_init(host, 0);
2744 host->pwr = 0;
2745 host->clock = 0;
2746 sdhci_do_set_ios(host, &host->mmc->ios);
2747 } else {
2748 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2749 mmiowb();
2750 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002751
2752 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002753 sdhci_enable_card_detection(host);
2754
Chris Balla1b13b42012-02-06 00:43:59 -05002755 if (host->ops->platform_resume)
2756 host->ops->platform_resume(host);
2757
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302758 /* Set the re-tuning expiration flag */
2759 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2760 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2761 host->flags |= SDHCI_NEEDS_RETUNING;
2762
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002763 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002764}
2765
2766EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002767
Daniel Drake5f619702010-11-04 22:20:39 +00002768void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2769{
2770 u8 val;
2771 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2772 val |= SDHCI_WAKE_ON_INT;
2773 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2774}
2775
2776EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2777
Pierre Ossmand129bce2006-03-24 03:18:17 -08002778#endif /* CONFIG_PM */
2779
Adrian Hunter50accb92011-10-03 15:33:34 +03002780#ifdef CONFIG_PM_RUNTIME
2781
2782static int sdhci_runtime_pm_get(struct sdhci_host *host)
2783{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302784 if (!mmc_use_core_runtime_pm(host->mmc))
2785 return pm_runtime_get_sync(host->mmc->parent);
2786 else
2787 return 0;
Adrian Hunter50accb92011-10-03 15:33:34 +03002788}
2789
2790static int sdhci_runtime_pm_put(struct sdhci_host *host)
2791{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302792 if (!mmc_use_core_runtime_pm(host->mmc)) {
2793 pm_runtime_mark_last_busy(host->mmc->parent);
2794 return pm_runtime_put_autosuspend(host->mmc->parent);
2795 } else {
2796 return 0;
2797 }
Adrian Hunter50accb92011-10-03 15:33:34 +03002798}
2799
2800int sdhci_runtime_suspend_host(struct sdhci_host *host)
2801{
2802 unsigned long flags;
2803 int ret = 0;
2804
2805 /* Disable tuning since we are suspending */
2806 if (host->version >= SDHCI_SPEC_300 &&
2807 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2808 del_timer_sync(&host->tuning_timer);
2809 host->flags &= ~SDHCI_NEEDS_RETUNING;
2810 }
2811
2812 spin_lock_irqsave(&host->lock, flags);
2813 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2814 spin_unlock_irqrestore(&host->lock, flags);
2815
2816 synchronize_irq(host->irq);
2817
2818 spin_lock_irqsave(&host->lock, flags);
2819 host->runtime_suspended = true;
2820 spin_unlock_irqrestore(&host->lock, flags);
2821
2822 return ret;
2823}
2824EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2825
2826int sdhci_runtime_resume_host(struct sdhci_host *host)
2827{
2828 unsigned long flags;
2829 int ret = 0, host_flags = host->flags;
2830
2831 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2832 if (host->ops->enable_dma)
2833 host->ops->enable_dma(host);
2834 }
2835
2836 sdhci_init(host, 0);
2837
2838 /* Force clock and power re-program */
2839 host->pwr = 0;
2840 host->clock = 0;
2841 sdhci_do_set_ios(host, &host->mmc->ios);
2842
2843 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2844 if (host_flags & SDHCI_PV_ENABLED)
2845 sdhci_do_enable_preset_value(host, true);
2846
2847 /* Set the re-tuning expiration flag */
2848 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2849 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2850 host->flags |= SDHCI_NEEDS_RETUNING;
2851
2852 spin_lock_irqsave(&host->lock, flags);
2853
2854 host->runtime_suspended = false;
2855
2856 /* Enable SDIO IRQ */
2857 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2858 sdhci_enable_sdio_irq_nolock(host, true);
2859
2860 /* Enable Card Detection */
2861 sdhci_enable_card_detection(host);
2862
2863 spin_unlock_irqrestore(&host->lock, flags);
2864
2865 return ret;
2866}
2867EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2868
2869#endif
2870
Pierre Ossmand129bce2006-03-24 03:18:17 -08002871/*****************************************************************************\
2872 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002873 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002874 * *
2875\*****************************************************************************/
2876
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002877struct sdhci_host *sdhci_alloc_host(struct device *dev,
2878 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002879{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002880 struct mmc_host *mmc;
2881 struct sdhci_host *host;
2882
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002883 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002884
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002885 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002886 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002887 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002888
2889 host = mmc_priv(mmc);
2890 host->mmc = mmc;
2891
Sahitya Tummala951c1202013-05-24 08:47:26 +05302892 spin_lock_init(&host->lock);
2893
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002894 return host;
2895}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002896
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002897EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002898
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002899int sdhci_add_host(struct sdhci_host *host)
2900{
2901 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302902 u32 caps[2];
2903 u32 max_current_caps;
2904 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002905 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002906
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002907 WARN_ON(host == NULL);
2908 if (host == NULL)
2909 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002910
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002911 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002912
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002913 if (debug_quirks)
2914 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03002915 if (debug_quirks2)
2916 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002917
Pierre Ossmand96649e2006-06-30 02:22:30 -07002918 sdhci_reset(host, SDHCI_RESET_ALL);
2919
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002920 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002921 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2922 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002923 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302924 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002925 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002926 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002927 }
2928
Arindam Nathf2119df2011-05-05 12:18:57 +05302929 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002930 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002931
Arindam Nathf2119df2011-05-05 12:18:57 +05302932 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2933 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2934
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002935 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002936 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302937 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002938 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002939 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002940 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002941
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002942 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002943 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002944 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002945 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002946 }
2947
Arindam Nathf2119df2011-05-05 12:18:57 +05302948 if ((host->version >= SDHCI_SPEC_200) &&
2949 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002950 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002951
2952 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2953 (host->flags & SDHCI_USE_ADMA)) {
2954 DBG("Disabling ADMA as it is marked broken\n");
2955 host->flags &= ~SDHCI_USE_ADMA;
2956 }
2957
Richard Röjforsa13abc72009-09-22 16:45:30 -07002958 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002959 if (host->ops->enable_dma) {
2960 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302961 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002962 "available. Falling back to PIO.\n",
2963 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002964 host->flags &=
2965 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002966 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002967 }
2968 }
2969
Pierre Ossman2134a922008-06-28 18:28:51 +02002970 if (host->flags & SDHCI_USE_ADMA) {
2971 /*
2972 * We need to allocate descriptors for all sg entries
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302973 * (128/max_segments) and potentially one alignment transfer for
Pierre Ossman2134a922008-06-28 18:28:51 +02002974 * each of those entries.
2975 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05302976 if (host->ops->get_max_segments)
2977 host->adma_max_desc = host->ops->get_max_segments();
2978 else
2979 host->adma_max_desc = 128;
2980
2981 host->adma_desc_sz = (host->adma_max_desc * 2 + 1) * 4;
2982 host->align_buf_sz = host->adma_max_desc * 4;
2983
2984 pr_debug("%s: %s: dma_desc_size: %d\n",
2985 mmc_hostname(host->mmc), __func__, host->adma_desc_sz);
2986 host->adma_desc = kmalloc(host->adma_desc_sz,
2987 GFP_KERNEL);
2988 host->align_buffer = kmalloc(host->align_buf_sz,
2989 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02002990 if (!host->adma_desc || !host->align_buffer) {
2991 kfree(host->adma_desc);
2992 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302993 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002994 "buffers. Falling back to standard DMA.\n",
2995 mmc_hostname(mmc));
2996 host->flags &= ~SDHCI_USE_ADMA;
2997 }
2998 }
2999
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08003000 host->next_data.cookie = 1;
3001
Pierre Ossman76591502008-07-21 00:32:11 +02003002 /*
3003 * If we use DMA, then it's up to the caller to set the DMA
3004 * mask, but PIO does not need the hw shim so we set a new
3005 * mask here in that case.
3006 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003007 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003008 host->dma_mask = DMA_BIT_MASK(64);
3009 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3010 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003011
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003012 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05303013 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003014 >> SDHCI_CLOCK_BASE_SHIFT;
3015 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303016 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003017 >> SDHCI_CLOCK_BASE_SHIFT;
3018
Pierre Ossmand129bce2006-03-24 03:18:17 -08003019 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003020 if (host->max_clk == 0 || host->quirks &
3021 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003022 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303023 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03003024 "frequency.\n", mmc_hostname(mmc));
3025 return -ENODEV;
3026 }
3027 host->max_clk = host->ops->get_max_clock(host);
3028 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003029
3030 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303031 * In case of Host Controller v3.00, find out whether clock
3032 * multiplier is supported.
3033 */
3034 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3035 SDHCI_CLOCK_MUL_SHIFT;
3036
3037 /*
3038 * In case the value in Clock Multiplier is 0, then programmable
3039 * clock mode is not supported, otherwise the actual clock
3040 * multiplier is one more than the value of Clock Multiplier
3041 * in the Capabilities Register.
3042 */
3043 if (host->clk_mul)
3044 host->clk_mul += 1;
3045
3046 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003047 * Set host parameters.
3048 */
3049 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303050 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003051 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003052 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303053 else if (host->version >= SDHCI_SPEC_300) {
3054 if (host->clk_mul) {
3055 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3056 mmc->f_max = host->max_clk * host->clk_mul;
3057 } else
3058 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3059 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003060 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003061
Sahitya Tummalaca422112013-02-22 12:15:54 +05303062 host->timeout_clk =
3063 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
3064 if (host->timeout_clk == 0) {
3065 if (host->ops->get_timeout_clock) {
3066 host->timeout_clk = host->ops->get_timeout_clock(host);
3067 } else if (!(host->quirks &
3068 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3069 pr_err("%s: Hardware doesn't specify timeout clock "
3070 "frequency.\n", mmc_hostname(mmc));
3071 return -ENODEV;
3072 }
3073 }
3074 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3075 host->timeout_clk *= 1000;
3076
3077 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
3078 host->timeout_clk = mmc->f_max / 1000;
3079
Krishna Kondaa20d3362013-04-01 21:01:59 -07003080 if (!(host->quirks2 & SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE))
3081 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Sahitya Tummalaca422112013-02-22 12:15:54 +05303082
Andrei Warkentine89d4562011-05-23 15:06:37 -05003083 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
3084
3085 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3086 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003087
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003088 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003089 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003090 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003091 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003092 host->flags |= SDHCI_AUTO_CMD23;
3093 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3094 } else {
3095 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3096 }
3097
Philip Rakity15ec4462010-11-19 16:48:39 -05003098 /*
3099 * A controller may support 8-bit width, but the board itself
3100 * might not have the pins brought out. Boards that support
3101 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3102 * their platform code before calling sdhci_add_host(), and we
3103 * won't assume 8-bit width for hosts without that CAP.
3104 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003105 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003106 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003107
Arindam Nathf2119df2011-05-05 12:18:57 +05303108 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003109 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003110
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003111 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3112 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003113 mmc->caps |= MMC_CAP_NEEDS_POLL;
3114
Al Cooper4188bba2012-03-16 15:54:17 -04003115 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3116 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3117 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303118 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3119
3120 /* SDR104 supports also implies SDR50 support */
3121 if (caps[1] & SDHCI_SUPPORT_SDR104)
3122 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3123 else if (caps[1] & SDHCI_SUPPORT_SDR50)
3124 mmc->caps |= MMC_CAP_UHS_SDR50;
3125
3126 if (caps[1] & SDHCI_SUPPORT_DDR50)
3127 mmc->caps |= MMC_CAP_UHS_DDR50;
3128
Girish K S2cd06dc2012-01-06 09:56:39 +05303129 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303130 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3131 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3132
Girish K S2cd06dc2012-01-06 09:56:39 +05303133 /* Does the host need tuning for HS200? */
3134 if (mmc->caps2 & MMC_CAP2_HS200)
3135 host->flags |= SDHCI_HS200_NEEDS_TUNING;
3136
Arindam Nathd6d50a12011-05-05 12:18:59 +05303137 /* Driver Type(s) (A, C, D) supported by the host */
3138 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3139 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3140 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3141 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3142 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3143 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3144
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02003145 /* Initial value for re-tuning timer count */
3146 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3147 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3148
3149 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303150 * In case Re-tuning Timer is not disabled, the actual value of
3151 * re-tuning timer will be 2 ^ (n - 1).
3152 */
3153 if (host->tuning_count)
3154 host->tuning_count = 1 << (host->tuning_count - 1);
3155
3156 /* Re-tuning mode supported by the Host Controller */
3157 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3158 SDHCI_RETUNING_MODE_SHIFT;
3159
Takashi Iwai8f230f42010-12-08 10:04:30 +01003160 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05303161 /*
3162 * According to SD Host Controller spec v3.00, if the Host System
3163 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3164 * the value is meaningful only if Voltage Support in the Capabilities
3165 * register is set. The actual current value is 4 times the register
3166 * value.
3167 */
3168 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3169
3170 if (caps[0] & SDHCI_CAN_VDD_330) {
3171 int max_current_330;
3172
Takashi Iwai8f230f42010-12-08 10:04:30 +01003173 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303174
3175 max_current_330 = ((max_current_caps &
3176 SDHCI_MAX_CURRENT_330_MASK) >>
3177 SDHCI_MAX_CURRENT_330_SHIFT) *
3178 SDHCI_MAX_CURRENT_MULTIPLIER;
3179
3180 if (max_current_330 > 150)
3181 mmc->caps |= MMC_CAP_SET_XPC_330;
3182 }
3183 if (caps[0] & SDHCI_CAN_VDD_300) {
3184 int max_current_300;
3185
Takashi Iwai8f230f42010-12-08 10:04:30 +01003186 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303187
3188 max_current_300 = ((max_current_caps &
3189 SDHCI_MAX_CURRENT_300_MASK) >>
3190 SDHCI_MAX_CURRENT_300_SHIFT) *
3191 SDHCI_MAX_CURRENT_MULTIPLIER;
3192
3193 if (max_current_300 > 150)
3194 mmc->caps |= MMC_CAP_SET_XPC_300;
3195 }
3196 if (caps[0] & SDHCI_CAN_VDD_180) {
3197 int max_current_180;
3198
Takashi Iwai8f230f42010-12-08 10:04:30 +01003199 ocr_avail |= MMC_VDD_165_195;
3200
Arindam Nathf2119df2011-05-05 12:18:57 +05303201 max_current_180 = ((max_current_caps &
3202 SDHCI_MAX_CURRENT_180_MASK) >>
3203 SDHCI_MAX_CURRENT_180_SHIFT) *
3204 SDHCI_MAX_CURRENT_MULTIPLIER;
3205
3206 if (max_current_180 > 150)
3207 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05303208
3209 /* Maximum current capabilities of the host at 1.8V */
3210 if (max_current_180 >= 800)
3211 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
3212 else if (max_current_180 >= 600)
3213 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
3214 else if (max_current_180 >= 400)
3215 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
3216 else
3217 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05303218 }
3219
Takashi Iwai8f230f42010-12-08 10:04:30 +01003220 mmc->ocr_avail = ocr_avail;
3221 mmc->ocr_avail_sdio = ocr_avail;
3222 if (host->ocr_avail_sdio)
3223 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3224 mmc->ocr_avail_sd = ocr_avail;
3225 if (host->ocr_avail_sd)
3226 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3227 else /* normal SD controllers don't support 1.8V */
3228 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3229 mmc->ocr_avail_mmc = ocr_avail;
3230 if (host->ocr_avail_mmc)
3231 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003232
3233 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303234 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003235 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003236 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003237 }
3238
Pierre Ossmand129bce2006-03-24 03:18:17 -08003239 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003240 * Maximum number of segments. Depends on if the hardware
3241 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003242 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003243 if (host->flags & SDHCI_USE_ADMA)
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303244 mmc->max_segs = host->adma_max_desc;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003245 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003246 mmc->max_segs = 1;
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303247 else/* PIO */
3248 mmc->max_segs = host->adma_max_desc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003249
3250 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003251 * Maximum number of sectors in one transfer. Limited by DMA boundary
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303252 * size (512KiB), unless specified by platform specific driver. Each
3253 * descriptor can transfer a maximum of 64KB.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003254 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303255 if (host->ops->get_max_segments)
3256 mmc->max_req_size = (host->adma_max_desc * 65536);
3257 else
3258 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003259
3260 /*
3261 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003262 * of bytes. When doing hardware scatter/gather, each entry cannot
3263 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003264 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003265 if (host->flags & SDHCI_USE_ADMA) {
3266 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3267 mmc->max_seg_size = 65535;
3268 else
3269 mmc->max_seg_size = 65536;
3270 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003271 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003272 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003273
3274 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003275 * Maximum block size. This varies from controller to controller and
3276 * is specified in the capabilities register.
3277 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003278 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3279 mmc->max_blk_size = 2;
3280 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303281 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003282 SDHCI_MAX_BLOCK_SHIFT;
3283 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303284 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003285 "assuming 512 bytes\n", mmc_hostname(mmc));
3286 mmc->max_blk_size = 0;
3287 }
3288 }
3289
3290 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003291
3292 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003293 * Maximum block count.
3294 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003295 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003296
3297 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003298 * Init tasklets.
3299 */
3300 tasklet_init(&host->card_tasklet,
3301 sdhci_tasklet_card, (unsigned long)host);
3302 tasklet_init(&host->finish_tasklet,
3303 sdhci_tasklet_finish, (unsigned long)host);
3304
Al Viroe4cad1b2006-10-10 22:47:07 +01003305 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003306
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303307 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303308 init_waitqueue_head(&host->buf_ready_int);
3309
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303310 /* Initialize re-tuning timer */
3311 init_timer(&host->tuning_timer);
3312 host->tuning_timer.data = (unsigned long)host;
3313 host->tuning_timer.function = sdhci_tuning_timer;
3314 }
3315
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003316 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003317 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003318 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003319 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003320
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003321 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3322 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303323 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003324 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003325 }
3326
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003327 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003328
3329#ifdef CONFIG_MMC_DEBUG
3330 sdhci_dumpregs(host);
3331#endif
3332
Pierre Ossmanf9134312008-12-21 17:01:48 +01003333#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003334 snprintf(host->led_name, sizeof(host->led_name),
3335 "%s::", mmc_hostname(mmc));
3336 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003337 host->led.brightness = LED_OFF;
3338 host->led.default_trigger = mmc_hostname(mmc);
3339 host->led.brightness_set = sdhci_led_control;
3340
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003341 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003342 if (ret)
3343 goto reset;
3344#endif
3345
Pierre Ossman5f25a662006-10-04 02:15:39 -07003346 mmiowb();
3347
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303348 if (host->cpu_dma_latency_us)
3349 pm_qos_add_request(&host->pm_qos_req_dma,
3350 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003351 mmc_add_host(mmc);
3352
Sahitya Tummalaca422112013-02-22 12:15:54 +05303353 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003354 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003355 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3356 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003357
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003358 sdhci_enable_card_detection(host);
3359
Pierre Ossmand129bce2006-03-24 03:18:17 -08003360 return 0;
3361
Pierre Ossmanf9134312008-12-21 17:01:48 +01003362#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003363reset:
3364 sdhci_reset(host, SDHCI_RESET_ALL);
3365 free_irq(host->irq, host);
3366#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003367untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003368 tasklet_kill(&host->card_tasklet);
3369 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003370
3371 return ret;
3372}
3373
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003374EXPORT_SYMBOL_GPL(sdhci_add_host);
3375
Pierre Ossman1e728592008-04-16 19:13:13 +02003376void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003377{
Pierre Ossman1e728592008-04-16 19:13:13 +02003378 unsigned long flags;
3379
3380 if (dead) {
3381 spin_lock_irqsave(&host->lock, flags);
3382
3383 host->flags |= SDHCI_DEVICE_DEAD;
3384
3385 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303386 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003387 " transfer!\n", mmc_hostname(host->mmc));
3388
3389 host->mrq->cmd->error = -ENOMEDIUM;
3390 tasklet_schedule(&host->finish_tasklet);
3391 }
3392
3393 spin_unlock_irqrestore(&host->lock, flags);
3394 }
3395
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003396 sdhci_disable_card_detection(host);
3397
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303398 if (host->cpu_dma_latency_us)
3399 pm_qos_remove_request(&host->pm_qos_req_dma);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003400 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003401
Pierre Ossmanf9134312008-12-21 17:01:48 +01003402#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003403 led_classdev_unregister(&host->led);
3404#endif
3405
Pierre Ossman1e728592008-04-16 19:13:13 +02003406 if (!dead)
3407 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003408
3409 free_irq(host->irq, host);
3410
3411 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303412 if (host->version >= SDHCI_SPEC_300)
3413 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003414
3415 tasklet_kill(&host->card_tasklet);
3416 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003417
Sahitya Tummalaca422112013-02-22 12:15:54 +05303418 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003419 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003420
Pierre Ossman2134a922008-06-28 18:28:51 +02003421 kfree(host->adma_desc);
3422 kfree(host->align_buffer);
3423
3424 host->adma_desc = NULL;
3425 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003426}
3427
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003428EXPORT_SYMBOL_GPL(sdhci_remove_host);
3429
3430void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003431{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003432 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003433}
3434
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003435EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003436
3437/*****************************************************************************\
3438 * *
3439 * Driver init/exit *
3440 * *
3441\*****************************************************************************/
3442
3443static int __init sdhci_drv_init(void)
3444{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303445 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003446 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303447 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003448
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003449 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003450}
3451
3452static void __exit sdhci_drv_exit(void)
3453{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003454}
3455
3456module_init(sdhci_drv_init);
3457module_exit(sdhci_drv_exit);
3458
Pierre Ossmandf673b22006-06-30 02:22:31 -07003459module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003460module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003461
Pierre Ossman32710e82009-04-08 20:14:54 +02003462MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003463MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003464MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003465
Pierre Ossmandf673b22006-06-30 02:22:31 -07003466MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003467MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");