blob: 320c294b9d73c482677dbfdc89b869309131c19d [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
Arindam Nathb513ea22011-05-05 12:19:04 +053041#define MAX_TUNING_LOOP 40
42
Pierre Ossmandf673b22006-06-30 02:22:31 -070043static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030044static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070045
Pierre Ossmand129bce2006-03-24 03:18:17 -080046static void sdhci_finish_data(struct sdhci_host *);
47
Kevin Liu52983382013-01-31 11:31:37 +080048static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080049
50static void sdhci_dumpregs(struct sdhci_host *host)
51{
Chuanxiao Donga7c53672016-06-22 14:40:01 +030052 pr_err(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
53 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080054
Chuanxiao Donga7c53672016-06-22 14:40:01 +030055 pr_err(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
56 sdhci_readl(host, SDHCI_DMA_ADDRESS),
57 sdhci_readw(host, SDHCI_HOST_VERSION));
58 pr_err(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
59 sdhci_readw(host, SDHCI_BLOCK_SIZE),
60 sdhci_readw(host, SDHCI_BLOCK_COUNT));
61 pr_err(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
62 sdhci_readl(host, SDHCI_ARGUMENT),
63 sdhci_readw(host, SDHCI_TRANSFER_MODE));
64 pr_err(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
65 sdhci_readl(host, SDHCI_PRESENT_STATE),
66 sdhci_readb(host, SDHCI_HOST_CONTROL));
67 pr_err(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
68 sdhci_readb(host, SDHCI_POWER_CONTROL),
69 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
70 pr_err(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
71 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
72 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
73 pr_err(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
74 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
75 sdhci_readl(host, SDHCI_INT_STATUS));
76 pr_err(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
77 sdhci_readl(host, SDHCI_INT_ENABLE),
78 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
79 pr_err(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
80 sdhci_readw(host, SDHCI_ACMD12_ERR),
81 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
82 pr_err(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
83 sdhci_readl(host, SDHCI_CAPABILITIES),
84 sdhci_readl(host, SDHCI_CAPABILITIES_1));
85 pr_err(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
86 sdhci_readw(host, SDHCI_COMMAND),
87 sdhci_readl(host, SDHCI_MAX_CURRENT));
88 pr_err(DRIVER_NAME ": Host ctl2: 0x%08x\n",
89 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -080090
Adrian Huntere57a5f62014-11-04 12:42:46 +020091 if (host->flags & SDHCI_USE_ADMA) {
92 if (host->flags & SDHCI_USE_64_BIT_DMA)
Chuanxiao Donga7c53672016-06-22 14:40:01 +030093 pr_err(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
94 readl(host->ioaddr + SDHCI_ADMA_ERROR),
95 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
96 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
Adrian Huntere57a5f62014-11-04 12:42:46 +020097 else
Chuanxiao Donga7c53672016-06-22 14:40:01 +030098 pr_err(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
99 readl(host->ioaddr + SDHCI_ADMA_ERROR),
100 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
Adrian Huntere57a5f62014-11-04 12:42:46 +0200101 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100102
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300103 pr_err(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800104}
105
106/*****************************************************************************\
107 * *
108 * Low level functions *
109 * *
110\*****************************************************************************/
111
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300112static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
113{
Russell King5b4f1f62014-04-25 12:57:02 +0100114 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300115
Adrian Hunterc79396c2011-12-27 15:48:42 +0200116 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Jaehoon Chung860951c2016-06-21 10:13:26 +0900117 !mmc_card_is_removable(host->mmc))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300118 return;
119
Russell King5b4f1f62014-04-25 12:57:02 +0100120 if (enable) {
121 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
122 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800123
Russell King5b4f1f62014-04-25 12:57:02 +0100124 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
125 SDHCI_INT_CARD_INSERT;
126 } else {
127 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
128 }
Russell Kingb537f942014-04-25 12:56:01 +0100129
130 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
131 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300132}
133
134static void sdhci_enable_card_detection(struct sdhci_host *host)
135{
136 sdhci_set_card_detection(host, true);
137}
138
139static void sdhci_disable_card_detection(struct sdhci_host *host)
140{
141 sdhci_set_card_detection(host, false);
142}
143
Ulf Hansson02d0b682016-04-11 15:32:41 +0200144static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
145{
146 if (host->bus_on)
147 return;
148 host->bus_on = true;
149 pm_runtime_get_noresume(host->mmc->parent);
150}
151
152static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
153{
154 if (!host->bus_on)
155 return;
156 host->bus_on = false;
157 pm_runtime_put_noidle(host->mmc->parent);
158}
159
Russell King03231f92014-04-25 12:57:12 +0100160void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800161{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700162 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800163
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300164 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800165
Adrian Hunterf0710a52013-05-06 12:17:32 +0300166 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800167 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300168 /* Reset-all turns off SD Bus Power */
169 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
170 sdhci_runtime_pm_bus_off(host);
171 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800172
Pierre Ossmane16514d82006-06-30 02:22:24 -0700173 /* Wait max 100 ms */
174 timeout = 100;
175
176 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300177 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700178 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530179 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700180 mmc_hostname(host->mmc), (int)mask);
181 sdhci_dumpregs(host);
182 return;
183 }
184 timeout--;
185 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800186 }
Russell King03231f92014-04-25 12:57:12 +0100187}
188EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300189
Russell King03231f92014-04-25 12:57:12 +0100190static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
191{
192 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Adrian Hunterd3940f22016-06-29 16:24:14 +0300193 struct mmc_host *mmc = host->mmc;
194
195 if (!mmc->ops->get_cd(mmc))
Russell King03231f92014-04-25 12:57:12 +0100196 return;
197 }
198
199 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800200
Russell Kingda91a8f2014-04-25 13:00:12 +0100201 if (mask & SDHCI_RESET_ALL) {
202 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
203 if (host->ops->enable_dma)
204 host->ops->enable_dma(host);
205 }
206
207 /* Resetting the controller clears many */
208 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800209 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800210}
211
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800212static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800213{
Adrian Hunterd3940f22016-06-29 16:24:14 +0300214 struct mmc_host *mmc = host->mmc;
215
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800216 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100217 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800218 else
Russell King03231f92014-04-25 12:57:12 +0100219 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800220
Russell Kingb537f942014-04-25 12:56:01 +0100221 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
222 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
223 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
224 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
225 SDHCI_INT_RESPONSE;
226
227 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
228 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800229
230 if (soft) {
231 /* force clock reconfiguration */
232 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +0300233 mmc->ops->set_ios(mmc, &mmc->ios);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800234 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300235}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800236
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300237static void sdhci_reinit(struct sdhci_host *host)
238{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800239 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300240 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800241}
242
Adrian Hunter061d17a2016-04-12 14:25:09 +0300243static void __sdhci_led_activate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800244{
245 u8 ctrl;
246
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300247 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800248 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300249 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800250}
251
Adrian Hunter061d17a2016-04-12 14:25:09 +0300252static void __sdhci_led_deactivate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800253{
254 u8 ctrl;
255
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300256 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800257 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300258 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800259}
260
Masahiro Yamada4f782302016-04-14 13:19:39 +0900261#if IS_REACHABLE(CONFIG_LEDS_CLASS)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100262static void sdhci_led_control(struct led_classdev *led,
Adrian Hunter061d17a2016-04-12 14:25:09 +0300263 enum led_brightness brightness)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100264{
265 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
266 unsigned long flags;
267
268 spin_lock_irqsave(&host->lock, flags);
269
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300270 if (host->runtime_suspended)
271 goto out;
272
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100273 if (brightness == LED_OFF)
Adrian Hunter061d17a2016-04-12 14:25:09 +0300274 __sdhci_led_deactivate(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100275 else
Adrian Hunter061d17a2016-04-12 14:25:09 +0300276 __sdhci_led_activate(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300277out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100278 spin_unlock_irqrestore(&host->lock, flags);
279}
Adrian Hunter061d17a2016-04-12 14:25:09 +0300280
281static int sdhci_led_register(struct sdhci_host *host)
282{
283 struct mmc_host *mmc = host->mmc;
284
285 snprintf(host->led_name, sizeof(host->led_name),
286 "%s::", mmc_hostname(mmc));
287
288 host->led.name = host->led_name;
289 host->led.brightness = LED_OFF;
290 host->led.default_trigger = mmc_hostname(mmc);
291 host->led.brightness_set = sdhci_led_control;
292
293 return led_classdev_register(mmc_dev(mmc), &host->led);
294}
295
296static void sdhci_led_unregister(struct sdhci_host *host)
297{
298 led_classdev_unregister(&host->led);
299}
300
301static inline void sdhci_led_activate(struct sdhci_host *host)
302{
303}
304
305static inline void sdhci_led_deactivate(struct sdhci_host *host)
306{
307}
308
309#else
310
311static inline int sdhci_led_register(struct sdhci_host *host)
312{
313 return 0;
314}
315
316static inline void sdhci_led_unregister(struct sdhci_host *host)
317{
318}
319
320static inline void sdhci_led_activate(struct sdhci_host *host)
321{
322 __sdhci_led_activate(host);
323}
324
325static inline void sdhci_led_deactivate(struct sdhci_host *host)
326{
327 __sdhci_led_deactivate(host);
328}
329
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100330#endif
331
Pierre Ossmand129bce2006-03-24 03:18:17 -0800332/*****************************************************************************\
333 * *
334 * Core functions *
335 * *
336\*****************************************************************************/
337
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100338static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800339{
Pierre Ossman76591502008-07-21 00:32:11 +0200340 unsigned long flags;
341 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700342 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200343 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800344
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100345 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800346
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100347 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200348 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800349
Pierre Ossman76591502008-07-21 00:32:11 +0200350 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800351
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100352 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300353 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800354
Pierre Ossman76591502008-07-21 00:32:11 +0200355 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800356
Pierre Ossman76591502008-07-21 00:32:11 +0200357 blksize -= len;
358 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200359
Pierre Ossman76591502008-07-21 00:32:11 +0200360 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800361
Pierre Ossman76591502008-07-21 00:32:11 +0200362 while (len) {
363 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300364 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200365 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800366 }
Pierre Ossman76591502008-07-21 00:32:11 +0200367
368 *buf = scratch & 0xFF;
369
370 buf++;
371 scratch >>= 8;
372 chunk--;
373 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800374 }
375 }
Pierre Ossman76591502008-07-21 00:32:11 +0200376
377 sg_miter_stop(&host->sg_miter);
378
379 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100380}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800381
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100382static void sdhci_write_block_pio(struct sdhci_host *host)
383{
Pierre Ossman76591502008-07-21 00:32:11 +0200384 unsigned long flags;
385 size_t blksize, len, chunk;
386 u32 scratch;
387 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100388
389 DBG("PIO writing\n");
390
391 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200392 chunk = 0;
393 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100394
Pierre Ossman76591502008-07-21 00:32:11 +0200395 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100396
397 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300398 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100399
Pierre Ossman76591502008-07-21 00:32:11 +0200400 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200401
Pierre Ossman76591502008-07-21 00:32:11 +0200402 blksize -= len;
403 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100404
Pierre Ossman76591502008-07-21 00:32:11 +0200405 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100406
Pierre Ossman76591502008-07-21 00:32:11 +0200407 while (len) {
408 scratch |= (u32)*buf << (chunk * 8);
409
410 buf++;
411 chunk++;
412 len--;
413
414 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300415 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200416 chunk = 0;
417 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100418 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100419 }
420 }
Pierre Ossman76591502008-07-21 00:32:11 +0200421
422 sg_miter_stop(&host->sg_miter);
423
424 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100425}
426
427static void sdhci_transfer_pio(struct sdhci_host *host)
428{
429 u32 mask;
430
Pierre Ossman76591502008-07-21 00:32:11 +0200431 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100432 return;
433
434 if (host->data->flags & MMC_DATA_READ)
435 mask = SDHCI_DATA_AVAILABLE;
436 else
437 mask = SDHCI_SPACE_AVAILABLE;
438
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200439 /*
440 * Some controllers (JMicron JMB38x) mess up the buffer bits
441 * for transfers < 4 bytes. As long as it is just one block,
442 * we can ignore the bits.
443 */
444 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
445 (host->data->blocks == 1))
446 mask = ~0;
447
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300448 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300449 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
450 udelay(100);
451
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100452 if (host->data->flags & MMC_DATA_READ)
453 sdhci_read_block_pio(host);
454 else
455 sdhci_write_block_pio(host);
456
Pierre Ossman76591502008-07-21 00:32:11 +0200457 host->blocks--;
458 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100459 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100460 }
461
462 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800463}
464
Russell King48857d92016-01-26 13:40:16 +0000465static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000466 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000467{
468 int sg_count;
469
Russell King94538e52016-01-26 13:40:37 +0000470 /*
471 * If the data buffers are already mapped, return the previous
472 * dma_map_sg() result.
473 */
474 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000475 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000476
477 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
478 data->flags & MMC_DATA_WRITE ?
479 DMA_TO_DEVICE : DMA_FROM_DEVICE);
480
481 if (sg_count == 0)
482 return -ENOSPC;
483
484 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000485 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000486
487 return sg_count;
488}
489
Pierre Ossman2134a922008-06-28 18:28:51 +0200490static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
491{
492 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800493 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200494}
495
496static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
497{
Cong Wang482fce92011-11-27 13:27:00 +0800498 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200499 local_irq_restore(*flags);
500}
501
Adrian Huntere57a5f62014-11-04 12:42:46 +0200502static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
503 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800504{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200505 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800506
Adrian Huntere57a5f62014-11-04 12:42:46 +0200507 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200508 dma_desc->cmd = cpu_to_le16(cmd);
509 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200510 dma_desc->addr_lo = cpu_to_le32((u32)addr);
511
512 if (host->flags & SDHCI_USE_64_BIT_DMA)
513 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800514}
515
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200516static void sdhci_adma_mark_end(void *desc)
517{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200518 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200519
Adrian Huntere57a5f62014-11-04 12:42:46 +0200520 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200521 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200522}
523
Russell King60c64762016-01-26 13:40:22 +0000524static void sdhci_adma_table_pre(struct sdhci_host *host,
525 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200526{
Pierre Ossman2134a922008-06-28 18:28:51 +0200527 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200528 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000529 dma_addr_t addr, align_addr;
530 void *desc, *align;
531 char *buffer;
532 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200533
534 /*
535 * The spec does not specify endianness of descriptor table.
536 * We currently guess that it is LE.
537 */
538
Russell King60c64762016-01-26 13:40:22 +0000539 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200540
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200541 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200542 align = host->align_buffer;
543
544 align_addr = host->align_addr;
545
546 for_each_sg(data->sg, sg, host->sg_count, i) {
547 addr = sg_dma_address(sg);
548 len = sg_dma_len(sg);
549
550 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000551 * The SDHCI specification states that ADMA addresses must
552 * be 32-bit aligned. If they aren't, then we use a bounce
553 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200554 * alignment.
555 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200556 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
557 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200558 if (offset) {
559 if (data->flags & MMC_DATA_WRITE) {
560 buffer = sdhci_kmap_atomic(sg, &flags);
561 memcpy(align, buffer, offset);
562 sdhci_kunmap_atomic(buffer, &flags);
563 }
564
Ben Dooks118cd172010-03-05 13:43:26 -0800565 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200566 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200567 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200568
569 BUG_ON(offset > 65536);
570
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200571 align += SDHCI_ADMA2_ALIGN;
572 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200573
Adrian Hunter76fe3792014-11-04 12:42:42 +0200574 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200575
576 addr += offset;
577 len -= offset;
578 }
579
Pierre Ossman2134a922008-06-28 18:28:51 +0200580 BUG_ON(len > 65536);
581
Adrian Hunter347ea322015-11-26 14:00:48 +0200582 if (len) {
583 /* tran, valid */
584 sdhci_adma_write_desc(host, desc, addr, len,
585 ADMA2_TRAN_VALID);
586 desc += host->desc_sz;
587 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200588
589 /*
590 * If this triggers then we have a calculation bug
591 * somewhere. :/
592 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200593 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200594 }
595
Thomas Abraham70764a92010-05-26 14:42:04 -0700596 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000597 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200598 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200599 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200600 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700601 }
602 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000603 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200604 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700605 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200606}
607
608static void sdhci_adma_table_post(struct sdhci_host *host,
609 struct mmc_data *data)
610{
Pierre Ossman2134a922008-06-28 18:28:51 +0200611 struct scatterlist *sg;
612 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200613 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200614 char *buffer;
615 unsigned long flags;
616
Russell King47fa9612016-01-26 13:40:06 +0000617 if (data->flags & MMC_DATA_READ) {
618 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100619
Russell King47fa9612016-01-26 13:40:06 +0000620 /* Do a quick scan of the SG list for any unaligned mappings */
621 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200622 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000623 has_unaligned = true;
624 break;
625 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200626
Russell King47fa9612016-01-26 13:40:06 +0000627 if (has_unaligned) {
628 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000629 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200630
Russell King47fa9612016-01-26 13:40:06 +0000631 align = host->align_buffer;
632
633 for_each_sg(data->sg, sg, host->sg_count, i) {
634 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
635 size = SDHCI_ADMA2_ALIGN -
636 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
637
638 buffer = sdhci_kmap_atomic(sg, &flags);
639 memcpy(buffer, align, size);
640 sdhci_kunmap_atomic(buffer, &flags);
641
642 align += SDHCI_ADMA2_ALIGN;
643 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200644 }
645 }
646 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200647}
648
Andrei Warkentina3c77782011-04-11 16:13:42 -0500649static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800650{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700651 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500652 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700653 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800654
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200655 /*
656 * If the host controller provides us with an incorrect timeout
657 * value, just skip the check and use 0xE. The hardware may take
658 * longer to time out, but that's much better than having a too-short
659 * timeout value.
660 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200661 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200662 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200663
Andrei Warkentina3c77782011-04-11 16:13:42 -0500664 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100665 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500666 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800667
Andrei Warkentina3c77782011-04-11 16:13:42 -0500668 /* timeout in us */
669 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100670 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300671 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000672 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000673 if (host->clock && data->timeout_clks) {
674 unsigned long long val;
675
676 /*
677 * data->timeout_clks is in units of clock cycles.
678 * host->clock is in Hz. target_timeout is in us.
679 * Hence, us = 1000000 * cycles / Hz. Round up.
680 */
681 val = 1000000 * data->timeout_clks;
682 if (do_div(val, host->clock))
683 target_timeout++;
684 target_timeout += val;
685 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300686 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700687
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700688 /*
689 * Figure out needed cycles.
690 * We do this in steps in order to fit inside a 32 bit int.
691 * The first step is the minimum timeout, which will have a
692 * minimum resolution of 6 bits:
693 * (1) 2^13*1000 > 2^22,
694 * (2) host->timeout_clk < 2^16
695 * =>
696 * (1) / (2) > 2^6
697 */
698 count = 0;
699 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
700 while (current_timeout < target_timeout) {
701 count++;
702 current_timeout <<= 1;
703 if (count >= 0xF)
704 break;
705 }
706
707 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400708 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
709 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700710 count = 0xE;
711 }
712
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200713 return count;
714}
715
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300716static void sdhci_set_transfer_irqs(struct sdhci_host *host)
717{
718 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
719 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
720
721 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100722 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300723 else
Russell Kingb537f942014-04-25 12:56:01 +0100724 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
725
726 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
727 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300728}
729
Aisheng Dongb45e6682014-08-27 15:26:29 +0800730static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200731{
732 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800733
734 if (host->ops->set_timeout) {
735 host->ops->set_timeout(host, cmd);
736 } else {
737 count = sdhci_calc_timeout(host, cmd);
738 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
739 }
740}
741
742static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
743{
Pierre Ossman2134a922008-06-28 18:28:51 +0200744 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500745 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200746
Aisheng Dongb45e6682014-08-27 15:26:29 +0800747 if (data || (cmd->flags & MMC_RSP_BUSY))
748 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500749
750 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200751 return;
752
Adrian Hunter43dea092016-06-29 16:24:26 +0300753 WARN_ON(host->data);
754
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200755 /* Sanity checks */
756 BUG_ON(data->blksz * data->blocks > 524288);
757 BUG_ON(data->blksz > host->mmc->max_blk_size);
758 BUG_ON(data->blocks > 65535);
759
760 host->data = data;
761 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400762 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200763
Russell Kingfce14422016-01-26 13:41:20 +0000764 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200765 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000766 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000767 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200768
Russell Kingfce14422016-01-26 13:41:20 +0000769 host->flags |= SDHCI_REQ_USE_DMA;
770
771 /*
772 * FIXME: This doesn't account for merging when mapping the
773 * scatterlist.
774 *
775 * The assumption here being that alignment and lengths are
776 * the same after DMA mapping to device address space.
777 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000778 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000779 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200780 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000781 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000782 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000783 /*
784 * As we use up to 3 byte chunks to work
785 * around alignment problems, we need to
786 * check the offset as well.
787 */
788 offset_mask = 3;
789 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200790 } else {
791 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000792 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000793 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
794 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200795 }
796
Russell Kingdf953922016-01-26 13:41:14 +0000797 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200798 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000799 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100800 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000801 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200802 host->flags &= ~SDHCI_REQ_USE_DMA;
803 break;
804 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000805 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100806 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200807 host->flags &= ~SDHCI_REQ_USE_DMA;
808 break;
809 }
810 }
811 }
812 }
813
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200814 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000815 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200816
Russell King60c64762016-01-26 13:40:22 +0000817 if (sg_cnt <= 0) {
818 /*
819 * This only happens when someone fed
820 * us an invalid request.
821 */
822 WARN_ON(1);
823 host->flags &= ~SDHCI_REQ_USE_DMA;
824 } else if (host->flags & SDHCI_USE_ADMA) {
825 sdhci_adma_table_pre(host, data, sg_cnt);
826
827 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
828 if (host->flags & SDHCI_USE_64_BIT_DMA)
829 sdhci_writel(host,
830 (u64)host->adma_addr >> 32,
831 SDHCI_ADMA_ADDRESS_HI);
832 } else {
833 WARN_ON(sg_cnt != 1);
834 sdhci_writel(host, sg_dma_address(data->sg),
835 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200836 }
837 }
838
Pierre Ossman2134a922008-06-28 18:28:51 +0200839 /*
840 * Always adjust the DMA selection as some controllers
841 * (e.g. JMicron) can't do PIO properly when the selection
842 * is ADMA.
843 */
844 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300845 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200846 ctrl &= ~SDHCI_CTRL_DMA_MASK;
847 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200848 (host->flags & SDHCI_USE_ADMA)) {
849 if (host->flags & SDHCI_USE_64_BIT_DMA)
850 ctrl |= SDHCI_CTRL_ADMA64;
851 else
852 ctrl |= SDHCI_CTRL_ADMA32;
853 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200854 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200855 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300856 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100857 }
858
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200859 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200860 int flags;
861
862 flags = SG_MITER_ATOMIC;
863 if (host->data->flags & MMC_DATA_READ)
864 flags |= SG_MITER_TO_SG;
865 else
866 flags |= SG_MITER_FROM_SG;
867 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200868 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800869 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700870
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300871 sdhci_set_transfer_irqs(host);
872
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400873 /* Set the DMA boundary value and block size */
874 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
875 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300876 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700877}
878
879static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500880 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700881{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800882 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500883 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700884
Dong Aisheng2b558c12013-10-30 22:09:48 +0800885 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800886 if (host->quirks2 &
887 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
888 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
889 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800890 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800891 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
892 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800893 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800894 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700895 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800896 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700897
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200898 WARN_ON(!host->data);
899
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800900 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
901 mode = SDHCI_TRNS_BLK_CNT_EN;
902
Andrei Warkentine89d4562011-05-23 15:06:37 -0500903 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800904 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500905 /*
906 * If we are sending CMD23, CMD12 never gets sent
907 * on successful completion (so no Auto-CMD12).
908 */
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300909 if (!cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800910 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500911 mode |= SDHCI_TRNS_AUTO_CMD12;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300912 else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500913 mode |= SDHCI_TRNS_AUTO_CMD23;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300914 sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500915 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700916 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500917
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700918 if (data->flags & MMC_DATA_READ)
919 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100920 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700921 mode |= SDHCI_TRNS_DMA;
922
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300923 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800924}
925
Adrian Hunter0cc563c2016-06-29 16:24:28 +0300926static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
927{
928 return (!(host->flags & SDHCI_DEVICE_DEAD) &&
929 ((mrq->cmd && mrq->cmd->error) ||
930 (mrq->sbc && mrq->sbc->error) ||
931 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
932 (mrq->data->stop && mrq->data->stop->error))) ||
933 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
934}
935
Adrian Huntera6d3bdd2016-06-29 16:24:27 +0300936static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
937{
Adrian Huntered1563d2016-06-29 16:24:29 +0300938 if (sdhci_needs_reset(host, mrq))
939 host->pending_reset = true;
940
Adrian Huntera6d3bdd2016-06-29 16:24:27 +0300941 tasklet_schedule(&host->finish_tasklet);
942}
943
Pierre Ossmand129bce2006-03-24 03:18:17 -0800944static void sdhci_finish_data(struct sdhci_host *host)
945{
946 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800947
Pierre Ossmand129bce2006-03-24 03:18:17 -0800948 data = host->data;
949 host->data = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +0300950 host->data_cmd = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800951
Russell Kingadd89132016-01-26 13:40:42 +0000952 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
953 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
954 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800955
956 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200957 * The specification states that the block count register must
958 * be updated, but it does not specify at what point in the
959 * data flow. That makes the register entirely useless to read
960 * back so we have to assume that nothing made it to the card
961 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800962 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200963 if (data->error)
964 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800965 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200966 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800967
Andrei Warkentine89d4562011-05-23 15:06:37 -0500968 /*
969 * Need to send CMD12 if -
970 * a) open-ended multiblock transfer (no CMD23)
971 * b) error in multiblock transfer
972 */
973 if (data->stop &&
974 (data->error ||
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300975 !data->mrq->sbc)) {
Andrei Warkentine89d4562011-05-23 15:06:37 -0500976
Pierre Ossmand129bce2006-03-24 03:18:17 -0800977 /*
978 * The controller needs a reset of internal state machines
979 * upon error conditions.
980 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200981 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100982 sdhci_do_reset(host, SDHCI_RESET_CMD);
983 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800984 }
985
986 sdhci_send_command(host, data->stop);
Adrian Huntera6d3bdd2016-06-29 16:24:27 +0300987 } else {
988 sdhci_finish_mrq(host, data->mrq);
989 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800990}
991
Dong Aishengc0e551292013-09-13 19:11:31 +0800992void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800993{
994 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700995 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700996 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997
998 WARN_ON(host->cmd);
999
Russell King96776202016-01-26 13:39:34 +00001000 /* Initially, a command has no error */
1001 cmd->error = 0;
1002
Pierre Ossmand129bce2006-03-24 03:18:17 -08001003 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001004 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001005
1006 mask = SDHCI_CMD_INHIBIT;
1007 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1008 mask |= SDHCI_DATA_INHIBIT;
1009
1010 /* We shouldn't wait for data inihibit for stop commands, even
1011 though they might use busy signaling */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001012 if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001013 mask &= ~SDHCI_DATA_INHIBIT;
1014
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001015 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001016 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001017 pr_err("%s: Controller never released inhibit bit(s).\n",
1018 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001019 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001020 cmd->error = -EIO;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001021 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001022 return;
1023 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001024 timeout--;
1025 mdelay(1);
1026 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001027
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001028 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001029 if (!cmd->data && cmd->busy_timeout > 9000)
1030 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001031 else
1032 timeout += 10 * HZ;
1033 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001034
1035 host->cmd = cmd;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001036 if (cmd->data || cmd->flags & MMC_RSP_BUSY) {
1037 WARN_ON(host->data_cmd);
1038 host->data_cmd = cmd;
1039 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001040
Andrei Warkentina3c77782011-04-11 16:13:42 -05001041 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001042
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001043 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001044
Andrei Warkentine89d4562011-05-23 15:06:37 -05001045 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001046
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301048 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001049 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001050 cmd->error = -EINVAL;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001051 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001052 return;
1053 }
1054
1055 if (!(cmd->flags & MMC_RSP_PRESENT))
1056 flags = SDHCI_CMD_RESP_NONE;
1057 else if (cmd->flags & MMC_RSP_136)
1058 flags = SDHCI_CMD_RESP_LONG;
1059 else if (cmd->flags & MMC_RSP_BUSY)
1060 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1061 else
1062 flags = SDHCI_CMD_RESP_SHORT;
1063
1064 if (cmd->flags & MMC_RSP_CRC)
1065 flags |= SDHCI_CMD_CRC;
1066 if (cmd->flags & MMC_RSP_OPCODE)
1067 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301068
1069 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301070 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1071 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072 flags |= SDHCI_CMD_DATA;
1073
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001074 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001075}
Dong Aishengc0e551292013-09-13 19:11:31 +08001076EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001077
1078static void sdhci_finish_command(struct sdhci_host *host)
1079{
Adrian Huntere0a56402016-06-29 16:24:22 +03001080 struct mmc_command *cmd = host->cmd;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001081 int i;
1082
Adrian Huntere0a56402016-06-29 16:24:22 +03001083 host->cmd = NULL;
1084
1085 if (cmd->flags & MMC_RSP_PRESENT) {
1086 if (cmd->flags & MMC_RSP_136) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001087 /* CRC is stripped so we need to do some shifting. */
1088 for (i = 0;i < 4;i++) {
Adrian Huntere0a56402016-06-29 16:24:22 +03001089 cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001090 SDHCI_RESPONSE + (3-i)*4) << 8;
1091 if (i != 3)
Adrian Huntere0a56402016-06-29 16:24:22 +03001092 cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001093 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001094 SDHCI_RESPONSE + (3-i)*4-1);
1095 }
1096 } else {
Adrian Huntere0a56402016-06-29 16:24:22 +03001097 cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001098 }
1099 }
1100
Adrian Hunter6bde8682016-06-29 16:24:20 +03001101 /*
1102 * The host can send and interrupt when the busy state has
1103 * ended, allowing us to wait without wasting CPU cycles.
1104 * The busy signal uses DAT0 so this is similar to waiting
1105 * for data to complete.
1106 *
1107 * Note: The 1.0 specification is a bit ambiguous about this
1108 * feature so there might be some problems with older
1109 * controllers.
1110 */
Adrian Huntere0a56402016-06-29 16:24:22 +03001111 if (cmd->flags & MMC_RSP_BUSY) {
1112 if (cmd->data) {
Adrian Hunter6bde8682016-06-29 16:24:20 +03001113 DBG("Cannot wait for busy signal when also doing a data transfer");
1114 } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
Adrian Hunterea968022016-06-29 16:24:24 +03001115 cmd == host->data_cmd) {
1116 /* Command complete before busy is ended */
Adrian Hunter6bde8682016-06-29 16:24:20 +03001117 return;
1118 }
1119 }
1120
Andrei Warkentine89d4562011-05-23 15:06:37 -05001121 /* Finished CMD23, now send actual command. */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001122 if (cmd == cmd->mrq->sbc) {
1123 sdhci_send_command(host, cmd->mrq->cmd);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001124 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001125
Andrei Warkentine89d4562011-05-23 15:06:37 -05001126 /* Processed actual command. */
1127 if (host->data && host->data_early)
1128 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001129
Adrian Huntere0a56402016-06-29 16:24:22 +03001130 if (!cmd->data)
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001131 sdhci_finish_mrq(host, cmd->mrq);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001132 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001133}
1134
Kevin Liu52983382013-01-31 11:31:37 +08001135static u16 sdhci_get_preset_value(struct sdhci_host *host)
1136{
Russell Kingd975f122014-04-25 12:59:31 +01001137 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001138
Russell Kingd975f122014-04-25 12:59:31 +01001139 switch (host->timing) {
1140 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001141 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1142 break;
Russell Kingd975f122014-04-25 12:59:31 +01001143 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001144 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1145 break;
Russell Kingd975f122014-04-25 12:59:31 +01001146 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001147 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1148 break;
Russell Kingd975f122014-04-25 12:59:31 +01001149 case MMC_TIMING_UHS_SDR104:
1150 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001151 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1152 break;
Russell Kingd975f122014-04-25 12:59:31 +01001153 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001154 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001155 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1156 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001157 case MMC_TIMING_MMC_HS400:
1158 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1159 break;
Kevin Liu52983382013-01-31 11:31:37 +08001160 default:
1161 pr_warn("%s: Invalid UHS-I mode selected\n",
1162 mmc_hostname(host->mmc));
1163 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1164 break;
1165 }
1166 return preset;
1167}
1168
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001169u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1170 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001171{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301172 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001173 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301174 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001175 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001176
Zhangfei Gao85105c52010-08-06 07:10:01 +08001177 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001178 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001179 u16 pre_val;
1180
1181 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1182 pre_val = sdhci_get_preset_value(host);
1183 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1184 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1185 if (host->clk_mul &&
1186 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1187 clk = SDHCI_PROG_CLOCK_MODE;
1188 real_div = div + 1;
1189 clk_mul = host->clk_mul;
1190 } else {
1191 real_div = max_t(int, 1, div << 1);
1192 }
1193 goto clock_set;
1194 }
1195
Arindam Nathc3ed3872011-05-05 12:19:06 +05301196 /*
1197 * Check if the Host Controller supports Programmable Clock
1198 * Mode.
1199 */
1200 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001201 for (div = 1; div <= 1024; div++) {
1202 if ((host->max_clk * host->clk_mul / div)
1203 <= clock)
1204 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001205 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001206 if ((host->max_clk * host->clk_mul / div) <= clock) {
1207 /*
1208 * Set Programmable Clock Mode in the Clock
1209 * Control register.
1210 */
1211 clk = SDHCI_PROG_CLOCK_MODE;
1212 real_div = div;
1213 clk_mul = host->clk_mul;
1214 div--;
1215 } else {
1216 /*
1217 * Divisor can be too small to reach clock
1218 * speed requirement. Then use the base clock.
1219 */
1220 switch_base_clk = true;
1221 }
1222 }
1223
1224 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301225 /* Version 3.00 divisors must be a multiple of 2. */
1226 if (host->max_clk <= clock)
1227 div = 1;
1228 else {
1229 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1230 div += 2) {
1231 if ((host->max_clk / div) <= clock)
1232 break;
1233 }
1234 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001235 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301236 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301237 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1238 && !div && host->max_clk <= 25000000)
1239 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001240 }
1241 } else {
1242 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001243 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001244 if ((host->max_clk / div) <= clock)
1245 break;
1246 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001247 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301248 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001249 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001250
Kevin Liu52983382013-01-31 11:31:37 +08001251clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001252 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001253 *actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301254 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001255 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1256 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001257
1258 return clk;
1259}
1260EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1261
1262void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1263{
1264 u16 clk;
1265 unsigned long timeout;
1266
1267 host->mmc->actual_clock = 0;
1268
1269 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001270
1271 if (clock == 0)
1272 return;
1273
1274 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1275
Pierre Ossmand129bce2006-03-24 03:18:17 -08001276 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001277 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001278
Chris Ball27f6cb12009-09-22 16:45:31 -07001279 /* Wait max 20 ms */
1280 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001281 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001282 & SDHCI_CLOCK_INT_STABLE)) {
1283 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001284 pr_err("%s: Internal clock never stabilised.\n",
1285 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001286 sdhci_dumpregs(host);
1287 return;
1288 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001289 timeout--;
1290 mdelay(1);
1291 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001292
1293 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001294 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001295}
Russell King17710592014-04-25 12:58:55 +01001296EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001297
Adrian Hunter1dceb042016-03-29 12:45:43 +03001298static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1299 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001300{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001301 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001302
1303 spin_unlock_irq(&host->lock);
1304 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1305 spin_lock_irq(&host->lock);
1306
1307 if (mode != MMC_POWER_OFF)
1308 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1309 else
1310 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1311}
1312
1313void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1314 unsigned short vdd)
1315{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001316 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001317
Russell King24fbb3c2014-04-25 13:00:06 +01001318 if (mode != MMC_POWER_OFF) {
1319 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001320 case MMC_VDD_165_195:
1321 pwr = SDHCI_POWER_180;
1322 break;
1323 case MMC_VDD_29_30:
1324 case MMC_VDD_30_31:
1325 pwr = SDHCI_POWER_300;
1326 break;
1327 case MMC_VDD_32_33:
1328 case MMC_VDD_33_34:
1329 pwr = SDHCI_POWER_330;
1330 break;
1331 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001332 WARN(1, "%s: Invalid vdd %#x\n",
1333 mmc_hostname(host->mmc), vdd);
1334 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001335 }
1336 }
1337
1338 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001339 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001340
Pierre Ossmanae628902009-05-03 20:45:03 +02001341 host->pwr = pwr;
1342
1343 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001344 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001345 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1346 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001347 } else {
1348 /*
1349 * Spec says that we should clear the power reg before setting
1350 * a new value. Some controllers don't seem to like this though.
1351 */
1352 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1353 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001354
Russell Kinge921a8b2014-04-25 13:00:01 +01001355 /*
1356 * At least the Marvell CaFe chip gets confused if we set the
1357 * voltage and set turn on power at the same time, so set the
1358 * voltage first.
1359 */
1360 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1361 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001362
Russell Kinge921a8b2014-04-25 13:00:01 +01001363 pwr |= SDHCI_POWER_ON;
1364
Pierre Ossmanae628902009-05-03 20:45:03 +02001365 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1366
Russell Kinge921a8b2014-04-25 13:00:01 +01001367 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1368 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001369
Russell Kinge921a8b2014-04-25 13:00:01 +01001370 /*
1371 * Some controllers need an extra 10ms delay of 10ms before
1372 * they can apply clock after applying power
1373 */
1374 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1375 mdelay(10);
1376 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001377}
1378EXPORT_SYMBOL_GPL(sdhci_set_power);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001379
Adrian Hunter1dceb042016-03-29 12:45:43 +03001380static void __sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1381 unsigned short vdd)
1382{
1383 struct mmc_host *mmc = host->mmc;
1384
1385 if (host->ops->set_power)
1386 host->ops->set_power(host, mode, vdd);
1387 else if (!IS_ERR(mmc->supply.vmmc))
1388 sdhci_set_power_reg(host, mode, vdd);
1389 else
1390 sdhci_set_power(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001391}
1392
Pierre Ossmand129bce2006-03-24 03:18:17 -08001393/*****************************************************************************\
1394 * *
1395 * MMC callbacks *
1396 * *
1397\*****************************************************************************/
1398
1399static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1400{
1401 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001402 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001403 unsigned long flags;
1404
1405 host = mmc_priv(mmc);
1406
Scott Branden04e079cf2015-03-10 11:35:10 -07001407 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001408 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001409
Pierre Ossmand129bce2006-03-24 03:18:17 -08001410 spin_lock_irqsave(&host->lock, flags);
1411
1412 WARN_ON(host->mrq != NULL);
1413
Adrian Hunter061d17a2016-04-12 14:25:09 +03001414 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001415
1416 /*
1417 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1418 * requests if Auto-CMD12 is enabled.
1419 */
1420 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001421 if (mrq->stop) {
1422 mrq->data->stop = NULL;
1423 mrq->stop = NULL;
1424 }
1425 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001426
1427 host->mrq = mrq;
1428
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001429 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001430 mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001431 sdhci_finish_mrq(host, mrq);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301432 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001433 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001434 sdhci_send_command(host, mrq->sbc);
1435 else
1436 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301437 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001438
Pierre Ossman5f25a662006-10-04 02:15:39 -07001439 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001440 spin_unlock_irqrestore(&host->lock, flags);
1441}
1442
Russell King2317f562014-04-25 12:57:07 +01001443void sdhci_set_bus_width(struct sdhci_host *host, int width)
1444{
1445 u8 ctrl;
1446
1447 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1448 if (width == MMC_BUS_WIDTH_8) {
1449 ctrl &= ~SDHCI_CTRL_4BITBUS;
1450 if (host->version >= SDHCI_SPEC_300)
1451 ctrl |= SDHCI_CTRL_8BITBUS;
1452 } else {
1453 if (host->version >= SDHCI_SPEC_300)
1454 ctrl &= ~SDHCI_CTRL_8BITBUS;
1455 if (width == MMC_BUS_WIDTH_4)
1456 ctrl |= SDHCI_CTRL_4BITBUS;
1457 else
1458 ctrl &= ~SDHCI_CTRL_4BITBUS;
1459 }
1460 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1461}
1462EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1463
Russell King96d7b782014-04-25 12:59:26 +01001464void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1465{
1466 u16 ctrl_2;
1467
1468 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1469 /* Select Bus Speed Mode for host */
1470 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1471 if ((timing == MMC_TIMING_MMC_HS200) ||
1472 (timing == MMC_TIMING_UHS_SDR104))
1473 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1474 else if (timing == MMC_TIMING_UHS_SDR12)
1475 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1476 else if (timing == MMC_TIMING_UHS_SDR25)
1477 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1478 else if (timing == MMC_TIMING_UHS_SDR50)
1479 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1480 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1481 (timing == MMC_TIMING_MMC_DDR52))
1482 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001483 else if (timing == MMC_TIMING_MMC_HS400)
1484 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001485 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1486}
1487EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1488
Dong Aishengded97e02016-04-16 01:29:25 +08001489static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001490{
Dong Aishengded97e02016-04-16 01:29:25 +08001491 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001492 unsigned long flags;
1493 u8 ctrl;
1494
Pierre Ossmand129bce2006-03-24 03:18:17 -08001495 spin_lock_irqsave(&host->lock, flags);
1496
Adrian Hunterceb61432011-12-27 15:48:41 +02001497 if (host->flags & SDHCI_DEVICE_DEAD) {
1498 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001499 if (!IS_ERR(mmc->supply.vmmc) &&
1500 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001501 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001502 return;
1503 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001504
Pierre Ossmand129bce2006-03-24 03:18:17 -08001505 /*
1506 * Reset the chip on each power off.
1507 * Should clear out any weird states.
1508 */
1509 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001510 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001511 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001512 }
1513
Kevin Liu52983382013-01-31 11:31:37 +08001514 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001515 (ios->power_mode == MMC_POWER_UP) &&
1516 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001517 sdhci_enable_preset_value(host, false);
1518
Russell King373073e2014-04-25 12:58:45 +01001519 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001520 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001521 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001522
1523 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1524 host->clock) {
1525 host->timeout_clk = host->mmc->actual_clock ?
1526 host->mmc->actual_clock / 1000 :
1527 host->clock / 1000;
1528 host->mmc->max_busy_timeout =
1529 host->ops->get_max_timeout_count ?
1530 host->ops->get_max_timeout_count(host) :
1531 1 << 27;
1532 host->mmc->max_busy_timeout /= host->timeout_clk;
1533 }
Russell King373073e2014-04-25 12:58:45 +01001534 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001535
Adrian Hunter1dceb042016-03-29 12:45:43 +03001536 __sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001537
Philip Rakity643a81f2010-09-23 08:24:32 -07001538 if (host->ops->platform_send_init_74_clocks)
1539 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1540
Russell King2317f562014-04-25 12:57:07 +01001541 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001542
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001543 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001544
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001545 if ((ios->timing == MMC_TIMING_SD_HS ||
1546 ios->timing == MMC_TIMING_MMC_HS)
1547 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001548 ctrl |= SDHCI_CTRL_HISPD;
1549 else
1550 ctrl &= ~SDHCI_CTRL_HISPD;
1551
Arindam Nathd6d50a12011-05-05 12:18:59 +05301552 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301553 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301554
1555 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001556 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1557 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001558 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301559 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301560 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1561 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001562 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301563 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301564
Russell Kingda91a8f2014-04-25 13:00:12 +01001565 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301566 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301567 /*
1568 * We only need to set Driver Strength if the
1569 * preset value enable is not set.
1570 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001571 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301572 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1573 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1574 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001575 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1576 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301577 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1578 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001579 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1580 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1581 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001582 pr_warn("%s: invalid driver type, default to driver type B\n",
1583 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001584 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1585 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301586
1587 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301588 } else {
1589 /*
1590 * According to SDHC Spec v3.00, if the Preset Value
1591 * Enable in the Host Control 2 register is set, we
1592 * need to reset SD Clock Enable before changing High
1593 * Speed Enable to avoid generating clock gliches.
1594 */
Arindam Nath758535c2011-05-05 12:19:00 +05301595
1596 /* Reset SD Clock Enable */
1597 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1598 clk &= ~SDHCI_CLOCK_CARD_EN;
1599 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1600
1601 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1602
1603 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001604 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301605 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301606
Arindam Nath49c468f2011-05-05 12:19:01 +05301607 /* Reset SD Clock Enable */
1608 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1609 clk &= ~SDHCI_CLOCK_CARD_EN;
1610 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1611
Russell King96d7b782014-04-25 12:59:26 +01001612 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001613 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301614
Kevin Liu52983382013-01-31 11:31:37 +08001615 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1616 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1617 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1618 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1619 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001620 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1621 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001622 u16 preset;
1623
1624 sdhci_enable_preset_value(host, true);
1625 preset = sdhci_get_preset_value(host);
1626 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1627 >> SDHCI_PRESET_DRV_SHIFT;
1628 }
1629
Arindam Nath49c468f2011-05-05 12:19:01 +05301630 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001631 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301632 } else
1633 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301634
Leandro Dorileob8352262007-07-25 23:47:04 +02001635 /*
1636 * Some (ENE) controllers go apeshit on some ios operation,
1637 * signalling timeout and CRC errors even on CMD0. Resetting
1638 * it on each ios seems to solve the problem.
1639 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301640 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001641 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001642
Pierre Ossman5f25a662006-10-04 02:15:39 -07001643 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001644 spin_unlock_irqrestore(&host->lock, flags);
1645}
1646
Dong Aishengded97e02016-04-16 01:29:25 +08001647static int sdhci_get_cd(struct mmc_host *mmc)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001648{
1649 struct sdhci_host *host = mmc_priv(mmc);
Dong Aishengded97e02016-04-16 01:29:25 +08001650 int gpio_cd = mmc_gpio_get_cd(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08001651
1652 if (host->flags & SDHCI_DEVICE_DEAD)
1653 return 0;
1654
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001655 /* If nonremovable, assume that the card is always present. */
Jaehoon Chung860951c2016-06-21 10:13:26 +09001656 if (!mmc_card_is_removable(host->mmc))
Kevin Liu94144a42013-02-28 17:35:53 +08001657 return 1;
1658
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001659 /*
1660 * Try slot gpio detect, if defined it take precedence
1661 * over build in controller functionality
1662 */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001663 if (gpio_cd >= 0)
Kevin Liu94144a42013-02-28 17:35:53 +08001664 return !!gpio_cd;
1665
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001666 /* If polling, assume that the card is always present. */
1667 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1668 return 1;
1669
Kevin Liu94144a42013-02-28 17:35:53 +08001670 /* Host native card detect */
1671 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1672}
1673
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001674static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001675{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001676 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001677 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001678
Pierre Ossmand129bce2006-03-24 03:18:17 -08001679 spin_lock_irqsave(&host->lock, flags);
1680
Pierre Ossman1e728592008-04-16 19:13:13 +02001681 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001682 is_readonly = 0;
1683 else if (host->ops->get_ro)
1684 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001685 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001686 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1687 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001688
1689 spin_unlock_irqrestore(&host->lock, flags);
1690
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001691 /* This quirk needs to be replaced by a callback-function later */
1692 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1693 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001694}
1695
Takashi Iwai82b0e232011-04-21 20:26:38 +02001696#define SAMPLE_COUNT 5
1697
Dong Aishengded97e02016-04-16 01:29:25 +08001698static int sdhci_get_ro(struct mmc_host *mmc)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001699{
Dong Aishengded97e02016-04-16 01:29:25 +08001700 struct sdhci_host *host = mmc_priv(mmc);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001701 int i, ro_count;
1702
Takashi Iwai82b0e232011-04-21 20:26:38 +02001703 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001704 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001705
1706 ro_count = 0;
1707 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001708 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001709 if (++ro_count > SAMPLE_COUNT / 2)
1710 return 1;
1711 }
1712 msleep(30);
1713 }
1714 return 0;
1715}
1716
Adrian Hunter20758b62011-08-29 16:42:12 +03001717static void sdhci_hw_reset(struct mmc_host *mmc)
1718{
1719 struct sdhci_host *host = mmc_priv(mmc);
1720
1721 if (host->ops && host->ops->hw_reset)
1722 host->ops->hw_reset(host);
1723}
1724
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001725static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1726{
Russell Kingbe138552014-04-25 12:55:56 +01001727 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001728 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001729 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001730 else
Russell Kingb537f942014-04-25 12:56:01 +01001731 host->ier &= ~SDHCI_INT_CARD_INT;
1732
1733 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1734 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001735 mmiowb();
1736 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001737}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001738
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001739static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1740{
1741 struct sdhci_host *host = mmc_priv(mmc);
1742 unsigned long flags;
1743
1744 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001745 if (enable)
1746 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1747 else
1748 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1749
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001750 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001751 spin_unlock_irqrestore(&host->lock, flags);
1752}
1753
Dong Aishengded97e02016-04-16 01:29:25 +08001754static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1755 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001756{
Dong Aishengded97e02016-04-16 01:29:25 +08001757 struct sdhci_host *host = mmc_priv(mmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07001758 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001759 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001760
1761 /*
1762 * Signal Voltage Switching is only applicable for Host Controllers
1763 * v3.00 and above.
1764 */
1765 if (host->version < SDHCI_SPEC_300)
1766 return 0;
1767
Philip Rakity6231f3d2012-07-23 15:56:23 -07001768 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001769
Fabio Estevam21f59982013-02-14 10:35:03 -02001770 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001771 case MMC_SIGNAL_VOLTAGE_330:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001772 if (!(host->flags & SDHCI_SIGNALING_330))
1773 return -EINVAL;
Kevin Liu20b92a32012-12-17 19:29:26 +08001774 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1775 ctrl &= ~SDHCI_CTRL_VDD_180;
1776 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1777
Tim Kryger3a48edc2014-06-13 10:13:56 -07001778 if (!IS_ERR(mmc->supply.vqmmc)) {
1779 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1780 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001781 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001782 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1783 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001784 return -EIO;
1785 }
1786 }
1787 /* Wait for 5ms */
1788 usleep_range(5000, 5500);
1789
1790 /* 3.3V regulator output should be stable within 5 ms */
1791 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1792 if (!(ctrl & SDHCI_CTRL_VDD_180))
1793 return 0;
1794
Joe Perches66061102014-09-12 14:56:56 -07001795 pr_warn("%s: 3.3V regulator output did not became stable\n",
1796 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001797
1798 return -EAGAIN;
1799 case MMC_SIGNAL_VOLTAGE_180:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001800 if (!(host->flags & SDHCI_SIGNALING_180))
1801 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001802 if (!IS_ERR(mmc->supply.vqmmc)) {
1803 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001804 1700000, 1950000);
1805 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001806 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1807 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001808 return -EIO;
1809 }
1810 }
1811
1812 /*
1813 * Enable 1.8V Signal Enable in the Host Control2
1814 * register
1815 */
1816 ctrl |= SDHCI_CTRL_VDD_180;
1817 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1818
Vincent Yang9d967a62015-01-20 16:05:15 +08001819 /* Some controller need to do more when switching */
1820 if (host->ops->voltage_switch)
1821 host->ops->voltage_switch(host);
1822
Kevin Liu20b92a32012-12-17 19:29:26 +08001823 /* 1.8V regulator output should be stable within 5 ms */
1824 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1825 if (ctrl & SDHCI_CTRL_VDD_180)
1826 return 0;
1827
Joe Perches66061102014-09-12 14:56:56 -07001828 pr_warn("%s: 1.8V regulator output did not became stable\n",
1829 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001830
1831 return -EAGAIN;
1832 case MMC_SIGNAL_VOLTAGE_120:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001833 if (!(host->flags & SDHCI_SIGNALING_120))
1834 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001835 if (!IS_ERR(mmc->supply.vqmmc)) {
1836 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1837 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001838 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001839 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1840 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001841 return -EIO;
1842 }
1843 }
1844 return 0;
1845 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301846 /* No signal voltage switch required */
1847 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001848 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301849}
1850
Kevin Liu20b92a32012-12-17 19:29:26 +08001851static int sdhci_card_busy(struct mmc_host *mmc)
1852{
1853 struct sdhci_host *host = mmc_priv(mmc);
1854 u32 present_state;
1855
Adrian Huntere613cc42016-06-23 14:00:58 +03001856 /* Check whether DAT[0] is 0 */
Kevin Liu20b92a32012-12-17 19:29:26 +08001857 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08001858
Adrian Huntere613cc42016-06-23 14:00:58 +03001859 return !(present_state & SDHCI_DATA_0_LVL_MASK);
Kevin Liu20b92a32012-12-17 19:29:26 +08001860}
1861
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001862static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1863{
1864 struct sdhci_host *host = mmc_priv(mmc);
1865 unsigned long flags;
1866
1867 spin_lock_irqsave(&host->lock, flags);
1868 host->flags |= SDHCI_HS400_TUNING;
1869 spin_unlock_irqrestore(&host->lock, flags);
1870
1871 return 0;
1872}
1873
Girish K S069c9f12012-01-06 09:56:39 +05301874static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301875{
Russell King4b6f37d2014-04-25 12:59:36 +01001876 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301877 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301878 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301879 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001880 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001881 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001882 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301883
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001884 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301885
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001886 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1887 host->flags &= ~SDHCI_HS400_TUNING;
1888
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001889 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1890 tuning_count = host->tuning_count;
1891
Arindam Nathb513ea22011-05-05 12:19:04 +05301892 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001893 * The Host Controller needs tuning in case of SDR104 and DDR50
1894 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1895 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301896 * If the Host Controller supports the HS200 mode then the
1897 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301898 */
Russell King4b6f37d2014-04-25 12:59:36 +01001899 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001900 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001901 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001902 err = -EINVAL;
1903 goto out_unlock;
1904
Russell King4b6f37d2014-04-25 12:59:36 +01001905 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001906 /*
1907 * Periodic re-tuning for HS400 is not expected to be needed, so
1908 * disable it here.
1909 */
1910 if (hs400_tuning)
1911 tuning_count = 0;
1912 break;
1913
Russell King4b6f37d2014-04-25 12:59:36 +01001914 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001915 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001916 break;
Girish K S069c9f12012-01-06 09:56:39 +05301917
Russell King4b6f37d2014-04-25 12:59:36 +01001918 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03001919 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01001920 break;
1921 /* FALLTHROUGH */
1922
1923 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001924 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301925 }
1926
Dong Aisheng45251812013-09-13 19:11:30 +08001927 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001928 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001929 err = host->ops->platform_execute_tuning(host, opcode);
Dong Aisheng45251812013-09-13 19:11:30 +08001930 return err;
1931 }
1932
Russell King4b6f37d2014-04-25 12:59:36 +01001933 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1934 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001935 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1936 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301937 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1938
1939 /*
1940 * As per the Host Controller spec v3.00, tuning command
1941 * generates Buffer Read Ready interrupt, so enable that.
1942 *
1943 * Note: The spec clearly says that when tuning sequence
1944 * is being performed, the controller does not generate
1945 * interrupts other than Buffer Read Ready interrupt. But
1946 * to make sure we don't hit a controller bug, we _only_
1947 * enable Buffer Read Ready interrupt here.
1948 */
Russell Kingb537f942014-04-25 12:56:01 +01001949 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1950 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301951
1952 /*
1953 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
Simon Horman1473bdd2016-05-13 13:24:31 +09001954 * of loops reaches 40 times.
Arindam Nathb513ea22011-05-05 12:19:04 +05301955 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301956 do {
1957 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001958 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301959
Girish K S069c9f12012-01-06 09:56:39 +05301960 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301961 cmd.arg = 0;
1962 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1963 cmd.retries = 0;
1964 cmd.data = NULL;
1965 cmd.error = 0;
1966
Al Cooper7ce45e92014-05-09 11:34:07 -04001967 if (tuning_loop_counter-- == 0)
1968 break;
1969
Arindam Nathb513ea22011-05-05 12:19:04 +05301970 mrq.cmd = &cmd;
1971 host->mrq = &mrq;
1972
1973 /*
1974 * In response to CMD19, the card sends 64 bytes of tuning
1975 * block to the Host Controller. So we set the block size
1976 * to 64 here.
1977 */
Girish K S069c9f12012-01-06 09:56:39 +05301978 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1979 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1980 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1981 SDHCI_BLOCK_SIZE);
1982 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1983 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1984 SDHCI_BLOCK_SIZE);
1985 } else {
1986 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1987 SDHCI_BLOCK_SIZE);
1988 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301989
1990 /*
1991 * The tuning block is sent by the card to the host controller.
1992 * So we set the TRNS_READ bit in the Transfer Mode register.
1993 * This also takes care of setting DMA Enable and Multi Block
1994 * Select in the same register to 0.
1995 */
1996 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1997
1998 sdhci_send_command(host, &cmd);
1999
2000 host->cmd = NULL;
2001 host->mrq = NULL;
2002
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002003 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302004 /* Wait for Buffer Read Ready interrupt */
2005 wait_event_interruptible_timeout(host->buf_ready_int,
2006 (host->tuning_done == 1),
2007 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002008 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302009
2010 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002011 pr_info(DRIVER_NAME ": Timeout waiting for Buffer Read Ready interrupt during tuning procedure, falling back to fixed sampling clock\n");
Arindam Nathb513ea22011-05-05 12:19:04 +05302012 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2013 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2014 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2015 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2016
2017 err = -EIO;
2018 goto out;
2019 }
2020
2021 host->tuning_done = 0;
2022
2023 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07002024
2025 /* eMMC spec does not require a delay between tuning cycles */
2026 if (opcode == MMC_SEND_TUNING_BLOCK)
2027 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302028 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2029
2030 /*
2031 * The Host Driver has exhausted the maximum number of loops allowed,
2032 * so use fixed sampling frequency.
2033 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002034 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302035 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2036 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002037 }
2038 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002039 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002040 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302041 }
2042
2043out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002044 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002045 /*
2046 * In case tuning fails, host controllers which support
2047 * re-tuning can try tuning again at a later time, when the
2048 * re-tuning timer expires. So for these controllers, we
2049 * return 0. Since there might be other controllers who do not
2050 * have this capability, we return error for them.
2051 */
2052 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302053 }
2054
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002055 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302056
Russell Kingb537f942014-04-25 12:56:01 +01002057 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2058 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002059out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002060 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302061 return err;
2062}
2063
Adrian Huntercb849642015-02-06 14:12:59 +02002064static int sdhci_select_drive_strength(struct mmc_card *card,
2065 unsigned int max_dtr, int host_drv,
2066 int card_drv, int *drv_type)
2067{
2068 struct sdhci_host *host = mmc_priv(card->host);
2069
2070 if (!host->ops->select_drive_strength)
2071 return 0;
2072
2073 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2074 card_drv, drv_type);
2075}
Kevin Liu52983382013-01-31 11:31:37 +08002076
2077static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302078{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302079 /* Host Controller v3.00 defines preset value registers */
2080 if (host->version < SDHCI_SPEC_300)
2081 return;
2082
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302083 /*
2084 * We only enable or disable Preset Value if they are not already
2085 * enabled or disabled respectively. Otherwise, we bail out.
2086 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002087 if (host->preset_enabled != enable) {
2088 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2089
2090 if (enable)
2091 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2092 else
2093 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2094
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302095 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002096
2097 if (enable)
2098 host->flags |= SDHCI_PV_ENABLED;
2099 else
2100 host->flags &= ~SDHCI_PV_ENABLED;
2101
2102 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302103 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002104}
2105
Haibo Chen348487c2014-12-09 17:04:05 +08002106static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2107 int err)
2108{
2109 struct sdhci_host *host = mmc_priv(mmc);
2110 struct mmc_data *data = mrq->data;
2111
Russell Kingf48f0392016-01-26 13:40:32 +00002112 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002113 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2114 data->flags & MMC_DATA_WRITE ?
2115 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2116
2117 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002118}
2119
Haibo Chen348487c2014-12-09 17:04:05 +08002120static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2121 bool is_first_req)
2122{
2123 struct sdhci_host *host = mmc_priv(mmc);
2124
Haibo Chend31911b2015-08-25 10:02:11 +08002125 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002126
2127 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002128 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002129}
2130
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002131static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002132{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002133 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002134 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002135 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002136
Christian Daudt722e1282013-06-20 14:26:36 -07002137 /* First check if client has provided their own card event */
2138 if (host->ops->card_event)
2139 host->ops->card_event(host);
2140
Adrian Hunterd3940f22016-06-29 16:24:14 +03002141 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002142
Pierre Ossmand129bce2006-03-24 03:18:17 -08002143 spin_lock_irqsave(&host->lock, flags);
2144
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002145 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002146 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302147 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002148 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302149 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002150 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002151
Russell King03231f92014-04-25 12:57:12 +01002152 sdhci_do_reset(host, SDHCI_RESET_CMD);
2153 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002154
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002155 host->mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002156 sdhci_finish_mrq(host, host->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002157 }
2158
2159 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002160}
2161
2162static const struct mmc_host_ops sdhci_ops = {
2163 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002164 .post_req = sdhci_post_req,
2165 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002166 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002167 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002168 .get_ro = sdhci_get_ro,
2169 .hw_reset = sdhci_hw_reset,
2170 .enable_sdio_irq = sdhci_enable_sdio_irq,
2171 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002172 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002173 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002174 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002175 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002176 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002177};
2178
2179/*****************************************************************************\
2180 * *
2181 * Tasklets *
2182 * *
2183\*****************************************************************************/
2184
Pierre Ossmand129bce2006-03-24 03:18:17 -08002185static void sdhci_tasklet_finish(unsigned long param)
2186{
2187 struct sdhci_host *host;
2188 unsigned long flags;
2189 struct mmc_request *mrq;
2190
2191 host = (struct sdhci_host*)param;
2192
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002193 spin_lock_irqsave(&host->lock, flags);
2194
Chris Ball0c9c99a2011-04-27 17:35:31 -04002195 /*
2196 * If this tasklet gets rescheduled while running, it will
2197 * be run again afterwards but without any active request.
2198 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002199 if (!host->mrq) {
2200 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002201 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002202 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002203
2204 del_timer(&host->timer);
2205
2206 mrq = host->mrq;
2207
Pierre Ossmand129bce2006-03-24 03:18:17 -08002208 /*
Russell King054cedf2016-01-26 13:40:42 +00002209 * Always unmap the data buffers if they were mapped by
2210 * sdhci_prepare_data() whenever we finish with a request.
2211 * This avoids leaking DMA mappings on error.
2212 */
2213 if (host->flags & SDHCI_REQ_USE_DMA) {
2214 struct mmc_data *data = mrq->data;
2215
2216 if (data && data->host_cookie == COOKIE_MAPPED) {
2217 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2218 (data->flags & MMC_DATA_READ) ?
2219 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2220 data->host_cookie = COOKIE_UNMAPPED;
2221 }
2222 }
2223
2224 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002225 * The controller needs a reset of internal state machines
2226 * upon error conditions.
2227 */
Adrian Hunter0cc563c2016-06-29 16:24:28 +03002228 if (sdhci_needs_reset(host, mrq)) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002229 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002230 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002231 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002232 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002233
2234 /* Spec says we should do both at the same time, but Ricoh
2235 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002236 sdhci_do_reset(host, SDHCI_RESET_CMD);
2237 sdhci_do_reset(host, SDHCI_RESET_DATA);
Adrian Huntered1563d2016-06-29 16:24:29 +03002238
2239 host->pending_reset = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002240 }
2241
2242 host->mrq = NULL;
2243 host->cmd = NULL;
2244 host->data = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002245 host->data_cmd = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002246
Adrian Hunter061d17a2016-04-12 14:25:09 +03002247 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002248
Pierre Ossman5f25a662006-10-04 02:15:39 -07002249 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002250 spin_unlock_irqrestore(&host->lock, flags);
2251
2252 mmc_request_done(host->mmc, mrq);
2253}
2254
2255static void sdhci_timeout_timer(unsigned long data)
2256{
2257 struct sdhci_host *host;
2258 unsigned long flags;
2259
2260 host = (struct sdhci_host*)data;
2261
2262 spin_lock_irqsave(&host->lock, flags);
2263
2264 if (host->mrq) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002265 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2266 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002267 sdhci_dumpregs(host);
2268
2269 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002270 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002271 sdhci_finish_data(host);
2272 } else {
2273 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002274 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002275 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002276 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002277
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002278 sdhci_finish_mrq(host, host->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002279 }
2280 }
2281
Pierre Ossman5f25a662006-10-04 02:15:39 -07002282 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002283 spin_unlock_irqrestore(&host->lock, flags);
2284}
2285
2286/*****************************************************************************\
2287 * *
2288 * Interrupt handling *
2289 * *
2290\*****************************************************************************/
2291
Adrian Hunter61541392014-09-24 10:27:27 +03002292static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002293{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002294 if (!host->cmd) {
Adrian Huntered1563d2016-06-29 16:24:29 +03002295 /*
2296 * SDHCI recovers from errors by resetting the cmd and data
2297 * circuits. Until that is done, there very well might be more
2298 * interrupts, so ignore them in that case.
2299 */
2300 if (host->pending_reset)
2301 return;
Marek Vasut2e4456f2015-11-18 10:47:02 +01002302 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2303 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002304 sdhci_dumpregs(host);
2305 return;
2306 }
2307
Russell Kingec014cb2016-01-26 13:39:39 +00002308 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2309 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2310 if (intmask & SDHCI_INT_TIMEOUT)
2311 host->cmd->error = -ETIMEDOUT;
2312 else
2313 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002314
Russell King71fcbda2016-01-26 13:39:45 +00002315 /*
2316 * If this command initiates a data phase and a response
2317 * CRC error is signalled, the card can start transferring
2318 * data - the card may have received the command without
2319 * error. We must not terminate the mmc_request early.
2320 *
2321 * If the card did not receive the command or returned an
2322 * error which prevented it sending data, the data phase
2323 * will time out.
2324 */
2325 if (host->cmd->data &&
2326 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2327 SDHCI_INT_CRC) {
2328 host->cmd = NULL;
2329 return;
2330 }
2331
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002332 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002333 return;
2334 }
2335
Adrian Hunter6bde8682016-06-29 16:24:20 +03002336 if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2337 !(host->cmd->flags & MMC_RSP_BUSY) && !host->data &&
2338 host->cmd->opcode == MMC_STOP_TRANSMISSION)
Adrian Hunter61541392014-09-24 10:27:27 +03002339 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002340
2341 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002342 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002343}
2344
George G. Davis0957c332010-02-18 12:32:12 -05002345#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002346static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002347{
2348 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002349 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002350
2351 sdhci_dumpregs(host);
2352
2353 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002354 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002355
Adrian Huntere57a5f62014-11-04 12:42:46 +02002356 if (host->flags & SDHCI_USE_64_BIT_DMA)
2357 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2358 name, desc, le32_to_cpu(dma_desc->addr_hi),
2359 le32_to_cpu(dma_desc->addr_lo),
2360 le16_to_cpu(dma_desc->len),
2361 le16_to_cpu(dma_desc->cmd));
2362 else
2363 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2364 name, desc, le32_to_cpu(dma_desc->addr_lo),
2365 le16_to_cpu(dma_desc->len),
2366 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002367
Adrian Hunter76fe3792014-11-04 12:42:42 +02002368 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002369
Adrian Hunter05452302014-11-04 12:42:45 +02002370 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002371 break;
2372 }
2373}
2374#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002375static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002376#endif
2377
Pierre Ossmand129bce2006-03-24 03:18:17 -08002378static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2379{
Girish K S069c9f12012-01-06 09:56:39 +05302380 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002381
Arindam Nathb513ea22011-05-05 12:19:04 +05302382 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2383 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302384 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2385 if (command == MMC_SEND_TUNING_BLOCK ||
2386 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302387 host->tuning_done = 1;
2388 wake_up(&host->buf_ready_int);
2389 return;
2390 }
2391 }
2392
Pierre Ossmand129bce2006-03-24 03:18:17 -08002393 if (!host->data) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002394 struct mmc_command *data_cmd = host->data_cmd;
2395
2396 if (data_cmd)
2397 host->data_cmd = NULL;
2398
Pierre Ossmand129bce2006-03-24 03:18:17 -08002399 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002400 * The "data complete" interrupt is also used to
2401 * indicate that a busy state has ended. See comment
2402 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002403 */
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002404 if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002405 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002406 data_cmd->error = -ETIMEDOUT;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002407 sdhci_finish_mrq(host, data_cmd->mrq);
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002408 return;
2409 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002410 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002411 /*
2412 * Some cards handle busy-end interrupt
2413 * before the command completed, so make
2414 * sure we do things in the proper order.
2415 */
Adrian Hunterea968022016-06-29 16:24:24 +03002416 if (host->cmd == data_cmd)
2417 return;
2418
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002419 sdhci_finish_mrq(host, data_cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002420 return;
2421 }
2422 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002423
Adrian Huntered1563d2016-06-29 16:24:29 +03002424 /*
2425 * SDHCI recovers from errors by resetting the cmd and data
2426 * circuits. Until that is done, there very well might be more
2427 * interrupts, so ignore them in that case.
2428 */
2429 if (host->pending_reset)
2430 return;
2431
Marek Vasut2e4456f2015-11-18 10:47:02 +01002432 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2433 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002434 sdhci_dumpregs(host);
2435
2436 return;
2437 }
2438
2439 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002440 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002441 else if (intmask & SDHCI_INT_DATA_END_BIT)
2442 host->data->error = -EILSEQ;
2443 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2444 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2445 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002446 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002447 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302448 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002449 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002450 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002451 if (host->ops->adma_workaround)
2452 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002453 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002454
Pierre Ossman17b04292007-07-22 22:18:46 +02002455 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002456 sdhci_finish_data(host);
2457 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002458 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002459 sdhci_transfer_pio(host);
2460
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002461 /*
2462 * We currently don't do anything fancy with DMA
2463 * boundaries, but as we can't disable the feature
2464 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002465 *
2466 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2467 * should return a valid address to continue from, but as
2468 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002469 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002470 if (intmask & SDHCI_INT_DMA_END) {
2471 u32 dmastart, dmanow;
2472 dmastart = sg_dma_address(host->data->sg);
2473 dmanow = dmastart + host->data->bytes_xfered;
2474 /*
2475 * Force update to the next DMA block boundary.
2476 */
2477 dmanow = (dmanow &
2478 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2479 SDHCI_DEFAULT_BOUNDARY_SIZE;
2480 host->data->bytes_xfered = dmanow - dmastart;
2481 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2482 " next 0x%08x\n",
2483 mmc_hostname(host->mmc), dmastart,
2484 host->data->bytes_xfered, dmanow);
2485 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2486 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002487
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002488 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002489 if (host->cmd == host->data_cmd) {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002490 /*
2491 * Data managed to finish before the
2492 * command completed. Make sure we do
2493 * things in the proper order.
2494 */
2495 host->data_early = 1;
2496 } else {
2497 sdhci_finish_data(host);
2498 }
2499 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002500 }
2501}
2502
David Howells7d12e782006-10-05 14:55:46 +01002503static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002504{
Russell King781e9892014-04-25 12:55:46 +01002505 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002506 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002507 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002508 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002509
2510 spin_lock(&host->lock);
2511
Russell Kingbe138552014-04-25 12:55:56 +01002512 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002513 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002514 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002515 }
2516
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002517 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002518 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002519 result = IRQ_NONE;
2520 goto out;
2521 }
2522
Russell King41005002014-04-25 12:55:36 +01002523 do {
2524 /* Clear selected interrupts. */
2525 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2526 SDHCI_INT_BUS_POWER);
2527 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002528
Russell King41005002014-04-25 12:55:36 +01002529 DBG("*** %s got interrupt: 0x%08x\n",
2530 mmc_hostname(host->mmc), intmask);
2531
2532 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2533 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2534 SDHCI_CARD_PRESENT;
2535
2536 /*
2537 * There is a observation on i.mx esdhc. INSERT
2538 * bit will be immediately set again when it gets
2539 * cleared, if a card is inserted. We have to mask
2540 * the irq to prevent interrupt storm which will
2541 * freeze the system. And the REMOVE gets the
2542 * same situation.
2543 *
2544 * More testing are needed here to ensure it works
2545 * for other platforms though.
2546 */
Russell Kingb537f942014-04-25 12:56:01 +01002547 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2548 SDHCI_INT_CARD_REMOVE);
2549 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2550 SDHCI_INT_CARD_INSERT;
2551 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2552 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002553
2554 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2555 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002556
2557 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2558 SDHCI_INT_CARD_REMOVE);
2559 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002560 }
2561
2562 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002563 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2564 &intmask);
Russell King41005002014-04-25 12:55:36 +01002565
2566 if (intmask & SDHCI_INT_DATA_MASK)
2567 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2568
2569 if (intmask & SDHCI_INT_BUS_POWER)
2570 pr_err("%s: Card is consuming too much power!\n",
2571 mmc_hostname(host->mmc));
2572
Russell King781e9892014-04-25 12:55:46 +01002573 if (intmask & SDHCI_INT_CARD_INT) {
2574 sdhci_enable_sdio_irq_nolock(host, false);
2575 host->thread_isr |= SDHCI_INT_CARD_INT;
2576 result = IRQ_WAKE_THREAD;
2577 }
Russell King41005002014-04-25 12:55:36 +01002578
2579 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2580 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2581 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2582 SDHCI_INT_CARD_INT);
2583
2584 if (intmask) {
2585 unexpected |= intmask;
2586 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2587 }
2588
Russell King781e9892014-04-25 12:55:46 +01002589 if (result == IRQ_NONE)
2590 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002591
2592 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002593 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002594out:
2595 spin_unlock(&host->lock);
2596
Alexander Stein6379b232012-03-14 09:52:10 +01002597 if (unexpected) {
2598 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2599 mmc_hostname(host->mmc), unexpected);
2600 sdhci_dumpregs(host);
2601 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002602
Pierre Ossmand129bce2006-03-24 03:18:17 -08002603 return result;
2604}
2605
Russell King781e9892014-04-25 12:55:46 +01002606static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2607{
2608 struct sdhci_host *host = dev_id;
2609 unsigned long flags;
2610 u32 isr;
2611
2612 spin_lock_irqsave(&host->lock, flags);
2613 isr = host->thread_isr;
2614 host->thread_isr = 0;
2615 spin_unlock_irqrestore(&host->lock, flags);
2616
Russell King3560db82014-04-25 12:55:51 +01002617 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Adrian Hunterd3940f22016-06-29 16:24:14 +03002618 struct mmc_host *mmc = host->mmc;
2619
2620 mmc->ops->card_event(mmc);
2621 mmc_detect_change(mmc, msecs_to_jiffies(200));
Russell King3560db82014-04-25 12:55:51 +01002622 }
2623
Russell King781e9892014-04-25 12:55:46 +01002624 if (isr & SDHCI_INT_CARD_INT) {
2625 sdio_run_irqs(host->mmc);
2626
2627 spin_lock_irqsave(&host->lock, flags);
2628 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2629 sdhci_enable_sdio_irq_nolock(host, true);
2630 spin_unlock_irqrestore(&host->lock, flags);
2631 }
2632
2633 return isr ? IRQ_HANDLED : IRQ_NONE;
2634}
2635
Pierre Ossmand129bce2006-03-24 03:18:17 -08002636/*****************************************************************************\
2637 * *
2638 * Suspend/resume *
2639 * *
2640\*****************************************************************************/
2641
2642#ifdef CONFIG_PM
Ludovic Desroches84d62602016-05-13 15:16:02 +02002643/*
2644 * To enable wakeup events, the corresponding events have to be enabled in
2645 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
2646 * Table' in the SD Host Controller Standard Specification.
2647 * It is useless to restore SDHCI_INT_ENABLE state in
2648 * sdhci_disable_irq_wakeups() since it will be set by
2649 * sdhci_enable_card_detection() or sdhci_init().
2650 */
Kevin Liuad080d72013-01-05 17:21:33 +08002651void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2652{
2653 u8 val;
2654 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2655 | SDHCI_WAKE_ON_INT;
Ludovic Desroches84d62602016-05-13 15:16:02 +02002656 u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2657 SDHCI_INT_CARD_INT;
Kevin Liuad080d72013-01-05 17:21:33 +08002658
2659 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2660 val |= mask ;
2661 /* Avoid fake wake up */
Ludovic Desroches84d62602016-05-13 15:16:02 +02002662 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) {
Kevin Liuad080d72013-01-05 17:21:33 +08002663 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002664 irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
2665 }
Kevin Liuad080d72013-01-05 17:21:33 +08002666 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002667 sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002668}
2669EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2670
Fabio Estevam0b10f472014-08-30 14:53:13 -03002671static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002672{
2673 u8 val;
2674 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2675 | SDHCI_WAKE_ON_INT;
2676
2677 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2678 val &= ~mask;
2679 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2680}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002681
Manuel Lauss29495aa2011-11-03 11:09:45 +01002682int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002683{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002684 sdhci_disable_card_detection(host);
2685
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002686 mmc_retune_timer_stop(host->mmc);
2687 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302688
Kevin Liuad080d72013-01-05 17:21:33 +08002689 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002690 host->ier = 0;
2691 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2692 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002693 free_irq(host->irq, host);
2694 } else {
2695 sdhci_enable_irq_wakeups(host);
2696 enable_irq_wake(host->irq);
2697 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002698 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002699}
2700
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002701EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002702
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002703int sdhci_resume_host(struct sdhci_host *host)
2704{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002705 struct mmc_host *mmc = host->mmc;
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002706 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002707
Richard Röjforsa13abc72009-09-22 16:45:30 -07002708 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002709 if (host->ops->enable_dma)
2710 host->ops->enable_dma(host);
2711 }
2712
Adrian Hunter6308d292012-02-07 14:48:54 +02002713 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2714 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2715 /* Card keeps power but host controller does not */
2716 sdhci_init(host, 0);
2717 host->pwr = 0;
2718 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002719 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02002720 } else {
2721 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2722 mmiowb();
2723 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002724
Haibo Chen14a7b41642015-09-15 18:32:58 +08002725 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2726 ret = request_threaded_irq(host->irq, sdhci_irq,
2727 sdhci_thread_irq, IRQF_SHARED,
2728 mmc_hostname(host->mmc), host);
2729 if (ret)
2730 return ret;
2731 } else {
2732 sdhci_disable_irq_wakeups(host);
2733 disable_irq_wake(host->irq);
2734 }
2735
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002736 sdhci_enable_card_detection(host);
2737
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002738 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002739}
2740
2741EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002742
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002743int sdhci_runtime_suspend_host(struct sdhci_host *host)
2744{
2745 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002746
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002747 mmc_retune_timer_stop(host->mmc);
2748 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002749
2750 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002751 host->ier &= SDHCI_INT_CARD_INT;
2752 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2753 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002754 spin_unlock_irqrestore(&host->lock, flags);
2755
Russell King781e9892014-04-25 12:55:46 +01002756 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002757
2758 spin_lock_irqsave(&host->lock, flags);
2759 host->runtime_suspended = true;
2760 spin_unlock_irqrestore(&host->lock, flags);
2761
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002762 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002763}
2764EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2765
2766int sdhci_runtime_resume_host(struct sdhci_host *host)
2767{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002768 struct mmc_host *mmc = host->mmc;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002769 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002770 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002771
2772 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2773 if (host->ops->enable_dma)
2774 host->ops->enable_dma(host);
2775 }
2776
2777 sdhci_init(host, 0);
2778
2779 /* Force clock and power re-program */
2780 host->pwr = 0;
2781 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002782 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
2783 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002784
Kevin Liu52983382013-01-31 11:31:37 +08002785 if ((host_flags & SDHCI_PV_ENABLED) &&
2786 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2787 spin_lock_irqsave(&host->lock, flags);
2788 sdhci_enable_preset_value(host, true);
2789 spin_unlock_irqrestore(&host->lock, flags);
2790 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002791
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002792 spin_lock_irqsave(&host->lock, flags);
2793
2794 host->runtime_suspended = false;
2795
2796 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002797 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002798 sdhci_enable_sdio_irq_nolock(host, true);
2799
2800 /* Enable Card Detection */
2801 sdhci_enable_card_detection(host);
2802
2803 spin_unlock_irqrestore(&host->lock, flags);
2804
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002805 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002806}
2807EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2808
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002809#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002810
Pierre Ossmand129bce2006-03-24 03:18:17 -08002811/*****************************************************************************\
2812 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002813 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002814 * *
2815\*****************************************************************************/
2816
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002817struct sdhci_host *sdhci_alloc_host(struct device *dev,
2818 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002819{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002820 struct mmc_host *mmc;
2821 struct sdhci_host *host;
2822
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002823 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002824
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002825 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002826 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002827 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002828
2829 host = mmc_priv(mmc);
2830 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002831 host->mmc_host_ops = sdhci_ops;
2832 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002833
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002834 host->flags = SDHCI_SIGNALING_330;
2835
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002836 return host;
2837}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002838
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002839EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002840
Alexandre Courbot7b913692016-03-07 11:07:55 +09002841static int sdhci_set_dma_mask(struct sdhci_host *host)
2842{
2843 struct mmc_host *mmc = host->mmc;
2844 struct device *dev = mmc_dev(mmc);
2845 int ret = -EINVAL;
2846
2847 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
2848 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2849
2850 /* Try 64-bit mask if hardware is capable of it */
2851 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2852 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2853 if (ret) {
2854 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
2855 mmc_hostname(mmc));
2856 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2857 }
2858 }
2859
2860 /* 32-bit mask as default & fallback */
2861 if (ret) {
2862 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2863 if (ret)
2864 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
2865 mmc_hostname(mmc));
2866 }
2867
2868 return ret;
2869}
2870
Adrian Hunter6132a3b2016-06-29 16:24:18 +03002871void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
2872{
2873 u16 v;
2874
2875 if (host->read_caps)
2876 return;
2877
2878 host->read_caps = true;
2879
2880 if (debug_quirks)
2881 host->quirks = debug_quirks;
2882
2883 if (debug_quirks2)
2884 host->quirks2 = debug_quirks2;
2885
2886 sdhci_do_reset(host, SDHCI_RESET_ALL);
2887
2888 v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
2889 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
2890
2891 if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
2892 return;
2893
2894 host->caps = caps ? *caps : sdhci_readl(host, SDHCI_CAPABILITIES);
2895
2896 if (host->version < SDHCI_SPEC_300)
2897 return;
2898
2899 host->caps1 = caps1 ? *caps1 : sdhci_readl(host, SDHCI_CAPABILITIES_1);
2900}
2901EXPORT_SYMBOL_GPL(__sdhci_read_caps);
2902
Adrian Hunter52f53362016-06-29 16:24:15 +03002903int sdhci_setup_host(struct sdhci_host *host)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002904{
2905 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302906 u32 max_current_caps;
2907 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002908 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08002909 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002910 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002911
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002912 WARN_ON(host == NULL);
2913 if (host == NULL)
2914 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002915
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002916 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002917
Adrian Hunter6132a3b2016-06-29 16:24:18 +03002918 sdhci_read_caps(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002919
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002920 override_timeout_clk = host->timeout_clk;
2921
Zhangfei Gao85105c52010-08-06 07:10:01 +08002922 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002923 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
2924 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002925 }
2926
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002927 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002928 host->flags |= SDHCI_USE_SDMA;
Adrian Hunter28da3582016-06-29 16:24:17 +03002929 else if (!(host->caps & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002930 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002931 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002932 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002933
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002934 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002935 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002936 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002937 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002938 }
2939
Arindam Nathf2119df2011-05-05 12:18:57 +05302940 if ((host->version >= SDHCI_SPEC_200) &&
Adrian Hunter28da3582016-06-29 16:24:17 +03002941 (host->caps & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002942 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002943
2944 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2945 (host->flags & SDHCI_USE_ADMA)) {
2946 DBG("Disabling ADMA as it is marked broken\n");
2947 host->flags &= ~SDHCI_USE_ADMA;
2948 }
2949
Adrian Huntere57a5f62014-11-04 12:42:46 +02002950 /*
2951 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2952 * and *must* do 64-bit DMA. A driver has the opportunity to change
2953 * that during the first call to ->enable_dma(). Similarly
2954 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2955 * implement.
2956 */
Adrian Hunter28da3582016-06-29 16:24:17 +03002957 if (host->caps & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02002958 host->flags |= SDHCI_USE_64_BIT_DMA;
2959
Richard Röjforsa13abc72009-09-22 16:45:30 -07002960 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09002961 ret = sdhci_set_dma_mask(host);
2962
2963 if (!ret && host->ops->enable_dma)
2964 ret = host->ops->enable_dma(host);
2965
2966 if (ret) {
2967 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
2968 mmc_hostname(mmc));
2969 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2970
2971 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002972 }
2973 }
2974
Adrian Huntere57a5f62014-11-04 12:42:46 +02002975 /* SDMA does not support 64-bit DMA */
2976 if (host->flags & SDHCI_USE_64_BIT_DMA)
2977 host->flags &= ~SDHCI_USE_SDMA;
2978
Pierre Ossman2134a922008-06-28 18:28:51 +02002979 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00002980 dma_addr_t dma;
2981 void *buf;
2982
Pierre Ossman2134a922008-06-28 18:28:51 +02002983 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002984 * The DMA descriptor table size is calculated as the maximum
2985 * number of segments times 2, to allow for an alignment
2986 * descriptor for each segment, plus 1 for a nop end descriptor,
2987 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002988 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02002989 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2990 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2991 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002992 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002993 } else {
2994 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2995 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002996 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002997 }
Russell Kinge66e61c2016-01-26 13:39:55 +00002998
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002999 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00003000 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
3001 host->adma_table_sz, &dma, GFP_KERNEL);
3002 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07003003 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003004 mmc_hostname(mmc));
3005 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003006 } else if ((dma + host->align_buffer_sz) &
3007 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07003008 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3009 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003010 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003011 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3012 host->adma_table_sz, buf, dma);
3013 } else {
3014 host->align_buffer = buf;
3015 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00003016
Russell Kinge66e61c2016-01-26 13:39:55 +00003017 host->adma_table = buf + host->align_buffer_sz;
3018 host->adma_addr = dma + host->align_buffer_sz;
3019 }
Pierre Ossman2134a922008-06-28 18:28:51 +02003020 }
3021
Pierre Ossman76591502008-07-21 00:32:11 +02003022 /*
3023 * If we use DMA, then it's up to the caller to set the DMA
3024 * mask, but PIO does not need the hw shim so we set a new
3025 * mask here in that case.
3026 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003027 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003028 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003029 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003030 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003031
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003032 if (host->version >= SDHCI_SPEC_300)
Adrian Hunter28da3582016-06-29 16:24:17 +03003033 host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003034 >> SDHCI_CLOCK_BASE_SHIFT;
3035 else
Adrian Hunter28da3582016-06-29 16:24:17 +03003036 host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003037 >> SDHCI_CLOCK_BASE_SHIFT;
3038
Pierre Ossmand129bce2006-03-24 03:18:17 -08003039 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003040 if (host->max_clk == 0 || host->quirks &
3041 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003042 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003043 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3044 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003045 ret = -ENODEV;
3046 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003047 }
3048 host->max_clk = host->ops->get_max_clock(host);
3049 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003050
3051 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303052 * In case of Host Controller v3.00, find out whether clock
3053 * multiplier is supported.
3054 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003055 host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
Arindam Nathc3ed3872011-05-05 12:19:06 +05303056 SDHCI_CLOCK_MUL_SHIFT;
3057
3058 /*
3059 * In case the value in Clock Multiplier is 0, then programmable
3060 * clock mode is not supported, otherwise the actual clock
3061 * multiplier is one more than the value of Clock Multiplier
3062 * in the Capabilities Register.
3063 */
3064 if (host->clk_mul)
3065 host->clk_mul += 1;
3066
3067 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003068 * Set host parameters.
3069 */
Dong Aisheng59241752015-07-22 20:53:07 +08003070 max_clk = host->max_clk;
3071
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003072 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003073 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303074 else if (host->version >= SDHCI_SPEC_300) {
3075 if (host->clk_mul) {
3076 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003077 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303078 } else
3079 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3080 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003081 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003082
Adrian Hunterd310ae42016-04-12 14:25:07 +03003083 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003084 mmc->f_max = max_clk;
3085
Aisheng Dong28aab052014-08-27 15:26:31 +08003086 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Adrian Hunter28da3582016-06-29 16:24:17 +03003087 host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
Aisheng Dong28aab052014-08-27 15:26:31 +08003088 SDHCI_TIMEOUT_CLK_SHIFT;
3089 if (host->timeout_clk == 0) {
3090 if (host->ops->get_timeout_clock) {
3091 host->timeout_clk =
3092 host->ops->get_timeout_clock(host);
3093 } else {
3094 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3095 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003096 ret = -ENODEV;
3097 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003098 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003099 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003100
Adrian Hunter28da3582016-06-29 16:24:17 +03003101 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
Aisheng Dong28aab052014-08-27 15:26:31 +08003102 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003103
Adrian Hunter99513622016-03-07 13:33:55 +02003104 if (override_timeout_clk)
3105 host->timeout_clk = override_timeout_clk;
3106
Aisheng Dong28aab052014-08-27 15:26:31 +08003107 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003108 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003109 mmc->max_busy_timeout /= host->timeout_clk;
3110 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003111
Andrei Warkentine89d4562011-05-23 15:06:37 -05003112 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003113 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003114
3115 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3116 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003117
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003118 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003119 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003120 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003121 !(host->flags & SDHCI_USE_SDMA)) &&
3122 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003123 host->flags |= SDHCI_AUTO_CMD23;
3124 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3125 } else {
3126 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3127 }
3128
Philip Rakity15ec4462010-11-19 16:48:39 -05003129 /*
3130 * A controller may support 8-bit width, but the board itself
3131 * might not have the pins brought out. Boards that support
3132 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3133 * their platform code before calling sdhci_add_host(), and we
3134 * won't assume 8-bit width for hosts without that CAP.
3135 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003136 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003137 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003138
Jerry Huang63ef5d82012-10-25 13:47:19 +08003139 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3140 mmc->caps &= ~MMC_CAP_CMD23;
3141
Adrian Hunter28da3582016-06-29 16:24:17 +03003142 if (host->caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003143 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003144
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003145 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Jaehoon Chung860951c2016-06-21 10:13:26 +09003146 mmc_card_is_removable(mmc) &&
Arnd Bergmann287980e2016-05-27 23:23:25 +02003147 mmc_gpio_get_cd(host->mmc) < 0)
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003148 mmc->caps |= MMC_CAP_NEEDS_POLL;
3149
Tim Kryger3a48edc2014-06-13 10:13:56 -07003150 /* If there are external regulators, get them */
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003151 ret = mmc_regulator_get_supply(mmc);
3152 if (ret == -EPROBE_DEFER)
3153 goto undma;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003154
Philip Rakity6231f3d2012-07-23 15:56:23 -07003155 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003156 if (!IS_ERR(mmc->supply.vqmmc)) {
3157 ret = regulator_enable(mmc->supply.vqmmc);
3158 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3159 1950000))
Adrian Hunter28da3582016-06-29 16:24:17 +03003160 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
3161 SDHCI_SUPPORT_SDR50 |
3162 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003163 if (ret) {
3164 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3165 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003166 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003167 }
Kevin Liu8363c372012-11-17 17:55:51 -05003168 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003169
Adrian Hunter28da3582016-06-29 16:24:17 +03003170 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
3171 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3172 SDHCI_SUPPORT_DDR50);
3173 }
Daniel Drake6a661802012-11-25 13:01:19 -05003174
Al Cooper4188bba2012-03-16 15:54:17 -04003175 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
Adrian Hunter28da3582016-06-29 16:24:17 +03003176 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3177 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303178 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3179
3180 /* SDR104 supports also implies SDR50 support */
Adrian Hunter28da3582016-06-29 16:24:17 +03003181 if (host->caps1 & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303182 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003183 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3184 * field can be promoted to support HS200.
3185 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003186 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003187 mmc->caps2 |= MMC_CAP2_HS200;
Adrian Hunter28da3582016-06-29 16:24:17 +03003188 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303189 mmc->caps |= MMC_CAP_UHS_SDR50;
Adrian Hunter28da3582016-06-29 16:24:17 +03003190 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303191
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003192 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003193 (host->caps1 & SDHCI_SUPPORT_HS400))
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003194 mmc->caps2 |= MMC_CAP2_HS400;
3195
Adrian Hunter549c0b12014-11-06 15:19:05 +02003196 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3197 (IS_ERR(mmc->supply.vqmmc) ||
3198 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3199 1300000)))
3200 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3201
Adrian Hunter28da3582016-06-29 16:24:17 +03003202 if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
3203 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303204 mmc->caps |= MMC_CAP_UHS_DDR50;
3205
Girish K S069c9f12012-01-06 09:56:39 +05303206 /* Does the host need tuning for SDR50? */
Adrian Hunter28da3582016-06-29 16:24:17 +03003207 if (host->caps1 & SDHCI_USE_SDR50_TUNING)
Arindam Nathb513ea22011-05-05 12:19:04 +05303208 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3209
Arindam Nathd6d50a12011-05-05 12:18:59 +05303210 /* Driver Type(s) (A, C, D) supported by the host */
Adrian Hunter28da3582016-06-29 16:24:17 +03003211 if (host->caps1 & SDHCI_DRIVER_TYPE_A)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303212 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
Adrian Hunter28da3582016-06-29 16:24:17 +03003213 if (host->caps1 & SDHCI_DRIVER_TYPE_C)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303214 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
Adrian Hunter28da3582016-06-29 16:24:17 +03003215 if (host->caps1 & SDHCI_DRIVER_TYPE_D)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303216 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3217
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303218 /* Initial value for re-tuning timer count */
Adrian Hunter28da3582016-06-29 16:24:17 +03003219 host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3220 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303221
3222 /*
3223 * In case Re-tuning Timer is not disabled, the actual value of
3224 * re-tuning timer will be 2 ^ (n - 1).
3225 */
3226 if (host->tuning_count)
3227 host->tuning_count = 1 << (host->tuning_count - 1);
3228
3229 /* Re-tuning mode supported by the Host Controller */
Adrian Hunter28da3582016-06-29 16:24:17 +03003230 host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303231 SDHCI_RETUNING_MODE_SHIFT;
3232
Takashi Iwai8f230f42010-12-08 10:04:30 +01003233 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003234
Arindam Nathf2119df2011-05-05 12:18:57 +05303235 /*
3236 * According to SD Host Controller spec v3.00, if the Host System
3237 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3238 * the value is meaningful only if Voltage Support in the Capabilities
3239 * register is set. The actual current value is 4 times the register
3240 * value.
3241 */
3242 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003243 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003244 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003245 if (curr > 0) {
3246
3247 /* convert to SDHCI_MAX_CURRENT format */
3248 curr = curr/1000; /* convert to mA */
3249 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3250
3251 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3252 max_current_caps =
3253 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3254 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3255 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3256 }
3257 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303258
Adrian Hunter28da3582016-06-29 16:24:17 +03003259 if (host->caps & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003260 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303261
Aaron Lu55c46652012-07-04 13:31:48 +08003262 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303263 SDHCI_MAX_CURRENT_330_MASK) >>
3264 SDHCI_MAX_CURRENT_330_SHIFT) *
3265 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303266 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003267 if (host->caps & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003268 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303269
Aaron Lu55c46652012-07-04 13:31:48 +08003270 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303271 SDHCI_MAX_CURRENT_300_MASK) >>
3272 SDHCI_MAX_CURRENT_300_SHIFT) *
3273 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303274 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003275 if (host->caps & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003276 ocr_avail |= MMC_VDD_165_195;
3277
Aaron Lu55c46652012-07-04 13:31:48 +08003278 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303279 SDHCI_MAX_CURRENT_180_MASK) >>
3280 SDHCI_MAX_CURRENT_180_SHIFT) *
3281 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303282 }
3283
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003284 /* If OCR set by host, use it instead. */
3285 if (host->ocr_mask)
3286 ocr_avail = host->ocr_mask;
3287
3288 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003289 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003290 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003291
Takashi Iwai8f230f42010-12-08 10:04:30 +01003292 mmc->ocr_avail = ocr_avail;
3293 mmc->ocr_avail_sdio = ocr_avail;
3294 if (host->ocr_avail_sdio)
3295 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3296 mmc->ocr_avail_sd = ocr_avail;
3297 if (host->ocr_avail_sd)
3298 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3299 else /* normal SD controllers don't support 1.8V */
3300 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3301 mmc->ocr_avail_mmc = ocr_avail;
3302 if (host->ocr_avail_mmc)
3303 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003304
3305 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003306 pr_err("%s: Hardware doesn't report any support voltages.\n",
3307 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003308 ret = -ENODEV;
3309 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07003310 }
3311
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003312 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
3313 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
3314 MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
3315 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
3316 host->flags |= SDHCI_SIGNALING_180;
3317
3318 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
3319 host->flags |= SDHCI_SIGNALING_120;
3320
Pierre Ossmand129bce2006-03-24 03:18:17 -08003321 spin_lock_init(&host->lock);
3322
3323 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003324 * Maximum number of segments. Depends on if the hardware
3325 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003326 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003327 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003328 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003329 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003330 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003331 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003332 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003333
3334 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003335 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3336 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3337 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003338 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003339 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003340
3341 /*
3342 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003343 * of bytes. When doing hardware scatter/gather, each entry cannot
3344 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003345 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003346 if (host->flags & SDHCI_USE_ADMA) {
3347 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3348 mmc->max_seg_size = 65535;
3349 else
3350 mmc->max_seg_size = 65536;
3351 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003352 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003353 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003354
3355 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003356 * Maximum block size. This varies from controller to controller and
3357 * is specified in the capabilities register.
3358 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003359 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3360 mmc->max_blk_size = 2;
3361 } else {
Adrian Hunter28da3582016-06-29 16:24:17 +03003362 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003363 SDHCI_MAX_BLOCK_SHIFT;
3364 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003365 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3366 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003367 mmc->max_blk_size = 0;
3368 }
3369 }
3370
3371 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003372
3373 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003374 * Maximum block count.
3375 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003376 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003377
Adrian Hunter52f53362016-06-29 16:24:15 +03003378 return 0;
3379
3380unreg:
3381 if (!IS_ERR(mmc->supply.vqmmc))
3382 regulator_disable(mmc->supply.vqmmc);
3383undma:
3384 if (host->align_buffer)
3385 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3386 host->adma_table_sz, host->align_buffer,
3387 host->align_addr);
3388 host->adma_table = NULL;
3389 host->align_buffer = NULL;
3390
3391 return ret;
3392}
3393EXPORT_SYMBOL_GPL(sdhci_setup_host);
3394
3395int __sdhci_add_host(struct sdhci_host *host)
3396{
3397 struct mmc_host *mmc = host->mmc;
3398 int ret;
3399
Pierre Ossman55db8902006-11-21 17:55:45 +01003400 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003401 * Init tasklets.
3402 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003403 tasklet_init(&host->finish_tasklet,
3404 sdhci_tasklet_finish, (unsigned long)host);
3405
Al Viroe4cad1b2006-10-10 22:47:07 +01003406 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003407
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003408 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303409
Shawn Guo2af502c2013-07-05 14:38:55 +08003410 sdhci_init(host, 0);
3411
Russell King781e9892014-04-25 12:55:46 +01003412 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3413 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003414 if (ret) {
3415 pr_err("%s: Failed to request IRQ %d: %d\n",
3416 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003417 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003418 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003419
Pierre Ossmand129bce2006-03-24 03:18:17 -08003420#ifdef CONFIG_MMC_DEBUG
3421 sdhci_dumpregs(host);
3422#endif
3423
Adrian Hunter061d17a2016-04-12 14:25:09 +03003424 ret = sdhci_led_register(host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003425 if (ret) {
3426 pr_err("%s: Failed to register LED device: %d\n",
3427 mmc_hostname(mmc), ret);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003428 goto unirq;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003429 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003430
Pierre Ossman5f25a662006-10-04 02:15:39 -07003431 mmiowb();
3432
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003433 ret = mmc_add_host(mmc);
3434 if (ret)
3435 goto unled;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003436
Girish K Sa3c76eb2011-10-11 11:44:09 +05303437 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003438 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003439 (host->flags & SDHCI_USE_ADMA) ?
3440 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003441 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003442
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003443 sdhci_enable_card_detection(host);
3444
Pierre Ossmand129bce2006-03-24 03:18:17 -08003445 return 0;
3446
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003447unled:
Adrian Hunter061d17a2016-04-12 14:25:09 +03003448 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003449unirq:
Russell King03231f92014-04-25 12:57:12 +01003450 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003451 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3452 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003453 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003454untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003455 tasklet_kill(&host->finish_tasklet);
Adrian Hunter52f53362016-06-29 16:24:15 +03003456
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003457 if (!IS_ERR(mmc->supply.vqmmc))
3458 regulator_disable(mmc->supply.vqmmc);
Adrian Hunter52f53362016-06-29 16:24:15 +03003459
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003460 if (host->align_buffer)
3461 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3462 host->adma_table_sz, host->align_buffer,
3463 host->align_addr);
3464 host->adma_table = NULL;
3465 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003466
3467 return ret;
3468}
Adrian Hunter52f53362016-06-29 16:24:15 +03003469EXPORT_SYMBOL_GPL(__sdhci_add_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003470
Adrian Hunter52f53362016-06-29 16:24:15 +03003471int sdhci_add_host(struct sdhci_host *host)
3472{
3473 int ret;
3474
3475 ret = sdhci_setup_host(host);
3476 if (ret)
3477 return ret;
3478
3479 return __sdhci_add_host(host);
3480}
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003481EXPORT_SYMBOL_GPL(sdhci_add_host);
3482
Pierre Ossman1e728592008-04-16 19:13:13 +02003483void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003484{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003485 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003486 unsigned long flags;
3487
3488 if (dead) {
3489 spin_lock_irqsave(&host->lock, flags);
3490
3491 host->flags |= SDHCI_DEVICE_DEAD;
3492
3493 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303494 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003495 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003496
3497 host->mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03003498 sdhci_finish_mrq(host, host->mrq);
Pierre Ossman1e728592008-04-16 19:13:13 +02003499 }
3500
3501 spin_unlock_irqrestore(&host->lock, flags);
3502 }
3503
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003504 sdhci_disable_card_detection(host);
3505
Markus Mayer4e743f12014-07-03 13:27:42 -07003506 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003507
Adrian Hunter061d17a2016-04-12 14:25:09 +03003508 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003509
Pierre Ossman1e728592008-04-16 19:13:13 +02003510 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003511 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003512
Russell Kingb537f942014-04-25 12:56:01 +01003513 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3514 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003515 free_irq(host->irq, host);
3516
3517 del_timer_sync(&host->timer);
3518
Pierre Ossmand129bce2006-03-24 03:18:17 -08003519 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003520
Tim Kryger3a48edc2014-06-13 10:13:56 -07003521 if (!IS_ERR(mmc->supply.vqmmc))
3522 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003523
Russell Kingedd63fc2016-01-26 13:39:50 +00003524 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003525 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3526 host->adma_table_sz, host->align_buffer,
3527 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003528
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003529 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003530 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003531}
3532
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003533EXPORT_SYMBOL_GPL(sdhci_remove_host);
3534
3535void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003536{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003537 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003538}
3539
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003540EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003541
3542/*****************************************************************************\
3543 * *
3544 * Driver init/exit *
3545 * *
3546\*****************************************************************************/
3547
3548static int __init sdhci_drv_init(void)
3549{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303550 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003551 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303552 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003553
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003554 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003555}
3556
3557static void __exit sdhci_drv_exit(void)
3558{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003559}
3560
3561module_init(sdhci_drv_init);
3562module_exit(sdhci_drv_exit);
3563
Pierre Ossmandf673b22006-06-30 02:22:31 -07003564module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003565module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003566
Pierre Ossman32710e82009-04-08 20:14:54 +02003567MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003568MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003569MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003570
Pierre Ossmandf673b22006-06-30 02:22:31 -07003571MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003572MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");