blob: 229b467a409ad14342abfbb07bde731bf7fabc0a [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 Lu473b095a2012-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
Adrian Hunter56a590d2016-06-29 16:24:32 +0300112static inline bool sdhci_data_line_cmd(struct mmc_command *cmd)
113{
114 return cmd->data || cmd->flags & MMC_RSP_BUSY;
115}
116
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300117static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
118{
Russell King5b4f1f62014-04-25 12:57:02 +0100119 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300120
Adrian Hunterc79396c2011-12-27 15:48:42 +0200121 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Jaehoon Chung860951c2016-06-21 10:13:26 +0900122 !mmc_card_is_removable(host->mmc))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300123 return;
124
Russell King5b4f1f62014-04-25 12:57:02 +0100125 if (enable) {
126 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
127 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800128
Russell King5b4f1f62014-04-25 12:57:02 +0100129 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
130 SDHCI_INT_CARD_INSERT;
131 } else {
132 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
133 }
Russell Kingb537f942014-04-25 12:56:01 +0100134
135 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
136 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300137}
138
139static void sdhci_enable_card_detection(struct sdhci_host *host)
140{
141 sdhci_set_card_detection(host, true);
142}
143
144static void sdhci_disable_card_detection(struct sdhci_host *host)
145{
146 sdhci_set_card_detection(host, false);
147}
148
Ulf Hansson02d0b682016-04-11 15:32:41 +0200149static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
150{
151 if (host->bus_on)
152 return;
153 host->bus_on = true;
154 pm_runtime_get_noresume(host->mmc->parent);
155}
156
157static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
158{
159 if (!host->bus_on)
160 return;
161 host->bus_on = false;
162 pm_runtime_put_noidle(host->mmc->parent);
163}
164
Russell King03231f92014-04-25 12:57:12 +0100165void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800166{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700167 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800168
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300169 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800170
Adrian Hunterf0710a52013-05-06 12:17:32 +0300171 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800172 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300173 /* Reset-all turns off SD Bus Power */
174 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
175 sdhci_runtime_pm_bus_off(host);
176 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800177
Pierre Ossmane16514d82006-06-30 02:22:24 -0700178 /* Wait max 100 ms */
179 timeout = 100;
180
181 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300182 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700183 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530184 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700185 mmc_hostname(host->mmc), (int)mask);
186 sdhci_dumpregs(host);
187 return;
188 }
189 timeout--;
190 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800191 }
Russell King03231f92014-04-25 12:57:12 +0100192}
193EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300194
Russell King03231f92014-04-25 12:57:12 +0100195static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
196{
197 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Adrian Hunterd3940f22016-06-29 16:24:14 +0300198 struct mmc_host *mmc = host->mmc;
199
200 if (!mmc->ops->get_cd(mmc))
Russell King03231f92014-04-25 12:57:12 +0100201 return;
202 }
203
204 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800205
Russell Kingda91a8f2014-04-25 13:00:12 +0100206 if (mask & SDHCI_RESET_ALL) {
207 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
208 if (host->ops->enable_dma)
209 host->ops->enable_dma(host);
210 }
211
212 /* Resetting the controller clears many */
213 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800214 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800215}
216
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800217static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800218{
Adrian Hunterd3940f22016-06-29 16:24:14 +0300219 struct mmc_host *mmc = host->mmc;
220
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800221 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100222 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800223 else
Russell King03231f92014-04-25 12:57:12 +0100224 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800225
Russell Kingb537f942014-04-25 12:56:01 +0100226 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
227 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
228 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
229 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
230 SDHCI_INT_RESPONSE;
231
232 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
233 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800234
235 if (soft) {
236 /* force clock reconfiguration */
237 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +0300238 mmc->ops->set_ios(mmc, &mmc->ios);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800239 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300240}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800241
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300242static void sdhci_reinit(struct sdhci_host *host)
243{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800244 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300245 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800246}
247
Adrian Hunter061d17a2016-04-12 14:25:09 +0300248static void __sdhci_led_activate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800249{
250 u8 ctrl;
251
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300252 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800253 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300254 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800255}
256
Adrian Hunter061d17a2016-04-12 14:25:09 +0300257static void __sdhci_led_deactivate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800258{
259 u8 ctrl;
260
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300261 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800262 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300263 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800264}
265
Masahiro Yamada4f782302016-04-14 13:19:39 +0900266#if IS_REACHABLE(CONFIG_LEDS_CLASS)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100267static void sdhci_led_control(struct led_classdev *led,
Adrian Hunter061d17a2016-04-12 14:25:09 +0300268 enum led_brightness brightness)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100269{
270 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
271 unsigned long flags;
272
273 spin_lock_irqsave(&host->lock, flags);
274
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300275 if (host->runtime_suspended)
276 goto out;
277
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100278 if (brightness == LED_OFF)
Adrian Hunter061d17a2016-04-12 14:25:09 +0300279 __sdhci_led_deactivate(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100280 else
Adrian Hunter061d17a2016-04-12 14:25:09 +0300281 __sdhci_led_activate(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300282out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100283 spin_unlock_irqrestore(&host->lock, flags);
284}
Adrian Hunter061d17a2016-04-12 14:25:09 +0300285
286static int sdhci_led_register(struct sdhci_host *host)
287{
288 struct mmc_host *mmc = host->mmc;
289
290 snprintf(host->led_name, sizeof(host->led_name),
291 "%s::", mmc_hostname(mmc));
292
293 host->led.name = host->led_name;
294 host->led.brightness = LED_OFF;
295 host->led.default_trigger = mmc_hostname(mmc);
296 host->led.brightness_set = sdhci_led_control;
297
298 return led_classdev_register(mmc_dev(mmc), &host->led);
299}
300
301static void sdhci_led_unregister(struct sdhci_host *host)
302{
303 led_classdev_unregister(&host->led);
304}
305
306static inline void sdhci_led_activate(struct sdhci_host *host)
307{
308}
309
310static inline void sdhci_led_deactivate(struct sdhci_host *host)
311{
312}
313
314#else
315
316static inline int sdhci_led_register(struct sdhci_host *host)
317{
318 return 0;
319}
320
321static inline void sdhci_led_unregister(struct sdhci_host *host)
322{
323}
324
325static inline void sdhci_led_activate(struct sdhci_host *host)
326{
327 __sdhci_led_activate(host);
328}
329
330static inline void sdhci_led_deactivate(struct sdhci_host *host)
331{
332 __sdhci_led_deactivate(host);
333}
334
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100335#endif
336
Pierre Ossmand129bce2006-03-24 03:18:17 -0800337/*****************************************************************************\
338 * *
339 * Core functions *
340 * *
341\*****************************************************************************/
342
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100343static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800344{
Pierre Ossman76591502008-07-21 00:32:11 +0200345 unsigned long flags;
346 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700347 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200348 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800349
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100350 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800351
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100352 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200353 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800354
Pierre Ossman76591502008-07-21 00:32:11 +0200355 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800356
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100357 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300358 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800359
Pierre Ossman76591502008-07-21 00:32:11 +0200360 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800361
Pierre Ossman76591502008-07-21 00:32:11 +0200362 blksize -= len;
363 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200364
Pierre Ossman76591502008-07-21 00:32:11 +0200365 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800366
Pierre Ossman76591502008-07-21 00:32:11 +0200367 while (len) {
368 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300369 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200370 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800371 }
Pierre Ossman76591502008-07-21 00:32:11 +0200372
373 *buf = scratch & 0xFF;
374
375 buf++;
376 scratch >>= 8;
377 chunk--;
378 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800379 }
380 }
Pierre Ossman76591502008-07-21 00:32:11 +0200381
382 sg_miter_stop(&host->sg_miter);
383
384 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100385}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800386
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100387static void sdhci_write_block_pio(struct sdhci_host *host)
388{
Pierre Ossman76591502008-07-21 00:32:11 +0200389 unsigned long flags;
390 size_t blksize, len, chunk;
391 u32 scratch;
392 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100393
394 DBG("PIO writing\n");
395
396 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200397 chunk = 0;
398 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100399
Pierre Ossman76591502008-07-21 00:32:11 +0200400 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100401
402 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300403 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100404
Pierre Ossman76591502008-07-21 00:32:11 +0200405 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200406
Pierre Ossman76591502008-07-21 00:32:11 +0200407 blksize -= len;
408 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100409
Pierre Ossman76591502008-07-21 00:32:11 +0200410 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100411
Pierre Ossman76591502008-07-21 00:32:11 +0200412 while (len) {
413 scratch |= (u32)*buf << (chunk * 8);
414
415 buf++;
416 chunk++;
417 len--;
418
419 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300420 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200421 chunk = 0;
422 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100423 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100424 }
425 }
Pierre Ossman76591502008-07-21 00:32:11 +0200426
427 sg_miter_stop(&host->sg_miter);
428
429 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100430}
431
432static void sdhci_transfer_pio(struct sdhci_host *host)
433{
434 u32 mask;
435
Pierre Ossman76591502008-07-21 00:32:11 +0200436 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100437 return;
438
439 if (host->data->flags & MMC_DATA_READ)
440 mask = SDHCI_DATA_AVAILABLE;
441 else
442 mask = SDHCI_SPACE_AVAILABLE;
443
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200444 /*
445 * Some controllers (JMicron JMB38x) mess up the buffer bits
446 * for transfers < 4 bytes. As long as it is just one block,
447 * we can ignore the bits.
448 */
449 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
450 (host->data->blocks == 1))
451 mask = ~0;
452
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300453 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300454 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
455 udelay(100);
456
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100457 if (host->data->flags & MMC_DATA_READ)
458 sdhci_read_block_pio(host);
459 else
460 sdhci_write_block_pio(host);
461
Pierre Ossman76591502008-07-21 00:32:11 +0200462 host->blocks--;
463 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100464 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100465 }
466
467 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800468}
469
Russell King48857d92016-01-26 13:40:16 +0000470static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000471 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000472{
473 int sg_count;
474
Russell King94538e52016-01-26 13:40:37 +0000475 /*
476 * If the data buffers are already mapped, return the previous
477 * dma_map_sg() result.
478 */
479 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000480 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000481
482 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
483 data->flags & MMC_DATA_WRITE ?
484 DMA_TO_DEVICE : DMA_FROM_DEVICE);
485
486 if (sg_count == 0)
487 return -ENOSPC;
488
489 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000490 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000491
492 return sg_count;
493}
494
Pierre Ossman2134a922008-06-28 18:28:51 +0200495static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
496{
497 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800498 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200499}
500
501static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
502{
Cong Wang482fce92011-11-27 13:27:00 +0800503 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200504 local_irq_restore(*flags);
505}
506
Adrian Huntere57a5f62014-11-04 12:42:46 +0200507static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
508 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800509{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200510 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800511
Adrian Huntere57a5f62014-11-04 12:42:46 +0200512 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200513 dma_desc->cmd = cpu_to_le16(cmd);
514 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200515 dma_desc->addr_lo = cpu_to_le32((u32)addr);
516
517 if (host->flags & SDHCI_USE_64_BIT_DMA)
518 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800519}
520
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200521static void sdhci_adma_mark_end(void *desc)
522{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200523 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200524
Adrian Huntere57a5f62014-11-04 12:42:46 +0200525 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200526 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200527}
528
Russell King60c64762016-01-26 13:40:22 +0000529static void sdhci_adma_table_pre(struct sdhci_host *host,
530 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200531{
Pierre Ossman2134a922008-06-28 18:28:51 +0200532 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200533 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000534 dma_addr_t addr, align_addr;
535 void *desc, *align;
536 char *buffer;
537 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200538
539 /*
540 * The spec does not specify endianness of descriptor table.
541 * We currently guess that it is LE.
542 */
543
Russell King60c64762016-01-26 13:40:22 +0000544 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200545
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200546 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200547 align = host->align_buffer;
548
549 align_addr = host->align_addr;
550
551 for_each_sg(data->sg, sg, host->sg_count, i) {
552 addr = sg_dma_address(sg);
553 len = sg_dma_len(sg);
554
555 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000556 * The SDHCI specification states that ADMA addresses must
557 * be 32-bit aligned. If they aren't, then we use a bounce
558 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200559 * alignment.
560 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200561 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
562 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200563 if (offset) {
564 if (data->flags & MMC_DATA_WRITE) {
565 buffer = sdhci_kmap_atomic(sg, &flags);
566 memcpy(align, buffer, offset);
567 sdhci_kunmap_atomic(buffer, &flags);
568 }
569
Ben Dooks118cd172010-03-05 13:43:26 -0800570 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200571 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200572 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200573
574 BUG_ON(offset > 65536);
575
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200576 align += SDHCI_ADMA2_ALIGN;
577 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200578
Adrian Hunter76fe3792014-11-04 12:42:42 +0200579 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200580
581 addr += offset;
582 len -= offset;
583 }
584
Pierre Ossman2134a922008-06-28 18:28:51 +0200585 BUG_ON(len > 65536);
586
Adrian Hunter347ea322015-11-26 14:00:48 +0200587 if (len) {
588 /* tran, valid */
589 sdhci_adma_write_desc(host, desc, addr, len,
590 ADMA2_TRAN_VALID);
591 desc += host->desc_sz;
592 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200593
594 /*
595 * If this triggers then we have a calculation bug
596 * somewhere. :/
597 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200598 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200599 }
600
Thomas Abraham70764a92010-05-26 14:42:04 -0700601 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000602 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200603 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200604 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200605 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700606 }
607 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000608 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200609 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700610 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200611}
612
613static void sdhci_adma_table_post(struct sdhci_host *host,
614 struct mmc_data *data)
615{
Pierre Ossman2134a922008-06-28 18:28:51 +0200616 struct scatterlist *sg;
617 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200618 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200619 char *buffer;
620 unsigned long flags;
621
Russell King47fa9612016-01-26 13:40:06 +0000622 if (data->flags & MMC_DATA_READ) {
623 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100624
Russell King47fa9612016-01-26 13:40:06 +0000625 /* Do a quick scan of the SG list for any unaligned mappings */
626 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200627 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000628 has_unaligned = true;
629 break;
630 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200631
Russell King47fa9612016-01-26 13:40:06 +0000632 if (has_unaligned) {
633 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000634 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200635
Russell King47fa9612016-01-26 13:40:06 +0000636 align = host->align_buffer;
637
638 for_each_sg(data->sg, sg, host->sg_count, i) {
639 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
640 size = SDHCI_ADMA2_ALIGN -
641 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
642
643 buffer = sdhci_kmap_atomic(sg, &flags);
644 memcpy(buffer, align, size);
645 sdhci_kunmap_atomic(buffer, &flags);
646
647 align += SDHCI_ADMA2_ALIGN;
648 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200649 }
650 }
651 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200652}
653
Andrei Warkentina3c77782011-04-11 16:13:42 -0500654static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800655{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700656 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500657 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700658 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800659
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200660 /*
661 * If the host controller provides us with an incorrect timeout
662 * value, just skip the check and use 0xE. The hardware may take
663 * longer to time out, but that's much better than having a too-short
664 * timeout value.
665 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200666 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200667 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200668
Andrei Warkentina3c77782011-04-11 16:13:42 -0500669 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100670 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500671 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800672
Andrei Warkentina3c77782011-04-11 16:13:42 -0500673 /* timeout in us */
674 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100675 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300676 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000677 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000678 if (host->clock && data->timeout_clks) {
679 unsigned long long val;
680
681 /*
682 * data->timeout_clks is in units of clock cycles.
683 * host->clock is in Hz. target_timeout is in us.
684 * Hence, us = 1000000 * cycles / Hz. Round up.
685 */
686 val = 1000000 * data->timeout_clks;
687 if (do_div(val, host->clock))
688 target_timeout++;
689 target_timeout += val;
690 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300691 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700692
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700693 /*
694 * Figure out needed cycles.
695 * We do this in steps in order to fit inside a 32 bit int.
696 * The first step is the minimum timeout, which will have a
697 * minimum resolution of 6 bits:
698 * (1) 2^13*1000 > 2^22,
699 * (2) host->timeout_clk < 2^16
700 * =>
701 * (1) / (2) > 2^6
702 */
703 count = 0;
704 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
705 while (current_timeout < target_timeout) {
706 count++;
707 current_timeout <<= 1;
708 if (count >= 0xF)
709 break;
710 }
711
712 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400713 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
714 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700715 count = 0xE;
716 }
717
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200718 return count;
719}
720
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300721static void sdhci_set_transfer_irqs(struct sdhci_host *host)
722{
723 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
724 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
725
726 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100727 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300728 else
Russell Kingb537f942014-04-25 12:56:01 +0100729 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
730
731 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
732 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300733}
734
Aisheng Dongb45e6682014-08-27 15:26:29 +0800735static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200736{
737 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800738
739 if (host->ops->set_timeout) {
740 host->ops->set_timeout(host, cmd);
741 } else {
742 count = sdhci_calc_timeout(host, cmd);
743 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
744 }
745}
746
747static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
748{
Pierre Ossman2134a922008-06-28 18:28:51 +0200749 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500750 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200751
Adrian Hunter56a590d2016-06-29 16:24:32 +0300752 if (sdhci_data_line_cmd(cmd))
Aisheng Dongb45e6682014-08-27 15:26:29 +0800753 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500754
755 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200756 return;
757
Adrian Hunter43dea092016-06-29 16:24:26 +0300758 WARN_ON(host->data);
759
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200760 /* Sanity checks */
761 BUG_ON(data->blksz * data->blocks > 524288);
762 BUG_ON(data->blksz > host->mmc->max_blk_size);
763 BUG_ON(data->blocks > 65535);
764
765 host->data = data;
766 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400767 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200768
Russell Kingfce14422016-01-26 13:41:20 +0000769 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200770 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000771 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000772 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200773
Russell Kingfce14422016-01-26 13:41:20 +0000774 host->flags |= SDHCI_REQ_USE_DMA;
775
776 /*
777 * FIXME: This doesn't account for merging when mapping the
778 * scatterlist.
779 *
780 * The assumption here being that alignment and lengths are
781 * the same after DMA mapping to device address space.
782 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000783 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000784 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200785 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000786 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000787 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000788 /*
789 * As we use up to 3 byte chunks to work
790 * around alignment problems, we need to
791 * check the offset as well.
792 */
793 offset_mask = 3;
794 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200795 } else {
796 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000797 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000798 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
799 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200800 }
801
Russell Kingdf953922016-01-26 13:41:14 +0000802 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200803 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000804 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100805 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000806 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200807 host->flags &= ~SDHCI_REQ_USE_DMA;
808 break;
809 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000810 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100811 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200812 host->flags &= ~SDHCI_REQ_USE_DMA;
813 break;
814 }
815 }
816 }
817 }
818
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200819 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000820 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200821
Russell King60c64762016-01-26 13:40:22 +0000822 if (sg_cnt <= 0) {
823 /*
824 * This only happens when someone fed
825 * us an invalid request.
826 */
827 WARN_ON(1);
828 host->flags &= ~SDHCI_REQ_USE_DMA;
829 } else if (host->flags & SDHCI_USE_ADMA) {
830 sdhci_adma_table_pre(host, data, sg_cnt);
831
832 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
833 if (host->flags & SDHCI_USE_64_BIT_DMA)
834 sdhci_writel(host,
835 (u64)host->adma_addr >> 32,
836 SDHCI_ADMA_ADDRESS_HI);
837 } else {
838 WARN_ON(sg_cnt != 1);
839 sdhci_writel(host, sg_dma_address(data->sg),
840 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200841 }
842 }
843
Pierre Ossman2134a922008-06-28 18:28:51 +0200844 /*
845 * Always adjust the DMA selection as some controllers
846 * (e.g. JMicron) can't do PIO properly when the selection
847 * is ADMA.
848 */
849 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300850 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200851 ctrl &= ~SDHCI_CTRL_DMA_MASK;
852 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200853 (host->flags & SDHCI_USE_ADMA)) {
854 if (host->flags & SDHCI_USE_64_BIT_DMA)
855 ctrl |= SDHCI_CTRL_ADMA64;
856 else
857 ctrl |= SDHCI_CTRL_ADMA32;
858 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200859 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200860 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300861 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100862 }
863
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200864 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200865 int flags;
866
867 flags = SG_MITER_ATOMIC;
868 if (host->data->flags & MMC_DATA_READ)
869 flags |= SG_MITER_TO_SG;
870 else
871 flags |= SG_MITER_FROM_SG;
872 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200873 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800874 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700875
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300876 sdhci_set_transfer_irqs(host);
877
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400878 /* Set the DMA boundary value and block size */
879 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
880 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300881 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700882}
883
Adrian Hunter0293d502016-06-29 16:24:35 +0300884static inline bool sdhci_auto_cmd12(struct sdhci_host *host,
885 struct mmc_request *mrq)
886{
887 return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12);
888}
889
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700890static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500891 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700892{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800893 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500894 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700895
Dong Aisheng2b558c12013-10-30 22:09:48 +0800896 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800897 if (host->quirks2 &
898 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
899 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
900 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800901 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800902 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
903 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800904 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800905 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700906 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800907 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700908
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200909 WARN_ON(!host->data);
910
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800911 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
912 mode = SDHCI_TRNS_BLK_CNT_EN;
913
Andrei Warkentine89d4562011-05-23 15:06:37 -0500914 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800915 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500916 /*
917 * If we are sending CMD23, CMD12 never gets sent
918 * on successful completion (so no Auto-CMD12).
919 */
Adrian Hunter0293d502016-06-29 16:24:35 +0300920 if (sdhci_auto_cmd12(host, cmd->mrq) &&
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800921 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500922 mode |= SDHCI_TRNS_AUTO_CMD12;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300923 else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500924 mode |= SDHCI_TRNS_AUTO_CMD23;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300925 sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500926 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700927 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500928
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700929 if (data->flags & MMC_DATA_READ)
930 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100931 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700932 mode |= SDHCI_TRNS_DMA;
933
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300934 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800935}
936
Adrian Hunter0cc563c2016-06-29 16:24:28 +0300937static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
938{
939 return (!(host->flags & SDHCI_DEVICE_DEAD) &&
940 ((mrq->cmd && mrq->cmd->error) ||
941 (mrq->sbc && mrq->sbc->error) ||
942 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
943 (mrq->data->stop && mrq->data->stop->error))) ||
944 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
945}
946
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +0300947static void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
948{
949 int i;
950
951 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
952 if (host->mrqs_done[i] == mrq) {
953 WARN_ON(1);
954 return;
955 }
956 }
957
958 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
959 if (!host->mrqs_done[i]) {
960 host->mrqs_done[i] = mrq;
961 break;
962 }
963 }
964
965 WARN_ON(i >= SDHCI_MAX_MRQS);
966
967 tasklet_schedule(&host->finish_tasklet);
968}
969
Adrian Huntera6d3bdd2016-06-29 16:24:27 +0300970static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
971{
Adrian Hunter5a8a3fe2016-06-29 16:24:30 +0300972 if (host->cmd && host->cmd->mrq == mrq)
973 host->cmd = NULL;
974
975 if (host->data_cmd && host->data_cmd->mrq == mrq)
976 host->data_cmd = NULL;
977
978 if (host->data && host->data->mrq == mrq)
979 host->data = NULL;
980
Adrian Huntered1563d2016-06-29 16:24:29 +0300981 if (sdhci_needs_reset(host, mrq))
982 host->pending_reset = true;
983
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +0300984 __sdhci_finish_mrq(host, mrq);
Adrian Huntera6d3bdd2016-06-29 16:24:27 +0300985}
986
Pierre Ossmand129bce2006-03-24 03:18:17 -0800987static void sdhci_finish_data(struct sdhci_host *host)
988{
989 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800990
Pierre Ossmand129bce2006-03-24 03:18:17 -0800991 data = host->data;
992 host->data = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +0300993 host->data_cmd = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994
Russell Kingadd89132016-01-26 13:40:42 +0000995 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
996 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
997 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998
999 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001000 * The specification states that the block count register must
1001 * be updated, but it does not specify at what point in the
1002 * data flow. That makes the register entirely useless to read
1003 * back so we have to assume that nothing made it to the card
1004 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001005 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001006 if (data->error)
1007 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001008 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001009 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001010
Andrei Warkentine89d4562011-05-23 15:06:37 -05001011 /*
1012 * Need to send CMD12 if -
1013 * a) open-ended multiblock transfer (no CMD23)
1014 * b) error in multiblock transfer
1015 */
1016 if (data->stop &&
1017 (data->error ||
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001018 !data->mrq->sbc)) {
Andrei Warkentine89d4562011-05-23 15:06:37 -05001019
Pierre Ossmand129bce2006-03-24 03:18:17 -08001020 /*
1021 * The controller needs a reset of internal state machines
1022 * upon error conditions.
1023 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001024 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +01001025 sdhci_do_reset(host, SDHCI_RESET_CMD);
1026 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001027 }
1028
1029 sdhci_send_command(host, data->stop);
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001030 } else {
1031 sdhci_finish_mrq(host, data->mrq);
1032 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033}
1034
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001035static void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq,
1036 unsigned long timeout)
1037{
1038 if (sdhci_data_line_cmd(mrq->cmd))
1039 mod_timer(&host->data_timer, timeout);
1040 else
1041 mod_timer(&host->timer, timeout);
1042}
1043
1044static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq)
1045{
1046 if (sdhci_data_line_cmd(mrq->cmd))
1047 del_timer(&host->data_timer);
1048 else
1049 del_timer(&host->timer);
1050}
1051
Dong Aishengc0e551292013-09-13 19:11:31 +08001052void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001053{
1054 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001055 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001056 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057
1058 WARN_ON(host->cmd);
1059
Russell King96776202016-01-26 13:39:34 +00001060 /* Initially, a command has no error */
1061 cmd->error = 0;
1062
Pierre Ossmand129bce2006-03-24 03:18:17 -08001063 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001064 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001065
1066 mask = SDHCI_CMD_INHIBIT;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001067 if (sdhci_data_line_cmd(cmd))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001068 mask |= SDHCI_DATA_INHIBIT;
1069
1070 /* We shouldn't wait for data inihibit for stop commands, even
1071 though they might use busy signaling */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001072 if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001073 mask &= ~SDHCI_DATA_INHIBIT;
1074
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001075 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001076 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001077 pr_err("%s: Controller never released inhibit bit(s).\n",
1078 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001079 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001080 cmd->error = -EIO;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001081 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001082 return;
1083 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001084 timeout--;
1085 mdelay(1);
1086 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001087
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001088 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001089 if (!cmd->data && cmd->busy_timeout > 9000)
1090 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001091 else
1092 timeout += 10 * HZ;
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001093 sdhci_mod_timer(host, cmd->mrq, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001094
1095 host->cmd = cmd;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001096 if (sdhci_data_line_cmd(cmd)) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001097 WARN_ON(host->data_cmd);
1098 host->data_cmd = cmd;
1099 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001100
Andrei Warkentina3c77782011-04-11 16:13:42 -05001101 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001102
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001103 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001104
Andrei Warkentine89d4562011-05-23 15:06:37 -05001105 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001106
Pierre Ossmand129bce2006-03-24 03:18:17 -08001107 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301108 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001109 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001110 cmd->error = -EINVAL;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001111 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001112 return;
1113 }
1114
1115 if (!(cmd->flags & MMC_RSP_PRESENT))
1116 flags = SDHCI_CMD_RESP_NONE;
1117 else if (cmd->flags & MMC_RSP_136)
1118 flags = SDHCI_CMD_RESP_LONG;
1119 else if (cmd->flags & MMC_RSP_BUSY)
1120 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1121 else
1122 flags = SDHCI_CMD_RESP_SHORT;
1123
1124 if (cmd->flags & MMC_RSP_CRC)
1125 flags |= SDHCI_CMD_CRC;
1126 if (cmd->flags & MMC_RSP_OPCODE)
1127 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301128
1129 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301130 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1131 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001132 flags |= SDHCI_CMD_DATA;
1133
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001134 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001135}
Dong Aishengc0e551292013-09-13 19:11:31 +08001136EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001137
1138static void sdhci_finish_command(struct sdhci_host *host)
1139{
Adrian Huntere0a56402016-06-29 16:24:22 +03001140 struct mmc_command *cmd = host->cmd;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001141 int i;
1142
Adrian Huntere0a56402016-06-29 16:24:22 +03001143 host->cmd = NULL;
1144
1145 if (cmd->flags & MMC_RSP_PRESENT) {
1146 if (cmd->flags & MMC_RSP_136) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001147 /* CRC is stripped so we need to do some shifting. */
1148 for (i = 0;i < 4;i++) {
Adrian Huntere0a56402016-06-29 16:24:22 +03001149 cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001150 SDHCI_RESPONSE + (3-i)*4) << 8;
1151 if (i != 3)
Adrian Huntere0a56402016-06-29 16:24:22 +03001152 cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001153 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001154 SDHCI_RESPONSE + (3-i)*4-1);
1155 }
1156 } else {
Adrian Huntere0a56402016-06-29 16:24:22 +03001157 cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001158 }
1159 }
1160
Adrian Hunter6bde8682016-06-29 16:24:20 +03001161 /*
1162 * The host can send and interrupt when the busy state has
1163 * ended, allowing us to wait without wasting CPU cycles.
1164 * The busy signal uses DAT0 so this is similar to waiting
1165 * for data to complete.
1166 *
1167 * Note: The 1.0 specification is a bit ambiguous about this
1168 * feature so there might be some problems with older
1169 * controllers.
1170 */
Adrian Huntere0a56402016-06-29 16:24:22 +03001171 if (cmd->flags & MMC_RSP_BUSY) {
1172 if (cmd->data) {
Adrian Hunter6bde8682016-06-29 16:24:20 +03001173 DBG("Cannot wait for busy signal when also doing a data transfer");
1174 } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
Adrian Hunterea968022016-06-29 16:24:24 +03001175 cmd == host->data_cmd) {
1176 /* Command complete before busy is ended */
Adrian Hunter6bde8682016-06-29 16:24:20 +03001177 return;
1178 }
1179 }
1180
Andrei Warkentine89d4562011-05-23 15:06:37 -05001181 /* Finished CMD23, now send actual command. */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001182 if (cmd == cmd->mrq->sbc) {
1183 sdhci_send_command(host, cmd->mrq->cmd);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001184 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001185
Andrei Warkentine89d4562011-05-23 15:06:37 -05001186 /* Processed actual command. */
1187 if (host->data && host->data_early)
1188 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001189
Adrian Huntere0a56402016-06-29 16:24:22 +03001190 if (!cmd->data)
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001191 sdhci_finish_mrq(host, cmd->mrq);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001192 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001193}
1194
Kevin Liu52983382013-01-31 11:31:37 +08001195static u16 sdhci_get_preset_value(struct sdhci_host *host)
1196{
Russell Kingd975f122014-04-25 12:59:31 +01001197 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001198
Russell Kingd975f122014-04-25 12:59:31 +01001199 switch (host->timing) {
1200 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001201 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1202 break;
Russell Kingd975f122014-04-25 12:59:31 +01001203 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001204 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1205 break;
Russell Kingd975f122014-04-25 12:59:31 +01001206 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001207 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1208 break;
Russell Kingd975f122014-04-25 12:59:31 +01001209 case MMC_TIMING_UHS_SDR104:
1210 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001211 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1212 break;
Russell Kingd975f122014-04-25 12:59:31 +01001213 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001214 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001215 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1216 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001217 case MMC_TIMING_MMC_HS400:
1218 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1219 break;
Kevin Liu52983382013-01-31 11:31:37 +08001220 default:
1221 pr_warn("%s: Invalid UHS-I mode selected\n",
1222 mmc_hostname(host->mmc));
1223 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1224 break;
1225 }
1226 return preset;
1227}
1228
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001229u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1230 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001231{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301232 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001233 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301234 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001235 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001236
Zhangfei Gao85105c52010-08-06 07:10:01 +08001237 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001238 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001239 u16 pre_val;
1240
1241 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1242 pre_val = sdhci_get_preset_value(host);
1243 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1244 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1245 if (host->clk_mul &&
1246 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1247 clk = SDHCI_PROG_CLOCK_MODE;
1248 real_div = div + 1;
1249 clk_mul = host->clk_mul;
1250 } else {
1251 real_div = max_t(int, 1, div << 1);
1252 }
1253 goto clock_set;
1254 }
1255
Arindam Nathc3ed3872011-05-05 12:19:06 +05301256 /*
1257 * Check if the Host Controller supports Programmable Clock
1258 * Mode.
1259 */
1260 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001261 for (div = 1; div <= 1024; div++) {
1262 if ((host->max_clk * host->clk_mul / div)
1263 <= clock)
1264 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001265 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001266 if ((host->max_clk * host->clk_mul / div) <= clock) {
1267 /*
1268 * Set Programmable Clock Mode in the Clock
1269 * Control register.
1270 */
1271 clk = SDHCI_PROG_CLOCK_MODE;
1272 real_div = div;
1273 clk_mul = host->clk_mul;
1274 div--;
1275 } else {
1276 /*
1277 * Divisor can be too small to reach clock
1278 * speed requirement. Then use the base clock.
1279 */
1280 switch_base_clk = true;
1281 }
1282 }
1283
1284 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301285 /* Version 3.00 divisors must be a multiple of 2. */
1286 if (host->max_clk <= clock)
1287 div = 1;
1288 else {
1289 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1290 div += 2) {
1291 if ((host->max_clk / div) <= clock)
1292 break;
1293 }
1294 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001295 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301296 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301297 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1298 && !div && host->max_clk <= 25000000)
1299 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001300 }
1301 } else {
1302 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001303 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001304 if ((host->max_clk / div) <= clock)
1305 break;
1306 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001307 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301308 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001309 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001310
Kevin Liu52983382013-01-31 11:31:37 +08001311clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001312 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001313 *actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301314 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001315 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1316 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001317
1318 return clk;
1319}
1320EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1321
1322void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1323{
1324 u16 clk;
1325 unsigned long timeout;
1326
1327 host->mmc->actual_clock = 0;
1328
1329 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001330
1331 if (clock == 0)
1332 return;
1333
1334 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1335
Pierre Ossmand129bce2006-03-24 03:18:17 -08001336 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001337 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001338
Chris Ball27f6cb12009-09-22 16:45:31 -07001339 /* Wait max 20 ms */
1340 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001341 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001342 & SDHCI_CLOCK_INT_STABLE)) {
1343 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001344 pr_err("%s: Internal clock never stabilised.\n",
1345 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001346 sdhci_dumpregs(host);
1347 return;
1348 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001349 timeout--;
1350 mdelay(1);
1351 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001352
1353 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001354 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001355}
Russell King17710592014-04-25 12:58:55 +01001356EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001357
Adrian Hunter1dceb042016-03-29 12:45:43 +03001358static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1359 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001360{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001361 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001362
1363 spin_unlock_irq(&host->lock);
1364 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1365 spin_lock_irq(&host->lock);
1366
1367 if (mode != MMC_POWER_OFF)
1368 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1369 else
1370 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1371}
1372
1373void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1374 unsigned short vdd)
1375{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001376 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001377
Russell King24fbb3c2014-04-25 13:00:06 +01001378 if (mode != MMC_POWER_OFF) {
1379 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001380 case MMC_VDD_165_195:
1381 pwr = SDHCI_POWER_180;
1382 break;
1383 case MMC_VDD_29_30:
1384 case MMC_VDD_30_31:
1385 pwr = SDHCI_POWER_300;
1386 break;
1387 case MMC_VDD_32_33:
1388 case MMC_VDD_33_34:
1389 pwr = SDHCI_POWER_330;
1390 break;
1391 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001392 WARN(1, "%s: Invalid vdd %#x\n",
1393 mmc_hostname(host->mmc), vdd);
1394 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001395 }
1396 }
1397
1398 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001399 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001400
Pierre Ossmanae628902009-05-03 20:45:03 +02001401 host->pwr = pwr;
1402
1403 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001404 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001405 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1406 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001407 } else {
1408 /*
1409 * Spec says that we should clear the power reg before setting
1410 * a new value. Some controllers don't seem to like this though.
1411 */
1412 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1413 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001414
Russell Kinge921a8b2014-04-25 13:00:01 +01001415 /*
1416 * At least the Marvell CaFe chip gets confused if we set the
1417 * voltage and set turn on power at the same time, so set the
1418 * voltage first.
1419 */
1420 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1421 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001422
Russell Kinge921a8b2014-04-25 13:00:01 +01001423 pwr |= SDHCI_POWER_ON;
1424
Pierre Ossmanae628902009-05-03 20:45:03 +02001425 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1426
Russell Kinge921a8b2014-04-25 13:00:01 +01001427 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1428 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001429
Russell Kinge921a8b2014-04-25 13:00:01 +01001430 /*
1431 * Some controllers need an extra 10ms delay of 10ms before
1432 * they can apply clock after applying power
1433 */
1434 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1435 mdelay(10);
1436 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001437}
1438EXPORT_SYMBOL_GPL(sdhci_set_power);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001439
Adrian Hunter1dceb042016-03-29 12:45:43 +03001440static void __sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1441 unsigned short vdd)
1442{
1443 struct mmc_host *mmc = host->mmc;
1444
1445 if (host->ops->set_power)
1446 host->ops->set_power(host, mode, vdd);
1447 else if (!IS_ERR(mmc->supply.vmmc))
1448 sdhci_set_power_reg(host, mode, vdd);
1449 else
1450 sdhci_set_power(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001451}
1452
Pierre Ossmand129bce2006-03-24 03:18:17 -08001453/*****************************************************************************\
1454 * *
1455 * MMC callbacks *
1456 * *
1457\*****************************************************************************/
1458
1459static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1460{
1461 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001462 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001463 unsigned long flags;
1464
1465 host = mmc_priv(mmc);
1466
Scott Branden04e079c2015-03-10 11:35:10 -07001467 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001468 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001469
Pierre Ossmand129bce2006-03-24 03:18:17 -08001470 spin_lock_irqsave(&host->lock, flags);
1471
Adrian Hunter061d17a2016-04-12 14:25:09 +03001472 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001473
1474 /*
1475 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1476 * requests if Auto-CMD12 is enabled.
1477 */
Adrian Hunter0293d502016-06-29 16:24:35 +03001478 if (sdhci_auto_cmd12(host, mrq)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001479 if (mrq->stop) {
1480 mrq->data->stop = NULL;
1481 mrq->stop = NULL;
1482 }
1483 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001484
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001485 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001486 mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001487 sdhci_finish_mrq(host, mrq);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301488 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001489 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001490 sdhci_send_command(host, mrq->sbc);
1491 else
1492 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301493 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001494
Pierre Ossman5f25a662006-10-04 02:15:39 -07001495 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001496 spin_unlock_irqrestore(&host->lock, flags);
1497}
1498
Russell King2317f562014-04-25 12:57:07 +01001499void sdhci_set_bus_width(struct sdhci_host *host, int width)
1500{
1501 u8 ctrl;
1502
1503 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1504 if (width == MMC_BUS_WIDTH_8) {
1505 ctrl &= ~SDHCI_CTRL_4BITBUS;
1506 if (host->version >= SDHCI_SPEC_300)
1507 ctrl |= SDHCI_CTRL_8BITBUS;
1508 } else {
1509 if (host->version >= SDHCI_SPEC_300)
1510 ctrl &= ~SDHCI_CTRL_8BITBUS;
1511 if (width == MMC_BUS_WIDTH_4)
1512 ctrl |= SDHCI_CTRL_4BITBUS;
1513 else
1514 ctrl &= ~SDHCI_CTRL_4BITBUS;
1515 }
1516 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1517}
1518EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1519
Russell King96d7b782014-04-25 12:59:26 +01001520void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1521{
1522 u16 ctrl_2;
1523
1524 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1525 /* Select Bus Speed Mode for host */
1526 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1527 if ((timing == MMC_TIMING_MMC_HS200) ||
1528 (timing == MMC_TIMING_UHS_SDR104))
1529 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1530 else if (timing == MMC_TIMING_UHS_SDR12)
1531 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1532 else if (timing == MMC_TIMING_UHS_SDR25)
1533 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1534 else if (timing == MMC_TIMING_UHS_SDR50)
1535 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1536 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1537 (timing == MMC_TIMING_MMC_DDR52))
1538 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001539 else if (timing == MMC_TIMING_MMC_HS400)
1540 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001541 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1542}
1543EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1544
Dong Aishengded97e02016-04-16 01:29:25 +08001545static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001546{
Dong Aishengded97e02016-04-16 01:29:25 +08001547 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001548 unsigned long flags;
1549 u8 ctrl;
1550
Pierre Ossmand129bce2006-03-24 03:18:17 -08001551 spin_lock_irqsave(&host->lock, flags);
1552
Adrian Hunterceb61432011-12-27 15:48:41 +02001553 if (host->flags & SDHCI_DEVICE_DEAD) {
1554 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001555 if (!IS_ERR(mmc->supply.vmmc) &&
1556 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001557 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001558 return;
1559 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001560
Pierre Ossmand129bce2006-03-24 03:18:17 -08001561 /*
1562 * Reset the chip on each power off.
1563 * Should clear out any weird states.
1564 */
1565 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001566 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001567 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001568 }
1569
Kevin Liu52983382013-01-31 11:31:37 +08001570 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001571 (ios->power_mode == MMC_POWER_UP) &&
1572 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001573 sdhci_enable_preset_value(host, false);
1574
Russell King373073e2014-04-25 12:58:45 +01001575 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001576 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001577 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001578
1579 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1580 host->clock) {
1581 host->timeout_clk = host->mmc->actual_clock ?
1582 host->mmc->actual_clock / 1000 :
1583 host->clock / 1000;
1584 host->mmc->max_busy_timeout =
1585 host->ops->get_max_timeout_count ?
1586 host->ops->get_max_timeout_count(host) :
1587 1 << 27;
1588 host->mmc->max_busy_timeout /= host->timeout_clk;
1589 }
Russell King373073e2014-04-25 12:58:45 +01001590 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001591
Adrian Hunter1dceb042016-03-29 12:45:43 +03001592 __sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001593
Philip Rakity643a81f2010-09-23 08:24:32 -07001594 if (host->ops->platform_send_init_74_clocks)
1595 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1596
Russell King2317f562014-04-25 12:57:07 +01001597 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001598
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001599 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001600
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001601 if ((ios->timing == MMC_TIMING_SD_HS ||
1602 ios->timing == MMC_TIMING_MMC_HS)
1603 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001604 ctrl |= SDHCI_CTRL_HISPD;
1605 else
1606 ctrl &= ~SDHCI_CTRL_HISPD;
1607
Arindam Nathd6d50a12011-05-05 12:18:59 +05301608 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301609 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301610
1611 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001612 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1613 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001614 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301615 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301616 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1617 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001618 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301619 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301620
Russell Kingda91a8f2014-04-25 13:00:12 +01001621 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301622 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301623 /*
1624 * We only need to set Driver Strength if the
1625 * preset value enable is not set.
1626 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001627 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301628 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1629 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1630 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001631 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1632 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301633 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1634 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001635 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1636 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1637 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001638 pr_warn("%s: invalid driver type, default to driver type B\n",
1639 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001640 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1641 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301642
1643 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301644 } else {
1645 /*
1646 * According to SDHC Spec v3.00, if the Preset Value
1647 * Enable in the Host Control 2 register is set, we
1648 * need to reset SD Clock Enable before changing High
1649 * Speed Enable to avoid generating clock gliches.
1650 */
Arindam Nath758535c2011-05-05 12:19:00 +05301651
1652 /* Reset SD Clock Enable */
1653 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1654 clk &= ~SDHCI_CLOCK_CARD_EN;
1655 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1656
1657 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1658
1659 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001660 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301661 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301662
Arindam Nath49c468f2011-05-05 12:19:01 +05301663 /* Reset SD Clock Enable */
1664 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1665 clk &= ~SDHCI_CLOCK_CARD_EN;
1666 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1667
Russell King96d7b782014-04-25 12:59:26 +01001668 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001669 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301670
Kevin Liu52983382013-01-31 11:31:37 +08001671 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1672 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1673 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1674 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1675 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001676 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1677 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001678 u16 preset;
1679
1680 sdhci_enable_preset_value(host, true);
1681 preset = sdhci_get_preset_value(host);
1682 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1683 >> SDHCI_PRESET_DRV_SHIFT;
1684 }
1685
Arindam Nath49c468f2011-05-05 12:19:01 +05301686 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001687 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301688 } else
1689 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301690
Leandro Dorileob8352262007-07-25 23:47:04 +02001691 /*
1692 * Some (ENE) controllers go apeshit on some ios operation,
1693 * signalling timeout and CRC errors even on CMD0. Resetting
1694 * it on each ios seems to solve the problem.
1695 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301696 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001697 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001698
Pierre Ossman5f25a662006-10-04 02:15:39 -07001699 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001700 spin_unlock_irqrestore(&host->lock, flags);
1701}
1702
Dong Aishengded97e02016-04-16 01:29:25 +08001703static int sdhci_get_cd(struct mmc_host *mmc)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001704{
1705 struct sdhci_host *host = mmc_priv(mmc);
Dong Aishengded97e02016-04-16 01:29:25 +08001706 int gpio_cd = mmc_gpio_get_cd(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08001707
1708 if (host->flags & SDHCI_DEVICE_DEAD)
1709 return 0;
1710
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001711 /* If nonremovable, assume that the card is always present. */
Jaehoon Chung860951c2016-06-21 10:13:26 +09001712 if (!mmc_card_is_removable(host->mmc))
Kevin Liu94144a42013-02-28 17:35:53 +08001713 return 1;
1714
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001715 /*
1716 * Try slot gpio detect, if defined it take precedence
1717 * over build in controller functionality
1718 */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001719 if (gpio_cd >= 0)
Kevin Liu94144a42013-02-28 17:35:53 +08001720 return !!gpio_cd;
1721
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001722 /* If polling, assume that the card is always present. */
1723 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1724 return 1;
1725
Kevin Liu94144a42013-02-28 17:35:53 +08001726 /* Host native card detect */
1727 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1728}
1729
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001730static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001731{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001732 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001733 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001734
Pierre Ossmand129bce2006-03-24 03:18:17 -08001735 spin_lock_irqsave(&host->lock, flags);
1736
Pierre Ossman1e728592008-04-16 19:13:13 +02001737 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001738 is_readonly = 0;
1739 else if (host->ops->get_ro)
1740 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001741 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001742 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1743 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001744
1745 spin_unlock_irqrestore(&host->lock, flags);
1746
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001747 /* This quirk needs to be replaced by a callback-function later */
1748 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1749 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001750}
1751
Takashi Iwai82b0e232011-04-21 20:26:38 +02001752#define SAMPLE_COUNT 5
1753
Dong Aishengded97e02016-04-16 01:29:25 +08001754static int sdhci_get_ro(struct mmc_host *mmc)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001755{
Dong Aishengded97e02016-04-16 01:29:25 +08001756 struct sdhci_host *host = mmc_priv(mmc);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001757 int i, ro_count;
1758
Takashi Iwai82b0e232011-04-21 20:26:38 +02001759 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001760 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001761
1762 ro_count = 0;
1763 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001764 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001765 if (++ro_count > SAMPLE_COUNT / 2)
1766 return 1;
1767 }
1768 msleep(30);
1769 }
1770 return 0;
1771}
1772
Adrian Hunter20758b62011-08-29 16:42:12 +03001773static void sdhci_hw_reset(struct mmc_host *mmc)
1774{
1775 struct sdhci_host *host = mmc_priv(mmc);
1776
1777 if (host->ops && host->ops->hw_reset)
1778 host->ops->hw_reset(host);
1779}
1780
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001781static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1782{
Russell Kingbe138552014-04-25 12:55:56 +01001783 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001784 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001785 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001786 else
Russell Kingb537f942014-04-25 12:56:01 +01001787 host->ier &= ~SDHCI_INT_CARD_INT;
1788
1789 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1790 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001791 mmiowb();
1792 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001793}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001794
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001795static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1796{
1797 struct sdhci_host *host = mmc_priv(mmc);
1798 unsigned long flags;
1799
1800 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001801 if (enable)
1802 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1803 else
1804 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1805
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001806 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001807 spin_unlock_irqrestore(&host->lock, flags);
1808}
1809
Dong Aishengded97e02016-04-16 01:29:25 +08001810static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1811 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001812{
Dong Aishengded97e02016-04-16 01:29:25 +08001813 struct sdhci_host *host = mmc_priv(mmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07001814 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001815 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001816
1817 /*
1818 * Signal Voltage Switching is only applicable for Host Controllers
1819 * v3.00 and above.
1820 */
1821 if (host->version < SDHCI_SPEC_300)
1822 return 0;
1823
Philip Rakity6231f3d2012-07-23 15:56:23 -07001824 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001825
Fabio Estevam21f59982013-02-14 10:35:03 -02001826 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001827 case MMC_SIGNAL_VOLTAGE_330:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001828 if (!(host->flags & SDHCI_SIGNALING_330))
1829 return -EINVAL;
Kevin Liu20b92a32012-12-17 19:29:26 +08001830 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1831 ctrl &= ~SDHCI_CTRL_VDD_180;
1832 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1833
Tim Kryger3a48edc2014-06-13 10:13:56 -07001834 if (!IS_ERR(mmc->supply.vqmmc)) {
1835 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1836 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001837 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001838 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1839 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001840 return -EIO;
1841 }
1842 }
1843 /* Wait for 5ms */
1844 usleep_range(5000, 5500);
1845
1846 /* 3.3V regulator output should be stable within 5 ms */
1847 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1848 if (!(ctrl & SDHCI_CTRL_VDD_180))
1849 return 0;
1850
Joe Perches66061102014-09-12 14:56:56 -07001851 pr_warn("%s: 3.3V regulator output did not became stable\n",
1852 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001853
1854 return -EAGAIN;
1855 case MMC_SIGNAL_VOLTAGE_180:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001856 if (!(host->flags & SDHCI_SIGNALING_180))
1857 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001858 if (!IS_ERR(mmc->supply.vqmmc)) {
1859 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001860 1700000, 1950000);
1861 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001862 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1863 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001864 return -EIO;
1865 }
1866 }
1867
1868 /*
1869 * Enable 1.8V Signal Enable in the Host Control2
1870 * register
1871 */
1872 ctrl |= SDHCI_CTRL_VDD_180;
1873 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1874
Vincent Yang9d967a62015-01-20 16:05:15 +08001875 /* Some controller need to do more when switching */
1876 if (host->ops->voltage_switch)
1877 host->ops->voltage_switch(host);
1878
Kevin Liu20b92a32012-12-17 19:29:26 +08001879 /* 1.8V regulator output should be stable within 5 ms */
1880 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1881 if (ctrl & SDHCI_CTRL_VDD_180)
1882 return 0;
1883
Joe Perches66061102014-09-12 14:56:56 -07001884 pr_warn("%s: 1.8V regulator output did not became stable\n",
1885 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001886
1887 return -EAGAIN;
1888 case MMC_SIGNAL_VOLTAGE_120:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001889 if (!(host->flags & SDHCI_SIGNALING_120))
1890 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001891 if (!IS_ERR(mmc->supply.vqmmc)) {
1892 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1893 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001894 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001895 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1896 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001897 return -EIO;
1898 }
1899 }
1900 return 0;
1901 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301902 /* No signal voltage switch required */
1903 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001904 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301905}
1906
Kevin Liu20b92a32012-12-17 19:29:26 +08001907static int sdhci_card_busy(struct mmc_host *mmc)
1908{
1909 struct sdhci_host *host = mmc_priv(mmc);
1910 u32 present_state;
1911
Adrian Huntere613cc42016-06-23 14:00:58 +03001912 /* Check whether DAT[0] is 0 */
Kevin Liu20b92a32012-12-17 19:29:26 +08001913 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08001914
Adrian Huntere613cc42016-06-23 14:00:58 +03001915 return !(present_state & SDHCI_DATA_0_LVL_MASK);
Kevin Liu20b92a32012-12-17 19:29:26 +08001916}
1917
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001918static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1919{
1920 struct sdhci_host *host = mmc_priv(mmc);
1921 unsigned long flags;
1922
1923 spin_lock_irqsave(&host->lock, flags);
1924 host->flags |= SDHCI_HS400_TUNING;
1925 spin_unlock_irqrestore(&host->lock, flags);
1926
1927 return 0;
1928}
1929
Girish K S069c9f12012-01-06 09:56:39 +05301930static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301931{
Russell King4b6f37d2014-04-25 12:59:36 +01001932 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301933 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301934 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301935 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001936 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001937 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001938 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301939
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001940 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301941
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001942 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1943 host->flags &= ~SDHCI_HS400_TUNING;
1944
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001945 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1946 tuning_count = host->tuning_count;
1947
Arindam Nathb513ea22011-05-05 12:19:04 +05301948 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001949 * The Host Controller needs tuning in case of SDR104 and DDR50
1950 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1951 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301952 * If the Host Controller supports the HS200 mode then the
1953 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301954 */
Russell King4b6f37d2014-04-25 12:59:36 +01001955 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001956 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001957 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001958 err = -EINVAL;
1959 goto out_unlock;
1960
Russell King4b6f37d2014-04-25 12:59:36 +01001961 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001962 /*
1963 * Periodic re-tuning for HS400 is not expected to be needed, so
1964 * disable it here.
1965 */
1966 if (hs400_tuning)
1967 tuning_count = 0;
1968 break;
1969
Russell King4b6f37d2014-04-25 12:59:36 +01001970 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001971 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001972 break;
Girish K S069c9f12012-01-06 09:56:39 +05301973
Russell King4b6f37d2014-04-25 12:59:36 +01001974 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03001975 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01001976 break;
1977 /* FALLTHROUGH */
1978
1979 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001980 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301981 }
1982
Dong Aisheng45251812013-09-13 19:11:30 +08001983 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001984 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001985 err = host->ops->platform_execute_tuning(host, opcode);
Dong Aisheng45251812013-09-13 19:11:30 +08001986 return err;
1987 }
1988
Russell King4b6f37d2014-04-25 12:59:36 +01001989 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1990 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001991 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1992 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301993 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1994
1995 /*
1996 * As per the Host Controller spec v3.00, tuning command
1997 * generates Buffer Read Ready interrupt, so enable that.
1998 *
1999 * Note: The spec clearly says that when tuning sequence
2000 * is being performed, the controller does not generate
2001 * interrupts other than Buffer Read Ready interrupt. But
2002 * to make sure we don't hit a controller bug, we _only_
2003 * enable Buffer Read Ready interrupt here.
2004 */
Russell Kingb537f942014-04-25 12:56:01 +01002005 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
2006 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05302007
2008 /*
2009 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
Simon Horman1473bdd2016-05-13 13:24:31 +09002010 * of loops reaches 40 times.
Arindam Nathb513ea22011-05-05 12:19:04 +05302011 */
Arindam Nathb513ea22011-05-05 12:19:04 +05302012 do {
2013 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002014 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05302015
Girish K S069c9f12012-01-06 09:56:39 +05302016 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05302017 cmd.arg = 0;
2018 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2019 cmd.retries = 0;
2020 cmd.data = NULL;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002021 cmd.mrq = &mrq;
Arindam Nathb513ea22011-05-05 12:19:04 +05302022 cmd.error = 0;
2023
Al Cooper7ce45e92014-05-09 11:34:07 -04002024 if (tuning_loop_counter-- == 0)
2025 break;
2026
Arindam Nathb513ea22011-05-05 12:19:04 +05302027 mrq.cmd = &cmd;
Arindam Nathb513ea22011-05-05 12:19:04 +05302028
2029 /*
2030 * In response to CMD19, the card sends 64 bytes of tuning
2031 * block to the Host Controller. So we set the block size
2032 * to 64 here.
2033 */
Girish K S069c9f12012-01-06 09:56:39 +05302034 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
2035 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2036 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
2037 SDHCI_BLOCK_SIZE);
2038 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
2039 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2040 SDHCI_BLOCK_SIZE);
2041 } else {
2042 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2043 SDHCI_BLOCK_SIZE);
2044 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302045
2046 /*
2047 * The tuning block is sent by the card to the host controller.
2048 * So we set the TRNS_READ bit in the Transfer Mode register.
2049 * This also takes care of setting DMA Enable and Multi Block
2050 * Select in the same register to 0.
2051 */
2052 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2053
2054 sdhci_send_command(host, &cmd);
2055
2056 host->cmd = NULL;
Arindam Nathb513ea22011-05-05 12:19:04 +05302057
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002058 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302059 /* Wait for Buffer Read Ready interrupt */
2060 wait_event_interruptible_timeout(host->buf_ready_int,
2061 (host->tuning_done == 1),
2062 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002063 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302064
2065 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002066 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 +05302067 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2068 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2069 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2070 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2071
2072 err = -EIO;
2073 goto out;
2074 }
2075
2076 host->tuning_done = 0;
2077
2078 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07002079
2080 /* eMMC spec does not require a delay between tuning cycles */
2081 if (opcode == MMC_SEND_TUNING_BLOCK)
2082 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302083 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2084
2085 /*
2086 * The Host Driver has exhausted the maximum number of loops allowed,
2087 * so use fixed sampling frequency.
2088 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002089 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302090 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2091 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002092 }
2093 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002094 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002095 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302096 }
2097
2098out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002099 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002100 /*
2101 * In case tuning fails, host controllers which support
2102 * re-tuning can try tuning again at a later time, when the
2103 * re-tuning timer expires. So for these controllers, we
2104 * return 0. Since there might be other controllers who do not
2105 * have this capability, we return error for them.
2106 */
2107 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302108 }
2109
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002110 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302111
Russell Kingb537f942014-04-25 12:56:01 +01002112 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2113 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002114out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002115 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302116 return err;
2117}
2118
Adrian Huntercb849642015-02-06 14:12:59 +02002119static int sdhci_select_drive_strength(struct mmc_card *card,
2120 unsigned int max_dtr, int host_drv,
2121 int card_drv, int *drv_type)
2122{
2123 struct sdhci_host *host = mmc_priv(card->host);
2124
2125 if (!host->ops->select_drive_strength)
2126 return 0;
2127
2128 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2129 card_drv, drv_type);
2130}
Kevin Liu52983382013-01-31 11:31:37 +08002131
2132static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302133{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302134 /* Host Controller v3.00 defines preset value registers */
2135 if (host->version < SDHCI_SPEC_300)
2136 return;
2137
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302138 /*
2139 * We only enable or disable Preset Value if they are not already
2140 * enabled or disabled respectively. Otherwise, we bail out.
2141 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002142 if (host->preset_enabled != enable) {
2143 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2144
2145 if (enable)
2146 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2147 else
2148 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2149
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302150 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002151
2152 if (enable)
2153 host->flags |= SDHCI_PV_ENABLED;
2154 else
2155 host->flags &= ~SDHCI_PV_ENABLED;
2156
2157 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302158 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002159}
2160
Haibo Chen348487c2014-12-09 17:04:05 +08002161static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2162 int err)
2163{
2164 struct sdhci_host *host = mmc_priv(mmc);
2165 struct mmc_data *data = mrq->data;
2166
Russell Kingf48f0392016-01-26 13:40:32 +00002167 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002168 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2169 data->flags & MMC_DATA_WRITE ?
2170 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2171
2172 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002173}
2174
Haibo Chen348487c2014-12-09 17:04:05 +08002175static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2176 bool is_first_req)
2177{
2178 struct sdhci_host *host = mmc_priv(mmc);
2179
Haibo Chend31911b2015-08-25 10:02:11 +08002180 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002181
2182 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002183 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002184}
2185
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002186static inline bool sdhci_has_requests(struct sdhci_host *host)
2187{
2188 return host->cmd || host->data_cmd;
2189}
2190
2191static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
2192{
2193 if (host->data_cmd) {
2194 host->data_cmd->error = err;
2195 sdhci_finish_mrq(host, host->data_cmd->mrq);
2196 }
2197
2198 if (host->cmd) {
2199 host->cmd->error = err;
2200 sdhci_finish_mrq(host, host->cmd->mrq);
2201 }
2202}
2203
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002204static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002205{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002206 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002207 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002208 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002209
Christian Daudt722e1282013-06-20 14:26:36 -07002210 /* First check if client has provided their own card event */
2211 if (host->ops->card_event)
2212 host->ops->card_event(host);
2213
Adrian Hunterd3940f22016-06-29 16:24:14 +03002214 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002215
Pierre Ossmand129bce2006-03-24 03:18:17 -08002216 spin_lock_irqsave(&host->lock, flags);
2217
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002218 /* Check sdhci_has_requests() first in case we are runtime suspended */
2219 if (sdhci_has_requests(host) && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302220 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002221 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302222 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002223 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002224
Russell King03231f92014-04-25 12:57:12 +01002225 sdhci_do_reset(host, SDHCI_RESET_CMD);
2226 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002227
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002228 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002229 }
2230
2231 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002232}
2233
2234static const struct mmc_host_ops sdhci_ops = {
2235 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002236 .post_req = sdhci_post_req,
2237 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002238 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002239 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002240 .get_ro = sdhci_get_ro,
2241 .hw_reset = sdhci_hw_reset,
2242 .enable_sdio_irq = sdhci_enable_sdio_irq,
2243 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002244 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002245 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002246 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002247 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002248 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002249};
2250
2251/*****************************************************************************\
2252 * *
2253 * Tasklets *
2254 * *
2255\*****************************************************************************/
2256
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002257static bool sdhci_request_done(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002258{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002259 unsigned long flags;
2260 struct mmc_request *mrq;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002261 int i;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002262
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002263 spin_lock_irqsave(&host->lock, flags);
2264
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002265 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
2266 mrq = host->mrqs_done[i];
2267 if (mrq) {
2268 host->mrqs_done[i] = NULL;
2269 break;
2270 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002271 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002272
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002273 if (!mrq) {
2274 spin_unlock_irqrestore(&host->lock, flags);
2275 return true;
2276 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002277
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002278 sdhci_del_timer(host, mrq);
2279
Pierre Ossmand129bce2006-03-24 03:18:17 -08002280 /*
Russell King054cedf2016-01-26 13:40:42 +00002281 * Always unmap the data buffers if they were mapped by
2282 * sdhci_prepare_data() whenever we finish with a request.
2283 * This avoids leaking DMA mappings on error.
2284 */
2285 if (host->flags & SDHCI_REQ_USE_DMA) {
2286 struct mmc_data *data = mrq->data;
2287
2288 if (data && data->host_cookie == COOKIE_MAPPED) {
2289 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2290 (data->flags & MMC_DATA_READ) ?
2291 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2292 data->host_cookie = COOKIE_UNMAPPED;
2293 }
2294 }
2295
2296 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002297 * The controller needs a reset of internal state machines
2298 * upon error conditions.
2299 */
Adrian Hunter0cc563c2016-06-29 16:24:28 +03002300 if (sdhci_needs_reset(host, mrq)) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002301 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002302 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002303 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002304 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002305
2306 /* Spec says we should do both at the same time, but Ricoh
2307 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002308 sdhci_do_reset(host, SDHCI_RESET_CMD);
2309 sdhci_do_reset(host, SDHCI_RESET_DATA);
Adrian Huntered1563d2016-06-29 16:24:29 +03002310
2311 host->pending_reset = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002312 }
2313
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002314 if (!sdhci_has_requests(host))
2315 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002316
Pierre Ossman5f25a662006-10-04 02:15:39 -07002317 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002318 spin_unlock_irqrestore(&host->lock, flags);
2319
2320 mmc_request_done(host->mmc, mrq);
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002321
2322 return false;
2323}
2324
2325static void sdhci_tasklet_finish(unsigned long param)
2326{
2327 struct sdhci_host *host = (struct sdhci_host *)param;
2328
2329 while (!sdhci_request_done(host))
2330 ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002331}
2332
2333static void sdhci_timeout_timer(unsigned long data)
2334{
2335 struct sdhci_host *host;
2336 unsigned long flags;
2337
2338 host = (struct sdhci_host*)data;
2339
2340 spin_lock_irqsave(&host->lock, flags);
2341
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002342 if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
2343 pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
2344 mmc_hostname(host->mmc));
2345 sdhci_dumpregs(host);
2346
2347 host->cmd->error = -ETIMEDOUT;
2348 sdhci_finish_mrq(host, host->cmd->mrq);
2349 }
2350
2351 mmiowb();
2352 spin_unlock_irqrestore(&host->lock, flags);
2353}
2354
2355static void sdhci_timeout_data_timer(unsigned long data)
2356{
2357 struct sdhci_host *host;
2358 unsigned long flags;
2359
2360 host = (struct sdhci_host *)data;
2361
2362 spin_lock_irqsave(&host->lock, flags);
2363
2364 if (host->data || host->data_cmd ||
2365 (host->cmd && sdhci_data_line_cmd(host->cmd))) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002366 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2367 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002368 sdhci_dumpregs(host);
2369
2370 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002371 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002372 sdhci_finish_data(host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002373 } else if (host->data_cmd) {
2374 host->data_cmd->error = -ETIMEDOUT;
2375 sdhci_finish_mrq(host, host->data_cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002376 } else {
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002377 host->cmd->error = -ETIMEDOUT;
2378 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002379 }
2380 }
2381
Pierre Ossman5f25a662006-10-04 02:15:39 -07002382 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002383 spin_unlock_irqrestore(&host->lock, flags);
2384}
2385
2386/*****************************************************************************\
2387 * *
2388 * Interrupt handling *
2389 * *
2390\*****************************************************************************/
2391
Adrian Hunter61541392014-09-24 10:27:27 +03002392static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002393{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002394 if (!host->cmd) {
Adrian Huntered1563d2016-06-29 16:24:29 +03002395 /*
2396 * SDHCI recovers from errors by resetting the cmd and data
2397 * circuits. Until that is done, there very well might be more
2398 * interrupts, so ignore them in that case.
2399 */
2400 if (host->pending_reset)
2401 return;
Marek Vasut2e4456f2015-11-18 10:47:02 +01002402 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2403 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002404 sdhci_dumpregs(host);
2405 return;
2406 }
2407
Russell Kingec014cb2016-01-26 13:39:39 +00002408 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2409 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2410 if (intmask & SDHCI_INT_TIMEOUT)
2411 host->cmd->error = -ETIMEDOUT;
2412 else
2413 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002414
Russell King71fcbda2016-01-26 13:39:45 +00002415 /*
2416 * If this command initiates a data phase and a response
2417 * CRC error is signalled, the card can start transferring
2418 * data - the card may have received the command without
2419 * error. We must not terminate the mmc_request early.
2420 *
2421 * If the card did not receive the command or returned an
2422 * error which prevented it sending data, the data phase
2423 * will time out.
2424 */
2425 if (host->cmd->data &&
2426 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2427 SDHCI_INT_CRC) {
2428 host->cmd = NULL;
2429 return;
2430 }
2431
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002432 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002433 return;
2434 }
2435
Adrian Hunter6bde8682016-06-29 16:24:20 +03002436 if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2437 !(host->cmd->flags & MMC_RSP_BUSY) && !host->data &&
2438 host->cmd->opcode == MMC_STOP_TRANSMISSION)
Adrian Hunter61541392014-09-24 10:27:27 +03002439 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002440
2441 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002442 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002443}
2444
George G. Davis0957c332010-02-18 12:32:12 -05002445#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002446static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002447{
2448 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002449 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002450
2451 sdhci_dumpregs(host);
2452
2453 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002454 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002455
Adrian Huntere57a5f62014-11-04 12:42:46 +02002456 if (host->flags & SDHCI_USE_64_BIT_DMA)
2457 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2458 name, desc, le32_to_cpu(dma_desc->addr_hi),
2459 le32_to_cpu(dma_desc->addr_lo),
2460 le16_to_cpu(dma_desc->len),
2461 le16_to_cpu(dma_desc->cmd));
2462 else
2463 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2464 name, desc, le32_to_cpu(dma_desc->addr_lo),
2465 le16_to_cpu(dma_desc->len),
2466 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002467
Adrian Hunter76fe3792014-11-04 12:42:42 +02002468 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002469
Adrian Hunter05452302014-11-04 12:42:45 +02002470 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002471 break;
2472 }
2473}
2474#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002475static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002476#endif
2477
Pierre Ossmand129bce2006-03-24 03:18:17 -08002478static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2479{
Girish K S069c9f12012-01-06 09:56:39 +05302480 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002481
Arindam Nathb513ea22011-05-05 12:19:04 +05302482 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2483 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302484 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2485 if (command == MMC_SEND_TUNING_BLOCK ||
2486 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302487 host->tuning_done = 1;
2488 wake_up(&host->buf_ready_int);
2489 return;
2490 }
2491 }
2492
Pierre Ossmand129bce2006-03-24 03:18:17 -08002493 if (!host->data) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002494 struct mmc_command *data_cmd = host->data_cmd;
2495
2496 if (data_cmd)
2497 host->data_cmd = NULL;
2498
Pierre Ossmand129bce2006-03-24 03:18:17 -08002499 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002500 * The "data complete" interrupt is also used to
2501 * indicate that a busy state has ended. See comment
2502 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002503 */
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002504 if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002505 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002506 data_cmd->error = -ETIMEDOUT;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002507 sdhci_finish_mrq(host, data_cmd->mrq);
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002508 return;
2509 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002510 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002511 /*
2512 * Some cards handle busy-end interrupt
2513 * before the command completed, so make
2514 * sure we do things in the proper order.
2515 */
Adrian Hunterea968022016-06-29 16:24:24 +03002516 if (host->cmd == data_cmd)
2517 return;
2518
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002519 sdhci_finish_mrq(host, data_cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002520 return;
2521 }
2522 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002523
Adrian Huntered1563d2016-06-29 16:24:29 +03002524 /*
2525 * SDHCI recovers from errors by resetting the cmd and data
2526 * circuits. Until that is done, there very well might be more
2527 * interrupts, so ignore them in that case.
2528 */
2529 if (host->pending_reset)
2530 return;
2531
Marek Vasut2e4456f2015-11-18 10:47:02 +01002532 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2533 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002534 sdhci_dumpregs(host);
2535
2536 return;
2537 }
2538
2539 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002540 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002541 else if (intmask & SDHCI_INT_DATA_END_BIT)
2542 host->data->error = -EILSEQ;
2543 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2544 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2545 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002546 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002547 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302548 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002549 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002550 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002551 if (host->ops->adma_workaround)
2552 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002553 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002554
Pierre Ossman17b04292007-07-22 22:18:46 +02002555 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002556 sdhci_finish_data(host);
2557 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002558 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002559 sdhci_transfer_pio(host);
2560
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002561 /*
2562 * We currently don't do anything fancy with DMA
2563 * boundaries, but as we can't disable the feature
2564 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002565 *
2566 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2567 * should return a valid address to continue from, but as
2568 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002569 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002570 if (intmask & SDHCI_INT_DMA_END) {
2571 u32 dmastart, dmanow;
2572 dmastart = sg_dma_address(host->data->sg);
2573 dmanow = dmastart + host->data->bytes_xfered;
2574 /*
2575 * Force update to the next DMA block boundary.
2576 */
2577 dmanow = (dmanow &
2578 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2579 SDHCI_DEFAULT_BOUNDARY_SIZE;
2580 host->data->bytes_xfered = dmanow - dmastart;
2581 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2582 " next 0x%08x\n",
2583 mmc_hostname(host->mmc), dmastart,
2584 host->data->bytes_xfered, dmanow);
2585 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2586 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002587
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002588 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002589 if (host->cmd == host->data_cmd) {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002590 /*
2591 * Data managed to finish before the
2592 * command completed. Make sure we do
2593 * things in the proper order.
2594 */
2595 host->data_early = 1;
2596 } else {
2597 sdhci_finish_data(host);
2598 }
2599 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002600 }
2601}
2602
David Howells7d12e782006-10-05 14:55:46 +01002603static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002604{
Russell King781e9892014-04-25 12:55:46 +01002605 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002606 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002607 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002608 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002609
2610 spin_lock(&host->lock);
2611
Russell Kingbe138552014-04-25 12:55:56 +01002612 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002613 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002614 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002615 }
2616
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002617 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002618 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002619 result = IRQ_NONE;
2620 goto out;
2621 }
2622
Russell King41005002014-04-25 12:55:36 +01002623 do {
2624 /* Clear selected interrupts. */
2625 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2626 SDHCI_INT_BUS_POWER);
2627 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002628
Russell King41005002014-04-25 12:55:36 +01002629 DBG("*** %s got interrupt: 0x%08x\n",
2630 mmc_hostname(host->mmc), intmask);
2631
2632 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2633 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2634 SDHCI_CARD_PRESENT;
2635
2636 /*
2637 * There is a observation on i.mx esdhc. INSERT
2638 * bit will be immediately set again when it gets
2639 * cleared, if a card is inserted. We have to mask
2640 * the irq to prevent interrupt storm which will
2641 * freeze the system. And the REMOVE gets the
2642 * same situation.
2643 *
2644 * More testing are needed here to ensure it works
2645 * for other platforms though.
2646 */
Russell Kingb537f942014-04-25 12:56:01 +01002647 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2648 SDHCI_INT_CARD_REMOVE);
2649 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2650 SDHCI_INT_CARD_INSERT;
2651 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2652 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002653
2654 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2655 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002656
2657 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2658 SDHCI_INT_CARD_REMOVE);
2659 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002660 }
2661
2662 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002663 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2664 &intmask);
Russell King41005002014-04-25 12:55:36 +01002665
2666 if (intmask & SDHCI_INT_DATA_MASK)
2667 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2668
2669 if (intmask & SDHCI_INT_BUS_POWER)
2670 pr_err("%s: Card is consuming too much power!\n",
2671 mmc_hostname(host->mmc));
2672
Russell King781e9892014-04-25 12:55:46 +01002673 if (intmask & SDHCI_INT_CARD_INT) {
2674 sdhci_enable_sdio_irq_nolock(host, false);
2675 host->thread_isr |= SDHCI_INT_CARD_INT;
2676 result = IRQ_WAKE_THREAD;
2677 }
Russell King41005002014-04-25 12:55:36 +01002678
2679 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2680 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2681 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2682 SDHCI_INT_CARD_INT);
2683
2684 if (intmask) {
2685 unexpected |= intmask;
2686 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2687 }
2688
Russell King781e9892014-04-25 12:55:46 +01002689 if (result == IRQ_NONE)
2690 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002691
2692 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002693 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002694out:
2695 spin_unlock(&host->lock);
2696
Alexander Stein6379b232012-03-14 09:52:10 +01002697 if (unexpected) {
2698 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2699 mmc_hostname(host->mmc), unexpected);
2700 sdhci_dumpregs(host);
2701 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002702
Pierre Ossmand129bce2006-03-24 03:18:17 -08002703 return result;
2704}
2705
Russell King781e9892014-04-25 12:55:46 +01002706static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2707{
2708 struct sdhci_host *host = dev_id;
2709 unsigned long flags;
2710 u32 isr;
2711
2712 spin_lock_irqsave(&host->lock, flags);
2713 isr = host->thread_isr;
2714 host->thread_isr = 0;
2715 spin_unlock_irqrestore(&host->lock, flags);
2716
Russell King3560db82014-04-25 12:55:51 +01002717 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Adrian Hunterd3940f22016-06-29 16:24:14 +03002718 struct mmc_host *mmc = host->mmc;
2719
2720 mmc->ops->card_event(mmc);
2721 mmc_detect_change(mmc, msecs_to_jiffies(200));
Russell King3560db82014-04-25 12:55:51 +01002722 }
2723
Russell King781e9892014-04-25 12:55:46 +01002724 if (isr & SDHCI_INT_CARD_INT) {
2725 sdio_run_irqs(host->mmc);
2726
2727 spin_lock_irqsave(&host->lock, flags);
2728 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2729 sdhci_enable_sdio_irq_nolock(host, true);
2730 spin_unlock_irqrestore(&host->lock, flags);
2731 }
2732
2733 return isr ? IRQ_HANDLED : IRQ_NONE;
2734}
2735
Pierre Ossmand129bce2006-03-24 03:18:17 -08002736/*****************************************************************************\
2737 * *
2738 * Suspend/resume *
2739 * *
2740\*****************************************************************************/
2741
2742#ifdef CONFIG_PM
Ludovic Desroches84d62602016-05-13 15:16:02 +02002743/*
2744 * To enable wakeup events, the corresponding events have to be enabled in
2745 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
2746 * Table' in the SD Host Controller Standard Specification.
2747 * It is useless to restore SDHCI_INT_ENABLE state in
2748 * sdhci_disable_irq_wakeups() since it will be set by
2749 * sdhci_enable_card_detection() or sdhci_init().
2750 */
Kevin Liuad080d72013-01-05 17:21:33 +08002751void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2752{
2753 u8 val;
2754 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2755 | SDHCI_WAKE_ON_INT;
Ludovic Desroches84d62602016-05-13 15:16:02 +02002756 u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2757 SDHCI_INT_CARD_INT;
Kevin Liuad080d72013-01-05 17:21:33 +08002758
2759 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2760 val |= mask ;
2761 /* Avoid fake wake up */
Ludovic Desroches84d62602016-05-13 15:16:02 +02002762 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) {
Kevin Liuad080d72013-01-05 17:21:33 +08002763 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002764 irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
2765 }
Kevin Liuad080d72013-01-05 17:21:33 +08002766 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002767 sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002768}
2769EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2770
Fabio Estevam0b10f472014-08-30 14:53:13 -03002771static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002772{
2773 u8 val;
2774 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2775 | SDHCI_WAKE_ON_INT;
2776
2777 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2778 val &= ~mask;
2779 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2780}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002781
Manuel Lauss29495aa2011-11-03 11:09:45 +01002782int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002783{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002784 sdhci_disable_card_detection(host);
2785
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002786 mmc_retune_timer_stop(host->mmc);
2787 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302788
Kevin Liuad080d72013-01-05 17:21:33 +08002789 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002790 host->ier = 0;
2791 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2792 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002793 free_irq(host->irq, host);
2794 } else {
2795 sdhci_enable_irq_wakeups(host);
2796 enable_irq_wake(host->irq);
2797 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002798 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002799}
2800
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002801EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002802
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002803int sdhci_resume_host(struct sdhci_host *host)
2804{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002805 struct mmc_host *mmc = host->mmc;
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002806 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002807
Richard Röjforsa13abc72009-09-22 16:45:30 -07002808 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002809 if (host->ops->enable_dma)
2810 host->ops->enable_dma(host);
2811 }
2812
Adrian Hunter6308d292012-02-07 14:48:54 +02002813 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2814 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2815 /* Card keeps power but host controller does not */
2816 sdhci_init(host, 0);
2817 host->pwr = 0;
2818 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002819 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02002820 } else {
2821 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2822 mmiowb();
2823 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002824
Haibo Chen14a7b41642015-09-15 18:32:58 +08002825 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2826 ret = request_threaded_irq(host->irq, sdhci_irq,
2827 sdhci_thread_irq, IRQF_SHARED,
2828 mmc_hostname(host->mmc), host);
2829 if (ret)
2830 return ret;
2831 } else {
2832 sdhci_disable_irq_wakeups(host);
2833 disable_irq_wake(host->irq);
2834 }
2835
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002836 sdhci_enable_card_detection(host);
2837
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002838 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002839}
2840
2841EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002842
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002843int sdhci_runtime_suspend_host(struct sdhci_host *host)
2844{
2845 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002846
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002847 mmc_retune_timer_stop(host->mmc);
2848 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002849
2850 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002851 host->ier &= SDHCI_INT_CARD_INT;
2852 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2853 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002854 spin_unlock_irqrestore(&host->lock, flags);
2855
Russell King781e9892014-04-25 12:55:46 +01002856 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002857
2858 spin_lock_irqsave(&host->lock, flags);
2859 host->runtime_suspended = true;
2860 spin_unlock_irqrestore(&host->lock, flags);
2861
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002862 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002863}
2864EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2865
2866int sdhci_runtime_resume_host(struct sdhci_host *host)
2867{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002868 struct mmc_host *mmc = host->mmc;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002869 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002870 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002871
2872 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2873 if (host->ops->enable_dma)
2874 host->ops->enable_dma(host);
2875 }
2876
2877 sdhci_init(host, 0);
2878
2879 /* Force clock and power re-program */
2880 host->pwr = 0;
2881 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002882 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
2883 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002884
Kevin Liu52983382013-01-31 11:31:37 +08002885 if ((host_flags & SDHCI_PV_ENABLED) &&
2886 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2887 spin_lock_irqsave(&host->lock, flags);
2888 sdhci_enable_preset_value(host, true);
2889 spin_unlock_irqrestore(&host->lock, flags);
2890 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002891
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002892 spin_lock_irqsave(&host->lock, flags);
2893
2894 host->runtime_suspended = false;
2895
2896 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002897 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002898 sdhci_enable_sdio_irq_nolock(host, true);
2899
2900 /* Enable Card Detection */
2901 sdhci_enable_card_detection(host);
2902
2903 spin_unlock_irqrestore(&host->lock, flags);
2904
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002905 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002906}
2907EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2908
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002909#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002910
Pierre Ossmand129bce2006-03-24 03:18:17 -08002911/*****************************************************************************\
2912 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002913 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002914 * *
2915\*****************************************************************************/
2916
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002917struct sdhci_host *sdhci_alloc_host(struct device *dev,
2918 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002919{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002920 struct mmc_host *mmc;
2921 struct sdhci_host *host;
2922
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002923 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002924
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002925 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002926 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002927 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002928
2929 host = mmc_priv(mmc);
2930 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002931 host->mmc_host_ops = sdhci_ops;
2932 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002933
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002934 host->flags = SDHCI_SIGNALING_330;
2935
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002936 return host;
2937}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002938
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002939EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002940
Alexandre Courbot7b913692016-03-07 11:07:55 +09002941static int sdhci_set_dma_mask(struct sdhci_host *host)
2942{
2943 struct mmc_host *mmc = host->mmc;
2944 struct device *dev = mmc_dev(mmc);
2945 int ret = -EINVAL;
2946
2947 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
2948 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2949
2950 /* Try 64-bit mask if hardware is capable of it */
2951 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2952 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2953 if (ret) {
2954 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
2955 mmc_hostname(mmc));
2956 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2957 }
2958 }
2959
2960 /* 32-bit mask as default & fallback */
2961 if (ret) {
2962 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2963 if (ret)
2964 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
2965 mmc_hostname(mmc));
2966 }
2967
2968 return ret;
2969}
2970
Adrian Hunter6132a3b2016-06-29 16:24:18 +03002971void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
2972{
2973 u16 v;
2974
2975 if (host->read_caps)
2976 return;
2977
2978 host->read_caps = true;
2979
2980 if (debug_quirks)
2981 host->quirks = debug_quirks;
2982
2983 if (debug_quirks2)
2984 host->quirks2 = debug_quirks2;
2985
2986 sdhci_do_reset(host, SDHCI_RESET_ALL);
2987
2988 v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
2989 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
2990
2991 if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
2992 return;
2993
2994 host->caps = caps ? *caps : sdhci_readl(host, SDHCI_CAPABILITIES);
2995
2996 if (host->version < SDHCI_SPEC_300)
2997 return;
2998
2999 host->caps1 = caps1 ? *caps1 : sdhci_readl(host, SDHCI_CAPABILITIES_1);
3000}
3001EXPORT_SYMBOL_GPL(__sdhci_read_caps);
3002
Adrian Hunter52f53362016-06-29 16:24:15 +03003003int sdhci_setup_host(struct sdhci_host *host)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003004{
3005 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303006 u32 max_current_caps;
3007 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003008 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08003009 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003010 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003011
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003012 WARN_ON(host == NULL);
3013 if (host == NULL)
3014 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003015
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003016 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003017
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003018 sdhci_read_caps(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003019
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003020 override_timeout_clk = host->timeout_clk;
3021
Zhangfei Gao85105c52010-08-06 07:10:01 +08003022 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003023 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
3024 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003025 }
3026
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003027 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003028 host->flags |= SDHCI_USE_SDMA;
Adrian Hunter28da3582016-06-29 16:24:17 +03003029 else if (!(host->caps & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003030 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003031 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003032 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003033
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003034 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003035 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003036 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003037 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003038 }
3039
Arindam Nathf2119df2011-05-05 12:18:57 +05303040 if ((host->version >= SDHCI_SPEC_200) &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003041 (host->caps & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003042 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003043
3044 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3045 (host->flags & SDHCI_USE_ADMA)) {
3046 DBG("Disabling ADMA as it is marked broken\n");
3047 host->flags &= ~SDHCI_USE_ADMA;
3048 }
3049
Adrian Huntere57a5f62014-11-04 12:42:46 +02003050 /*
3051 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
3052 * and *must* do 64-bit DMA. A driver has the opportunity to change
3053 * that during the first call to ->enable_dma(). Similarly
3054 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
3055 * implement.
3056 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003057 if (host->caps & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02003058 host->flags |= SDHCI_USE_64_BIT_DMA;
3059
Richard Röjforsa13abc72009-09-22 16:45:30 -07003060 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09003061 ret = sdhci_set_dma_mask(host);
3062
3063 if (!ret && host->ops->enable_dma)
3064 ret = host->ops->enable_dma(host);
3065
3066 if (ret) {
3067 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
3068 mmc_hostname(mmc));
3069 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
3070
3071 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003072 }
3073 }
3074
Adrian Huntere57a5f62014-11-04 12:42:46 +02003075 /* SDMA does not support 64-bit DMA */
3076 if (host->flags & SDHCI_USE_64_BIT_DMA)
3077 host->flags &= ~SDHCI_USE_SDMA;
3078
Pierre Ossman2134a922008-06-28 18:28:51 +02003079 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00003080 dma_addr_t dma;
3081 void *buf;
3082
Pierre Ossman2134a922008-06-28 18:28:51 +02003083 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02003084 * The DMA descriptor table size is calculated as the maximum
3085 * number of segments times 2, to allow for an alignment
3086 * descriptor for each segment, plus 1 for a nop end descriptor,
3087 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02003088 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02003089 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3090 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3091 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003092 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003093 } else {
3094 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3095 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003096 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003097 }
Russell Kinge66e61c2016-01-26 13:39:55 +00003098
Adrian Hunter04a5ae62015-11-26 14:00:49 +02003099 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00003100 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
3101 host->adma_table_sz, &dma, GFP_KERNEL);
3102 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07003103 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003104 mmc_hostname(mmc));
3105 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003106 } else if ((dma + host->align_buffer_sz) &
3107 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07003108 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3109 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003110 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003111 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3112 host->adma_table_sz, buf, dma);
3113 } else {
3114 host->align_buffer = buf;
3115 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00003116
Russell Kinge66e61c2016-01-26 13:39:55 +00003117 host->adma_table = buf + host->align_buffer_sz;
3118 host->adma_addr = dma + host->align_buffer_sz;
3119 }
Pierre Ossman2134a922008-06-28 18:28:51 +02003120 }
3121
Pierre Ossman76591502008-07-21 00:32:11 +02003122 /*
3123 * If we use DMA, then it's up to the caller to set the DMA
3124 * mask, but PIO does not need the hw shim so we set a new
3125 * mask here in that case.
3126 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003127 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003128 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003129 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003130 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003131
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003132 if (host->version >= SDHCI_SPEC_300)
Adrian Hunter28da3582016-06-29 16:24:17 +03003133 host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003134 >> SDHCI_CLOCK_BASE_SHIFT;
3135 else
Adrian Hunter28da3582016-06-29 16:24:17 +03003136 host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003137 >> SDHCI_CLOCK_BASE_SHIFT;
3138
Pierre Ossmand129bce2006-03-24 03:18:17 -08003139 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003140 if (host->max_clk == 0 || host->quirks &
3141 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003142 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003143 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3144 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003145 ret = -ENODEV;
3146 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003147 }
3148 host->max_clk = host->ops->get_max_clock(host);
3149 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003150
3151 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303152 * In case of Host Controller v3.00, find out whether clock
3153 * multiplier is supported.
3154 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003155 host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
Arindam Nathc3ed3872011-05-05 12:19:06 +05303156 SDHCI_CLOCK_MUL_SHIFT;
3157
3158 /*
3159 * In case the value in Clock Multiplier is 0, then programmable
3160 * clock mode is not supported, otherwise the actual clock
3161 * multiplier is one more than the value of Clock Multiplier
3162 * in the Capabilities Register.
3163 */
3164 if (host->clk_mul)
3165 host->clk_mul += 1;
3166
3167 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003168 * Set host parameters.
3169 */
Dong Aisheng59241752015-07-22 20:53:07 +08003170 max_clk = host->max_clk;
3171
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003172 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003173 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303174 else if (host->version >= SDHCI_SPEC_300) {
3175 if (host->clk_mul) {
3176 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003177 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303178 } else
3179 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3180 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003181 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003182
Adrian Hunterd310ae42016-04-12 14:25:07 +03003183 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003184 mmc->f_max = max_clk;
3185
Aisheng Dong28aab052014-08-27 15:26:31 +08003186 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Adrian Hunter28da3582016-06-29 16:24:17 +03003187 host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
Aisheng Dong28aab052014-08-27 15:26:31 +08003188 SDHCI_TIMEOUT_CLK_SHIFT;
3189 if (host->timeout_clk == 0) {
3190 if (host->ops->get_timeout_clock) {
3191 host->timeout_clk =
3192 host->ops->get_timeout_clock(host);
3193 } else {
3194 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3195 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003196 ret = -ENODEV;
3197 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003198 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003199 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003200
Adrian Hunter28da3582016-06-29 16:24:17 +03003201 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
Aisheng Dong28aab052014-08-27 15:26:31 +08003202 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003203
Adrian Hunter99513622016-03-07 13:33:55 +02003204 if (override_timeout_clk)
3205 host->timeout_clk = override_timeout_clk;
3206
Aisheng Dong28aab052014-08-27 15:26:31 +08003207 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003208 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003209 mmc->max_busy_timeout /= host->timeout_clk;
3210 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003211
Andrei Warkentine89d4562011-05-23 15:06:37 -05003212 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003213 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003214
3215 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3216 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003217
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003218 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003219 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003220 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003221 !(host->flags & SDHCI_USE_SDMA)) &&
3222 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003223 host->flags |= SDHCI_AUTO_CMD23;
3224 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3225 } else {
3226 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3227 }
3228
Philip Rakity15ec4462010-11-19 16:48:39 -05003229 /*
3230 * A controller may support 8-bit width, but the board itself
3231 * might not have the pins brought out. Boards that support
3232 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3233 * their platform code before calling sdhci_add_host(), and we
3234 * won't assume 8-bit width for hosts without that CAP.
3235 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003236 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003237 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003238
Jerry Huang63ef5d82012-10-25 13:47:19 +08003239 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3240 mmc->caps &= ~MMC_CAP_CMD23;
3241
Adrian Hunter28da3582016-06-29 16:24:17 +03003242 if (host->caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003243 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003244
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003245 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Jaehoon Chung860951c2016-06-21 10:13:26 +09003246 mmc_card_is_removable(mmc) &&
Arnd Bergmann287980e2016-05-27 23:23:25 +02003247 mmc_gpio_get_cd(host->mmc) < 0)
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003248 mmc->caps |= MMC_CAP_NEEDS_POLL;
3249
Tim Kryger3a48edc2014-06-13 10:13:56 -07003250 /* If there are external regulators, get them */
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003251 ret = mmc_regulator_get_supply(mmc);
3252 if (ret == -EPROBE_DEFER)
3253 goto undma;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003254
Philip Rakity6231f3d2012-07-23 15:56:23 -07003255 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003256 if (!IS_ERR(mmc->supply.vqmmc)) {
3257 ret = regulator_enable(mmc->supply.vqmmc);
3258 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3259 1950000))
Adrian Hunter28da3582016-06-29 16:24:17 +03003260 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
3261 SDHCI_SUPPORT_SDR50 |
3262 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003263 if (ret) {
3264 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3265 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003266 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003267 }
Kevin Liu8363c372012-11-17 17:55:51 -05003268 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003269
Adrian Hunter28da3582016-06-29 16:24:17 +03003270 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
3271 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3272 SDHCI_SUPPORT_DDR50);
3273 }
Daniel Drake6a661802012-11-25 13:01:19 -05003274
Al Cooper4188bba2012-03-16 15:54:17 -04003275 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
Adrian Hunter28da3582016-06-29 16:24:17 +03003276 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3277 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303278 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3279
3280 /* SDR104 supports also implies SDR50 support */
Adrian Hunter28da3582016-06-29 16:24:17 +03003281 if (host->caps1 & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303282 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003283 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3284 * field can be promoted to support HS200.
3285 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003286 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003287 mmc->caps2 |= MMC_CAP2_HS200;
Adrian Hunter28da3582016-06-29 16:24:17 +03003288 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303289 mmc->caps |= MMC_CAP_UHS_SDR50;
Adrian Hunter28da3582016-06-29 16:24:17 +03003290 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303291
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003292 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003293 (host->caps1 & SDHCI_SUPPORT_HS400))
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003294 mmc->caps2 |= MMC_CAP2_HS400;
3295
Adrian Hunter549c0b12014-11-06 15:19:05 +02003296 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3297 (IS_ERR(mmc->supply.vqmmc) ||
3298 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3299 1300000)))
3300 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3301
Adrian Hunter28da3582016-06-29 16:24:17 +03003302 if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
3303 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303304 mmc->caps |= MMC_CAP_UHS_DDR50;
3305
Girish K S069c9f12012-01-06 09:56:39 +05303306 /* Does the host need tuning for SDR50? */
Adrian Hunter28da3582016-06-29 16:24:17 +03003307 if (host->caps1 & SDHCI_USE_SDR50_TUNING)
Arindam Nathb513ea22011-05-05 12:19:04 +05303308 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3309
Arindam Nathd6d50a12011-05-05 12:18:59 +05303310 /* Driver Type(s) (A, C, D) supported by the host */
Adrian Hunter28da3582016-06-29 16:24:17 +03003311 if (host->caps1 & SDHCI_DRIVER_TYPE_A)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303312 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
Adrian Hunter28da3582016-06-29 16:24:17 +03003313 if (host->caps1 & SDHCI_DRIVER_TYPE_C)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303314 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
Adrian Hunter28da3582016-06-29 16:24:17 +03003315 if (host->caps1 & SDHCI_DRIVER_TYPE_D)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303316 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3317
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303318 /* Initial value for re-tuning timer count */
Adrian Hunter28da3582016-06-29 16:24:17 +03003319 host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3320 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303321
3322 /*
3323 * In case Re-tuning Timer is not disabled, the actual value of
3324 * re-tuning timer will be 2 ^ (n - 1).
3325 */
3326 if (host->tuning_count)
3327 host->tuning_count = 1 << (host->tuning_count - 1);
3328
3329 /* Re-tuning mode supported by the Host Controller */
Adrian Hunter28da3582016-06-29 16:24:17 +03003330 host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303331 SDHCI_RETUNING_MODE_SHIFT;
3332
Takashi Iwai8f230f42010-12-08 10:04:30 +01003333 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003334
Arindam Nathf2119df2011-05-05 12:18:57 +05303335 /*
3336 * According to SD Host Controller spec v3.00, if the Host System
3337 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3338 * the value is meaningful only if Voltage Support in the Capabilities
3339 * register is set. The actual current value is 4 times the register
3340 * value.
3341 */
3342 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003343 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003344 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003345 if (curr > 0) {
3346
3347 /* convert to SDHCI_MAX_CURRENT format */
3348 curr = curr/1000; /* convert to mA */
3349 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3350
3351 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3352 max_current_caps =
3353 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3354 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3355 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3356 }
3357 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303358
Adrian Hunter28da3582016-06-29 16:24:17 +03003359 if (host->caps & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003360 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303361
Aaron Lu55c46652012-07-04 13:31:48 +08003362 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303363 SDHCI_MAX_CURRENT_330_MASK) >>
3364 SDHCI_MAX_CURRENT_330_SHIFT) *
3365 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303366 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003367 if (host->caps & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003368 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303369
Aaron Lu55c46652012-07-04 13:31:48 +08003370 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303371 SDHCI_MAX_CURRENT_300_MASK) >>
3372 SDHCI_MAX_CURRENT_300_SHIFT) *
3373 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303374 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003375 if (host->caps & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003376 ocr_avail |= MMC_VDD_165_195;
3377
Aaron Lu55c46652012-07-04 13:31:48 +08003378 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303379 SDHCI_MAX_CURRENT_180_MASK) >>
3380 SDHCI_MAX_CURRENT_180_SHIFT) *
3381 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303382 }
3383
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003384 /* If OCR set by host, use it instead. */
3385 if (host->ocr_mask)
3386 ocr_avail = host->ocr_mask;
3387
3388 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003389 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003390 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003391
Takashi Iwai8f230f42010-12-08 10:04:30 +01003392 mmc->ocr_avail = ocr_avail;
3393 mmc->ocr_avail_sdio = ocr_avail;
3394 if (host->ocr_avail_sdio)
3395 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3396 mmc->ocr_avail_sd = ocr_avail;
3397 if (host->ocr_avail_sd)
3398 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3399 else /* normal SD controllers don't support 1.8V */
3400 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3401 mmc->ocr_avail_mmc = ocr_avail;
3402 if (host->ocr_avail_mmc)
3403 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003404
3405 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003406 pr_err("%s: Hardware doesn't report any support voltages.\n",
3407 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003408 ret = -ENODEV;
3409 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07003410 }
3411
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003412 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
3413 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
3414 MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
3415 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
3416 host->flags |= SDHCI_SIGNALING_180;
3417
3418 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
3419 host->flags |= SDHCI_SIGNALING_120;
3420
Pierre Ossmand129bce2006-03-24 03:18:17 -08003421 spin_lock_init(&host->lock);
3422
3423 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003424 * Maximum number of segments. Depends on if the hardware
3425 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003426 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003427 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003428 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003429 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003430 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003431 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003432 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003433
3434 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003435 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3436 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3437 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003438 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003439 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003440
3441 /*
3442 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003443 * of bytes. When doing hardware scatter/gather, each entry cannot
3444 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003445 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003446 if (host->flags & SDHCI_USE_ADMA) {
3447 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3448 mmc->max_seg_size = 65535;
3449 else
3450 mmc->max_seg_size = 65536;
3451 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003452 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003453 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003454
3455 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003456 * Maximum block size. This varies from controller to controller and
3457 * is specified in the capabilities register.
3458 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003459 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3460 mmc->max_blk_size = 2;
3461 } else {
Adrian Hunter28da3582016-06-29 16:24:17 +03003462 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003463 SDHCI_MAX_BLOCK_SHIFT;
3464 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003465 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3466 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003467 mmc->max_blk_size = 0;
3468 }
3469 }
3470
3471 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003472
3473 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003474 * Maximum block count.
3475 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003476 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003477
Adrian Hunter52f53362016-06-29 16:24:15 +03003478 return 0;
3479
3480unreg:
3481 if (!IS_ERR(mmc->supply.vqmmc))
3482 regulator_disable(mmc->supply.vqmmc);
3483undma:
3484 if (host->align_buffer)
3485 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3486 host->adma_table_sz, host->align_buffer,
3487 host->align_addr);
3488 host->adma_table = NULL;
3489 host->align_buffer = NULL;
3490
3491 return ret;
3492}
3493EXPORT_SYMBOL_GPL(sdhci_setup_host);
3494
3495int __sdhci_add_host(struct sdhci_host *host)
3496{
3497 struct mmc_host *mmc = host->mmc;
3498 int ret;
3499
Pierre Ossman55db8902006-11-21 17:55:45 +01003500 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003501 * Init tasklets.
3502 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003503 tasklet_init(&host->finish_tasklet,
3504 sdhci_tasklet_finish, (unsigned long)host);
3505
Al Viroe4cad1b2006-10-10 22:47:07 +01003506 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03003507 setup_timer(&host->data_timer, sdhci_timeout_data_timer,
3508 (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003509
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003510 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303511
Shawn Guo2af502c2013-07-05 14:38:55 +08003512 sdhci_init(host, 0);
3513
Russell King781e9892014-04-25 12:55:46 +01003514 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3515 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003516 if (ret) {
3517 pr_err("%s: Failed to request IRQ %d: %d\n",
3518 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003519 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003520 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003521
Pierre Ossmand129bce2006-03-24 03:18:17 -08003522#ifdef CONFIG_MMC_DEBUG
3523 sdhci_dumpregs(host);
3524#endif
3525
Adrian Hunter061d17a2016-04-12 14:25:09 +03003526 ret = sdhci_led_register(host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003527 if (ret) {
3528 pr_err("%s: Failed to register LED device: %d\n",
3529 mmc_hostname(mmc), ret);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003530 goto unirq;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003531 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003532
Pierre Ossman5f25a662006-10-04 02:15:39 -07003533 mmiowb();
3534
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003535 ret = mmc_add_host(mmc);
3536 if (ret)
3537 goto unled;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003538
Girish K Sa3c76eb2011-10-11 11:44:09 +05303539 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003540 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003541 (host->flags & SDHCI_USE_ADMA) ?
3542 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003543 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003544
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003545 sdhci_enable_card_detection(host);
3546
Pierre Ossmand129bce2006-03-24 03:18:17 -08003547 return 0;
3548
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003549unled:
Adrian Hunter061d17a2016-04-12 14:25:09 +03003550 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003551unirq:
Russell King03231f92014-04-25 12:57:12 +01003552 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003553 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3554 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003555 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003556untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003557 tasklet_kill(&host->finish_tasklet);
Adrian Hunter52f53362016-06-29 16:24:15 +03003558
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003559 if (!IS_ERR(mmc->supply.vqmmc))
3560 regulator_disable(mmc->supply.vqmmc);
Adrian Hunter52f53362016-06-29 16:24:15 +03003561
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003562 if (host->align_buffer)
3563 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3564 host->adma_table_sz, host->align_buffer,
3565 host->align_addr);
3566 host->adma_table = NULL;
3567 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003568
3569 return ret;
3570}
Adrian Hunter52f53362016-06-29 16:24:15 +03003571EXPORT_SYMBOL_GPL(__sdhci_add_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003572
Adrian Hunter52f53362016-06-29 16:24:15 +03003573int sdhci_add_host(struct sdhci_host *host)
3574{
3575 int ret;
3576
3577 ret = sdhci_setup_host(host);
3578 if (ret)
3579 return ret;
3580
3581 return __sdhci_add_host(host);
3582}
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003583EXPORT_SYMBOL_GPL(sdhci_add_host);
3584
Pierre Ossman1e728592008-04-16 19:13:13 +02003585void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003586{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003587 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003588 unsigned long flags;
3589
3590 if (dead) {
3591 spin_lock_irqsave(&host->lock, flags);
3592
3593 host->flags |= SDHCI_DEVICE_DEAD;
3594
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003595 if (sdhci_has_requests(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303596 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003597 " transfer!\n", mmc_hostname(mmc));
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003598 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossman1e728592008-04-16 19:13:13 +02003599 }
3600
3601 spin_unlock_irqrestore(&host->lock, flags);
3602 }
3603
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003604 sdhci_disable_card_detection(host);
3605
Markus Mayer4e743f12014-07-03 13:27:42 -07003606 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003607
Adrian Hunter061d17a2016-04-12 14:25:09 +03003608 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003609
Pierre Ossman1e728592008-04-16 19:13:13 +02003610 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003611 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003612
Russell Kingb537f942014-04-25 12:56:01 +01003613 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3614 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003615 free_irq(host->irq, host);
3616
3617 del_timer_sync(&host->timer);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03003618 del_timer_sync(&host->data_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003619
Pierre Ossmand129bce2006-03-24 03:18:17 -08003620 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003621
Tim Kryger3a48edc2014-06-13 10:13:56 -07003622 if (!IS_ERR(mmc->supply.vqmmc))
3623 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003624
Russell Kingedd63fc2016-01-26 13:39:50 +00003625 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003626 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3627 host->adma_table_sz, host->align_buffer,
3628 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003629
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003630 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003631 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003632}
3633
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003634EXPORT_SYMBOL_GPL(sdhci_remove_host);
3635
3636void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003637{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003638 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003639}
3640
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003641EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003642
3643/*****************************************************************************\
3644 * *
3645 * Driver init/exit *
3646 * *
3647\*****************************************************************************/
3648
3649static int __init sdhci_drv_init(void)
3650{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303651 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003652 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303653 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003654
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003655 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003656}
3657
3658static void __exit sdhci_drv_exit(void)
3659{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003660}
3661
3662module_init(sdhci_drv_init);
3663module_exit(sdhci_drv_exit);
3664
Pierre Ossmandf673b22006-06-30 02:22:31 -07003665module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003666module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003667
Pierre Ossman32710e82009-04-08 20:14:54 +02003668MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003669MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003670MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003671
Pierre Ossmandf673b22006-06-30 02:22:31 -07003672MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003673MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");