blob: b7b68b31d36d94aa21119992408b9ca58bcb654f [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
884static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500885 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700886{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800887 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500888 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700889
Dong Aisheng2b558c12013-10-30 22:09:48 +0800890 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800891 if (host->quirks2 &
892 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
893 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
894 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800895 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800896 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
897 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800898 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800899 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700900 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800901 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700902
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200903 WARN_ON(!host->data);
904
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800905 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
906 mode = SDHCI_TRNS_BLK_CNT_EN;
907
Andrei Warkentine89d4562011-05-23 15:06:37 -0500908 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800909 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500910 /*
911 * If we are sending CMD23, CMD12 never gets sent
912 * on successful completion (so no Auto-CMD12).
913 */
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300914 if (!cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800915 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500916 mode |= SDHCI_TRNS_AUTO_CMD12;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300917 else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500918 mode |= SDHCI_TRNS_AUTO_CMD23;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300919 sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500920 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700921 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500922
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700923 if (data->flags & MMC_DATA_READ)
924 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100925 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700926 mode |= SDHCI_TRNS_DMA;
927
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300928 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800929}
930
Adrian Hunter0cc563c2016-06-29 16:24:28 +0300931static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
932{
933 return (!(host->flags & SDHCI_DEVICE_DEAD) &&
934 ((mrq->cmd && mrq->cmd->error) ||
935 (mrq->sbc && mrq->sbc->error) ||
936 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
937 (mrq->data->stop && mrq->data->stop->error))) ||
938 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
939}
940
Adrian Huntera6d3bdd2016-06-29 16:24:27 +0300941static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
942{
Adrian Hunter5a8a3fe2016-06-29 16:24:30 +0300943 if (host->cmd && host->cmd->mrq == mrq)
944 host->cmd = NULL;
945
946 if (host->data_cmd && host->data_cmd->mrq == mrq)
947 host->data_cmd = NULL;
948
949 if (host->data && host->data->mrq == mrq)
950 host->data = NULL;
951
Adrian Huntered1563d2016-06-29 16:24:29 +0300952 if (sdhci_needs_reset(host, mrq))
953 host->pending_reset = true;
954
Adrian Huntera6d3bdd2016-06-29 16:24:27 +0300955 tasklet_schedule(&host->finish_tasklet);
956}
957
Pierre Ossmand129bce2006-03-24 03:18:17 -0800958static void sdhci_finish_data(struct sdhci_host *host)
959{
960 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800961
Pierre Ossmand129bce2006-03-24 03:18:17 -0800962 data = host->data;
963 host->data = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +0300964 host->data_cmd = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800965
Russell Kingadd89132016-01-26 13:40:42 +0000966 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
967 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
968 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800969
970 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200971 * The specification states that the block count register must
972 * be updated, but it does not specify at what point in the
973 * data flow. That makes the register entirely useless to read
974 * back so we have to assume that nothing made it to the card
975 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800976 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200977 if (data->error)
978 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800979 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200980 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800981
Andrei Warkentine89d4562011-05-23 15:06:37 -0500982 /*
983 * Need to send CMD12 if -
984 * a) open-ended multiblock transfer (no CMD23)
985 * b) error in multiblock transfer
986 */
987 if (data->stop &&
988 (data->error ||
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300989 !data->mrq->sbc)) {
Andrei Warkentine89d4562011-05-23 15:06:37 -0500990
Pierre Ossmand129bce2006-03-24 03:18:17 -0800991 /*
992 * The controller needs a reset of internal state machines
993 * upon error conditions.
994 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200995 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100996 sdhci_do_reset(host, SDHCI_RESET_CMD);
997 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998 }
999
1000 sdhci_send_command(host, data->stop);
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001001 } else {
1002 sdhci_finish_mrq(host, data->mrq);
1003 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001004}
1005
Dong Aishengc0e551292013-09-13 19:11:31 +08001006void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001007{
1008 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001009 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001010 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001011
1012 WARN_ON(host->cmd);
1013
Russell King96776202016-01-26 13:39:34 +00001014 /* Initially, a command has no error */
1015 cmd->error = 0;
1016
Pierre Ossmand129bce2006-03-24 03:18:17 -08001017 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001018 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001019
1020 mask = SDHCI_CMD_INHIBIT;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001021 if (sdhci_data_line_cmd(cmd))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001022 mask |= SDHCI_DATA_INHIBIT;
1023
1024 /* We shouldn't wait for data inihibit for stop commands, even
1025 though they might use busy signaling */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001026 if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001027 mask &= ~SDHCI_DATA_INHIBIT;
1028
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001029 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001030 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001031 pr_err("%s: Controller never released inhibit bit(s).\n",
1032 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001034 cmd->error = -EIO;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001035 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001036 return;
1037 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001038 timeout--;
1039 mdelay(1);
1040 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001041
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001042 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001043 if (!cmd->data && cmd->busy_timeout > 9000)
1044 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001045 else
1046 timeout += 10 * HZ;
1047 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001048
1049 host->cmd = cmd;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001050 if (sdhci_data_line_cmd(cmd)) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001051 WARN_ON(host->data_cmd);
1052 host->data_cmd = cmd;
1053 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001054
Andrei Warkentina3c77782011-04-11 16:13:42 -05001055 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001056
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001057 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001058
Andrei Warkentine89d4562011-05-23 15:06:37 -05001059 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001060
Pierre Ossmand129bce2006-03-24 03:18:17 -08001061 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301062 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001063 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001064 cmd->error = -EINVAL;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001065 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001066 return;
1067 }
1068
1069 if (!(cmd->flags & MMC_RSP_PRESENT))
1070 flags = SDHCI_CMD_RESP_NONE;
1071 else if (cmd->flags & MMC_RSP_136)
1072 flags = SDHCI_CMD_RESP_LONG;
1073 else if (cmd->flags & MMC_RSP_BUSY)
1074 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1075 else
1076 flags = SDHCI_CMD_RESP_SHORT;
1077
1078 if (cmd->flags & MMC_RSP_CRC)
1079 flags |= SDHCI_CMD_CRC;
1080 if (cmd->flags & MMC_RSP_OPCODE)
1081 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301082
1083 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301084 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1085 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001086 flags |= SDHCI_CMD_DATA;
1087
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001088 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001089}
Dong Aishengc0e551292013-09-13 19:11:31 +08001090EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001091
1092static void sdhci_finish_command(struct sdhci_host *host)
1093{
Adrian Huntere0a56402016-06-29 16:24:22 +03001094 struct mmc_command *cmd = host->cmd;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001095 int i;
1096
Adrian Huntere0a56402016-06-29 16:24:22 +03001097 host->cmd = NULL;
1098
1099 if (cmd->flags & MMC_RSP_PRESENT) {
1100 if (cmd->flags & MMC_RSP_136) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001101 /* CRC is stripped so we need to do some shifting. */
1102 for (i = 0;i < 4;i++) {
Adrian Huntere0a56402016-06-29 16:24:22 +03001103 cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001104 SDHCI_RESPONSE + (3-i)*4) << 8;
1105 if (i != 3)
Adrian Huntere0a56402016-06-29 16:24:22 +03001106 cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001107 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001108 SDHCI_RESPONSE + (3-i)*4-1);
1109 }
1110 } else {
Adrian Huntere0a56402016-06-29 16:24:22 +03001111 cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001112 }
1113 }
1114
Adrian Hunter6bde8682016-06-29 16:24:20 +03001115 /*
1116 * The host can send and interrupt when the busy state has
1117 * ended, allowing us to wait without wasting CPU cycles.
1118 * The busy signal uses DAT0 so this is similar to waiting
1119 * for data to complete.
1120 *
1121 * Note: The 1.0 specification is a bit ambiguous about this
1122 * feature so there might be some problems with older
1123 * controllers.
1124 */
Adrian Huntere0a56402016-06-29 16:24:22 +03001125 if (cmd->flags & MMC_RSP_BUSY) {
1126 if (cmd->data) {
Adrian Hunter6bde8682016-06-29 16:24:20 +03001127 DBG("Cannot wait for busy signal when also doing a data transfer");
1128 } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
Adrian Hunterea968022016-06-29 16:24:24 +03001129 cmd == host->data_cmd) {
1130 /* Command complete before busy is ended */
Adrian Hunter6bde8682016-06-29 16:24:20 +03001131 return;
1132 }
1133 }
1134
Andrei Warkentine89d4562011-05-23 15:06:37 -05001135 /* Finished CMD23, now send actual command. */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001136 if (cmd == cmd->mrq->sbc) {
1137 sdhci_send_command(host, cmd->mrq->cmd);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001138 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001139
Andrei Warkentine89d4562011-05-23 15:06:37 -05001140 /* Processed actual command. */
1141 if (host->data && host->data_early)
1142 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001143
Adrian Huntere0a56402016-06-29 16:24:22 +03001144 if (!cmd->data)
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001145 sdhci_finish_mrq(host, cmd->mrq);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001146 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001147}
1148
Kevin Liu52983382013-01-31 11:31:37 +08001149static u16 sdhci_get_preset_value(struct sdhci_host *host)
1150{
Russell Kingd975f122014-04-25 12:59:31 +01001151 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001152
Russell Kingd975f122014-04-25 12:59:31 +01001153 switch (host->timing) {
1154 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001155 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1156 break;
Russell Kingd975f122014-04-25 12:59:31 +01001157 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001158 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1159 break;
Russell Kingd975f122014-04-25 12:59:31 +01001160 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001161 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1162 break;
Russell Kingd975f122014-04-25 12:59:31 +01001163 case MMC_TIMING_UHS_SDR104:
1164 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001165 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1166 break;
Russell Kingd975f122014-04-25 12:59:31 +01001167 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001168 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001169 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1170 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001171 case MMC_TIMING_MMC_HS400:
1172 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1173 break;
Kevin Liu52983382013-01-31 11:31:37 +08001174 default:
1175 pr_warn("%s: Invalid UHS-I mode selected\n",
1176 mmc_hostname(host->mmc));
1177 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1178 break;
1179 }
1180 return preset;
1181}
1182
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001183u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1184 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001185{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301186 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001187 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301188 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001189 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001190
Zhangfei Gao85105c52010-08-06 07:10:01 +08001191 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001192 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001193 u16 pre_val;
1194
1195 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1196 pre_val = sdhci_get_preset_value(host);
1197 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1198 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1199 if (host->clk_mul &&
1200 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1201 clk = SDHCI_PROG_CLOCK_MODE;
1202 real_div = div + 1;
1203 clk_mul = host->clk_mul;
1204 } else {
1205 real_div = max_t(int, 1, div << 1);
1206 }
1207 goto clock_set;
1208 }
1209
Arindam Nathc3ed3872011-05-05 12:19:06 +05301210 /*
1211 * Check if the Host Controller supports Programmable Clock
1212 * Mode.
1213 */
1214 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001215 for (div = 1; div <= 1024; div++) {
1216 if ((host->max_clk * host->clk_mul / div)
1217 <= clock)
1218 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001219 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001220 if ((host->max_clk * host->clk_mul / div) <= clock) {
1221 /*
1222 * Set Programmable Clock Mode in the Clock
1223 * Control register.
1224 */
1225 clk = SDHCI_PROG_CLOCK_MODE;
1226 real_div = div;
1227 clk_mul = host->clk_mul;
1228 div--;
1229 } else {
1230 /*
1231 * Divisor can be too small to reach clock
1232 * speed requirement. Then use the base clock.
1233 */
1234 switch_base_clk = true;
1235 }
1236 }
1237
1238 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301239 /* Version 3.00 divisors must be a multiple of 2. */
1240 if (host->max_clk <= clock)
1241 div = 1;
1242 else {
1243 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1244 div += 2) {
1245 if ((host->max_clk / div) <= clock)
1246 break;
1247 }
1248 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001249 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301250 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301251 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1252 && !div && host->max_clk <= 25000000)
1253 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001254 }
1255 } else {
1256 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001257 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001258 if ((host->max_clk / div) <= clock)
1259 break;
1260 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001261 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301262 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001263 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001264
Kevin Liu52983382013-01-31 11:31:37 +08001265clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001266 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001267 *actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301268 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001269 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1270 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001271
1272 return clk;
1273}
1274EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1275
1276void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1277{
1278 u16 clk;
1279 unsigned long timeout;
1280
1281 host->mmc->actual_clock = 0;
1282
1283 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001284
1285 if (clock == 0)
1286 return;
1287
1288 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1289
Pierre Ossmand129bce2006-03-24 03:18:17 -08001290 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001291 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001292
Chris Ball27f6cb12009-09-22 16:45:31 -07001293 /* Wait max 20 ms */
1294 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001295 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001296 & SDHCI_CLOCK_INT_STABLE)) {
1297 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001298 pr_err("%s: Internal clock never stabilised.\n",
1299 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001300 sdhci_dumpregs(host);
1301 return;
1302 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001303 timeout--;
1304 mdelay(1);
1305 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001306
1307 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001308 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001309}
Russell King17710592014-04-25 12:58:55 +01001310EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001311
Adrian Hunter1dceb042016-03-29 12:45:43 +03001312static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1313 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001314{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001315 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001316
1317 spin_unlock_irq(&host->lock);
1318 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1319 spin_lock_irq(&host->lock);
1320
1321 if (mode != MMC_POWER_OFF)
1322 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1323 else
1324 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1325}
1326
1327void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1328 unsigned short vdd)
1329{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001330 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001331
Russell King24fbb3c2014-04-25 13:00:06 +01001332 if (mode != MMC_POWER_OFF) {
1333 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001334 case MMC_VDD_165_195:
1335 pwr = SDHCI_POWER_180;
1336 break;
1337 case MMC_VDD_29_30:
1338 case MMC_VDD_30_31:
1339 pwr = SDHCI_POWER_300;
1340 break;
1341 case MMC_VDD_32_33:
1342 case MMC_VDD_33_34:
1343 pwr = SDHCI_POWER_330;
1344 break;
1345 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001346 WARN(1, "%s: Invalid vdd %#x\n",
1347 mmc_hostname(host->mmc), vdd);
1348 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001349 }
1350 }
1351
1352 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001353 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001354
Pierre Ossmanae628902009-05-03 20:45:03 +02001355 host->pwr = pwr;
1356
1357 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001358 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001359 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1360 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001361 } else {
1362 /*
1363 * Spec says that we should clear the power reg before setting
1364 * a new value. Some controllers don't seem to like this though.
1365 */
1366 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1367 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001368
Russell Kinge921a8b2014-04-25 13:00:01 +01001369 /*
1370 * At least the Marvell CaFe chip gets confused if we set the
1371 * voltage and set turn on power at the same time, so set the
1372 * voltage first.
1373 */
1374 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1375 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001376
Russell Kinge921a8b2014-04-25 13:00:01 +01001377 pwr |= SDHCI_POWER_ON;
1378
Pierre Ossmanae628902009-05-03 20:45:03 +02001379 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1380
Russell Kinge921a8b2014-04-25 13:00:01 +01001381 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1382 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001383
Russell Kinge921a8b2014-04-25 13:00:01 +01001384 /*
1385 * Some controllers need an extra 10ms delay of 10ms before
1386 * they can apply clock after applying power
1387 */
1388 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1389 mdelay(10);
1390 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001391}
1392EXPORT_SYMBOL_GPL(sdhci_set_power);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001393
Adrian Hunter1dceb042016-03-29 12:45:43 +03001394static void __sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1395 unsigned short vdd)
1396{
1397 struct mmc_host *mmc = host->mmc;
1398
1399 if (host->ops->set_power)
1400 host->ops->set_power(host, mode, vdd);
1401 else if (!IS_ERR(mmc->supply.vmmc))
1402 sdhci_set_power_reg(host, mode, vdd);
1403 else
1404 sdhci_set_power(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001405}
1406
Pierre Ossmand129bce2006-03-24 03:18:17 -08001407/*****************************************************************************\
1408 * *
1409 * MMC callbacks *
1410 * *
1411\*****************************************************************************/
1412
1413static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1414{
1415 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001416 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001417 unsigned long flags;
1418
1419 host = mmc_priv(mmc);
1420
Scott Branden04e079c2015-03-10 11:35:10 -07001421 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001422 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001423
Pierre Ossmand129bce2006-03-24 03:18:17 -08001424 spin_lock_irqsave(&host->lock, flags);
1425
1426 WARN_ON(host->mrq != NULL);
1427
Adrian Hunter061d17a2016-04-12 14:25:09 +03001428 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001429
1430 /*
1431 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1432 * requests if Auto-CMD12 is enabled.
1433 */
1434 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001435 if (mrq->stop) {
1436 mrq->data->stop = NULL;
1437 mrq->stop = NULL;
1438 }
1439 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001440
1441 host->mrq = mrq;
1442
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001443 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001444 mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001445 sdhci_finish_mrq(host, mrq);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301446 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001447 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001448 sdhci_send_command(host, mrq->sbc);
1449 else
1450 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301451 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001452
Pierre Ossman5f25a662006-10-04 02:15:39 -07001453 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001454 spin_unlock_irqrestore(&host->lock, flags);
1455}
1456
Russell King2317f562014-04-25 12:57:07 +01001457void sdhci_set_bus_width(struct sdhci_host *host, int width)
1458{
1459 u8 ctrl;
1460
1461 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1462 if (width == MMC_BUS_WIDTH_8) {
1463 ctrl &= ~SDHCI_CTRL_4BITBUS;
1464 if (host->version >= SDHCI_SPEC_300)
1465 ctrl |= SDHCI_CTRL_8BITBUS;
1466 } else {
1467 if (host->version >= SDHCI_SPEC_300)
1468 ctrl &= ~SDHCI_CTRL_8BITBUS;
1469 if (width == MMC_BUS_WIDTH_4)
1470 ctrl |= SDHCI_CTRL_4BITBUS;
1471 else
1472 ctrl &= ~SDHCI_CTRL_4BITBUS;
1473 }
1474 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1475}
1476EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1477
Russell King96d7b782014-04-25 12:59:26 +01001478void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1479{
1480 u16 ctrl_2;
1481
1482 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1483 /* Select Bus Speed Mode for host */
1484 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1485 if ((timing == MMC_TIMING_MMC_HS200) ||
1486 (timing == MMC_TIMING_UHS_SDR104))
1487 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1488 else if (timing == MMC_TIMING_UHS_SDR12)
1489 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1490 else if (timing == MMC_TIMING_UHS_SDR25)
1491 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1492 else if (timing == MMC_TIMING_UHS_SDR50)
1493 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1494 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1495 (timing == MMC_TIMING_MMC_DDR52))
1496 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001497 else if (timing == MMC_TIMING_MMC_HS400)
1498 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001499 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1500}
1501EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1502
Dong Aishengded97e02016-04-16 01:29:25 +08001503static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001504{
Dong Aishengded97e02016-04-16 01:29:25 +08001505 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001506 unsigned long flags;
1507 u8 ctrl;
1508
Pierre Ossmand129bce2006-03-24 03:18:17 -08001509 spin_lock_irqsave(&host->lock, flags);
1510
Adrian Hunterceb61432011-12-27 15:48:41 +02001511 if (host->flags & SDHCI_DEVICE_DEAD) {
1512 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001513 if (!IS_ERR(mmc->supply.vmmc) &&
1514 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001515 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001516 return;
1517 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001518
Pierre Ossmand129bce2006-03-24 03:18:17 -08001519 /*
1520 * Reset the chip on each power off.
1521 * Should clear out any weird states.
1522 */
1523 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001524 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001525 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001526 }
1527
Kevin Liu52983382013-01-31 11:31:37 +08001528 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001529 (ios->power_mode == MMC_POWER_UP) &&
1530 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001531 sdhci_enable_preset_value(host, false);
1532
Russell King373073e2014-04-25 12:58:45 +01001533 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001534 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001535 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001536
1537 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1538 host->clock) {
1539 host->timeout_clk = host->mmc->actual_clock ?
1540 host->mmc->actual_clock / 1000 :
1541 host->clock / 1000;
1542 host->mmc->max_busy_timeout =
1543 host->ops->get_max_timeout_count ?
1544 host->ops->get_max_timeout_count(host) :
1545 1 << 27;
1546 host->mmc->max_busy_timeout /= host->timeout_clk;
1547 }
Russell King373073e2014-04-25 12:58:45 +01001548 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001549
Adrian Hunter1dceb042016-03-29 12:45:43 +03001550 __sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001551
Philip Rakity643a81f2010-09-23 08:24:32 -07001552 if (host->ops->platform_send_init_74_clocks)
1553 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1554
Russell King2317f562014-04-25 12:57:07 +01001555 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001556
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001557 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001558
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001559 if ((ios->timing == MMC_TIMING_SD_HS ||
1560 ios->timing == MMC_TIMING_MMC_HS)
1561 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001562 ctrl |= SDHCI_CTRL_HISPD;
1563 else
1564 ctrl &= ~SDHCI_CTRL_HISPD;
1565
Arindam Nathd6d50a12011-05-05 12:18:59 +05301566 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301567 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301568
1569 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001570 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1571 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001572 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301573 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301574 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1575 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001576 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301577 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301578
Russell Kingda91a8f2014-04-25 13:00:12 +01001579 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301580 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301581 /*
1582 * We only need to set Driver Strength if the
1583 * preset value enable is not set.
1584 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001585 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301586 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1587 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1588 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001589 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1590 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301591 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1592 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001593 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1594 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1595 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001596 pr_warn("%s: invalid driver type, default to driver type B\n",
1597 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001598 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1599 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301600
1601 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301602 } else {
1603 /*
1604 * According to SDHC Spec v3.00, if the Preset Value
1605 * Enable in the Host Control 2 register is set, we
1606 * need to reset SD Clock Enable before changing High
1607 * Speed Enable to avoid generating clock gliches.
1608 */
Arindam Nath758535c2011-05-05 12:19:00 +05301609
1610 /* Reset SD Clock Enable */
1611 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1612 clk &= ~SDHCI_CLOCK_CARD_EN;
1613 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1614
1615 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1616
1617 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001618 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301619 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301620
Arindam Nath49c468f2011-05-05 12:19:01 +05301621 /* Reset SD Clock Enable */
1622 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1623 clk &= ~SDHCI_CLOCK_CARD_EN;
1624 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1625
Russell King96d7b782014-04-25 12:59:26 +01001626 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001627 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301628
Kevin Liu52983382013-01-31 11:31:37 +08001629 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1630 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1631 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1632 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1633 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001634 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1635 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001636 u16 preset;
1637
1638 sdhci_enable_preset_value(host, true);
1639 preset = sdhci_get_preset_value(host);
1640 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1641 >> SDHCI_PRESET_DRV_SHIFT;
1642 }
1643
Arindam Nath49c468f2011-05-05 12:19:01 +05301644 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001645 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301646 } else
1647 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301648
Leandro Dorileob8352262007-07-25 23:47:04 +02001649 /*
1650 * Some (ENE) controllers go apeshit on some ios operation,
1651 * signalling timeout and CRC errors even on CMD0. Resetting
1652 * it on each ios seems to solve the problem.
1653 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301654 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001655 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001656
Pierre Ossman5f25a662006-10-04 02:15:39 -07001657 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001658 spin_unlock_irqrestore(&host->lock, flags);
1659}
1660
Dong Aishengded97e02016-04-16 01:29:25 +08001661static int sdhci_get_cd(struct mmc_host *mmc)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001662{
1663 struct sdhci_host *host = mmc_priv(mmc);
Dong Aishengded97e02016-04-16 01:29:25 +08001664 int gpio_cd = mmc_gpio_get_cd(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08001665
1666 if (host->flags & SDHCI_DEVICE_DEAD)
1667 return 0;
1668
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001669 /* If nonremovable, assume that the card is always present. */
Jaehoon Chung860951c2016-06-21 10:13:26 +09001670 if (!mmc_card_is_removable(host->mmc))
Kevin Liu94144a42013-02-28 17:35:53 +08001671 return 1;
1672
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001673 /*
1674 * Try slot gpio detect, if defined it take precedence
1675 * over build in controller functionality
1676 */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001677 if (gpio_cd >= 0)
Kevin Liu94144a42013-02-28 17:35:53 +08001678 return !!gpio_cd;
1679
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001680 /* If polling, assume that the card is always present. */
1681 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1682 return 1;
1683
Kevin Liu94144a42013-02-28 17:35:53 +08001684 /* Host native card detect */
1685 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1686}
1687
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001688static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001689{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001690 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001691 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001692
Pierre Ossmand129bce2006-03-24 03:18:17 -08001693 spin_lock_irqsave(&host->lock, flags);
1694
Pierre Ossman1e728592008-04-16 19:13:13 +02001695 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001696 is_readonly = 0;
1697 else if (host->ops->get_ro)
1698 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001699 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001700 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1701 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001702
1703 spin_unlock_irqrestore(&host->lock, flags);
1704
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001705 /* This quirk needs to be replaced by a callback-function later */
1706 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1707 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001708}
1709
Takashi Iwai82b0e232011-04-21 20:26:38 +02001710#define SAMPLE_COUNT 5
1711
Dong Aishengded97e02016-04-16 01:29:25 +08001712static int sdhci_get_ro(struct mmc_host *mmc)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001713{
Dong Aishengded97e02016-04-16 01:29:25 +08001714 struct sdhci_host *host = mmc_priv(mmc);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001715 int i, ro_count;
1716
Takashi Iwai82b0e232011-04-21 20:26:38 +02001717 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001718 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001719
1720 ro_count = 0;
1721 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001722 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001723 if (++ro_count > SAMPLE_COUNT / 2)
1724 return 1;
1725 }
1726 msleep(30);
1727 }
1728 return 0;
1729}
1730
Adrian Hunter20758b62011-08-29 16:42:12 +03001731static void sdhci_hw_reset(struct mmc_host *mmc)
1732{
1733 struct sdhci_host *host = mmc_priv(mmc);
1734
1735 if (host->ops && host->ops->hw_reset)
1736 host->ops->hw_reset(host);
1737}
1738
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001739static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1740{
Russell Kingbe138552014-04-25 12:55:56 +01001741 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001742 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001743 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001744 else
Russell Kingb537f942014-04-25 12:56:01 +01001745 host->ier &= ~SDHCI_INT_CARD_INT;
1746
1747 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1748 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001749 mmiowb();
1750 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001751}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001752
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001753static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1754{
1755 struct sdhci_host *host = mmc_priv(mmc);
1756 unsigned long flags;
1757
1758 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001759 if (enable)
1760 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1761 else
1762 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1763
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001764 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001765 spin_unlock_irqrestore(&host->lock, flags);
1766}
1767
Dong Aishengded97e02016-04-16 01:29:25 +08001768static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1769 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001770{
Dong Aishengded97e02016-04-16 01:29:25 +08001771 struct sdhci_host *host = mmc_priv(mmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07001772 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001773 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001774
1775 /*
1776 * Signal Voltage Switching is only applicable for Host Controllers
1777 * v3.00 and above.
1778 */
1779 if (host->version < SDHCI_SPEC_300)
1780 return 0;
1781
Philip Rakity6231f3d2012-07-23 15:56:23 -07001782 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001783
Fabio Estevam21f59982013-02-14 10:35:03 -02001784 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001785 case MMC_SIGNAL_VOLTAGE_330:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001786 if (!(host->flags & SDHCI_SIGNALING_330))
1787 return -EINVAL;
Kevin Liu20b92a32012-12-17 19:29:26 +08001788 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1789 ctrl &= ~SDHCI_CTRL_VDD_180;
1790 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1791
Tim Kryger3a48edc2014-06-13 10:13:56 -07001792 if (!IS_ERR(mmc->supply.vqmmc)) {
1793 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1794 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001795 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001796 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1797 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001798 return -EIO;
1799 }
1800 }
1801 /* Wait for 5ms */
1802 usleep_range(5000, 5500);
1803
1804 /* 3.3V regulator output should be stable within 5 ms */
1805 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1806 if (!(ctrl & SDHCI_CTRL_VDD_180))
1807 return 0;
1808
Joe Perches66061102014-09-12 14:56:56 -07001809 pr_warn("%s: 3.3V regulator output did not became stable\n",
1810 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001811
1812 return -EAGAIN;
1813 case MMC_SIGNAL_VOLTAGE_180:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001814 if (!(host->flags & SDHCI_SIGNALING_180))
1815 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001816 if (!IS_ERR(mmc->supply.vqmmc)) {
1817 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001818 1700000, 1950000);
1819 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001820 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1821 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001822 return -EIO;
1823 }
1824 }
1825
1826 /*
1827 * Enable 1.8V Signal Enable in the Host Control2
1828 * register
1829 */
1830 ctrl |= SDHCI_CTRL_VDD_180;
1831 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1832
Vincent Yang9d967a62015-01-20 16:05:15 +08001833 /* Some controller need to do more when switching */
1834 if (host->ops->voltage_switch)
1835 host->ops->voltage_switch(host);
1836
Kevin Liu20b92a32012-12-17 19:29:26 +08001837 /* 1.8V regulator output should be stable within 5 ms */
1838 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1839 if (ctrl & SDHCI_CTRL_VDD_180)
1840 return 0;
1841
Joe Perches66061102014-09-12 14:56:56 -07001842 pr_warn("%s: 1.8V regulator output did not became stable\n",
1843 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001844
1845 return -EAGAIN;
1846 case MMC_SIGNAL_VOLTAGE_120:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001847 if (!(host->flags & SDHCI_SIGNALING_120))
1848 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001849 if (!IS_ERR(mmc->supply.vqmmc)) {
1850 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1851 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001852 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001853 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1854 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001855 return -EIO;
1856 }
1857 }
1858 return 0;
1859 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301860 /* No signal voltage switch required */
1861 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001862 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301863}
1864
Kevin Liu20b92a32012-12-17 19:29:26 +08001865static int sdhci_card_busy(struct mmc_host *mmc)
1866{
1867 struct sdhci_host *host = mmc_priv(mmc);
1868 u32 present_state;
1869
Adrian Huntere613cc42016-06-23 14:00:58 +03001870 /* Check whether DAT[0] is 0 */
Kevin Liu20b92a32012-12-17 19:29:26 +08001871 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08001872
Adrian Huntere613cc42016-06-23 14:00:58 +03001873 return !(present_state & SDHCI_DATA_0_LVL_MASK);
Kevin Liu20b92a32012-12-17 19:29:26 +08001874}
1875
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001876static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1877{
1878 struct sdhci_host *host = mmc_priv(mmc);
1879 unsigned long flags;
1880
1881 spin_lock_irqsave(&host->lock, flags);
1882 host->flags |= SDHCI_HS400_TUNING;
1883 spin_unlock_irqrestore(&host->lock, flags);
1884
1885 return 0;
1886}
1887
Girish K S069c9f12012-01-06 09:56:39 +05301888static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301889{
Russell King4b6f37d2014-04-25 12:59:36 +01001890 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301891 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301892 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301893 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001894 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001895 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001896 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301897
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001898 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301899
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001900 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1901 host->flags &= ~SDHCI_HS400_TUNING;
1902
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001903 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1904 tuning_count = host->tuning_count;
1905
Arindam Nathb513ea22011-05-05 12:19:04 +05301906 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001907 * The Host Controller needs tuning in case of SDR104 and DDR50
1908 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1909 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301910 * If the Host Controller supports the HS200 mode then the
1911 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301912 */
Russell King4b6f37d2014-04-25 12:59:36 +01001913 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001914 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001915 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001916 err = -EINVAL;
1917 goto out_unlock;
1918
Russell King4b6f37d2014-04-25 12:59:36 +01001919 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001920 /*
1921 * Periodic re-tuning for HS400 is not expected to be needed, so
1922 * disable it here.
1923 */
1924 if (hs400_tuning)
1925 tuning_count = 0;
1926 break;
1927
Russell King4b6f37d2014-04-25 12:59:36 +01001928 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001929 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001930 break;
Girish K S069c9f12012-01-06 09:56:39 +05301931
Russell King4b6f37d2014-04-25 12:59:36 +01001932 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03001933 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01001934 break;
1935 /* FALLTHROUGH */
1936
1937 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001938 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301939 }
1940
Dong Aisheng45251812013-09-13 19:11:30 +08001941 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001942 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001943 err = host->ops->platform_execute_tuning(host, opcode);
Dong Aisheng45251812013-09-13 19:11:30 +08001944 return err;
1945 }
1946
Russell King4b6f37d2014-04-25 12:59:36 +01001947 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1948 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001949 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1950 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301951 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1952
1953 /*
1954 * As per the Host Controller spec v3.00, tuning command
1955 * generates Buffer Read Ready interrupt, so enable that.
1956 *
1957 * Note: The spec clearly says that when tuning sequence
1958 * is being performed, the controller does not generate
1959 * interrupts other than Buffer Read Ready interrupt. But
1960 * to make sure we don't hit a controller bug, we _only_
1961 * enable Buffer Read Ready interrupt here.
1962 */
Russell Kingb537f942014-04-25 12:56:01 +01001963 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1964 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301965
1966 /*
1967 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
Simon Horman1473bdd2016-05-13 13:24:31 +09001968 * of loops reaches 40 times.
Arindam Nathb513ea22011-05-05 12:19:04 +05301969 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301970 do {
1971 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001972 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301973
Girish K S069c9f12012-01-06 09:56:39 +05301974 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301975 cmd.arg = 0;
1976 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1977 cmd.retries = 0;
1978 cmd.data = NULL;
1979 cmd.error = 0;
1980
Al Cooper7ce45e92014-05-09 11:34:07 -04001981 if (tuning_loop_counter-- == 0)
1982 break;
1983
Arindam Nathb513ea22011-05-05 12:19:04 +05301984 mrq.cmd = &cmd;
1985 host->mrq = &mrq;
1986
1987 /*
1988 * In response to CMD19, the card sends 64 bytes of tuning
1989 * block to the Host Controller. So we set the block size
1990 * to 64 here.
1991 */
Girish K S069c9f12012-01-06 09:56:39 +05301992 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1993 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1994 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1995 SDHCI_BLOCK_SIZE);
1996 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1997 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1998 SDHCI_BLOCK_SIZE);
1999 } else {
2000 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2001 SDHCI_BLOCK_SIZE);
2002 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302003
2004 /*
2005 * The tuning block is sent by the card to the host controller.
2006 * So we set the TRNS_READ bit in the Transfer Mode register.
2007 * This also takes care of setting DMA Enable and Multi Block
2008 * Select in the same register to 0.
2009 */
2010 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2011
2012 sdhci_send_command(host, &cmd);
2013
2014 host->cmd = NULL;
2015 host->mrq = NULL;
2016
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002017 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302018 /* Wait for Buffer Read Ready interrupt */
2019 wait_event_interruptible_timeout(host->buf_ready_int,
2020 (host->tuning_done == 1),
2021 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002022 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302023
2024 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002025 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 +05302026 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2027 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2028 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2029 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2030
2031 err = -EIO;
2032 goto out;
2033 }
2034
2035 host->tuning_done = 0;
2036
2037 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07002038
2039 /* eMMC spec does not require a delay between tuning cycles */
2040 if (opcode == MMC_SEND_TUNING_BLOCK)
2041 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302042 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2043
2044 /*
2045 * The Host Driver has exhausted the maximum number of loops allowed,
2046 * so use fixed sampling frequency.
2047 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002048 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302049 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2050 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002051 }
2052 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002053 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002054 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302055 }
2056
2057out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002058 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002059 /*
2060 * In case tuning fails, host controllers which support
2061 * re-tuning can try tuning again at a later time, when the
2062 * re-tuning timer expires. So for these controllers, we
2063 * return 0. Since there might be other controllers who do not
2064 * have this capability, we return error for them.
2065 */
2066 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302067 }
2068
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002069 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302070
Russell Kingb537f942014-04-25 12:56:01 +01002071 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2072 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002073out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002074 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302075 return err;
2076}
2077
Adrian Huntercb849642015-02-06 14:12:59 +02002078static int sdhci_select_drive_strength(struct mmc_card *card,
2079 unsigned int max_dtr, int host_drv,
2080 int card_drv, int *drv_type)
2081{
2082 struct sdhci_host *host = mmc_priv(card->host);
2083
2084 if (!host->ops->select_drive_strength)
2085 return 0;
2086
2087 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2088 card_drv, drv_type);
2089}
Kevin Liu52983382013-01-31 11:31:37 +08002090
2091static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302092{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302093 /* Host Controller v3.00 defines preset value registers */
2094 if (host->version < SDHCI_SPEC_300)
2095 return;
2096
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302097 /*
2098 * We only enable or disable Preset Value if they are not already
2099 * enabled or disabled respectively. Otherwise, we bail out.
2100 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002101 if (host->preset_enabled != enable) {
2102 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2103
2104 if (enable)
2105 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2106 else
2107 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2108
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302109 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002110
2111 if (enable)
2112 host->flags |= SDHCI_PV_ENABLED;
2113 else
2114 host->flags &= ~SDHCI_PV_ENABLED;
2115
2116 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302117 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002118}
2119
Haibo Chen348487c2014-12-09 17:04:05 +08002120static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2121 int err)
2122{
2123 struct sdhci_host *host = mmc_priv(mmc);
2124 struct mmc_data *data = mrq->data;
2125
Russell Kingf48f0392016-01-26 13:40:32 +00002126 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002127 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2128 data->flags & MMC_DATA_WRITE ?
2129 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2130
2131 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002132}
2133
Haibo Chen348487c2014-12-09 17:04:05 +08002134static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2135 bool is_first_req)
2136{
2137 struct sdhci_host *host = mmc_priv(mmc);
2138
Haibo Chend31911b2015-08-25 10:02:11 +08002139 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002140
2141 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002142 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002143}
2144
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002145static inline bool sdhci_has_requests(struct sdhci_host *host)
2146{
2147 return host->cmd || host->data_cmd;
2148}
2149
2150static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
2151{
2152 if (host->data_cmd) {
2153 host->data_cmd->error = err;
2154 sdhci_finish_mrq(host, host->data_cmd->mrq);
2155 }
2156
2157 if (host->cmd) {
2158 host->cmd->error = err;
2159 sdhci_finish_mrq(host, host->cmd->mrq);
2160 }
2161}
2162
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002163static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002164{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002165 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002166 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002167 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002168
Christian Daudt722e1282013-06-20 14:26:36 -07002169 /* First check if client has provided their own card event */
2170 if (host->ops->card_event)
2171 host->ops->card_event(host);
2172
Adrian Hunterd3940f22016-06-29 16:24:14 +03002173 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002174
Pierre Ossmand129bce2006-03-24 03:18:17 -08002175 spin_lock_irqsave(&host->lock, flags);
2176
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002177 /* Check sdhci_has_requests() first in case we are runtime suspended */
2178 if (sdhci_has_requests(host) && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302179 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002180 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302181 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002182 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002183
Russell King03231f92014-04-25 12:57:12 +01002184 sdhci_do_reset(host, SDHCI_RESET_CMD);
2185 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002186
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002187 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002188 }
2189
2190 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002191}
2192
2193static const struct mmc_host_ops sdhci_ops = {
2194 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002195 .post_req = sdhci_post_req,
2196 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002197 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002198 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002199 .get_ro = sdhci_get_ro,
2200 .hw_reset = sdhci_hw_reset,
2201 .enable_sdio_irq = sdhci_enable_sdio_irq,
2202 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002203 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002204 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002205 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002206 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002207 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002208};
2209
2210/*****************************************************************************\
2211 * *
2212 * Tasklets *
2213 * *
2214\*****************************************************************************/
2215
Pierre Ossmand129bce2006-03-24 03:18:17 -08002216static void sdhci_tasklet_finish(unsigned long param)
2217{
2218 struct sdhci_host *host;
2219 unsigned long flags;
2220 struct mmc_request *mrq;
2221
2222 host = (struct sdhci_host*)param;
2223
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002224 spin_lock_irqsave(&host->lock, flags);
2225
Chris Ball0c9c99a2011-04-27 17:35:31 -04002226 /*
2227 * If this tasklet gets rescheduled while running, it will
2228 * be run again afterwards but without any active request.
2229 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002230 if (!host->mrq) {
2231 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002232 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002233 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002234
2235 del_timer(&host->timer);
2236
2237 mrq = host->mrq;
2238
Pierre Ossmand129bce2006-03-24 03:18:17 -08002239 /*
Russell King054cedf2016-01-26 13:40:42 +00002240 * Always unmap the data buffers if they were mapped by
2241 * sdhci_prepare_data() whenever we finish with a request.
2242 * This avoids leaking DMA mappings on error.
2243 */
2244 if (host->flags & SDHCI_REQ_USE_DMA) {
2245 struct mmc_data *data = mrq->data;
2246
2247 if (data && data->host_cookie == COOKIE_MAPPED) {
2248 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2249 (data->flags & MMC_DATA_READ) ?
2250 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2251 data->host_cookie = COOKIE_UNMAPPED;
2252 }
2253 }
2254
2255 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002256 * The controller needs a reset of internal state machines
2257 * upon error conditions.
2258 */
Adrian Hunter0cc563c2016-06-29 16:24:28 +03002259 if (sdhci_needs_reset(host, mrq)) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002260 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002261 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002262 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002263 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002264
2265 /* Spec says we should do both at the same time, but Ricoh
2266 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002267 sdhci_do_reset(host, SDHCI_RESET_CMD);
2268 sdhci_do_reset(host, SDHCI_RESET_DATA);
Adrian Huntered1563d2016-06-29 16:24:29 +03002269
2270 host->pending_reset = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002271 }
2272
2273 host->mrq = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002274
Adrian Hunter061d17a2016-04-12 14:25:09 +03002275 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002276
Pierre Ossman5f25a662006-10-04 02:15:39 -07002277 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002278 spin_unlock_irqrestore(&host->lock, flags);
2279
2280 mmc_request_done(host->mmc, mrq);
2281}
2282
2283static void sdhci_timeout_timer(unsigned long data)
2284{
2285 struct sdhci_host *host;
2286 unsigned long flags;
2287
2288 host = (struct sdhci_host*)data;
2289
2290 spin_lock_irqsave(&host->lock, flags);
2291
2292 if (host->mrq) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002293 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2294 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002295 sdhci_dumpregs(host);
2296
2297 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002298 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002299 sdhci_finish_data(host);
2300 } else {
2301 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002302 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002303 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002304 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002305
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002306 sdhci_finish_mrq(host, host->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002307 }
2308 }
2309
Pierre Ossman5f25a662006-10-04 02:15:39 -07002310 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002311 spin_unlock_irqrestore(&host->lock, flags);
2312}
2313
2314/*****************************************************************************\
2315 * *
2316 * Interrupt handling *
2317 * *
2318\*****************************************************************************/
2319
Adrian Hunter61541392014-09-24 10:27:27 +03002320static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002321{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002322 if (!host->cmd) {
Adrian Huntered1563d2016-06-29 16:24:29 +03002323 /*
2324 * SDHCI recovers from errors by resetting the cmd and data
2325 * circuits. Until that is done, there very well might be more
2326 * interrupts, so ignore them in that case.
2327 */
2328 if (host->pending_reset)
2329 return;
Marek Vasut2e4456f2015-11-18 10:47:02 +01002330 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2331 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002332 sdhci_dumpregs(host);
2333 return;
2334 }
2335
Russell Kingec014cb2016-01-26 13:39:39 +00002336 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2337 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2338 if (intmask & SDHCI_INT_TIMEOUT)
2339 host->cmd->error = -ETIMEDOUT;
2340 else
2341 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002342
Russell King71fcbda2016-01-26 13:39:45 +00002343 /*
2344 * If this command initiates a data phase and a response
2345 * CRC error is signalled, the card can start transferring
2346 * data - the card may have received the command without
2347 * error. We must not terminate the mmc_request early.
2348 *
2349 * If the card did not receive the command or returned an
2350 * error which prevented it sending data, the data phase
2351 * will time out.
2352 */
2353 if (host->cmd->data &&
2354 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2355 SDHCI_INT_CRC) {
2356 host->cmd = NULL;
2357 return;
2358 }
2359
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002360 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002361 return;
2362 }
2363
Adrian Hunter6bde8682016-06-29 16:24:20 +03002364 if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2365 !(host->cmd->flags & MMC_RSP_BUSY) && !host->data &&
2366 host->cmd->opcode == MMC_STOP_TRANSMISSION)
Adrian Hunter61541392014-09-24 10:27:27 +03002367 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002368
2369 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002370 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002371}
2372
George G. Davis0957c332010-02-18 12:32:12 -05002373#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002374static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002375{
2376 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002377 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002378
2379 sdhci_dumpregs(host);
2380
2381 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002382 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002383
Adrian Huntere57a5f62014-11-04 12:42:46 +02002384 if (host->flags & SDHCI_USE_64_BIT_DMA)
2385 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2386 name, desc, le32_to_cpu(dma_desc->addr_hi),
2387 le32_to_cpu(dma_desc->addr_lo),
2388 le16_to_cpu(dma_desc->len),
2389 le16_to_cpu(dma_desc->cmd));
2390 else
2391 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2392 name, desc, le32_to_cpu(dma_desc->addr_lo),
2393 le16_to_cpu(dma_desc->len),
2394 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002395
Adrian Hunter76fe3792014-11-04 12:42:42 +02002396 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002397
Adrian Hunter05452302014-11-04 12:42:45 +02002398 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002399 break;
2400 }
2401}
2402#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002403static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002404#endif
2405
Pierre Ossmand129bce2006-03-24 03:18:17 -08002406static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2407{
Girish K S069c9f12012-01-06 09:56:39 +05302408 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002409
Arindam Nathb513ea22011-05-05 12:19:04 +05302410 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2411 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302412 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2413 if (command == MMC_SEND_TUNING_BLOCK ||
2414 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302415 host->tuning_done = 1;
2416 wake_up(&host->buf_ready_int);
2417 return;
2418 }
2419 }
2420
Pierre Ossmand129bce2006-03-24 03:18:17 -08002421 if (!host->data) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002422 struct mmc_command *data_cmd = host->data_cmd;
2423
2424 if (data_cmd)
2425 host->data_cmd = NULL;
2426
Pierre Ossmand129bce2006-03-24 03:18:17 -08002427 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002428 * The "data complete" interrupt is also used to
2429 * indicate that a busy state has ended. See comment
2430 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002431 */
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002432 if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002433 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002434 data_cmd->error = -ETIMEDOUT;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002435 sdhci_finish_mrq(host, data_cmd->mrq);
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002436 return;
2437 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002438 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002439 /*
2440 * Some cards handle busy-end interrupt
2441 * before the command completed, so make
2442 * sure we do things in the proper order.
2443 */
Adrian Hunterea968022016-06-29 16:24:24 +03002444 if (host->cmd == data_cmd)
2445 return;
2446
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002447 sdhci_finish_mrq(host, data_cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002448 return;
2449 }
2450 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002451
Adrian Huntered1563d2016-06-29 16:24:29 +03002452 /*
2453 * SDHCI recovers from errors by resetting the cmd and data
2454 * circuits. Until that is done, there very well might be more
2455 * interrupts, so ignore them in that case.
2456 */
2457 if (host->pending_reset)
2458 return;
2459
Marek Vasut2e4456f2015-11-18 10:47:02 +01002460 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2461 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002462 sdhci_dumpregs(host);
2463
2464 return;
2465 }
2466
2467 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002468 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002469 else if (intmask & SDHCI_INT_DATA_END_BIT)
2470 host->data->error = -EILSEQ;
2471 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2472 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2473 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002474 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002475 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302476 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002477 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002478 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002479 if (host->ops->adma_workaround)
2480 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002481 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002482
Pierre Ossman17b04292007-07-22 22:18:46 +02002483 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002484 sdhci_finish_data(host);
2485 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002486 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002487 sdhci_transfer_pio(host);
2488
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002489 /*
2490 * We currently don't do anything fancy with DMA
2491 * boundaries, but as we can't disable the feature
2492 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002493 *
2494 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2495 * should return a valid address to continue from, but as
2496 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002497 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002498 if (intmask & SDHCI_INT_DMA_END) {
2499 u32 dmastart, dmanow;
2500 dmastart = sg_dma_address(host->data->sg);
2501 dmanow = dmastart + host->data->bytes_xfered;
2502 /*
2503 * Force update to the next DMA block boundary.
2504 */
2505 dmanow = (dmanow &
2506 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2507 SDHCI_DEFAULT_BOUNDARY_SIZE;
2508 host->data->bytes_xfered = dmanow - dmastart;
2509 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2510 " next 0x%08x\n",
2511 mmc_hostname(host->mmc), dmastart,
2512 host->data->bytes_xfered, dmanow);
2513 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2514 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002515
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002516 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002517 if (host->cmd == host->data_cmd) {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002518 /*
2519 * Data managed to finish before the
2520 * command completed. Make sure we do
2521 * things in the proper order.
2522 */
2523 host->data_early = 1;
2524 } else {
2525 sdhci_finish_data(host);
2526 }
2527 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002528 }
2529}
2530
David Howells7d12e782006-10-05 14:55:46 +01002531static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002532{
Russell King781e9892014-04-25 12:55:46 +01002533 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002534 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002535 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002536 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002537
2538 spin_lock(&host->lock);
2539
Russell Kingbe138552014-04-25 12:55:56 +01002540 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002541 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002542 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002543 }
2544
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002545 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002546 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002547 result = IRQ_NONE;
2548 goto out;
2549 }
2550
Russell King41005002014-04-25 12:55:36 +01002551 do {
2552 /* Clear selected interrupts. */
2553 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2554 SDHCI_INT_BUS_POWER);
2555 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002556
Russell King41005002014-04-25 12:55:36 +01002557 DBG("*** %s got interrupt: 0x%08x\n",
2558 mmc_hostname(host->mmc), intmask);
2559
2560 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2561 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2562 SDHCI_CARD_PRESENT;
2563
2564 /*
2565 * There is a observation on i.mx esdhc. INSERT
2566 * bit will be immediately set again when it gets
2567 * cleared, if a card is inserted. We have to mask
2568 * the irq to prevent interrupt storm which will
2569 * freeze the system. And the REMOVE gets the
2570 * same situation.
2571 *
2572 * More testing are needed here to ensure it works
2573 * for other platforms though.
2574 */
Russell Kingb537f942014-04-25 12:56:01 +01002575 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2576 SDHCI_INT_CARD_REMOVE);
2577 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2578 SDHCI_INT_CARD_INSERT;
2579 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2580 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002581
2582 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2583 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002584
2585 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2586 SDHCI_INT_CARD_REMOVE);
2587 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002588 }
2589
2590 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002591 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2592 &intmask);
Russell King41005002014-04-25 12:55:36 +01002593
2594 if (intmask & SDHCI_INT_DATA_MASK)
2595 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2596
2597 if (intmask & SDHCI_INT_BUS_POWER)
2598 pr_err("%s: Card is consuming too much power!\n",
2599 mmc_hostname(host->mmc));
2600
Russell King781e9892014-04-25 12:55:46 +01002601 if (intmask & SDHCI_INT_CARD_INT) {
2602 sdhci_enable_sdio_irq_nolock(host, false);
2603 host->thread_isr |= SDHCI_INT_CARD_INT;
2604 result = IRQ_WAKE_THREAD;
2605 }
Russell King41005002014-04-25 12:55:36 +01002606
2607 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2608 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2609 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2610 SDHCI_INT_CARD_INT);
2611
2612 if (intmask) {
2613 unexpected |= intmask;
2614 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2615 }
2616
Russell King781e9892014-04-25 12:55:46 +01002617 if (result == IRQ_NONE)
2618 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002619
2620 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002621 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002622out:
2623 spin_unlock(&host->lock);
2624
Alexander Stein6379b232012-03-14 09:52:10 +01002625 if (unexpected) {
2626 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2627 mmc_hostname(host->mmc), unexpected);
2628 sdhci_dumpregs(host);
2629 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002630
Pierre Ossmand129bce2006-03-24 03:18:17 -08002631 return result;
2632}
2633
Russell King781e9892014-04-25 12:55:46 +01002634static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2635{
2636 struct sdhci_host *host = dev_id;
2637 unsigned long flags;
2638 u32 isr;
2639
2640 spin_lock_irqsave(&host->lock, flags);
2641 isr = host->thread_isr;
2642 host->thread_isr = 0;
2643 spin_unlock_irqrestore(&host->lock, flags);
2644
Russell King3560db82014-04-25 12:55:51 +01002645 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Adrian Hunterd3940f22016-06-29 16:24:14 +03002646 struct mmc_host *mmc = host->mmc;
2647
2648 mmc->ops->card_event(mmc);
2649 mmc_detect_change(mmc, msecs_to_jiffies(200));
Russell King3560db82014-04-25 12:55:51 +01002650 }
2651
Russell King781e9892014-04-25 12:55:46 +01002652 if (isr & SDHCI_INT_CARD_INT) {
2653 sdio_run_irqs(host->mmc);
2654
2655 spin_lock_irqsave(&host->lock, flags);
2656 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2657 sdhci_enable_sdio_irq_nolock(host, true);
2658 spin_unlock_irqrestore(&host->lock, flags);
2659 }
2660
2661 return isr ? IRQ_HANDLED : IRQ_NONE;
2662}
2663
Pierre Ossmand129bce2006-03-24 03:18:17 -08002664/*****************************************************************************\
2665 * *
2666 * Suspend/resume *
2667 * *
2668\*****************************************************************************/
2669
2670#ifdef CONFIG_PM
Ludovic Desroches84d62602016-05-13 15:16:02 +02002671/*
2672 * To enable wakeup events, the corresponding events have to be enabled in
2673 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
2674 * Table' in the SD Host Controller Standard Specification.
2675 * It is useless to restore SDHCI_INT_ENABLE state in
2676 * sdhci_disable_irq_wakeups() since it will be set by
2677 * sdhci_enable_card_detection() or sdhci_init().
2678 */
Kevin Liuad080d72013-01-05 17:21:33 +08002679void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2680{
2681 u8 val;
2682 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2683 | SDHCI_WAKE_ON_INT;
Ludovic Desroches84d62602016-05-13 15:16:02 +02002684 u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2685 SDHCI_INT_CARD_INT;
Kevin Liuad080d72013-01-05 17:21:33 +08002686
2687 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2688 val |= mask ;
2689 /* Avoid fake wake up */
Ludovic Desroches84d62602016-05-13 15:16:02 +02002690 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) {
Kevin Liuad080d72013-01-05 17:21:33 +08002691 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002692 irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
2693 }
Kevin Liuad080d72013-01-05 17:21:33 +08002694 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002695 sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002696}
2697EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2698
Fabio Estevam0b10f472014-08-30 14:53:13 -03002699static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002700{
2701 u8 val;
2702 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2703 | SDHCI_WAKE_ON_INT;
2704
2705 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2706 val &= ~mask;
2707 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2708}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002709
Manuel Lauss29495aa2011-11-03 11:09:45 +01002710int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002711{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002712 sdhci_disable_card_detection(host);
2713
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002714 mmc_retune_timer_stop(host->mmc);
2715 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302716
Kevin Liuad080d72013-01-05 17:21:33 +08002717 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002718 host->ier = 0;
2719 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2720 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002721 free_irq(host->irq, host);
2722 } else {
2723 sdhci_enable_irq_wakeups(host);
2724 enable_irq_wake(host->irq);
2725 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002726 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002727}
2728
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002729EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002730
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002731int sdhci_resume_host(struct sdhci_host *host)
2732{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002733 struct mmc_host *mmc = host->mmc;
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002734 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002735
Richard Röjforsa13abc72009-09-22 16:45:30 -07002736 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002737 if (host->ops->enable_dma)
2738 host->ops->enable_dma(host);
2739 }
2740
Adrian Hunter6308d292012-02-07 14:48:54 +02002741 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2742 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2743 /* Card keeps power but host controller does not */
2744 sdhci_init(host, 0);
2745 host->pwr = 0;
2746 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002747 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02002748 } else {
2749 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2750 mmiowb();
2751 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002752
Haibo Chen14a7b41642015-09-15 18:32:58 +08002753 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2754 ret = request_threaded_irq(host->irq, sdhci_irq,
2755 sdhci_thread_irq, IRQF_SHARED,
2756 mmc_hostname(host->mmc), host);
2757 if (ret)
2758 return ret;
2759 } else {
2760 sdhci_disable_irq_wakeups(host);
2761 disable_irq_wake(host->irq);
2762 }
2763
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002764 sdhci_enable_card_detection(host);
2765
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002766 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002767}
2768
2769EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002770
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002771int sdhci_runtime_suspend_host(struct sdhci_host *host)
2772{
2773 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002774
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002775 mmc_retune_timer_stop(host->mmc);
2776 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002777
2778 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002779 host->ier &= SDHCI_INT_CARD_INT;
2780 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2781 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002782 spin_unlock_irqrestore(&host->lock, flags);
2783
Russell King781e9892014-04-25 12:55:46 +01002784 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002785
2786 spin_lock_irqsave(&host->lock, flags);
2787 host->runtime_suspended = true;
2788 spin_unlock_irqrestore(&host->lock, flags);
2789
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002790 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002791}
2792EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2793
2794int sdhci_runtime_resume_host(struct sdhci_host *host)
2795{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002796 struct mmc_host *mmc = host->mmc;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002797 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002798 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002799
2800 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2801 if (host->ops->enable_dma)
2802 host->ops->enable_dma(host);
2803 }
2804
2805 sdhci_init(host, 0);
2806
2807 /* Force clock and power re-program */
2808 host->pwr = 0;
2809 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002810 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
2811 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002812
Kevin Liu52983382013-01-31 11:31:37 +08002813 if ((host_flags & SDHCI_PV_ENABLED) &&
2814 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2815 spin_lock_irqsave(&host->lock, flags);
2816 sdhci_enable_preset_value(host, true);
2817 spin_unlock_irqrestore(&host->lock, flags);
2818 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002819
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002820 spin_lock_irqsave(&host->lock, flags);
2821
2822 host->runtime_suspended = false;
2823
2824 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002825 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002826 sdhci_enable_sdio_irq_nolock(host, true);
2827
2828 /* Enable Card Detection */
2829 sdhci_enable_card_detection(host);
2830
2831 spin_unlock_irqrestore(&host->lock, flags);
2832
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002833 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002834}
2835EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2836
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002837#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002838
Pierre Ossmand129bce2006-03-24 03:18:17 -08002839/*****************************************************************************\
2840 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002841 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002842 * *
2843\*****************************************************************************/
2844
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002845struct sdhci_host *sdhci_alloc_host(struct device *dev,
2846 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002847{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002848 struct mmc_host *mmc;
2849 struct sdhci_host *host;
2850
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002851 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002852
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002853 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002854 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002855 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002856
2857 host = mmc_priv(mmc);
2858 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002859 host->mmc_host_ops = sdhci_ops;
2860 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002861
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002862 host->flags = SDHCI_SIGNALING_330;
2863
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002864 return host;
2865}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002866
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002867EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002868
Alexandre Courbot7b913692016-03-07 11:07:55 +09002869static int sdhci_set_dma_mask(struct sdhci_host *host)
2870{
2871 struct mmc_host *mmc = host->mmc;
2872 struct device *dev = mmc_dev(mmc);
2873 int ret = -EINVAL;
2874
2875 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
2876 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2877
2878 /* Try 64-bit mask if hardware is capable of it */
2879 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2880 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2881 if (ret) {
2882 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
2883 mmc_hostname(mmc));
2884 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2885 }
2886 }
2887
2888 /* 32-bit mask as default & fallback */
2889 if (ret) {
2890 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2891 if (ret)
2892 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
2893 mmc_hostname(mmc));
2894 }
2895
2896 return ret;
2897}
2898
Adrian Hunter6132a3b2016-06-29 16:24:18 +03002899void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
2900{
2901 u16 v;
2902
2903 if (host->read_caps)
2904 return;
2905
2906 host->read_caps = true;
2907
2908 if (debug_quirks)
2909 host->quirks = debug_quirks;
2910
2911 if (debug_quirks2)
2912 host->quirks2 = debug_quirks2;
2913
2914 sdhci_do_reset(host, SDHCI_RESET_ALL);
2915
2916 v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
2917 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
2918
2919 if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
2920 return;
2921
2922 host->caps = caps ? *caps : sdhci_readl(host, SDHCI_CAPABILITIES);
2923
2924 if (host->version < SDHCI_SPEC_300)
2925 return;
2926
2927 host->caps1 = caps1 ? *caps1 : sdhci_readl(host, SDHCI_CAPABILITIES_1);
2928}
2929EXPORT_SYMBOL_GPL(__sdhci_read_caps);
2930
Adrian Hunter52f53362016-06-29 16:24:15 +03002931int sdhci_setup_host(struct sdhci_host *host)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002932{
2933 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302934 u32 max_current_caps;
2935 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002936 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08002937 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002938 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002939
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002940 WARN_ON(host == NULL);
2941 if (host == NULL)
2942 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002943
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002944 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002945
Adrian Hunter6132a3b2016-06-29 16:24:18 +03002946 sdhci_read_caps(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002947
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002948 override_timeout_clk = host->timeout_clk;
2949
Zhangfei Gao85105c52010-08-06 07:10:01 +08002950 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002951 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
2952 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002953 }
2954
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002955 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002956 host->flags |= SDHCI_USE_SDMA;
Adrian Hunter28da3582016-06-29 16:24:17 +03002957 else if (!(host->caps & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002958 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002959 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002960 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002961
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002962 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002963 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002964 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002965 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002966 }
2967
Arindam Nathf2119df2011-05-05 12:18:57 +05302968 if ((host->version >= SDHCI_SPEC_200) &&
Adrian Hunter28da3582016-06-29 16:24:17 +03002969 (host->caps & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002970 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002971
2972 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2973 (host->flags & SDHCI_USE_ADMA)) {
2974 DBG("Disabling ADMA as it is marked broken\n");
2975 host->flags &= ~SDHCI_USE_ADMA;
2976 }
2977
Adrian Huntere57a5f62014-11-04 12:42:46 +02002978 /*
2979 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2980 * and *must* do 64-bit DMA. A driver has the opportunity to change
2981 * that during the first call to ->enable_dma(). Similarly
2982 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2983 * implement.
2984 */
Adrian Hunter28da3582016-06-29 16:24:17 +03002985 if (host->caps & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02002986 host->flags |= SDHCI_USE_64_BIT_DMA;
2987
Richard Röjforsa13abc72009-09-22 16:45:30 -07002988 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09002989 ret = sdhci_set_dma_mask(host);
2990
2991 if (!ret && host->ops->enable_dma)
2992 ret = host->ops->enable_dma(host);
2993
2994 if (ret) {
2995 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
2996 mmc_hostname(mmc));
2997 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2998
2999 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003000 }
3001 }
3002
Adrian Huntere57a5f62014-11-04 12:42:46 +02003003 /* SDMA does not support 64-bit DMA */
3004 if (host->flags & SDHCI_USE_64_BIT_DMA)
3005 host->flags &= ~SDHCI_USE_SDMA;
3006
Pierre Ossman2134a922008-06-28 18:28:51 +02003007 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00003008 dma_addr_t dma;
3009 void *buf;
3010
Pierre Ossman2134a922008-06-28 18:28:51 +02003011 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02003012 * The DMA descriptor table size is calculated as the maximum
3013 * number of segments times 2, to allow for an alignment
3014 * descriptor for each segment, plus 1 for a nop end descriptor,
3015 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02003016 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02003017 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3018 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3019 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003020 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003021 } else {
3022 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3023 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003024 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003025 }
Russell Kinge66e61c2016-01-26 13:39:55 +00003026
Adrian Hunter04a5ae62015-11-26 14:00:49 +02003027 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00003028 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
3029 host->adma_table_sz, &dma, GFP_KERNEL);
3030 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07003031 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003032 mmc_hostname(mmc));
3033 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003034 } else if ((dma + host->align_buffer_sz) &
3035 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07003036 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3037 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003038 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003039 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3040 host->adma_table_sz, buf, dma);
3041 } else {
3042 host->align_buffer = buf;
3043 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00003044
Russell Kinge66e61c2016-01-26 13:39:55 +00003045 host->adma_table = buf + host->align_buffer_sz;
3046 host->adma_addr = dma + host->align_buffer_sz;
3047 }
Pierre Ossman2134a922008-06-28 18:28:51 +02003048 }
3049
Pierre Ossman76591502008-07-21 00:32:11 +02003050 /*
3051 * If we use DMA, then it's up to the caller to set the DMA
3052 * mask, but PIO does not need the hw shim so we set a new
3053 * mask here in that case.
3054 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003055 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003056 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003057 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003058 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003059
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003060 if (host->version >= SDHCI_SPEC_300)
Adrian Hunter28da3582016-06-29 16:24:17 +03003061 host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003062 >> SDHCI_CLOCK_BASE_SHIFT;
3063 else
Adrian Hunter28da3582016-06-29 16:24:17 +03003064 host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003065 >> SDHCI_CLOCK_BASE_SHIFT;
3066
Pierre Ossmand129bce2006-03-24 03:18:17 -08003067 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003068 if (host->max_clk == 0 || host->quirks &
3069 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003070 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003071 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3072 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003073 ret = -ENODEV;
3074 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003075 }
3076 host->max_clk = host->ops->get_max_clock(host);
3077 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003078
3079 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303080 * In case of Host Controller v3.00, find out whether clock
3081 * multiplier is supported.
3082 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003083 host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
Arindam Nathc3ed3872011-05-05 12:19:06 +05303084 SDHCI_CLOCK_MUL_SHIFT;
3085
3086 /*
3087 * In case the value in Clock Multiplier is 0, then programmable
3088 * clock mode is not supported, otherwise the actual clock
3089 * multiplier is one more than the value of Clock Multiplier
3090 * in the Capabilities Register.
3091 */
3092 if (host->clk_mul)
3093 host->clk_mul += 1;
3094
3095 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003096 * Set host parameters.
3097 */
Dong Aisheng59241752015-07-22 20:53:07 +08003098 max_clk = host->max_clk;
3099
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003100 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003101 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303102 else if (host->version >= SDHCI_SPEC_300) {
3103 if (host->clk_mul) {
3104 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003105 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303106 } else
3107 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3108 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003109 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003110
Adrian Hunterd310ae42016-04-12 14:25:07 +03003111 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003112 mmc->f_max = max_clk;
3113
Aisheng Dong28aab052014-08-27 15:26:31 +08003114 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Adrian Hunter28da3582016-06-29 16:24:17 +03003115 host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
Aisheng Dong28aab052014-08-27 15:26:31 +08003116 SDHCI_TIMEOUT_CLK_SHIFT;
3117 if (host->timeout_clk == 0) {
3118 if (host->ops->get_timeout_clock) {
3119 host->timeout_clk =
3120 host->ops->get_timeout_clock(host);
3121 } else {
3122 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3123 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003124 ret = -ENODEV;
3125 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003126 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003127 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003128
Adrian Hunter28da3582016-06-29 16:24:17 +03003129 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
Aisheng Dong28aab052014-08-27 15:26:31 +08003130 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003131
Adrian Hunter99513622016-03-07 13:33:55 +02003132 if (override_timeout_clk)
3133 host->timeout_clk = override_timeout_clk;
3134
Aisheng Dong28aab052014-08-27 15:26:31 +08003135 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003136 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003137 mmc->max_busy_timeout /= host->timeout_clk;
3138 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003139
Andrei Warkentine89d4562011-05-23 15:06:37 -05003140 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003141 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003142
3143 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3144 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003145
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003146 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003147 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003148 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003149 !(host->flags & SDHCI_USE_SDMA)) &&
3150 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003151 host->flags |= SDHCI_AUTO_CMD23;
3152 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3153 } else {
3154 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3155 }
3156
Philip Rakity15ec4462010-11-19 16:48:39 -05003157 /*
3158 * A controller may support 8-bit width, but the board itself
3159 * might not have the pins brought out. Boards that support
3160 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3161 * their platform code before calling sdhci_add_host(), and we
3162 * won't assume 8-bit width for hosts without that CAP.
3163 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003164 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003165 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003166
Jerry Huang63ef5d82012-10-25 13:47:19 +08003167 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3168 mmc->caps &= ~MMC_CAP_CMD23;
3169
Adrian Hunter28da3582016-06-29 16:24:17 +03003170 if (host->caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003171 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003172
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003173 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Jaehoon Chung860951c2016-06-21 10:13:26 +09003174 mmc_card_is_removable(mmc) &&
Arnd Bergmann287980e2016-05-27 23:23:25 +02003175 mmc_gpio_get_cd(host->mmc) < 0)
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003176 mmc->caps |= MMC_CAP_NEEDS_POLL;
3177
Tim Kryger3a48edc2014-06-13 10:13:56 -07003178 /* If there are external regulators, get them */
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003179 ret = mmc_regulator_get_supply(mmc);
3180 if (ret == -EPROBE_DEFER)
3181 goto undma;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003182
Philip Rakity6231f3d2012-07-23 15:56:23 -07003183 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003184 if (!IS_ERR(mmc->supply.vqmmc)) {
3185 ret = regulator_enable(mmc->supply.vqmmc);
3186 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3187 1950000))
Adrian Hunter28da3582016-06-29 16:24:17 +03003188 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
3189 SDHCI_SUPPORT_SDR50 |
3190 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003191 if (ret) {
3192 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3193 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003194 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003195 }
Kevin Liu8363c372012-11-17 17:55:51 -05003196 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003197
Adrian Hunter28da3582016-06-29 16:24:17 +03003198 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
3199 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3200 SDHCI_SUPPORT_DDR50);
3201 }
Daniel Drake6a661802012-11-25 13:01:19 -05003202
Al Cooper4188bba2012-03-16 15:54:17 -04003203 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
Adrian Hunter28da3582016-06-29 16:24:17 +03003204 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3205 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303206 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3207
3208 /* SDR104 supports also implies SDR50 support */
Adrian Hunter28da3582016-06-29 16:24:17 +03003209 if (host->caps1 & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303210 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003211 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3212 * field can be promoted to support HS200.
3213 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003214 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003215 mmc->caps2 |= MMC_CAP2_HS200;
Adrian Hunter28da3582016-06-29 16:24:17 +03003216 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303217 mmc->caps |= MMC_CAP_UHS_SDR50;
Adrian Hunter28da3582016-06-29 16:24:17 +03003218 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303219
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003220 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003221 (host->caps1 & SDHCI_SUPPORT_HS400))
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003222 mmc->caps2 |= MMC_CAP2_HS400;
3223
Adrian Hunter549c0b12014-11-06 15:19:05 +02003224 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3225 (IS_ERR(mmc->supply.vqmmc) ||
3226 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3227 1300000)))
3228 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3229
Adrian Hunter28da3582016-06-29 16:24:17 +03003230 if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
3231 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303232 mmc->caps |= MMC_CAP_UHS_DDR50;
3233
Girish K S069c9f12012-01-06 09:56:39 +05303234 /* Does the host need tuning for SDR50? */
Adrian Hunter28da3582016-06-29 16:24:17 +03003235 if (host->caps1 & SDHCI_USE_SDR50_TUNING)
Arindam Nathb513ea22011-05-05 12:19:04 +05303236 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3237
Arindam Nathd6d50a12011-05-05 12:18:59 +05303238 /* Driver Type(s) (A, C, D) supported by the host */
Adrian Hunter28da3582016-06-29 16:24:17 +03003239 if (host->caps1 & SDHCI_DRIVER_TYPE_A)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303240 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
Adrian Hunter28da3582016-06-29 16:24:17 +03003241 if (host->caps1 & SDHCI_DRIVER_TYPE_C)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303242 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
Adrian Hunter28da3582016-06-29 16:24:17 +03003243 if (host->caps1 & SDHCI_DRIVER_TYPE_D)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303244 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3245
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303246 /* Initial value for re-tuning timer count */
Adrian Hunter28da3582016-06-29 16:24:17 +03003247 host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3248 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303249
3250 /*
3251 * In case Re-tuning Timer is not disabled, the actual value of
3252 * re-tuning timer will be 2 ^ (n - 1).
3253 */
3254 if (host->tuning_count)
3255 host->tuning_count = 1 << (host->tuning_count - 1);
3256
3257 /* Re-tuning mode supported by the Host Controller */
Adrian Hunter28da3582016-06-29 16:24:17 +03003258 host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303259 SDHCI_RETUNING_MODE_SHIFT;
3260
Takashi Iwai8f230f42010-12-08 10:04:30 +01003261 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003262
Arindam Nathf2119df2011-05-05 12:18:57 +05303263 /*
3264 * According to SD Host Controller spec v3.00, if the Host System
3265 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3266 * the value is meaningful only if Voltage Support in the Capabilities
3267 * register is set. The actual current value is 4 times the register
3268 * value.
3269 */
3270 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003271 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003272 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003273 if (curr > 0) {
3274
3275 /* convert to SDHCI_MAX_CURRENT format */
3276 curr = curr/1000; /* convert to mA */
3277 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3278
3279 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3280 max_current_caps =
3281 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3282 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3283 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3284 }
3285 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303286
Adrian Hunter28da3582016-06-29 16:24:17 +03003287 if (host->caps & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003288 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303289
Aaron Lu55c46652012-07-04 13:31:48 +08003290 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303291 SDHCI_MAX_CURRENT_330_MASK) >>
3292 SDHCI_MAX_CURRENT_330_SHIFT) *
3293 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303294 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003295 if (host->caps & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003296 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303297
Aaron Lu55c46652012-07-04 13:31:48 +08003298 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303299 SDHCI_MAX_CURRENT_300_MASK) >>
3300 SDHCI_MAX_CURRENT_300_SHIFT) *
3301 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303302 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003303 if (host->caps & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003304 ocr_avail |= MMC_VDD_165_195;
3305
Aaron Lu55c46652012-07-04 13:31:48 +08003306 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303307 SDHCI_MAX_CURRENT_180_MASK) >>
3308 SDHCI_MAX_CURRENT_180_SHIFT) *
3309 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303310 }
3311
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003312 /* If OCR set by host, use it instead. */
3313 if (host->ocr_mask)
3314 ocr_avail = host->ocr_mask;
3315
3316 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003317 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003318 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003319
Takashi Iwai8f230f42010-12-08 10:04:30 +01003320 mmc->ocr_avail = ocr_avail;
3321 mmc->ocr_avail_sdio = ocr_avail;
3322 if (host->ocr_avail_sdio)
3323 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3324 mmc->ocr_avail_sd = ocr_avail;
3325 if (host->ocr_avail_sd)
3326 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3327 else /* normal SD controllers don't support 1.8V */
3328 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3329 mmc->ocr_avail_mmc = ocr_avail;
3330 if (host->ocr_avail_mmc)
3331 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003332
3333 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003334 pr_err("%s: Hardware doesn't report any support voltages.\n",
3335 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003336 ret = -ENODEV;
3337 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07003338 }
3339
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003340 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
3341 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
3342 MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
3343 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
3344 host->flags |= SDHCI_SIGNALING_180;
3345
3346 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
3347 host->flags |= SDHCI_SIGNALING_120;
3348
Pierre Ossmand129bce2006-03-24 03:18:17 -08003349 spin_lock_init(&host->lock);
3350
3351 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003352 * Maximum number of segments. Depends on if the hardware
3353 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003354 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003355 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003356 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003357 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003358 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003359 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003360 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003361
3362 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003363 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3364 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3365 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003366 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003367 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003368
3369 /*
3370 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003371 * of bytes. When doing hardware scatter/gather, each entry cannot
3372 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003373 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003374 if (host->flags & SDHCI_USE_ADMA) {
3375 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3376 mmc->max_seg_size = 65535;
3377 else
3378 mmc->max_seg_size = 65536;
3379 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003380 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003381 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003382
3383 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003384 * Maximum block size. This varies from controller to controller and
3385 * is specified in the capabilities register.
3386 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003387 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3388 mmc->max_blk_size = 2;
3389 } else {
Adrian Hunter28da3582016-06-29 16:24:17 +03003390 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003391 SDHCI_MAX_BLOCK_SHIFT;
3392 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003393 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3394 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003395 mmc->max_blk_size = 0;
3396 }
3397 }
3398
3399 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003400
3401 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003402 * Maximum block count.
3403 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003404 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003405
Adrian Hunter52f53362016-06-29 16:24:15 +03003406 return 0;
3407
3408unreg:
3409 if (!IS_ERR(mmc->supply.vqmmc))
3410 regulator_disable(mmc->supply.vqmmc);
3411undma:
3412 if (host->align_buffer)
3413 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3414 host->adma_table_sz, host->align_buffer,
3415 host->align_addr);
3416 host->adma_table = NULL;
3417 host->align_buffer = NULL;
3418
3419 return ret;
3420}
3421EXPORT_SYMBOL_GPL(sdhci_setup_host);
3422
3423int __sdhci_add_host(struct sdhci_host *host)
3424{
3425 struct mmc_host *mmc = host->mmc;
3426 int ret;
3427
Pierre Ossman55db8902006-11-21 17:55:45 +01003428 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003429 * Init tasklets.
3430 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003431 tasklet_init(&host->finish_tasklet,
3432 sdhci_tasklet_finish, (unsigned long)host);
3433
Al Viroe4cad1b2006-10-10 22:47:07 +01003434 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003435
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003436 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303437
Shawn Guo2af502c2013-07-05 14:38:55 +08003438 sdhci_init(host, 0);
3439
Russell King781e9892014-04-25 12:55:46 +01003440 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3441 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003442 if (ret) {
3443 pr_err("%s: Failed to request IRQ %d: %d\n",
3444 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003445 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003446 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003447
Pierre Ossmand129bce2006-03-24 03:18:17 -08003448#ifdef CONFIG_MMC_DEBUG
3449 sdhci_dumpregs(host);
3450#endif
3451
Adrian Hunter061d17a2016-04-12 14:25:09 +03003452 ret = sdhci_led_register(host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003453 if (ret) {
3454 pr_err("%s: Failed to register LED device: %d\n",
3455 mmc_hostname(mmc), ret);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003456 goto unirq;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003457 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003458
Pierre Ossman5f25a662006-10-04 02:15:39 -07003459 mmiowb();
3460
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003461 ret = mmc_add_host(mmc);
3462 if (ret)
3463 goto unled;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003464
Girish K Sa3c76eb2011-10-11 11:44:09 +05303465 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003466 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003467 (host->flags & SDHCI_USE_ADMA) ?
3468 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003469 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003470
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003471 sdhci_enable_card_detection(host);
3472
Pierre Ossmand129bce2006-03-24 03:18:17 -08003473 return 0;
3474
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003475unled:
Adrian Hunter061d17a2016-04-12 14:25:09 +03003476 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003477unirq:
Russell King03231f92014-04-25 12:57:12 +01003478 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003479 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3480 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003481 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003482untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003483 tasklet_kill(&host->finish_tasklet);
Adrian Hunter52f53362016-06-29 16:24:15 +03003484
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003485 if (!IS_ERR(mmc->supply.vqmmc))
3486 regulator_disable(mmc->supply.vqmmc);
Adrian Hunter52f53362016-06-29 16:24:15 +03003487
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003488 if (host->align_buffer)
3489 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3490 host->adma_table_sz, host->align_buffer,
3491 host->align_addr);
3492 host->adma_table = NULL;
3493 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003494
3495 return ret;
3496}
Adrian Hunter52f53362016-06-29 16:24:15 +03003497EXPORT_SYMBOL_GPL(__sdhci_add_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003498
Adrian Hunter52f53362016-06-29 16:24:15 +03003499int sdhci_add_host(struct sdhci_host *host)
3500{
3501 int ret;
3502
3503 ret = sdhci_setup_host(host);
3504 if (ret)
3505 return ret;
3506
3507 return __sdhci_add_host(host);
3508}
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003509EXPORT_SYMBOL_GPL(sdhci_add_host);
3510
Pierre Ossman1e728592008-04-16 19:13:13 +02003511void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003512{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003513 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003514 unsigned long flags;
3515
3516 if (dead) {
3517 spin_lock_irqsave(&host->lock, flags);
3518
3519 host->flags |= SDHCI_DEVICE_DEAD;
3520
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003521 if (sdhci_has_requests(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303522 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003523 " transfer!\n", mmc_hostname(mmc));
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003524 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossman1e728592008-04-16 19:13:13 +02003525 }
3526
3527 spin_unlock_irqrestore(&host->lock, flags);
3528 }
3529
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003530 sdhci_disable_card_detection(host);
3531
Markus Mayer4e743f12014-07-03 13:27:42 -07003532 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003533
Adrian Hunter061d17a2016-04-12 14:25:09 +03003534 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003535
Pierre Ossman1e728592008-04-16 19:13:13 +02003536 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003537 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003538
Russell Kingb537f942014-04-25 12:56:01 +01003539 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3540 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003541 free_irq(host->irq, host);
3542
3543 del_timer_sync(&host->timer);
3544
Pierre Ossmand129bce2006-03-24 03:18:17 -08003545 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003546
Tim Kryger3a48edc2014-06-13 10:13:56 -07003547 if (!IS_ERR(mmc->supply.vqmmc))
3548 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003549
Russell Kingedd63fc2016-01-26 13:39:50 +00003550 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003551 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3552 host->adma_table_sz, host->align_buffer,
3553 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003554
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003555 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003556 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003557}
3558
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003559EXPORT_SYMBOL_GPL(sdhci_remove_host);
3560
3561void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003562{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003563 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003564}
3565
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003566EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003567
3568/*****************************************************************************\
3569 * *
3570 * Driver init/exit *
3571 * *
3572\*****************************************************************************/
3573
3574static int __init sdhci_drv_init(void)
3575{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303576 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003577 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303578 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003579
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003580 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003581}
3582
3583static void __exit sdhci_drv_exit(void)
3584{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003585}
3586
3587module_init(sdhci_drv_init);
3588module_exit(sdhci_drv_exit);
3589
Pierre Ossmandf673b22006-06-30 02:22:31 -07003590module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003591module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003592
Pierre Ossman32710e82009-04-08 20:14:54 +02003593MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003594MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003595MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003596
Pierre Ossmandf673b22006-06-30 02:22:31 -07003597MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003598MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");