blob: b34584420a8f64bd1ec3b79189f554bf6ad1683b [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 Hunter66fd8ad2011-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>
Aaron Lu473b0952012-07-03 17:27:49 +080030#include <linux/mmc/card.h>
Corneliu Doban85cc1c32015-02-09 16:06:29 -080031#include <linux/mmc/sdio.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080032#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080033
Pierre Ossmand129bce2006-03-24 03:18:17 -080034#include "sdhci.h"
35
36#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmand129bce2006-03-24 03:18:17 -080038#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010039 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080040
Pierre Ossmanf9134312008-12-21 17:01:48 +010041#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
42 defined(CONFIG_MMC_SDHCI_MODULE))
43#define SDHCI_USE_LEDS_CLASS
44#endif
45
Arindam Nathb513ea22011-05-05 12:19:04 +053046#define MAX_TUNING_LOOP 40
47
Pierre Ossmandf673b22006-06-30 02:22:31 -070048static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030049static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070050
Pierre Ossmand129bce2006-03-24 03:18:17 -080051static void sdhci_finish_data(struct sdhci_host *);
52
Pierre Ossmand129bce2006-03-24 03:18:17 -080053static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053054static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Kevin Liu52983382013-01-31 11:31:37 +080055static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Haibo Chen348487c2014-12-09 17:04:05 +080056static int sdhci_pre_dma_transfer(struct sdhci_host *host,
57 struct mmc_data *data,
58 struct sdhci_host_next *next);
Scott Branden04e079cf2015-03-10 11:35:10 -070059static int sdhci_do_get_cd(struct sdhci_host *host);
Pierre Ossmand129bce2006-03-24 03:18:17 -080060
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +010061#ifdef CONFIG_PM
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030062static int sdhci_runtime_pm_get(struct sdhci_host *host);
63static int sdhci_runtime_pm_put(struct sdhci_host *host);
Adrian Hunterf0710a52013-05-06 12:17:32 +030064static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
65static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030066#else
67static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
68{
69 return 0;
70}
71static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
72{
73 return 0;
74}
Adrian Hunterf0710a52013-05-06 12:17:32 +030075static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
76{
77}
78static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
79{
80}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030081#endif
82
Pierre Ossmand129bce2006-03-24 03:18:17 -080083static void sdhci_dumpregs(struct sdhci_host *host)
84{
Girish K Sa3c76eb2011-10-11 11:44:09 +053085 pr_debug(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
Girish K Sa3c76eb2011-10-11 11:44:09 +053088 pr_debug(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));
Girish K Sa3c76eb2011-10-11 11:44:09 +053091 pr_debug(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));
Girish K Sa3c76eb2011-10-11 11:44:09 +053094 pr_debug(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));
Girish K Sa3c76eb2011-10-11 11:44:09 +053097 pr_debug(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));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530100 pr_debug(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));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530103 pr_debug(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));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530106 pr_debug(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));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530109 pr_debug(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));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530112 pr_debug(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));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530115 pr_debug(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));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530118 pr_debug(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));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530121 pr_debug(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
Adrian Huntere57a5f62014-11-04 12:42:46 +0200124 if (host->flags & SDHCI_USE_ADMA) {
125 if (host->flags & SDHCI_USE_64_BIT_DMA)
126 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
127 readl(host->ioaddr + SDHCI_ADMA_ERROR),
128 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
129 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
130 else
131 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
132 readl(host->ioaddr + SDHCI_ADMA_ERROR),
133 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
134 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100135
Girish K Sa3c76eb2011-10-11 11:44:09 +0530136 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800137}
138
139/*****************************************************************************\
140 * *
141 * Low level functions *
142 * *
143\*****************************************************************************/
144
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300145static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
146{
Russell King5b4f1f62014-04-25 12:57:02 +0100147 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300148
Adrian Hunterc79396c2011-12-27 15:48:42 +0200149 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100150 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300151 return;
152
Russell King5b4f1f62014-04-25 12:57:02 +0100153 if (enable) {
154 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
155 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800156
Russell King5b4f1f62014-04-25 12:57:02 +0100157 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
158 SDHCI_INT_CARD_INSERT;
159 } else {
160 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
161 }
Russell Kingb537f942014-04-25 12:56:01 +0100162
163 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
164 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300165}
166
167static void sdhci_enable_card_detection(struct sdhci_host *host)
168{
169 sdhci_set_card_detection(host, true);
170}
171
172static void sdhci_disable_card_detection(struct sdhci_host *host)
173{
174 sdhci_set_card_detection(host, false);
175}
176
Russell King03231f92014-04-25 12:57:12 +0100177void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800178{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700179 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800180
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300181 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800182
Adrian Hunterf0710a52013-05-06 12:17:32 +0300183 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800184 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300185 /* Reset-all turns off SD Bus Power */
186 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
187 sdhci_runtime_pm_bus_off(host);
188 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800189
Pierre Ossmane16514d82006-06-30 02:22:24 -0700190 /* Wait max 100 ms */
191 timeout = 100;
192
193 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300194 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700195 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530196 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700197 mmc_hostname(host->mmc), (int)mask);
198 sdhci_dumpregs(host);
199 return;
200 }
201 timeout--;
202 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800203 }
Russell King03231f92014-04-25 12:57:12 +0100204}
205EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300206
Russell King03231f92014-04-25 12:57:12 +0100207static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
208{
209 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
210 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
211 SDHCI_CARD_PRESENT))
212 return;
213 }
214
215 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800216
Russell Kingda91a8f2014-04-25 13:00:12 +0100217 if (mask & SDHCI_RESET_ALL) {
218 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
219 if (host->ops->enable_dma)
220 host->ops->enable_dma(host);
221 }
222
223 /* Resetting the controller clears many */
224 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800225 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226}
227
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800228static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
229
230static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800231{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800232 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100233 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800234 else
Russell King03231f92014-04-25 12:57:12 +0100235 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800236
Russell Kingb537f942014-04-25 12:56:01 +0100237 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
238 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
239 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
240 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
241 SDHCI_INT_RESPONSE;
242
243 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
244 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800245
246 if (soft) {
247 /* force clock reconfiguration */
248 host->clock = 0;
249 sdhci_set_ios(host->mmc, &host->mmc->ios);
250 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300251}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800252
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300253static void sdhci_reinit(struct sdhci_host *host)
254{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800255 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300256 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800257}
258
259static void sdhci_activate_led(struct sdhci_host *host)
260{
261 u8 ctrl;
262
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300263 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800264 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300265 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800266}
267
268static void sdhci_deactivate_led(struct sdhci_host *host)
269{
270 u8 ctrl;
271
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300272 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300274 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800275}
276
Pierre Ossmanf9134312008-12-21 17:01:48 +0100277#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100278static void sdhci_led_control(struct led_classdev *led,
279 enum led_brightness brightness)
280{
281 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
282 unsigned long flags;
283
284 spin_lock_irqsave(&host->lock, flags);
285
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300286 if (host->runtime_suspended)
287 goto out;
288
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100289 if (brightness == LED_OFF)
290 sdhci_deactivate_led(host);
291 else
292 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300293out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100294 spin_unlock_irqrestore(&host->lock, flags);
295}
296#endif
297
Pierre Ossmand129bce2006-03-24 03:18:17 -0800298/*****************************************************************************\
299 * *
300 * Core functions *
301 * *
302\*****************************************************************************/
303
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100304static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800305{
Pierre Ossman76591502008-07-21 00:32:11 +0200306 unsigned long flags;
307 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700308 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200309 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800310
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100311 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800312
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200314 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800315
Pierre Ossman76591502008-07-21 00:32:11 +0200316 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800317
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100318 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200319 if (!sg_miter_next(&host->sg_miter))
320 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800321
Pierre Ossman76591502008-07-21 00:32:11 +0200322 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800323
Pierre Ossman76591502008-07-21 00:32:11 +0200324 blksize -= len;
325 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200326
Pierre Ossman76591502008-07-21 00:32:11 +0200327 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800328
Pierre Ossman76591502008-07-21 00:32:11 +0200329 while (len) {
330 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300331 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200332 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800333 }
Pierre Ossman76591502008-07-21 00:32:11 +0200334
335 *buf = scratch & 0xFF;
336
337 buf++;
338 scratch >>= 8;
339 chunk--;
340 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800341 }
342 }
Pierre Ossman76591502008-07-21 00:32:11 +0200343
344 sg_miter_stop(&host->sg_miter);
345
346 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100347}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800348
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100349static void sdhci_write_block_pio(struct sdhci_host *host)
350{
Pierre Ossman76591502008-07-21 00:32:11 +0200351 unsigned long flags;
352 size_t blksize, len, chunk;
353 u32 scratch;
354 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100355
356 DBG("PIO writing\n");
357
358 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200359 chunk = 0;
360 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100361
Pierre Ossman76591502008-07-21 00:32:11 +0200362 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100363
364 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200365 if (!sg_miter_next(&host->sg_miter))
366 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100367
Pierre Ossman76591502008-07-21 00:32:11 +0200368 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200369
Pierre Ossman76591502008-07-21 00:32:11 +0200370 blksize -= len;
371 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100372
Pierre Ossman76591502008-07-21 00:32:11 +0200373 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100374
Pierre Ossman76591502008-07-21 00:32:11 +0200375 while (len) {
376 scratch |= (u32)*buf << (chunk * 8);
377
378 buf++;
379 chunk++;
380 len--;
381
382 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300383 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200384 chunk = 0;
385 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100386 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100387 }
388 }
Pierre Ossman76591502008-07-21 00:32:11 +0200389
390 sg_miter_stop(&host->sg_miter);
391
392 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100393}
394
395static void sdhci_transfer_pio(struct sdhci_host *host)
396{
397 u32 mask;
398
399 BUG_ON(!host->data);
400
Pierre Ossman76591502008-07-21 00:32:11 +0200401 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100402 return;
403
404 if (host->data->flags & MMC_DATA_READ)
405 mask = SDHCI_DATA_AVAILABLE;
406 else
407 mask = SDHCI_SPACE_AVAILABLE;
408
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200409 /*
410 * Some controllers (JMicron JMB38x) mess up the buffer bits
411 * for transfers < 4 bytes. As long as it is just one block,
412 * we can ignore the bits.
413 */
414 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
415 (host->data->blocks == 1))
416 mask = ~0;
417
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300418 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300419 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
420 udelay(100);
421
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100422 if (host->data->flags & MMC_DATA_READ)
423 sdhci_read_block_pio(host);
424 else
425 sdhci_write_block_pio(host);
426
Pierre Ossman76591502008-07-21 00:32:11 +0200427 host->blocks--;
428 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100429 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100430 }
431
432 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800433}
434
Pierre Ossman2134a922008-06-28 18:28:51 +0200435static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
436{
437 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800438 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200439}
440
441static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
442{
Cong Wang482fce92011-11-27 13:27:00 +0800443 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200444 local_irq_restore(*flags);
445}
446
Adrian Huntere57a5f62014-11-04 12:42:46 +0200447static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
448 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800449{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200450 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800451
Adrian Huntere57a5f62014-11-04 12:42:46 +0200452 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200453 dma_desc->cmd = cpu_to_le16(cmd);
454 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200455 dma_desc->addr_lo = cpu_to_le32((u32)addr);
456
457 if (host->flags & SDHCI_USE_64_BIT_DMA)
458 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800459}
460
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200461static void sdhci_adma_mark_end(void *desc)
462{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200463 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200464
Adrian Huntere57a5f62014-11-04 12:42:46 +0200465 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200466 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200467}
468
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200469static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200470 struct mmc_data *data)
471{
472 int direction;
473
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200474 void *desc;
475 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200476 dma_addr_t addr;
477 dma_addr_t align_addr;
478 int len, offset;
479
480 struct scatterlist *sg;
481 int i;
482 char *buffer;
483 unsigned long flags;
484
485 /*
486 * The spec does not specify endianness of descriptor table.
487 * We currently guess that it is LE.
488 */
489
490 if (data->flags & MMC_DATA_READ)
491 direction = DMA_FROM_DEVICE;
492 else
493 direction = DMA_TO_DEVICE;
494
Pierre Ossman2134a922008-06-28 18:28:51 +0200495 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200496 host->align_buffer, host->align_buffer_sz, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700497 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200498 goto fail;
Adrian Hunter76fe3792014-11-04 12:42:42 +0200499 BUG_ON(host->align_addr & host->align_mask);
Pierre Ossman2134a922008-06-28 18:28:51 +0200500
Haibo Chen348487c2014-12-09 17:04:05 +0800501 host->sg_count = sdhci_pre_dma_transfer(host, data, NULL);
502 if (host->sg_count < 0)
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200503 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200504
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200505 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200506 align = host->align_buffer;
507
508 align_addr = host->align_addr;
509
510 for_each_sg(data->sg, sg, host->sg_count, i) {
511 addr = sg_dma_address(sg);
512 len = sg_dma_len(sg);
513
514 /*
515 * The SDHCI specification states that ADMA
516 * addresses must be 32-bit aligned. If they
517 * aren't, then we use a bounce buffer for
518 * the (up to three) bytes that screw up the
519 * alignment.
520 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200521 offset = (host->align_sz - (addr & host->align_mask)) &
522 host->align_mask;
Pierre Ossman2134a922008-06-28 18:28:51 +0200523 if (offset) {
524 if (data->flags & MMC_DATA_WRITE) {
525 buffer = sdhci_kmap_atomic(sg, &flags);
526 memcpy(align, buffer, offset);
527 sdhci_kunmap_atomic(buffer, &flags);
528 }
529
Ben Dooks118cd172010-03-05 13:43:26 -0800530 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200531 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200532 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200533
534 BUG_ON(offset > 65536);
535
Adrian Hunter76fe3792014-11-04 12:42:42 +0200536 align += host->align_sz;
537 align_addr += host->align_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200538
Adrian Hunter76fe3792014-11-04 12:42:42 +0200539 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200540
541 addr += offset;
542 len -= offset;
543 }
544
Pierre Ossman2134a922008-06-28 18:28:51 +0200545 BUG_ON(len > 65536);
546
Ben Dooks118cd172010-03-05 13:43:26 -0800547 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200548 sdhci_adma_write_desc(host, desc, addr, len, ADMA2_TRAN_VALID);
Adrian Hunter76fe3792014-11-04 12:42:42 +0200549 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200550
551 /*
552 * If this triggers then we have a calculation bug
553 * somewhere. :/
554 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200555 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200556 }
557
Thomas Abraham70764a92010-05-26 14:42:04 -0700558 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
559 /*
560 * Mark the last descriptor as the terminating descriptor
561 */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200562 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200563 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200564 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700565 }
566 } else {
567 /*
568 * Add a terminating entry.
569 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200570
Thomas Abraham70764a92010-05-26 14:42:04 -0700571 /* nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200572 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700573 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200574
575 /*
576 * Resync align buffer as we might have changed it.
577 */
578 if (data->flags & MMC_DATA_WRITE) {
579 dma_sync_single_for_device(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200580 host->align_addr, host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200581 }
582
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200583 return 0;
584
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200585unmap_align:
586 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200587 host->align_buffer_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200588fail:
589 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200590}
591
592static void sdhci_adma_table_post(struct sdhci_host *host,
593 struct mmc_data *data)
594{
595 int direction;
596
597 struct scatterlist *sg;
598 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200599 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200600 char *buffer;
601 unsigned long flags;
Russell Kingde0b65a2014-04-25 12:58:29 +0100602 bool has_unaligned;
Pierre Ossman2134a922008-06-28 18:28:51 +0200603
604 if (data->flags & MMC_DATA_READ)
605 direction = DMA_FROM_DEVICE;
606 else
607 direction = DMA_TO_DEVICE;
608
Pierre Ossman2134a922008-06-28 18:28:51 +0200609 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200610 host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200611
Russell Kingde0b65a2014-04-25 12:58:29 +0100612 /* Do a quick scan of the SG list for any unaligned mappings */
613 has_unaligned = false;
614 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter76fe3792014-11-04 12:42:42 +0200615 if (sg_dma_address(sg) & host->align_mask) {
Russell Kingde0b65a2014-04-25 12:58:29 +0100616 has_unaligned = true;
617 break;
618 }
619
620 if (has_unaligned && data->flags & MMC_DATA_READ) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200621 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
622 data->sg_len, direction);
623
624 align = host->align_buffer;
625
626 for_each_sg(data->sg, sg, host->sg_count, i) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200627 if (sg_dma_address(sg) & host->align_mask) {
628 size = host->align_sz -
629 (sg_dma_address(sg) & host->align_mask);
Pierre Ossman2134a922008-06-28 18:28:51 +0200630
631 buffer = sdhci_kmap_atomic(sg, &flags);
632 memcpy(buffer, align, size);
633 sdhci_kunmap_atomic(buffer, &flags);
634
Adrian Hunter76fe3792014-11-04 12:42:42 +0200635 align += host->align_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200636 }
637 }
638 }
639
Haibo Chen348487c2014-12-09 17:04:05 +0800640 if (!data->host_cookie)
641 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
642 data->sg_len, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200643}
644
Andrei Warkentina3c77782011-04-11 16:13:42 -0500645static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800646{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700647 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500648 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700649 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800650
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200651 /*
652 * If the host controller provides us with an incorrect timeout
653 * value, just skip the check and use 0xE. The hardware may take
654 * longer to time out, but that's much better than having a too-short
655 * timeout value.
656 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200657 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200658 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200659
Andrei Warkentina3c77782011-04-11 16:13:42 -0500660 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100661 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500662 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800663
Andrei Warkentina3c77782011-04-11 16:13:42 -0500664 /* timeout in us */
665 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100666 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300667 else {
668 target_timeout = data->timeout_ns / 1000;
669 if (host->clock)
670 target_timeout += data->timeout_clks / host->clock;
671 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700672
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700673 /*
674 * Figure out needed cycles.
675 * We do this in steps in order to fit inside a 32 bit int.
676 * The first step is the minimum timeout, which will have a
677 * minimum resolution of 6 bits:
678 * (1) 2^13*1000 > 2^22,
679 * (2) host->timeout_clk < 2^16
680 * =>
681 * (1) / (2) > 2^6
682 */
683 count = 0;
684 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
685 while (current_timeout < target_timeout) {
686 count++;
687 current_timeout <<= 1;
688 if (count >= 0xF)
689 break;
690 }
691
692 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400693 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
694 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700695 count = 0xE;
696 }
697
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200698 return count;
699}
700
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300701static void sdhci_set_transfer_irqs(struct sdhci_host *host)
702{
703 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
704 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
705
706 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100707 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300708 else
Russell Kingb537f942014-04-25 12:56:01 +0100709 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
710
711 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
712 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300713}
714
Aisheng Dongb45e6682014-08-27 15:26:29 +0800715static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200716{
717 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800718
719 if (host->ops->set_timeout) {
720 host->ops->set_timeout(host, cmd);
721 } else {
722 count = sdhci_calc_timeout(host, cmd);
723 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
724 }
725}
726
727static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
728{
Pierre Ossman2134a922008-06-28 18:28:51 +0200729 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500730 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200731 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200732
733 WARN_ON(host->data);
734
Aisheng Dongb45e6682014-08-27 15:26:29 +0800735 if (data || (cmd->flags & MMC_RSP_BUSY))
736 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500737
738 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200739 return;
740
741 /* Sanity checks */
742 BUG_ON(data->blksz * data->blocks > 524288);
743 BUG_ON(data->blksz > host->mmc->max_blk_size);
744 BUG_ON(data->blocks > 65535);
745
746 host->data = data;
747 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400748 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200749
Richard Röjforsa13abc72009-09-22 16:45:30 -0700750 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100751 host->flags |= SDHCI_REQ_USE_DMA;
752
Pierre Ossman2134a922008-06-28 18:28:51 +0200753 /*
754 * FIXME: This doesn't account for merging when mapping the
755 * scatterlist.
756 */
757 if (host->flags & SDHCI_REQ_USE_DMA) {
758 int broken, i;
759 struct scatterlist *sg;
760
761 broken = 0;
762 if (host->flags & SDHCI_USE_ADMA) {
763 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
764 broken = 1;
765 } else {
766 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
767 broken = 1;
768 }
769
770 if (unlikely(broken)) {
771 for_each_sg(data->sg, sg, data->sg_len, i) {
772 if (sg->length & 0x3) {
773 DBG("Reverting to PIO because of "
774 "transfer size (%d)\n",
775 sg->length);
776 host->flags &= ~SDHCI_REQ_USE_DMA;
777 break;
778 }
779 }
780 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100781 }
782
783 /*
784 * The assumption here being that alignment is the same after
785 * translation to device address space.
786 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200787 if (host->flags & SDHCI_REQ_USE_DMA) {
788 int broken, i;
789 struct scatterlist *sg;
790
791 broken = 0;
792 if (host->flags & SDHCI_USE_ADMA) {
793 /*
794 * As we use 3 byte chunks to work around
795 * alignment problems, we need to check this
796 * quirk.
797 */
798 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
799 broken = 1;
800 } else {
801 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
802 broken = 1;
803 }
804
805 if (unlikely(broken)) {
806 for_each_sg(data->sg, sg, data->sg_len, i) {
807 if (sg->offset & 0x3) {
808 DBG("Reverting to PIO because of "
809 "bad alignment\n");
810 host->flags &= ~SDHCI_REQ_USE_DMA;
811 break;
812 }
813 }
814 }
815 }
816
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200817 if (host->flags & SDHCI_REQ_USE_DMA) {
818 if (host->flags & SDHCI_USE_ADMA) {
819 ret = sdhci_adma_table_pre(host, data);
820 if (ret) {
821 /*
822 * This only happens when someone fed
823 * us an invalid request.
824 */
825 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200826 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200827 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300828 sdhci_writel(host, host->adma_addr,
829 SDHCI_ADMA_ADDRESS);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200830 if (host->flags & SDHCI_USE_64_BIT_DMA)
831 sdhci_writel(host,
832 (u64)host->adma_addr >> 32,
833 SDHCI_ADMA_ADDRESS_HI);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200834 }
835 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300836 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200837
Haibo Chen348487c2014-12-09 17:04:05 +0800838 sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300839 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200840 /*
841 * This only happens when someone fed
842 * us an invalid request.
843 */
844 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200845 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200846 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200847 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300848 sdhci_writel(host, sg_dma_address(data->sg),
849 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200850 }
851 }
852 }
853
Pierre Ossman2134a922008-06-28 18:28:51 +0200854 /*
855 * Always adjust the DMA selection as some controllers
856 * (e.g. JMicron) can't do PIO properly when the selection
857 * is ADMA.
858 */
859 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300860 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200861 ctrl &= ~SDHCI_CTRL_DMA_MASK;
862 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200863 (host->flags & SDHCI_USE_ADMA)) {
864 if (host->flags & SDHCI_USE_64_BIT_DMA)
865 ctrl |= SDHCI_CTRL_ADMA64;
866 else
867 ctrl |= SDHCI_CTRL_ADMA32;
868 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200869 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200870 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300871 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100872 }
873
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200874 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200875 int flags;
876
877 flags = SG_MITER_ATOMIC;
878 if (host->data->flags & MMC_DATA_READ)
879 flags |= SG_MITER_TO_SG;
880 else
881 flags |= SG_MITER_FROM_SG;
882 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200883 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800884 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700885
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300886 sdhci_set_transfer_irqs(host);
887
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400888 /* Set the DMA boundary value and block size */
889 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
890 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300891 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700892}
893
894static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500895 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700896{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800897 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500898 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700899
Dong Aisheng2b558c12013-10-30 22:09:48 +0800900 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800901 if (host->quirks2 &
902 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
903 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
904 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800905 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800906 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
907 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800908 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800909 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700910 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800911 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700912
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200913 WARN_ON(!host->data);
914
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800915 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
916 mode = SDHCI_TRNS_BLK_CNT_EN;
917
Andrei Warkentine89d4562011-05-23 15:06:37 -0500918 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800919 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500920 /*
921 * If we are sending CMD23, CMD12 never gets sent
922 * on successful completion (so no Auto-CMD12).
923 */
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800924 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
925 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500926 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500927 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
928 mode |= SDHCI_TRNS_AUTO_CMD23;
929 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
930 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700931 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500932
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700933 if (data->flags & MMC_DATA_READ)
934 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100935 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700936 mode |= SDHCI_TRNS_DMA;
937
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300938 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800939}
940
941static void sdhci_finish_data(struct sdhci_host *host)
942{
943 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800944
945 BUG_ON(!host->data);
946
947 data = host->data;
948 host->data = NULL;
949
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100950 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200951 if (host->flags & SDHCI_USE_ADMA)
952 sdhci_adma_table_post(host, data);
953 else {
Haibo Chen348487c2014-12-09 17:04:05 +0800954 if (!data->host_cookie)
955 dma_unmap_sg(mmc_dev(host->mmc),
956 data->sg, data->sg_len,
957 (data->flags & MMC_DATA_READ) ?
Pierre Ossman2134a922008-06-28 18:28:51 +0200958 DMA_FROM_DEVICE : DMA_TO_DEVICE);
959 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800960 }
961
962 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200963 * The specification states that the block count register must
964 * be updated, but it does not specify at what point in the
965 * data flow. That makes the register entirely useless to read
966 * back so we have to assume that nothing made it to the card
967 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800968 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200969 if (data->error)
970 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800971 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200972 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800973
Andrei Warkentine89d4562011-05-23 15:06:37 -0500974 /*
975 * Need to send CMD12 if -
976 * a) open-ended multiblock transfer (no CMD23)
977 * b) error in multiblock transfer
978 */
979 if (data->stop &&
980 (data->error ||
981 !host->mrq->sbc)) {
982
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983 /*
984 * The controller needs a reset of internal state machines
985 * upon error conditions.
986 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200987 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100988 sdhci_do_reset(host, SDHCI_RESET_CMD);
989 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800990 }
991
992 sdhci_send_command(host, data->stop);
993 } else
994 tasklet_schedule(&host->finish_tasklet);
995}
996
Dong Aishengc0e551292013-09-13 19:11:31 +0800997void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998{
999 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001000 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001001 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001002
1003 WARN_ON(host->cmd);
1004
Pierre Ossmand129bce2006-03-24 03:18:17 -08001005 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001006 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001007
1008 mask = SDHCI_CMD_INHIBIT;
1009 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1010 mask |= SDHCI_DATA_INHIBIT;
1011
1012 /* We shouldn't wait for data inihibit for stop commands, even
1013 though they might use busy signaling */
1014 if (host->mrq->data && (cmd == host->mrq->data->stop))
1015 mask &= ~SDHCI_DATA_INHIBIT;
1016
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001017 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001018 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301019 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001020 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001021 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001022 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001023 tasklet_schedule(&host->finish_tasklet);
1024 return;
1025 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001026 timeout--;
1027 mdelay(1);
1028 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001029
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001030 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001031 if (!cmd->data && cmd->busy_timeout > 9000)
1032 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001033 else
1034 timeout += 10 * HZ;
1035 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001036
1037 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001038 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001039
Andrei Warkentina3c77782011-04-11 16:13:42 -05001040 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001041
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001042 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001043
Andrei Warkentine89d4562011-05-23 15:06:37 -05001044 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001045
Pierre Ossmand129bce2006-03-24 03:18:17 -08001046 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301047 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001048 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001049 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001050 tasklet_schedule(&host->finish_tasklet);
1051 return;
1052 }
1053
1054 if (!(cmd->flags & MMC_RSP_PRESENT))
1055 flags = SDHCI_CMD_RESP_NONE;
1056 else if (cmd->flags & MMC_RSP_136)
1057 flags = SDHCI_CMD_RESP_LONG;
1058 else if (cmd->flags & MMC_RSP_BUSY)
1059 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1060 else
1061 flags = SDHCI_CMD_RESP_SHORT;
1062
1063 if (cmd->flags & MMC_RSP_CRC)
1064 flags |= SDHCI_CMD_CRC;
1065 if (cmd->flags & MMC_RSP_OPCODE)
1066 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301067
1068 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301069 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1070 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001071 flags |= SDHCI_CMD_DATA;
1072
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001073 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001074}
Dong Aishengc0e551292013-09-13 19:11:31 +08001075EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001076
1077static void sdhci_finish_command(struct sdhci_host *host)
1078{
1079 int i;
1080
1081 BUG_ON(host->cmd == NULL);
1082
1083 if (host->cmd->flags & MMC_RSP_PRESENT) {
1084 if (host->cmd->flags & MMC_RSP_136) {
1085 /* CRC is stripped so we need to do some shifting. */
1086 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001087 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001088 SDHCI_RESPONSE + (3-i)*4) << 8;
1089 if (i != 3)
1090 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001091 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001092 SDHCI_RESPONSE + (3-i)*4-1);
1093 }
1094 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001095 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001096 }
1097 }
1098
Pierre Ossman17b04292007-07-22 22:18:46 +02001099 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001100
Andrei Warkentine89d4562011-05-23 15:06:37 -05001101 /* Finished CMD23, now send actual command. */
1102 if (host->cmd == host->mrq->sbc) {
1103 host->cmd = NULL;
1104 sdhci_send_command(host, host->mrq->cmd);
1105 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001106
Andrei Warkentine89d4562011-05-23 15:06:37 -05001107 /* Processed actual command. */
1108 if (host->data && host->data_early)
1109 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001110
Andrei Warkentine89d4562011-05-23 15:06:37 -05001111 if (!host->cmd->data)
1112 tasklet_schedule(&host->finish_tasklet);
1113
1114 host->cmd = NULL;
1115 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001116}
1117
Kevin Liu52983382013-01-31 11:31:37 +08001118static u16 sdhci_get_preset_value(struct sdhci_host *host)
1119{
Russell Kingd975f122014-04-25 12:59:31 +01001120 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001121
Russell Kingd975f122014-04-25 12:59:31 +01001122 switch (host->timing) {
1123 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001124 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1125 break;
Russell Kingd975f122014-04-25 12:59:31 +01001126 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001127 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1128 break;
Russell Kingd975f122014-04-25 12:59:31 +01001129 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001130 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1131 break;
Russell Kingd975f122014-04-25 12:59:31 +01001132 case MMC_TIMING_UHS_SDR104:
1133 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001134 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1135 break;
Russell Kingd975f122014-04-25 12:59:31 +01001136 case MMC_TIMING_UHS_DDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001137 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1138 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001139 case MMC_TIMING_MMC_HS400:
1140 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1141 break;
Kevin Liu52983382013-01-31 11:31:37 +08001142 default:
1143 pr_warn("%s: Invalid UHS-I mode selected\n",
1144 mmc_hostname(host->mmc));
1145 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1146 break;
1147 }
1148 return preset;
1149}
1150
Russell King17710592014-04-25 12:58:55 +01001151void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001152{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301153 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001154 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301155 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001156 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001157
Russell King1650d0c2014-04-25 12:58:50 +01001158 host->mmc->actual_clock = 0;
1159
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001160 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001161
1162 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001163 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001164
Zhangfei Gao85105c52010-08-06 07:10:01 +08001165 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001166 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001167 u16 pre_val;
1168
1169 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1170 pre_val = sdhci_get_preset_value(host);
1171 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1172 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1173 if (host->clk_mul &&
1174 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1175 clk = SDHCI_PROG_CLOCK_MODE;
1176 real_div = div + 1;
1177 clk_mul = host->clk_mul;
1178 } else {
1179 real_div = max_t(int, 1, div << 1);
1180 }
1181 goto clock_set;
1182 }
1183
Arindam Nathc3ed3872011-05-05 12:19:06 +05301184 /*
1185 * Check if the Host Controller supports Programmable Clock
1186 * Mode.
1187 */
1188 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001189 for (div = 1; div <= 1024; div++) {
1190 if ((host->max_clk * host->clk_mul / div)
1191 <= clock)
1192 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001193 }
Kevin Liu52983382013-01-31 11:31:37 +08001194 /*
1195 * Set Programmable Clock Mode in the Clock
1196 * Control register.
1197 */
1198 clk = SDHCI_PROG_CLOCK_MODE;
1199 real_div = div;
1200 clk_mul = host->clk_mul;
1201 div--;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301202 } else {
1203 /* Version 3.00 divisors must be a multiple of 2. */
1204 if (host->max_clk <= clock)
1205 div = 1;
1206 else {
1207 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1208 div += 2) {
1209 if ((host->max_clk / div) <= clock)
1210 break;
1211 }
1212 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001213 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301214 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001215 }
1216 } else {
1217 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001218 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001219 if ((host->max_clk / div) <= clock)
1220 break;
1221 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001222 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301223 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001224 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001225
Kevin Liu52983382013-01-31 11:31:37 +08001226clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001227 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001228 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301229 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001230 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1231 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001232 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001233 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001234
Chris Ball27f6cb12009-09-22 16:45:31 -07001235 /* Wait max 20 ms */
1236 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001237 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001238 & SDHCI_CLOCK_INT_STABLE)) {
1239 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301240 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001241 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001242 sdhci_dumpregs(host);
1243 return;
1244 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001245 timeout--;
1246 mdelay(1);
1247 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001248
1249 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001250 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001251}
Russell King17710592014-04-25 12:58:55 +01001252EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001253
Russell King24fbb3c2014-04-25 13:00:06 +01001254static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1255 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001256{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001257 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001258 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001259
Tim Kryger52221612014-06-25 00:25:34 -07001260 if (!IS_ERR(mmc->supply.vmmc)) {
1261 spin_unlock_irq(&host->lock);
Markus Mayer4e743f12014-07-03 13:27:42 -07001262 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Tim Kryger52221612014-06-25 00:25:34 -07001263 spin_lock_irq(&host->lock);
Tim Kryger3cbc6122015-01-14 07:24:12 +01001264
1265 if (mode != MMC_POWER_OFF)
1266 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1267 else
1268 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1269
Tim Kryger52221612014-06-25 00:25:34 -07001270 return;
1271 }
1272
Russell King24fbb3c2014-04-25 13:00:06 +01001273 if (mode != MMC_POWER_OFF) {
1274 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001275 case MMC_VDD_165_195:
1276 pwr = SDHCI_POWER_180;
1277 break;
1278 case MMC_VDD_29_30:
1279 case MMC_VDD_30_31:
1280 pwr = SDHCI_POWER_300;
1281 break;
1282 case MMC_VDD_32_33:
1283 case MMC_VDD_33_34:
1284 pwr = SDHCI_POWER_330;
1285 break;
1286 default:
1287 BUG();
1288 }
1289 }
1290
1291 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001292 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001293
Pierre Ossmanae628902009-05-03 20:45:03 +02001294 host->pwr = pwr;
1295
1296 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001297 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001298 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1299 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001300 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001301 } else {
1302 /*
1303 * Spec says that we should clear the power reg before setting
1304 * a new value. Some controllers don't seem to like this though.
1305 */
1306 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1307 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001308
Russell Kinge921a8b2014-04-25 13:00:01 +01001309 /*
1310 * At least the Marvell CaFe chip gets confused if we set the
1311 * voltage and set turn on power at the same time, so set the
1312 * voltage first.
1313 */
1314 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1315 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001316
Russell Kinge921a8b2014-04-25 13:00:01 +01001317 pwr |= SDHCI_POWER_ON;
1318
Pierre Ossmanae628902009-05-03 20:45:03 +02001319 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1320
Russell Kinge921a8b2014-04-25 13:00:01 +01001321 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1322 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001323
Russell Kinge921a8b2014-04-25 13:00:01 +01001324 /*
1325 * Some controllers need an extra 10ms delay of 10ms before
1326 * they can apply clock after applying power
1327 */
1328 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1329 mdelay(10);
1330 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001331}
1332
Pierre Ossmand129bce2006-03-24 03:18:17 -08001333/*****************************************************************************\
1334 * *
1335 * MMC callbacks *
1336 * *
1337\*****************************************************************************/
1338
1339static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1340{
1341 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001342 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001343 unsigned long flags;
1344
1345 host = mmc_priv(mmc);
1346
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001347 sdhci_runtime_pm_get(host);
1348
Scott Branden04e079cf2015-03-10 11:35:10 -07001349 /* Firstly check card presence */
1350 present = sdhci_do_get_cd(host);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001351
Pierre Ossmand129bce2006-03-24 03:18:17 -08001352 spin_lock_irqsave(&host->lock, flags);
1353
1354 WARN_ON(host->mrq != NULL);
1355
Pierre Ossmanf9134312008-12-21 17:01:48 +01001356#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001357 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001358#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001359
1360 /*
1361 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1362 * requests if Auto-CMD12 is enabled.
1363 */
1364 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001365 if (mrq->stop) {
1366 mrq->data->stop = NULL;
1367 mrq->stop = NULL;
1368 }
1369 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001370
1371 host->mrq = mrq;
1372
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001373 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001374 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001375 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301376 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001377 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001378 sdhci_send_command(host, mrq->sbc);
1379 else
1380 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301381 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001382
Pierre Ossman5f25a662006-10-04 02:15:39 -07001383 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001384 spin_unlock_irqrestore(&host->lock, flags);
1385}
1386
Russell King2317f562014-04-25 12:57:07 +01001387void sdhci_set_bus_width(struct sdhci_host *host, int width)
1388{
1389 u8 ctrl;
1390
1391 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1392 if (width == MMC_BUS_WIDTH_8) {
1393 ctrl &= ~SDHCI_CTRL_4BITBUS;
1394 if (host->version >= SDHCI_SPEC_300)
1395 ctrl |= SDHCI_CTRL_8BITBUS;
1396 } else {
1397 if (host->version >= SDHCI_SPEC_300)
1398 ctrl &= ~SDHCI_CTRL_8BITBUS;
1399 if (width == MMC_BUS_WIDTH_4)
1400 ctrl |= SDHCI_CTRL_4BITBUS;
1401 else
1402 ctrl &= ~SDHCI_CTRL_4BITBUS;
1403 }
1404 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1405}
1406EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1407
Russell King96d7b782014-04-25 12:59:26 +01001408void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1409{
1410 u16 ctrl_2;
1411
1412 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1413 /* Select Bus Speed Mode for host */
1414 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1415 if ((timing == MMC_TIMING_MMC_HS200) ||
1416 (timing == MMC_TIMING_UHS_SDR104))
1417 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1418 else if (timing == MMC_TIMING_UHS_SDR12)
1419 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1420 else if (timing == MMC_TIMING_UHS_SDR25)
1421 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1422 else if (timing == MMC_TIMING_UHS_SDR50)
1423 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1424 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1425 (timing == MMC_TIMING_MMC_DDR52))
1426 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001427 else if (timing == MMC_TIMING_MMC_HS400)
1428 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001429 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1430}
1431EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1432
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001433static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001434{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001435 unsigned long flags;
1436 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001437 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001438
Pierre Ossmand129bce2006-03-24 03:18:17 -08001439 spin_lock_irqsave(&host->lock, flags);
1440
Adrian Hunterceb61432011-12-27 15:48:41 +02001441 if (host->flags & SDHCI_DEVICE_DEAD) {
1442 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001443 if (!IS_ERR(mmc->supply.vmmc) &&
1444 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001445 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001446 return;
1447 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001448
Pierre Ossmand129bce2006-03-24 03:18:17 -08001449 /*
1450 * Reset the chip on each power off.
1451 * Should clear out any weird states.
1452 */
1453 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001454 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001455 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001456 }
1457
Kevin Liu52983382013-01-31 11:31:37 +08001458 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001459 (ios->power_mode == MMC_POWER_UP) &&
1460 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001461 sdhci_enable_preset_value(host, false);
1462
Russell King373073e2014-04-25 12:58:45 +01001463 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001464 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001465 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001466
1467 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1468 host->clock) {
1469 host->timeout_clk = host->mmc->actual_clock ?
1470 host->mmc->actual_clock / 1000 :
1471 host->clock / 1000;
1472 host->mmc->max_busy_timeout =
1473 host->ops->get_max_timeout_count ?
1474 host->ops->get_max_timeout_count(host) :
1475 1 << 27;
1476 host->mmc->max_busy_timeout /= host->timeout_clk;
1477 }
Russell King373073e2014-04-25 12:58:45 +01001478 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001479
Russell King24fbb3c2014-04-25 13:00:06 +01001480 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001481
Philip Rakity643a81f2010-09-23 08:24:32 -07001482 if (host->ops->platform_send_init_74_clocks)
1483 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1484
Russell King2317f562014-04-25 12:57:07 +01001485 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001486
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001487 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001488
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001489 if ((ios->timing == MMC_TIMING_SD_HS ||
1490 ios->timing == MMC_TIMING_MMC_HS)
1491 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001492 ctrl |= SDHCI_CTRL_HISPD;
1493 else
1494 ctrl &= ~SDHCI_CTRL_HISPD;
1495
Arindam Nathd6d50a12011-05-05 12:18:59 +05301496 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301497 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301498
1499 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001500 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1501 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001502 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301503 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301504 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1505 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001506 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301507 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301508
Russell Kingda91a8f2014-04-25 13:00:12 +01001509 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301510 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301511 /*
1512 * We only need to set Driver Strength if the
1513 * preset value enable is not set.
1514 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001515 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301516 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1517 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1518 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1519 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1520 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1521
1522 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301523 } else {
1524 /*
1525 * According to SDHC Spec v3.00, if the Preset Value
1526 * Enable in the Host Control 2 register is set, we
1527 * need to reset SD Clock Enable before changing High
1528 * Speed Enable to avoid generating clock gliches.
1529 */
Arindam Nath758535c2011-05-05 12:19:00 +05301530
1531 /* Reset SD Clock Enable */
1532 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1533 clk &= ~SDHCI_CLOCK_CARD_EN;
1534 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1535
1536 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1537
1538 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001539 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301540 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301541
Arindam Nath49c468f2011-05-05 12:19:01 +05301542 /* Reset SD Clock Enable */
1543 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1544 clk &= ~SDHCI_CLOCK_CARD_EN;
1545 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1546
Russell King96d7b782014-04-25 12:59:26 +01001547 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001548 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301549
Kevin Liu52983382013-01-31 11:31:37 +08001550 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1551 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1552 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1553 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1554 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1555 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1556 u16 preset;
1557
1558 sdhci_enable_preset_value(host, true);
1559 preset = sdhci_get_preset_value(host);
1560 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1561 >> SDHCI_PRESET_DRV_SHIFT;
1562 }
1563
Arindam Nath49c468f2011-05-05 12:19:01 +05301564 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001565 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301566 } else
1567 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301568
Leandro Dorileob8352262007-07-25 23:47:04 +02001569 /*
1570 * Some (ENE) controllers go apeshit on some ios operation,
1571 * signalling timeout and CRC errors even on CMD0. Resetting
1572 * it on each ios seems to solve the problem.
1573 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301574 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001575 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001576
Pierre Ossman5f25a662006-10-04 02:15:39 -07001577 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001578 spin_unlock_irqrestore(&host->lock, flags);
1579}
1580
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001581static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1582{
1583 struct sdhci_host *host = mmc_priv(mmc);
1584
1585 sdhci_runtime_pm_get(host);
1586 sdhci_do_set_ios(host, ios);
1587 sdhci_runtime_pm_put(host);
1588}
1589
Kevin Liu94144a42013-02-28 17:35:53 +08001590static int sdhci_do_get_cd(struct sdhci_host *host)
1591{
1592 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1593
1594 if (host->flags & SDHCI_DEVICE_DEAD)
1595 return 0;
1596
1597 /* If polling/nonremovable, assume that the card is always present. */
1598 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1599 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1600 return 1;
1601
1602 /* Try slot gpio detect */
1603 if (!IS_ERR_VALUE(gpio_cd))
1604 return !!gpio_cd;
1605
1606 /* Host native card detect */
1607 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1608}
1609
1610static int sdhci_get_cd(struct mmc_host *mmc)
1611{
1612 struct sdhci_host *host = mmc_priv(mmc);
1613 int ret;
1614
1615 sdhci_runtime_pm_get(host);
1616 ret = sdhci_do_get_cd(host);
1617 sdhci_runtime_pm_put(host);
1618 return ret;
1619}
1620
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001621static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001622{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001623 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001624 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001625
Pierre Ossmand129bce2006-03-24 03:18:17 -08001626 spin_lock_irqsave(&host->lock, flags);
1627
Pierre Ossman1e728592008-04-16 19:13:13 +02001628 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001629 is_readonly = 0;
1630 else if (host->ops->get_ro)
1631 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001632 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001633 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1634 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001635
1636 spin_unlock_irqrestore(&host->lock, flags);
1637
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001638 /* This quirk needs to be replaced by a callback-function later */
1639 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1640 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001641}
1642
Takashi Iwai82b0e232011-04-21 20:26:38 +02001643#define SAMPLE_COUNT 5
1644
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001645static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001646{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001647 int i, ro_count;
1648
Takashi Iwai82b0e232011-04-21 20:26:38 +02001649 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001650 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001651
1652 ro_count = 0;
1653 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001654 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001655 if (++ro_count > SAMPLE_COUNT / 2)
1656 return 1;
1657 }
1658 msleep(30);
1659 }
1660 return 0;
1661}
1662
Adrian Hunter20758b62011-08-29 16:42:12 +03001663static void sdhci_hw_reset(struct mmc_host *mmc)
1664{
1665 struct sdhci_host *host = mmc_priv(mmc);
1666
1667 if (host->ops && host->ops->hw_reset)
1668 host->ops->hw_reset(host);
1669}
1670
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001671static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001672{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001673 struct sdhci_host *host = mmc_priv(mmc);
1674 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001675
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001676 sdhci_runtime_pm_get(host);
1677 ret = sdhci_do_get_ro(host);
1678 sdhci_runtime_pm_put(host);
1679 return ret;
1680}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001681
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001682static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1683{
Russell Kingbe138552014-04-25 12:55:56 +01001684 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001685 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001686 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001687 else
Russell Kingb537f942014-04-25 12:56:01 +01001688 host->ier &= ~SDHCI_INT_CARD_INT;
1689
1690 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1691 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001692 mmiowb();
1693 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001694}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001695
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001696static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1697{
1698 struct sdhci_host *host = mmc_priv(mmc);
1699 unsigned long flags;
1700
Russell Kingef104332014-04-25 12:55:41 +01001701 sdhci_runtime_pm_get(host);
1702
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001703 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001704 if (enable)
1705 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1706 else
1707 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1708
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001709 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001710 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001711
1712 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001713}
1714
Philip Rakity6231f3d2012-07-23 15:56:23 -07001715static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001716 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001717{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001718 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001719 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001720 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001721
1722 /*
1723 * Signal Voltage Switching is only applicable for Host Controllers
1724 * v3.00 and above.
1725 */
1726 if (host->version < SDHCI_SPEC_300)
1727 return 0;
1728
Philip Rakity6231f3d2012-07-23 15:56:23 -07001729 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001730
Fabio Estevam21f59982013-02-14 10:35:03 -02001731 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001732 case MMC_SIGNAL_VOLTAGE_330:
1733 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1734 ctrl &= ~SDHCI_CTRL_VDD_180;
1735 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1736
Tim Kryger3a48edc2014-06-13 10:13:56 -07001737 if (!IS_ERR(mmc->supply.vqmmc)) {
1738 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1739 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001740 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001741 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1742 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001743 return -EIO;
1744 }
1745 }
1746 /* Wait for 5ms */
1747 usleep_range(5000, 5500);
1748
1749 /* 3.3V regulator output should be stable within 5 ms */
1750 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1751 if (!(ctrl & SDHCI_CTRL_VDD_180))
1752 return 0;
1753
Joe Perches66061102014-09-12 14:56:56 -07001754 pr_warn("%s: 3.3V regulator output did not became stable\n",
1755 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001756
1757 return -EAGAIN;
1758 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001759 if (!IS_ERR(mmc->supply.vqmmc)) {
1760 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001761 1700000, 1950000);
1762 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001763 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1764 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001765 return -EIO;
1766 }
1767 }
1768
1769 /*
1770 * Enable 1.8V Signal Enable in the Host Control2
1771 * register
1772 */
1773 ctrl |= SDHCI_CTRL_VDD_180;
1774 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1775
Vincent Yang9d967a62015-01-20 16:05:15 +08001776 /* Some controller need to do more when switching */
1777 if (host->ops->voltage_switch)
1778 host->ops->voltage_switch(host);
1779
Kevin Liu20b92a32012-12-17 19:29:26 +08001780 /* 1.8V regulator output should be stable within 5 ms */
1781 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1782 if (ctrl & SDHCI_CTRL_VDD_180)
1783 return 0;
1784
Joe Perches66061102014-09-12 14:56:56 -07001785 pr_warn("%s: 1.8V regulator output did not became stable\n",
1786 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001787
1788 return -EAGAIN;
1789 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001790 if (!IS_ERR(mmc->supply.vqmmc)) {
1791 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1792 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001793 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001794 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1795 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001796 return -EIO;
1797 }
1798 }
1799 return 0;
1800 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301801 /* No signal voltage switch required */
1802 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001803 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301804}
1805
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001806static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001807 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001808{
1809 struct sdhci_host *host = mmc_priv(mmc);
1810 int err;
1811
1812 if (host->version < SDHCI_SPEC_300)
1813 return 0;
1814 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001815 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001816 sdhci_runtime_pm_put(host);
1817 return err;
1818}
1819
Kevin Liu20b92a32012-12-17 19:29:26 +08001820static int sdhci_card_busy(struct mmc_host *mmc)
1821{
1822 struct sdhci_host *host = mmc_priv(mmc);
1823 u32 present_state;
1824
1825 sdhci_runtime_pm_get(host);
1826 /* Check whether DAT[3:0] is 0000 */
1827 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1828 sdhci_runtime_pm_put(host);
1829
1830 return !(present_state & SDHCI_DATA_LVL_MASK);
1831}
1832
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001833static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1834{
1835 struct sdhci_host *host = mmc_priv(mmc);
1836 unsigned long flags;
1837
1838 spin_lock_irqsave(&host->lock, flags);
1839 host->flags |= SDHCI_HS400_TUNING;
1840 spin_unlock_irqrestore(&host->lock, flags);
1841
1842 return 0;
1843}
1844
Girish K S069c9f12012-01-06 09:56:39 +05301845static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301846{
Russell King4b6f37d2014-04-25 12:59:36 +01001847 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301848 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301849 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301850 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001851 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001852 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001853 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301854
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001855 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001856 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301857
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001858 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1859 host->flags &= ~SDHCI_HS400_TUNING;
1860
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001861 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1862 tuning_count = host->tuning_count;
1863
Arindam Nathb513ea22011-05-05 12:19:04 +05301864 /*
Girish K S069c9f12012-01-06 09:56:39 +05301865 * The Host Controller needs tuning only in case of SDR104 mode
1866 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301867 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301868 * If the Host Controller supports the HS200 mode then the
1869 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301870 */
Russell King4b6f37d2014-04-25 12:59:36 +01001871 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001872 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001873 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001874 err = -EINVAL;
1875 goto out_unlock;
1876
Russell King4b6f37d2014-04-25 12:59:36 +01001877 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001878 /*
1879 * Periodic re-tuning for HS400 is not expected to be needed, so
1880 * disable it here.
1881 */
1882 if (hs400_tuning)
1883 tuning_count = 0;
1884 break;
1885
Russell King4b6f37d2014-04-25 12:59:36 +01001886 case MMC_TIMING_UHS_SDR104:
1887 break;
Girish K S069c9f12012-01-06 09:56:39 +05301888
Russell King4b6f37d2014-04-25 12:59:36 +01001889 case MMC_TIMING_UHS_SDR50:
1890 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1891 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1892 break;
1893 /* FALLTHROUGH */
1894
1895 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001896 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301897 }
1898
Dong Aisheng45251812013-09-13 19:11:30 +08001899 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001900 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001901 err = host->ops->platform_execute_tuning(host, opcode);
1902 sdhci_runtime_pm_put(host);
1903 return err;
1904 }
1905
Russell King4b6f37d2014-04-25 12:59:36 +01001906 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1907 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001908 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1909 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301910 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1911
1912 /*
1913 * As per the Host Controller spec v3.00, tuning command
1914 * generates Buffer Read Ready interrupt, so enable that.
1915 *
1916 * Note: The spec clearly says that when tuning sequence
1917 * is being performed, the controller does not generate
1918 * interrupts other than Buffer Read Ready interrupt. But
1919 * to make sure we don't hit a controller bug, we _only_
1920 * enable Buffer Read Ready interrupt here.
1921 */
Russell Kingb537f942014-04-25 12:56:01 +01001922 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1923 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301924
1925 /*
1926 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1927 * of loops reaches 40 times or a timeout of 150ms occurs.
1928 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301929 do {
1930 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001931 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301932
Girish K S069c9f12012-01-06 09:56:39 +05301933 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301934 cmd.arg = 0;
1935 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1936 cmd.retries = 0;
1937 cmd.data = NULL;
1938 cmd.error = 0;
1939
Al Cooper7ce45e92014-05-09 11:34:07 -04001940 if (tuning_loop_counter-- == 0)
1941 break;
1942
Arindam Nathb513ea22011-05-05 12:19:04 +05301943 mrq.cmd = &cmd;
1944 host->mrq = &mrq;
1945
1946 /*
1947 * In response to CMD19, the card sends 64 bytes of tuning
1948 * block to the Host Controller. So we set the block size
1949 * to 64 here.
1950 */
Girish K S069c9f12012-01-06 09:56:39 +05301951 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1952 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1953 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1954 SDHCI_BLOCK_SIZE);
1955 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1956 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1957 SDHCI_BLOCK_SIZE);
1958 } else {
1959 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1960 SDHCI_BLOCK_SIZE);
1961 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301962
1963 /*
1964 * The tuning block is sent by the card to the host controller.
1965 * So we set the TRNS_READ bit in the Transfer Mode register.
1966 * This also takes care of setting DMA Enable and Multi Block
1967 * Select in the same register to 0.
1968 */
1969 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1970
1971 sdhci_send_command(host, &cmd);
1972
1973 host->cmd = NULL;
1974 host->mrq = NULL;
1975
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001976 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301977 /* Wait for Buffer Read Ready interrupt */
1978 wait_event_interruptible_timeout(host->buf_ready_int,
1979 (host->tuning_done == 1),
1980 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001981 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301982
1983 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301984 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301985 "Buffer Read Ready interrupt during tuning "
1986 "procedure, falling back to fixed sampling "
1987 "clock\n");
1988 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1989 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1990 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1991 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1992
1993 err = -EIO;
1994 goto out;
1995 }
1996
1997 host->tuning_done = 0;
1998
1999 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07002000
2001 /* eMMC spec does not require a delay between tuning cycles */
2002 if (opcode == MMC_SEND_TUNING_BLOCK)
2003 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302004 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2005
2006 /*
2007 * The Host Driver has exhausted the maximum number of loops allowed,
2008 * so use fixed sampling frequency.
2009 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002010 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302011 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2012 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002013 }
2014 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
2015 pr_info(DRIVER_NAME ": Tuning procedure"
2016 " failed, falling back to fixed sampling"
2017 " clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002018 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302019 }
2020
2021out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002022 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002023 /*
2024 * In case tuning fails, host controllers which support
2025 * re-tuning can try tuning again at a later time, when the
2026 * re-tuning timer expires. So for these controllers, we
2027 * return 0. Since there might be other controllers who do not
2028 * have this capability, we return error for them.
2029 */
2030 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302031 }
2032
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002033 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302034
Russell Kingb537f942014-04-25 12:56:01 +01002035 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2036 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002037out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002038 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002039 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302040
2041 return err;
2042}
2043
Kevin Liu52983382013-01-31 11:31:37 +08002044
2045static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302046{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302047 /* Host Controller v3.00 defines preset value registers */
2048 if (host->version < SDHCI_SPEC_300)
2049 return;
2050
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302051 /*
2052 * We only enable or disable Preset Value if they are not already
2053 * enabled or disabled respectively. Otherwise, we bail out.
2054 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002055 if (host->preset_enabled != enable) {
2056 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2057
2058 if (enable)
2059 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2060 else
2061 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2062
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302063 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002064
2065 if (enable)
2066 host->flags |= SDHCI_PV_ENABLED;
2067 else
2068 host->flags &= ~SDHCI_PV_ENABLED;
2069
2070 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302071 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002072}
2073
Haibo Chen348487c2014-12-09 17:04:05 +08002074static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2075 int err)
2076{
2077 struct sdhci_host *host = mmc_priv(mmc);
2078 struct mmc_data *data = mrq->data;
2079
2080 if (host->flags & SDHCI_REQ_USE_DMA) {
2081 if (data->host_cookie)
2082 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2083 data->flags & MMC_DATA_WRITE ?
2084 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2085 mrq->data->host_cookie = 0;
2086 }
2087}
2088
2089static int sdhci_pre_dma_transfer(struct sdhci_host *host,
2090 struct mmc_data *data,
2091 struct sdhci_host_next *next)
2092{
2093 int sg_count;
2094
2095 if (!next && data->host_cookie &&
2096 data->host_cookie != host->next_data.cookie) {
2097 pr_debug(DRIVER_NAME "[%s] invalid cookie: %d, next-cookie %d\n",
2098 __func__, data->host_cookie, host->next_data.cookie);
2099 data->host_cookie = 0;
2100 }
2101
2102 /* Check if next job is already prepared */
2103 if (next ||
2104 (!next && data->host_cookie != host->next_data.cookie)) {
2105 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg,
2106 data->sg_len,
2107 data->flags & MMC_DATA_WRITE ?
2108 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2109
2110 } else {
2111 sg_count = host->next_data.sg_count;
2112 host->next_data.sg_count = 0;
2113 }
2114
2115
2116 if (sg_count == 0)
2117 return -EINVAL;
2118
2119 if (next) {
2120 next->sg_count = sg_count;
2121 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
2122 } else
2123 host->sg_count = sg_count;
2124
2125 return sg_count;
2126}
2127
2128static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2129 bool is_first_req)
2130{
2131 struct sdhci_host *host = mmc_priv(mmc);
2132
2133 if (mrq->data->host_cookie) {
2134 mrq->data->host_cookie = 0;
2135 return;
2136 }
2137
2138 if (host->flags & SDHCI_REQ_USE_DMA)
2139 if (sdhci_pre_dma_transfer(host,
2140 mrq->data,
2141 &host->next_data) < 0)
2142 mrq->data->host_cookie = 0;
2143}
2144
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002145static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002146{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002147 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002148 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002149 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002150
Christian Daudt722e1282013-06-20 14:26:36 -07002151 /* First check if client has provided their own card event */
2152 if (host->ops->card_event)
2153 host->ops->card_event(host);
2154
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002155 present = sdhci_do_get_cd(host);
2156
Pierre Ossmand129bce2006-03-24 03:18:17 -08002157 spin_lock_irqsave(&host->lock, flags);
2158
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002159 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002160 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302161 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002162 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302163 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002164 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002165
Russell King03231f92014-04-25 12:57:12 +01002166 sdhci_do_reset(host, SDHCI_RESET_CMD);
2167 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002168
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002169 host->mrq->cmd->error = -ENOMEDIUM;
2170 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002171 }
2172
2173 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002174}
2175
2176static const struct mmc_host_ops sdhci_ops = {
2177 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002178 .post_req = sdhci_post_req,
2179 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002180 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002181 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002182 .get_ro = sdhci_get_ro,
2183 .hw_reset = sdhci_hw_reset,
2184 .enable_sdio_irq = sdhci_enable_sdio_irq,
2185 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002186 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002187 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002188 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002189 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002190};
2191
2192/*****************************************************************************\
2193 * *
2194 * Tasklets *
2195 * *
2196\*****************************************************************************/
2197
Pierre Ossmand129bce2006-03-24 03:18:17 -08002198static void sdhci_tasklet_finish(unsigned long param)
2199{
2200 struct sdhci_host *host;
2201 unsigned long flags;
2202 struct mmc_request *mrq;
2203
2204 host = (struct sdhci_host*)param;
2205
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002206 spin_lock_irqsave(&host->lock, flags);
2207
Chris Ball0c9c99a2011-04-27 17:35:31 -04002208 /*
2209 * If this tasklet gets rescheduled while running, it will
2210 * be run again afterwards but without any active request.
2211 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002212 if (!host->mrq) {
2213 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002214 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002215 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002216
2217 del_timer(&host->timer);
2218
2219 mrq = host->mrq;
2220
Pierre Ossmand129bce2006-03-24 03:18:17 -08002221 /*
2222 * The controller needs a reset of internal state machines
2223 * upon error conditions.
2224 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002225 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002226 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002227 (mrq->sbc && mrq->sbc->error) ||
2228 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2229 (mrq->data->stop && mrq->data->stop->error))) ||
2230 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002231
2232 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002233 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002234 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002235 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002236
2237 /* Spec says we should do both at the same time, but Ricoh
2238 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002239 sdhci_do_reset(host, SDHCI_RESET_CMD);
2240 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002241 }
2242
2243 host->mrq = NULL;
2244 host->cmd = NULL;
2245 host->data = NULL;
2246
Pierre Ossmanf9134312008-12-21 17:01:48 +01002247#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002248 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002249#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002250
Pierre Ossman5f25a662006-10-04 02:15:39 -07002251 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002252 spin_unlock_irqrestore(&host->lock, flags);
2253
2254 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002255 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002256}
2257
2258static void sdhci_timeout_timer(unsigned long data)
2259{
2260 struct sdhci_host *host;
2261 unsigned long flags;
2262
2263 host = (struct sdhci_host*)data;
2264
2265 spin_lock_irqsave(&host->lock, flags);
2266
2267 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302268 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002269 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002270 sdhci_dumpregs(host);
2271
2272 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002273 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002274 sdhci_finish_data(host);
2275 } else {
2276 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002277 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002278 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002279 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002280
2281 tasklet_schedule(&host->finish_tasklet);
2282 }
2283 }
2284
Pierre Ossman5f25a662006-10-04 02:15:39 -07002285 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002286 spin_unlock_irqrestore(&host->lock, flags);
2287}
2288
2289/*****************************************************************************\
2290 * *
2291 * Interrupt handling *
2292 * *
2293\*****************************************************************************/
2294
Adrian Hunter61541392014-09-24 10:27:27 +03002295static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002296{
2297 BUG_ON(intmask == 0);
2298
2299 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302300 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002301 "though no command operation was in progress.\n",
2302 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002303 sdhci_dumpregs(host);
2304 return;
2305 }
2306
Pierre Ossman43b58b32007-07-25 23:15:27 +02002307 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002308 host->cmd->error = -ETIMEDOUT;
2309 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2310 SDHCI_INT_INDEX))
2311 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002312
Pierre Ossmane8095172008-07-25 01:09:08 +02002313 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002314 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002315 return;
2316 }
2317
2318 /*
2319 * The host can send and interrupt when the busy state has
2320 * ended, allowing us to wait without wasting CPU cycles.
2321 * Unfortunately this is overloaded on the "data complete"
2322 * interrupt, so we need to take some care when handling
2323 * it.
2324 *
2325 * Note: The 1.0 specification is a bit ambiguous about this
2326 * feature so there might be some problems with older
2327 * controllers.
2328 */
2329 if (host->cmd->flags & MMC_RSP_BUSY) {
2330 if (host->cmd->data)
2331 DBG("Cannot wait for busy signal when also "
2332 "doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002333 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2334 && !host->busy_handle) {
2335 /* Mark that command complete before busy is ended */
2336 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002337 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002338 }
Ben Dooksf9454052009-02-20 20:33:08 +03002339
2340 /* The controller does not support the end-of-busy IRQ,
2341 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002342 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2343 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2344 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002345 }
2346
2347 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002348 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002349}
2350
George G. Davis0957c332010-02-18 12:32:12 -05002351#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002352static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002353{
2354 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002355 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002356
2357 sdhci_dumpregs(host);
2358
2359 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002360 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002361
Adrian Huntere57a5f62014-11-04 12:42:46 +02002362 if (host->flags & SDHCI_USE_64_BIT_DMA)
2363 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2364 name, desc, le32_to_cpu(dma_desc->addr_hi),
2365 le32_to_cpu(dma_desc->addr_lo),
2366 le16_to_cpu(dma_desc->len),
2367 le16_to_cpu(dma_desc->cmd));
2368 else
2369 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2370 name, desc, le32_to_cpu(dma_desc->addr_lo),
2371 le16_to_cpu(dma_desc->len),
2372 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002373
Adrian Hunter76fe3792014-11-04 12:42:42 +02002374 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002375
Adrian Hunter05452302014-11-04 12:42:45 +02002376 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002377 break;
2378 }
2379}
2380#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002381static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002382#endif
2383
Pierre Ossmand129bce2006-03-24 03:18:17 -08002384static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2385{
Girish K S069c9f12012-01-06 09:56:39 +05302386 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002387 BUG_ON(intmask == 0);
2388
Arindam Nathb513ea22011-05-05 12:19:04 +05302389 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2390 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302391 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2392 if (command == MMC_SEND_TUNING_BLOCK ||
2393 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302394 host->tuning_done = 1;
2395 wake_up(&host->buf_ready_int);
2396 return;
2397 }
2398 }
2399
Pierre Ossmand129bce2006-03-24 03:18:17 -08002400 if (!host->data) {
2401 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002402 * The "data complete" interrupt is also used to
2403 * indicate that a busy state has ended. See comment
2404 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002405 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002406 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002407 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2408 host->cmd->error = -ETIMEDOUT;
2409 tasklet_schedule(&host->finish_tasklet);
2410 return;
2411 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002412 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002413 /*
2414 * Some cards handle busy-end interrupt
2415 * before the command completed, so make
2416 * sure we do things in the proper order.
2417 */
2418 if (host->busy_handle)
2419 sdhci_finish_command(host);
2420 else
2421 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002422 return;
2423 }
2424 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002425
Girish K Sa3c76eb2011-10-11 11:44:09 +05302426 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002427 "though no data operation was in progress.\n",
2428 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002429 sdhci_dumpregs(host);
2430
2431 return;
2432 }
2433
2434 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002435 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002436 else if (intmask & SDHCI_INT_DATA_END_BIT)
2437 host->data->error = -EILSEQ;
2438 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2439 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2440 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002441 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002442 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302443 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002444 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002445 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002446 if (host->ops->adma_workaround)
2447 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002448 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002449
Pierre Ossman17b04292007-07-22 22:18:46 +02002450 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002451 sdhci_finish_data(host);
2452 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002453 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002454 sdhci_transfer_pio(host);
2455
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002456 /*
2457 * We currently don't do anything fancy with DMA
2458 * boundaries, but as we can't disable the feature
2459 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002460 *
2461 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2462 * should return a valid address to continue from, but as
2463 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002464 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002465 if (intmask & SDHCI_INT_DMA_END) {
2466 u32 dmastart, dmanow;
2467 dmastart = sg_dma_address(host->data->sg);
2468 dmanow = dmastart + host->data->bytes_xfered;
2469 /*
2470 * Force update to the next DMA block boundary.
2471 */
2472 dmanow = (dmanow &
2473 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2474 SDHCI_DEFAULT_BOUNDARY_SIZE;
2475 host->data->bytes_xfered = dmanow - dmastart;
2476 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2477 " next 0x%08x\n",
2478 mmc_hostname(host->mmc), dmastart,
2479 host->data->bytes_xfered, dmanow);
2480 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2481 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002482
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002483 if (intmask & SDHCI_INT_DATA_END) {
2484 if (host->cmd) {
2485 /*
2486 * Data managed to finish before the
2487 * command completed. Make sure we do
2488 * things in the proper order.
2489 */
2490 host->data_early = 1;
2491 } else {
2492 sdhci_finish_data(host);
2493 }
2494 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002495 }
2496}
2497
David Howells7d12e782006-10-05 14:55:46 +01002498static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002499{
Russell King781e9892014-04-25 12:55:46 +01002500 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002501 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002502 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002503 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002504
2505 spin_lock(&host->lock);
2506
Russell Kingbe138552014-04-25 12:55:56 +01002507 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002508 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002509 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002510 }
2511
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002512 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002513 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002514 result = IRQ_NONE;
2515 goto out;
2516 }
2517
Russell King41005002014-04-25 12:55:36 +01002518 do {
2519 /* Clear selected interrupts. */
2520 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2521 SDHCI_INT_BUS_POWER);
2522 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002523
Russell King41005002014-04-25 12:55:36 +01002524 DBG("*** %s got interrupt: 0x%08x\n",
2525 mmc_hostname(host->mmc), intmask);
2526
2527 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2528 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2529 SDHCI_CARD_PRESENT;
2530
2531 /*
2532 * There is a observation on i.mx esdhc. INSERT
2533 * bit will be immediately set again when it gets
2534 * cleared, if a card is inserted. We have to mask
2535 * the irq to prevent interrupt storm which will
2536 * freeze the system. And the REMOVE gets the
2537 * same situation.
2538 *
2539 * More testing are needed here to ensure it works
2540 * for other platforms though.
2541 */
Russell Kingb537f942014-04-25 12:56:01 +01002542 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2543 SDHCI_INT_CARD_REMOVE);
2544 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2545 SDHCI_INT_CARD_INSERT;
2546 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2547 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002548
2549 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2550 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002551
2552 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2553 SDHCI_INT_CARD_REMOVE);
2554 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002555 }
2556
2557 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002558 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2559 &intmask);
Russell King41005002014-04-25 12:55:36 +01002560
2561 if (intmask & SDHCI_INT_DATA_MASK)
2562 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2563
2564 if (intmask & SDHCI_INT_BUS_POWER)
2565 pr_err("%s: Card is consuming too much power!\n",
2566 mmc_hostname(host->mmc));
2567
Russell King781e9892014-04-25 12:55:46 +01002568 if (intmask & SDHCI_INT_CARD_INT) {
2569 sdhci_enable_sdio_irq_nolock(host, false);
2570 host->thread_isr |= SDHCI_INT_CARD_INT;
2571 result = IRQ_WAKE_THREAD;
2572 }
Russell King41005002014-04-25 12:55:36 +01002573
2574 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2575 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2576 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2577 SDHCI_INT_CARD_INT);
2578
2579 if (intmask) {
2580 unexpected |= intmask;
2581 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2582 }
2583
Russell King781e9892014-04-25 12:55:46 +01002584 if (result == IRQ_NONE)
2585 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002586
2587 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002588 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002589out:
2590 spin_unlock(&host->lock);
2591
Alexander Stein6379b232012-03-14 09:52:10 +01002592 if (unexpected) {
2593 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2594 mmc_hostname(host->mmc), unexpected);
2595 sdhci_dumpregs(host);
2596 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002597
Pierre Ossmand129bce2006-03-24 03:18:17 -08002598 return result;
2599}
2600
Russell King781e9892014-04-25 12:55:46 +01002601static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2602{
2603 struct sdhci_host *host = dev_id;
2604 unsigned long flags;
2605 u32 isr;
2606
2607 spin_lock_irqsave(&host->lock, flags);
2608 isr = host->thread_isr;
2609 host->thread_isr = 0;
2610 spin_unlock_irqrestore(&host->lock, flags);
2611
Russell King3560db82014-04-25 12:55:51 +01002612 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2613 sdhci_card_event(host->mmc);
2614 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2615 }
2616
Russell King781e9892014-04-25 12:55:46 +01002617 if (isr & SDHCI_INT_CARD_INT) {
2618 sdio_run_irqs(host->mmc);
2619
2620 spin_lock_irqsave(&host->lock, flags);
2621 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2622 sdhci_enable_sdio_irq_nolock(host, true);
2623 spin_unlock_irqrestore(&host->lock, flags);
2624 }
2625
2626 return isr ? IRQ_HANDLED : IRQ_NONE;
2627}
2628
Pierre Ossmand129bce2006-03-24 03:18:17 -08002629/*****************************************************************************\
2630 * *
2631 * Suspend/resume *
2632 * *
2633\*****************************************************************************/
2634
2635#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002636void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2637{
2638 u8 val;
2639 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2640 | SDHCI_WAKE_ON_INT;
2641
2642 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2643 val |= mask ;
2644 /* Avoid fake wake up */
2645 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2646 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2647 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2648}
2649EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2650
Fabio Estevam0b10f472014-08-30 14:53:13 -03002651static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002652{
2653 u8 val;
2654 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2655 | SDHCI_WAKE_ON_INT;
2656
2657 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2658 val &= ~mask;
2659 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2660}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002661
Manuel Lauss29495aa2011-11-03 11:09:45 +01002662int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002663{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002664 sdhci_disable_card_detection(host);
2665
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002666 mmc_retune_timer_stop(host->mmc);
2667 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302668
Kevin Liuad080d72013-01-05 17:21:33 +08002669 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002670 host->ier = 0;
2671 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2672 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002673 free_irq(host->irq, host);
2674 } else {
2675 sdhci_enable_irq_wakeups(host);
2676 enable_irq_wake(host->irq);
2677 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002678 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002679}
2680
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002681EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002682
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002683int sdhci_resume_host(struct sdhci_host *host)
2684{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002685 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002686
Richard Röjforsa13abc72009-09-22 16:45:30 -07002687 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002688 if (host->ops->enable_dma)
2689 host->ops->enable_dma(host);
2690 }
2691
Kevin Liuad080d72013-01-05 17:21:33 +08002692 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell King781e9892014-04-25 12:55:46 +01002693 ret = request_threaded_irq(host->irq, sdhci_irq,
2694 sdhci_thread_irq, IRQF_SHARED,
2695 mmc_hostname(host->mmc), host);
Kevin Liuad080d72013-01-05 17:21:33 +08002696 if (ret)
2697 return ret;
2698 } else {
2699 sdhci_disable_irq_wakeups(host);
2700 disable_irq_wake(host->irq);
2701 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002702
Adrian Hunter6308d292012-02-07 14:48:54 +02002703 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2704 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2705 /* Card keeps power but host controller does not */
2706 sdhci_init(host, 0);
2707 host->pwr = 0;
2708 host->clock = 0;
2709 sdhci_do_set_ios(host, &host->mmc->ios);
2710 } else {
2711 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2712 mmiowb();
2713 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002714
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002715 sdhci_enable_card_detection(host);
2716
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002717 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002718}
2719
2720EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002721
2722static int sdhci_runtime_pm_get(struct sdhci_host *host)
2723{
2724 return pm_runtime_get_sync(host->mmc->parent);
2725}
2726
2727static int sdhci_runtime_pm_put(struct sdhci_host *host)
2728{
2729 pm_runtime_mark_last_busy(host->mmc->parent);
2730 return pm_runtime_put_autosuspend(host->mmc->parent);
2731}
2732
Adrian Hunterf0710a52013-05-06 12:17:32 +03002733static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2734{
2735 if (host->runtime_suspended || host->bus_on)
2736 return;
2737 host->bus_on = true;
2738 pm_runtime_get_noresume(host->mmc->parent);
2739}
2740
2741static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2742{
2743 if (host->runtime_suspended || !host->bus_on)
2744 return;
2745 host->bus_on = false;
2746 pm_runtime_put_noidle(host->mmc->parent);
2747}
2748
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002749int sdhci_runtime_suspend_host(struct sdhci_host *host)
2750{
2751 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002752
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002753 mmc_retune_timer_stop(host->mmc);
2754 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002755
2756 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002757 host->ier &= SDHCI_INT_CARD_INT;
2758 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2759 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002760 spin_unlock_irqrestore(&host->lock, flags);
2761
Russell King781e9892014-04-25 12:55:46 +01002762 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002763
2764 spin_lock_irqsave(&host->lock, flags);
2765 host->runtime_suspended = true;
2766 spin_unlock_irqrestore(&host->lock, flags);
2767
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002768 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002769}
2770EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2771
2772int sdhci_runtime_resume_host(struct sdhci_host *host)
2773{
2774 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002775 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002776
2777 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2778 if (host->ops->enable_dma)
2779 host->ops->enable_dma(host);
2780 }
2781
2782 sdhci_init(host, 0);
2783
2784 /* Force clock and power re-program */
2785 host->pwr = 0;
2786 host->clock = 0;
Jisheng Zhang3396e732015-01-29 17:42:12 +08002787 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002788 sdhci_do_set_ios(host, &host->mmc->ios);
2789
Kevin Liu52983382013-01-31 11:31:37 +08002790 if ((host_flags & SDHCI_PV_ENABLED) &&
2791 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2792 spin_lock_irqsave(&host->lock, flags);
2793 sdhci_enable_preset_value(host, true);
2794 spin_unlock_irqrestore(&host->lock, flags);
2795 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002796
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002797 spin_lock_irqsave(&host->lock, flags);
2798
2799 host->runtime_suspended = false;
2800
2801 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002802 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002803 sdhci_enable_sdio_irq_nolock(host, true);
2804
2805 /* Enable Card Detection */
2806 sdhci_enable_card_detection(host);
2807
2808 spin_unlock_irqrestore(&host->lock, flags);
2809
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002810 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002811}
2812EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2813
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002814#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002815
Pierre Ossmand129bce2006-03-24 03:18:17 -08002816/*****************************************************************************\
2817 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002818 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002819 * *
2820\*****************************************************************************/
2821
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002822struct sdhci_host *sdhci_alloc_host(struct device *dev,
2823 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002824{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002825 struct mmc_host *mmc;
2826 struct sdhci_host *host;
2827
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002828 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002829
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002830 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002831 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002832 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002833
2834 host = mmc_priv(mmc);
2835 host->mmc = mmc;
2836
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002837 return host;
2838}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002839
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002840EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002841
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002842int sdhci_add_host(struct sdhci_host *host)
2843{
2844 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002845 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302846 u32 max_current_caps;
2847 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002848 unsigned int override_timeout_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002849 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002850
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002851 WARN_ON(host == NULL);
2852 if (host == NULL)
2853 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002854
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002855 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002856
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002857 if (debug_quirks)
2858 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002859 if (debug_quirks2)
2860 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002861
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002862 override_timeout_clk = host->timeout_clk;
2863
Russell King03231f92014-04-25 12:57:12 +01002864 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002865
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002866 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002867 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2868 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002869 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302870 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002871 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002872 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002873 }
2874
Arindam Nathf2119df2011-05-05 12:18:57 +05302875 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002876 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002877
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002878 if (host->version >= SDHCI_SPEC_300)
2879 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2880 host->caps1 :
2881 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302882
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002883 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002884 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302885 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002886 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002887 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002888 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002889
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002890 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002891 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002892 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002893 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002894 }
2895
Arindam Nathf2119df2011-05-05 12:18:57 +05302896 if ((host->version >= SDHCI_SPEC_200) &&
2897 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002898 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002899
2900 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2901 (host->flags & SDHCI_USE_ADMA)) {
2902 DBG("Disabling ADMA as it is marked broken\n");
2903 host->flags &= ~SDHCI_USE_ADMA;
2904 }
2905
Adrian Huntere57a5f62014-11-04 12:42:46 +02002906 /*
2907 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2908 * and *must* do 64-bit DMA. A driver has the opportunity to change
2909 * that during the first call to ->enable_dma(). Similarly
2910 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2911 * implement.
2912 */
2913 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT)
2914 host->flags |= SDHCI_USE_64_BIT_DMA;
2915
Richard Röjforsa13abc72009-09-22 16:45:30 -07002916 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002917 if (host->ops->enable_dma) {
2918 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07002919 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002920 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002921 host->flags &=
2922 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002923 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002924 }
2925 }
2926
Adrian Huntere57a5f62014-11-04 12:42:46 +02002927 /* SDMA does not support 64-bit DMA */
2928 if (host->flags & SDHCI_USE_64_BIT_DMA)
2929 host->flags &= ~SDHCI_USE_SDMA;
2930
Pierre Ossman2134a922008-06-28 18:28:51 +02002931 if (host->flags & SDHCI_USE_ADMA) {
2932 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002933 * The DMA descriptor table size is calculated as the maximum
2934 * number of segments times 2, to allow for an alignment
2935 * descriptor for each segment, plus 1 for a nop end descriptor,
2936 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002937 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02002938 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2939 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2940 SDHCI_ADMA2_64_DESC_SZ;
2941 host->align_buffer_sz = SDHCI_MAX_SEGS *
2942 SDHCI_ADMA2_64_ALIGN;
2943 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
2944 host->align_sz = SDHCI_ADMA2_64_ALIGN;
2945 host->align_mask = SDHCI_ADMA2_64_ALIGN - 1;
2946 } else {
2947 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2948 SDHCI_ADMA2_32_DESC_SZ;
2949 host->align_buffer_sz = SDHCI_MAX_SEGS *
2950 SDHCI_ADMA2_32_ALIGN;
2951 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
2952 host->align_sz = SDHCI_ADMA2_32_ALIGN;
2953 host->align_mask = SDHCI_ADMA2_32_ALIGN - 1;
2954 }
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002955 host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +02002956 host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002957 &host->adma_addr,
2958 GFP_KERNEL);
Adrian Hunter76fe3792014-11-04 12:42:42 +02002959 host->align_buffer = kmalloc(host->align_buffer_sz, GFP_KERNEL);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002960 if (!host->adma_table || !host->align_buffer) {
Adrian Hunter76fe3792014-11-04 12:42:42 +02002961 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002962 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02002963 kfree(host->align_buffer);
Joe Perches66061102014-09-12 14:56:56 -07002964 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002965 mmc_hostname(mmc));
2966 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002967 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01002968 host->align_buffer = NULL;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002969 } else if (host->adma_addr & host->align_mask) {
Joe Perches66061102014-09-12 14:56:56 -07002970 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2971 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002972 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002973 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002974 host->adma_table, host->adma_addr);
Russell Kingd1e49f72014-04-25 12:58:34 +01002975 kfree(host->align_buffer);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002976 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01002977 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02002978 }
2979 }
2980
Pierre Ossman76591502008-07-21 00:32:11 +02002981 /*
2982 * If we use DMA, then it's up to the caller to set the DMA
2983 * mask, but PIO does not need the hw shim so we set a new
2984 * mask here in that case.
2985 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002986 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002987 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07002988 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02002989 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002990
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002991 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302992 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002993 >> SDHCI_CLOCK_BASE_SHIFT;
2994 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302995 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002996 >> SDHCI_CLOCK_BASE_SHIFT;
2997
Pierre Ossmand129bce2006-03-24 03:18:17 -08002998 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002999 if (host->max_clk == 0 || host->quirks &
3000 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003001 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303002 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03003003 "frequency.\n", mmc_hostname(mmc));
3004 return -ENODEV;
3005 }
3006 host->max_clk = host->ops->get_max_clock(host);
3007 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003008
Haibo Chen348487c2014-12-09 17:04:05 +08003009 host->next_data.cookie = 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003010 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303011 * In case of Host Controller v3.00, find out whether clock
3012 * multiplier is supported.
3013 */
3014 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3015 SDHCI_CLOCK_MUL_SHIFT;
3016
3017 /*
3018 * In case the value in Clock Multiplier is 0, then programmable
3019 * clock mode is not supported, otherwise the actual clock
3020 * multiplier is one more than the value of Clock Multiplier
3021 * in the Capabilities Register.
3022 */
3023 if (host->clk_mul)
3024 host->clk_mul += 1;
3025
3026 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003027 * Set host parameters.
3028 */
3029 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303030 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003031 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003032 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303033 else if (host->version >= SDHCI_SPEC_300) {
3034 if (host->clk_mul) {
3035 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3036 mmc->f_max = host->max_clk * host->clk_mul;
3037 } else
3038 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3039 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003040 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003041
Aisheng Dong28aab052014-08-27 15:26:31 +08003042 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3043 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3044 SDHCI_TIMEOUT_CLK_SHIFT;
3045 if (host->timeout_clk == 0) {
3046 if (host->ops->get_timeout_clock) {
3047 host->timeout_clk =
3048 host->ops->get_timeout_clock(host);
3049 } else {
3050 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3051 mmc_hostname(mmc));
3052 return -ENODEV;
3053 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003054 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003055
Aisheng Dong28aab052014-08-27 15:26:31 +08003056 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3057 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003058
Aisheng Dong28aab052014-08-27 15:26:31 +08003059 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003060 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003061 mmc->max_busy_timeout /= host->timeout_clk;
3062 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003063
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003064 if (override_timeout_clk)
3065 host->timeout_clk = override_timeout_clk;
3066
Andrei Warkentine89d4562011-05-23 15:06:37 -05003067 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003068 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003069
3070 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3071 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003072
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003073 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003074 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003075 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003076 !(host->flags & SDHCI_USE_SDMA)) &&
3077 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003078 host->flags |= SDHCI_AUTO_CMD23;
3079 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3080 } else {
3081 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3082 }
3083
Philip Rakity15ec4462010-11-19 16:48:39 -05003084 /*
3085 * A controller may support 8-bit width, but the board itself
3086 * might not have the pins brought out. Boards that support
3087 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3088 * their platform code before calling sdhci_add_host(), and we
3089 * won't assume 8-bit width for hosts without that CAP.
3090 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003091 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003092 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003093
Jerry Huang63ef5d82012-10-25 13:47:19 +08003094 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3095 mmc->caps &= ~MMC_CAP_CMD23;
3096
Arindam Nathf2119df2011-05-05 12:18:57 +05303097 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003098 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003099
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003100 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Markus Mayer4e743f12014-07-03 13:27:42 -07003101 !(mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003102 mmc->caps |= MMC_CAP_NEEDS_POLL;
3103
Tim Kryger3a48edc2014-06-13 10:13:56 -07003104 /* If there are external regulators, get them */
3105 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3106 return -EPROBE_DEFER;
3107
Philip Rakity6231f3d2012-07-23 15:56:23 -07003108 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003109 if (!IS_ERR(mmc->supply.vqmmc)) {
3110 ret = regulator_enable(mmc->supply.vqmmc);
3111 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3112 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003113 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3114 SDHCI_SUPPORT_SDR50 |
3115 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003116 if (ret) {
3117 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3118 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003119 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003120 }
Kevin Liu8363c372012-11-17 17:55:51 -05003121 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003122
Daniel Drake6a661802012-11-25 13:01:19 -05003123 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3124 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3125 SDHCI_SUPPORT_DDR50);
3126
Al Cooper4188bba2012-03-16 15:54:17 -04003127 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3128 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3129 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303130 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3131
3132 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003133 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303134 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003135 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3136 * field can be promoted to support HS200.
3137 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003138 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003139 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003140 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303141 mmc->caps |= MMC_CAP_UHS_SDR50;
3142
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003143 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3144 (caps[1] & SDHCI_SUPPORT_HS400))
3145 mmc->caps2 |= MMC_CAP2_HS400;
3146
Adrian Hunter549c0b12014-11-06 15:19:05 +02003147 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3148 (IS_ERR(mmc->supply.vqmmc) ||
3149 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3150 1300000)))
3151 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3152
Micky Ching9107ebb2014-02-21 18:40:35 +08003153 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3154 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303155 mmc->caps |= MMC_CAP_UHS_DDR50;
3156
Girish K S069c9f12012-01-06 09:56:39 +05303157 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303158 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3159 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3160
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003161 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303162 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003163 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303164
Arindam Nathd6d50a12011-05-05 12:18:59 +05303165 /* Driver Type(s) (A, C, D) supported by the host */
3166 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3167 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3168 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3169 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3170 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3171 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3172
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303173 /* Initial value for re-tuning timer count */
3174 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3175 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3176
3177 /*
3178 * In case Re-tuning Timer is not disabled, the actual value of
3179 * re-tuning timer will be 2 ^ (n - 1).
3180 */
3181 if (host->tuning_count)
3182 host->tuning_count = 1 << (host->tuning_count - 1);
3183
3184 /* Re-tuning mode supported by the Host Controller */
3185 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3186 SDHCI_RETUNING_MODE_SHIFT;
3187
Takashi Iwai8f230f42010-12-08 10:04:30 +01003188 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003189
Arindam Nathf2119df2011-05-05 12:18:57 +05303190 /*
3191 * According to SD Host Controller spec v3.00, if the Host System
3192 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3193 * the value is meaningful only if Voltage Support in the Capabilities
3194 * register is set. The actual current value is 4 times the register
3195 * value.
3196 */
3197 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003198 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003199 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003200 if (curr > 0) {
3201
3202 /* convert to SDHCI_MAX_CURRENT format */
3203 curr = curr/1000; /* convert to mA */
3204 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3205
3206 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3207 max_current_caps =
3208 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3209 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3210 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3211 }
3212 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303213
3214 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003215 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303216
Aaron Lu55c46652012-07-04 13:31:48 +08003217 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303218 SDHCI_MAX_CURRENT_330_MASK) >>
3219 SDHCI_MAX_CURRENT_330_SHIFT) *
3220 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303221 }
3222 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003223 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303224
Aaron Lu55c46652012-07-04 13:31:48 +08003225 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303226 SDHCI_MAX_CURRENT_300_MASK) >>
3227 SDHCI_MAX_CURRENT_300_SHIFT) *
3228 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303229 }
3230 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003231 ocr_avail |= MMC_VDD_165_195;
3232
Aaron Lu55c46652012-07-04 13:31:48 +08003233 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303234 SDHCI_MAX_CURRENT_180_MASK) >>
3235 SDHCI_MAX_CURRENT_180_SHIFT) *
3236 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303237 }
3238
Tim Kryger52221612014-06-25 00:25:34 -07003239 /* If OCR set by external regulators, use it instead */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003240 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003241 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003242
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003243 if (host->ocr_mask)
Tim Kryger3a48edc2014-06-13 10:13:56 -07003244 ocr_avail &= host->ocr_mask;
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003245
Takashi Iwai8f230f42010-12-08 10:04:30 +01003246 mmc->ocr_avail = ocr_avail;
3247 mmc->ocr_avail_sdio = ocr_avail;
3248 if (host->ocr_avail_sdio)
3249 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3250 mmc->ocr_avail_sd = ocr_avail;
3251 if (host->ocr_avail_sd)
3252 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3253 else /* normal SD controllers don't support 1.8V */
3254 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3255 mmc->ocr_avail_mmc = ocr_avail;
3256 if (host->ocr_avail_mmc)
3257 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003258
3259 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303260 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003261 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003262 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003263 }
3264
Pierre Ossmand129bce2006-03-24 03:18:17 -08003265 spin_lock_init(&host->lock);
3266
3267 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003268 * Maximum number of segments. Depends on if the hardware
3269 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003270 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003271 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003272 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003273 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003274 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003275 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003276 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003277
3278 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003279 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3280 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3281 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003282 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003283 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003284
3285 /*
3286 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003287 * of bytes. When doing hardware scatter/gather, each entry cannot
3288 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003289 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003290 if (host->flags & SDHCI_USE_ADMA) {
3291 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3292 mmc->max_seg_size = 65535;
3293 else
3294 mmc->max_seg_size = 65536;
3295 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003296 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003297 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003298
3299 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003300 * Maximum block size. This varies from controller to controller and
3301 * is specified in the capabilities register.
3302 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003303 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3304 mmc->max_blk_size = 2;
3305 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303306 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003307 SDHCI_MAX_BLOCK_SHIFT;
3308 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003309 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3310 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003311 mmc->max_blk_size = 0;
3312 }
3313 }
3314
3315 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003316
3317 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003318 * Maximum block count.
3319 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003320 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003321
3322 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003323 * Init tasklets.
3324 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003325 tasklet_init(&host->finish_tasklet,
3326 sdhci_tasklet_finish, (unsigned long)host);
3327
Al Viroe4cad1b2006-10-10 22:47:07 +01003328 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003329
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003330 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303331
Shawn Guo2af502c2013-07-05 14:38:55 +08003332 sdhci_init(host, 0);
3333
Russell King781e9892014-04-25 12:55:46 +01003334 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3335 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003336 if (ret) {
3337 pr_err("%s: Failed to request IRQ %d: %d\n",
3338 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003339 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003340 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003341
Pierre Ossmand129bce2006-03-24 03:18:17 -08003342#ifdef CONFIG_MMC_DEBUG
3343 sdhci_dumpregs(host);
3344#endif
3345
Pierre Ossmanf9134312008-12-21 17:01:48 +01003346#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003347 snprintf(host->led_name, sizeof(host->led_name),
3348 "%s::", mmc_hostname(mmc));
3349 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003350 host->led.brightness = LED_OFF;
3351 host->led.default_trigger = mmc_hostname(mmc);
3352 host->led.brightness_set = sdhci_led_control;
3353
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003354 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003355 if (ret) {
3356 pr_err("%s: Failed to register LED device: %d\n",
3357 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003358 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003359 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003360#endif
3361
Pierre Ossman5f25a662006-10-04 02:15:39 -07003362 mmiowb();
3363
Pierre Ossmand129bce2006-03-24 03:18:17 -08003364 mmc_add_host(mmc);
3365
Girish K Sa3c76eb2011-10-11 11:44:09 +05303366 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003367 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003368 (host->flags & SDHCI_USE_ADMA) ?
3369 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003370 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003371
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003372 sdhci_enable_card_detection(host);
3373
Pierre Ossmand129bce2006-03-24 03:18:17 -08003374 return 0;
3375
Pierre Ossmanf9134312008-12-21 17:01:48 +01003376#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003377reset:
Russell King03231f92014-04-25 12:57:12 +01003378 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003379 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3380 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003381 free_irq(host->irq, host);
3382#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003383untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003384 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003385
3386 return ret;
3387}
3388
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003389EXPORT_SYMBOL_GPL(sdhci_add_host);
3390
Pierre Ossman1e728592008-04-16 19:13:13 +02003391void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003392{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003393 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003394 unsigned long flags;
3395
3396 if (dead) {
3397 spin_lock_irqsave(&host->lock, flags);
3398
3399 host->flags |= SDHCI_DEVICE_DEAD;
3400
3401 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303402 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003403 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003404
3405 host->mrq->cmd->error = -ENOMEDIUM;
3406 tasklet_schedule(&host->finish_tasklet);
3407 }
3408
3409 spin_unlock_irqrestore(&host->lock, flags);
3410 }
3411
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003412 sdhci_disable_card_detection(host);
3413
Markus Mayer4e743f12014-07-03 13:27:42 -07003414 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003415
Pierre Ossmanf9134312008-12-21 17:01:48 +01003416#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003417 led_classdev_unregister(&host->led);
3418#endif
3419
Pierre Ossman1e728592008-04-16 19:13:13 +02003420 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003421 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003422
Russell Kingb537f942014-04-25 12:56:01 +01003423 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3424 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003425 free_irq(host->irq, host);
3426
3427 del_timer_sync(&host->timer);
3428
Pierre Ossmand129bce2006-03-24 03:18:17 -08003429 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003430
Tim Kryger3a48edc2014-06-13 10:13:56 -07003431 if (!IS_ERR(mmc->supply.vqmmc))
3432 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003433
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003434 if (host->adma_table)
Adrian Hunter76fe3792014-11-04 12:42:42 +02003435 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003436 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003437 kfree(host->align_buffer);
3438
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003439 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003440 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003441}
3442
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003443EXPORT_SYMBOL_GPL(sdhci_remove_host);
3444
3445void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003446{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003447 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003448}
3449
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003450EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003451
3452/*****************************************************************************\
3453 * *
3454 * Driver init/exit *
3455 * *
3456\*****************************************************************************/
3457
3458static int __init sdhci_drv_init(void)
3459{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303460 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003461 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303462 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003463
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003464 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003465}
3466
3467static void __exit sdhci_drv_exit(void)
3468{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003469}
3470
3471module_init(sdhci_drv_init);
3472module_exit(sdhci_drv_exit);
3473
Pierre Ossmandf673b22006-06-30 02:22:31 -07003474module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003475module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003476
Pierre Ossman32710e82009-04-08 20:14:54 +02003477MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003478MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003479MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003480
Pierre Ossmandf673b22006-06-30 02:22:31 -07003481MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003482MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");