blob: f636ef242e8483d79623b6dc7f7b348c0270611d [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Andy Shevchenkoac330f442011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
Russell Kingc5c98922012-04-13 12:14:39 +010022#include <linux/dmaengine.h>
Denis Karpovd900f712009-09-22 16:44:38 -070023#include <linux/seq_file.h>
Felipe Balbi031cd032013-07-11 16:09:05 +030024#include <linux/sizes.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010025#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/dma-mapping.h>
28#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010029#include <linux/timer.h>
30#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053031#include <linux/of.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020032#include <linux/of_irq.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053033#include <linux/of_gpio.h>
34#include <linux/of_device.h>
Balaji T Kee526d52014-05-09 22:16:53 +053035#include <linux/omap-dmaengine.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010036#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070037#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070038#include <linux/mmc/mmc.h>
NeilBrown41afa3142015-01-13 08:23:18 +130039#include <linux/mmc/slot-gpio.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010040#include <linux/io.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020041#include <linux/irq.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080042#include <linux/gpio.h>
43#include <linux/regulator/consumer.h>
Daniel Mack46b76032012-10-15 21:35:05 +053044#include <linux/pinctrl/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053045#include <linux/pm_runtime.h>
Tony Lindgren5b83b222015-05-21 15:51:52 -070046#include <linux/pm_wakeirq.h>
Andreas Fenkart551434382014-11-08 15:33:09 +010047#include <linux/platform_data/hsmmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010048
49/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070050#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010051#define OMAP_HSMMC_CON 0x002C
Balaji T Ka2e77152014-01-21 19:54:42 +053052#define OMAP_HSMMC_SDMASA 0x0100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010053#define OMAP_HSMMC_BLK 0x0104
54#define OMAP_HSMMC_ARG 0x0108
55#define OMAP_HSMMC_CMD 0x010C
56#define OMAP_HSMMC_RSP10 0x0110
57#define OMAP_HSMMC_RSP32 0x0114
58#define OMAP_HSMMC_RSP54 0x0118
59#define OMAP_HSMMC_RSP76 0x011C
60#define OMAP_HSMMC_DATA 0x0120
Andreas Fenkartbb0635f2014-05-29 10:28:01 +020061#define OMAP_HSMMC_PSTATE 0x0124
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010062#define OMAP_HSMMC_HCTL 0x0128
63#define OMAP_HSMMC_SYSCTL 0x012C
64#define OMAP_HSMMC_STAT 0x0130
65#define OMAP_HSMMC_IE 0x0134
66#define OMAP_HSMMC_ISE 0x0138
Balaji T Ka2e77152014-01-21 19:54:42 +053067#define OMAP_HSMMC_AC12 0x013C
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010068#define OMAP_HSMMC_CAPA 0x0140
69
70#define VS18 (1 << 26)
71#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053072#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010073#define SDVS18 (0x5 << 9)
74#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080075#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010076#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010077#define SDVSCLR 0xFFFFF1FF
78#define SDVSDET 0x00000400
79#define AUTOIDLE 0x1
80#define SDBP (1 << 8)
81#define DTO 0xe
82#define ICE 0x1
83#define ICS 0x2
84#define CEN (1 << 2)
Balaji T Ked164182013-10-21 00:25:21 +053085#define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010086#define CLKD_MASK 0x0000FFC0
87#define CLKD_SHIFT 6
88#define DTO_MASK 0x000F0000
89#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010090#define INIT_STREAM (1 << 1)
Balaji T Ka2e77152014-01-21 19:54:42 +053091#define ACEN_ACMD23 (2 << 2)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010092#define DP_SELECT (1 << 21)
93#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053094#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010095#define MSBS (1 << 5)
96#define BCE (1 << 1)
97#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053098#define HSPE (1 << 2)
Balaji T K5a52b082014-05-29 10:28:02 +020099#define IWE (1 << 24)
Balaji T K03b5d922012-04-09 12:08:33 +0530100#define DDR (1 << 19)
Balaji T K5a52b082014-05-29 10:28:02 +0200101#define CLKEXTFREE (1 << 16)
102#define CTPL (1 << 11)
Jarkko Lavinen73153012008-11-21 16:49:54 +0200103#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100104#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100105#define STAT_CLEAR 0xFFFFFFFF
106#define INIT_STREAM_CMD 0x00000000
107#define DUAL_VOLT_OCR_BIT 7
108#define SRC (1 << 25)
109#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700110#define SOFTRESET (1 << 1)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100111
Andreas Fenkartf9459012014-05-29 10:28:03 +0200112/* PSTATE */
113#define DLEV_DAT(x) (1 << (20 + (x)))
114
Venkatraman Sa7e96872012-11-19 22:00:01 +0530115/* Interrupt masks for IE and ISE register */
116#define CC_EN (1 << 0)
117#define TC_EN (1 << 1)
118#define BWR_EN (1 << 4)
119#define BRR_EN (1 << 5)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200120#define CIRQ_EN (1 << 8)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530121#define ERR_EN (1 << 15)
122#define CTO_EN (1 << 16)
123#define CCRC_EN (1 << 17)
124#define CEB_EN (1 << 18)
125#define CIE_EN (1 << 19)
126#define DTO_EN (1 << 20)
127#define DCRC_EN (1 << 21)
128#define DEB_EN (1 << 22)
Balaji T Ka2e77152014-01-21 19:54:42 +0530129#define ACE_EN (1 << 24)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530130#define CERR_EN (1 << 28)
131#define BADA_EN (1 << 29)
132
Balaji T Ka2e77152014-01-21 19:54:42 +0530133#define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
Venkatraman Sa7e96872012-11-19 22:00:01 +0530134 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
135 BRR_EN | BWR_EN | TC_EN | CC_EN)
136
Balaji T Ka2e77152014-01-21 19:54:42 +0530137#define CNI (1 << 7)
138#define ACIE (1 << 4)
139#define ACEB (1 << 3)
140#define ACCE (1 << 2)
141#define ACTO (1 << 1)
142#define ACNE (1 << 0)
143
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530144#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530145#define MMC_TIMEOUT_MS 20 /* 20 mSec */
146#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400147#define OMAP_MMC_MIN_CLOCK 400000
148#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530149#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100150
Balaji T Ke99448f2014-02-19 20:26:40 +0530151#define VDD_1V8 1800000 /* 180000 uV */
152#define VDD_3V0 3000000 /* 300000 uV */
153#define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
154
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100155/*
156 * One controller can have multiple slots, like on some omap boards using
157 * omap.c controller driver. Luckily this is not currently done on any known
158 * omap_hsmmc.c device.
159 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100160#define mmc_pdata(host) host->pdata
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100161
162/*
163 * MMC Host controller read/write API's
164 */
165#define OMAP_HSMMC_READ(base, reg) \
166 __raw_readl((base) + OMAP_HSMMC_##reg)
167
168#define OMAP_HSMMC_WRITE(base, reg, val) \
169 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
170
Per Forlin9782aff2011-07-01 18:55:23 +0200171struct omap_hsmmc_next {
172 unsigned int dma_len;
173 s32 cookie;
174};
175
Denis Karpov70a33412009-09-22 16:44:59 -0700176struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100177 struct device *dev;
178 struct mmc_host *mmc;
179 struct mmc_request *mrq;
180 struct mmc_command *cmd;
181 struct mmc_data *data;
182 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100183 struct clk *dbclk;
Balaji T Ke99448f2014-02-19 20:26:40 +0530184 struct regulator *pbias;
Tony Lindgrenbb2726b2015-10-07 06:22:24 -0700185 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100186 void __iomem *base;
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530187 int vqmmc_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100188 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700189 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100190 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200191 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100192 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700193 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100194 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530195 u32 con;
196 u32 hctl;
197 u32 sysctl;
198 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100199 int irq;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200200 int wake_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100201 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100202 struct dma_chan *tx_chan;
203 struct dma_chan *rx_chan;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200204 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700205 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700206 int protect_card;
207 int reqs_blocked;
Adrian Hunterb4175772010-05-26 14:42:06 -0700208 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530209 unsigned long clk_rate;
Balaji T Ka2e77152014-01-21 19:54:42 +0530210 unsigned int flags;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200211#define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
212#define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
Per Forlin9782aff2011-07-01 18:55:23 +0200213 struct omap_hsmmc_next next_data;
Andreas Fenkart551434382014-11-08 15:33:09 +0100214 struct omap_hsmmc_platform_data *pdata;
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100215
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100216 /* return MMC cover switch state, can be NULL if not supported.
217 *
218 * possible return values:
219 * 0 - closed
220 * 1 - open
221 */
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100222 int (*get_cover_state)(struct device *dev);
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100223
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100224 int (*card_detect)(struct device *dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100225};
226
Nishanth Menon59445b12014-02-13 23:45:48 -0600227struct omap_mmc_of_data {
228 u32 reg_offset;
229 u8 controller_flags;
230};
231
Balaji T Kbf129e12014-01-21 19:54:42 +0530232static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
233
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100234static int omap_hsmmc_card_detect(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800235{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530236 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800237
NeilBrown41afa3142015-01-13 08:23:18 +1300238 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800239}
240
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100241static int omap_hsmmc_get_cover_state(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800242{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530243 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800244
NeilBrown41afa3142015-01-13 08:23:18 +1300245 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800246}
247
Kishon Vijay Abraham I1d17f302015-08-27 14:44:06 +0530248static int omap_hsmmc_enable_supply(struct mmc_host *mmc)
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530249{
250 int ret;
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530251 struct omap_hsmmc_host *host = mmc_priv(mmc);
Kishon Vijay Abraham I1d17f302015-08-27 14:44:06 +0530252 struct mmc_ios *ios = &mmc->ios;
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530253
254 if (mmc->supply.vmmc) {
Kishon Vijay Abraham I1d17f302015-08-27 14:44:06 +0530255 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530256 if (ret)
257 return ret;
258 }
259
260 /* Enable interface voltage rail, if needed */
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530261 if (mmc->supply.vqmmc && !host->vqmmc_enabled) {
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530262 ret = regulator_enable(mmc->supply.vqmmc);
263 if (ret) {
264 dev_err(mmc_dev(mmc), "vmmc_aux reg enable failed\n");
265 goto err_vqmmc;
266 }
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530267 host->vqmmc_enabled = 1;
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530268 }
269
270 return 0;
271
272err_vqmmc:
273 if (mmc->supply.vmmc)
274 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
275
276 return ret;
277}
278
279static int omap_hsmmc_disable_supply(struct mmc_host *mmc)
280{
281 int ret;
282 int status;
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530283 struct omap_hsmmc_host *host = mmc_priv(mmc);
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530284
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530285 if (mmc->supply.vqmmc && host->vqmmc_enabled) {
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530286 ret = regulator_disable(mmc->supply.vqmmc);
287 if (ret) {
288 dev_err(mmc_dev(mmc), "vmmc_aux reg disable failed\n");
289 return ret;
290 }
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530291 host->vqmmc_enabled = 0;
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530292 }
293
294 if (mmc->supply.vmmc) {
295 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
296 if (ret)
297 goto err_set_ocr;
298 }
299
300 return 0;
301
302err_set_ocr:
303 if (mmc->supply.vqmmc) {
304 status = regulator_enable(mmc->supply.vqmmc);
305 if (status)
306 dev_err(mmc_dev(mmc), "vmmc_aux re-enable failed\n");
307 }
308
309 return ret;
310}
311
Kishon Vijay Abraham Iec85c952015-08-27 14:44:01 +0530312static int omap_hsmmc_set_pbias(struct omap_hsmmc_host *host, bool power_on,
313 int vdd)
314{
315 int ret;
316
317 if (!host->pbias)
318 return 0;
319
320 if (power_on) {
321 if (vdd <= VDD_165_195)
322 ret = regulator_set_voltage(host->pbias, VDD_1V8,
323 VDD_1V8);
324 else
325 ret = regulator_set_voltage(host->pbias, VDD_3V0,
326 VDD_3V0);
327 if (ret < 0) {
328 dev_err(host->dev, "pbias set voltage fail\n");
329 return ret;
330 }
331
Tony Lindgrenbb2726b2015-10-07 06:22:24 -0700332 if (host->pbias_enabled == 0) {
Kishon Vijay Abraham Iec85c952015-08-27 14:44:01 +0530333 ret = regulator_enable(host->pbias);
334 if (ret) {
335 dev_err(host->dev, "pbias reg enable fail\n");
336 return ret;
337 }
Tony Lindgrenbb2726b2015-10-07 06:22:24 -0700338 host->pbias_enabled = 1;
Kishon Vijay Abraham Iec85c952015-08-27 14:44:01 +0530339 }
340 } else {
Tony Lindgrenbb2726b2015-10-07 06:22:24 -0700341 if (host->pbias_enabled == 1) {
Kishon Vijay Abraham Iec85c952015-08-27 14:44:01 +0530342 ret = regulator_disable(host->pbias);
343 if (ret) {
344 dev_err(host->dev, "pbias reg disable fail\n");
345 return ret;
346 }
Tony Lindgrenbb2726b2015-10-07 06:22:24 -0700347 host->pbias_enabled = 0;
Kishon Vijay Abraham Iec85c952015-08-27 14:44:01 +0530348 }
349 }
350
351 return 0;
352}
353
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100354static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800355{
356 struct omap_hsmmc_host *host =
357 platform_get_drvdata(to_platform_device(dev));
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530358 struct mmc_host *mmc = host->mmc;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800359 int ret = 0;
360
Andreas Fenkartf7f0f032015-07-07 20:38:43 +0200361 if (mmc_pdata(host)->set_power)
362 return mmc_pdata(host)->set_power(dev, power_on, vdd);
363
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800364 /*
365 * If we don't see a Vcc regulator, assume it's a fixed
366 * voltage always-on regulator.
367 */
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530368 if (!mmc->supply.vmmc)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800369 return 0;
370
Andreas Fenkart326119c2014-11-08 15:33:14 +0100371 if (mmc_pdata(host)->before_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100372 mmc_pdata(host)->before_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800373
Kishon Vijay Abraham Iec85c952015-08-27 14:44:01 +0530374 ret = omap_hsmmc_set_pbias(host, false, 0);
375 if (ret)
376 return ret;
Balaji T Ke99448f2014-02-19 20:26:40 +0530377
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800378 /*
379 * Assume Vcc regulator is used only to power the card ... OMAP
380 * VDDS is used to power the pins, optionally with a transceiver to
381 * support cards using voltages other than VDDS (1.8V nominal). When a
382 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
383 *
384 * In some cases this regulator won't support enable/disable;
385 * e.g. it's a fixed rail for a WLAN chip.
386 *
387 * In other cases vcc_aux switches interface power. Example, for
388 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
389 * chips/cards need an interface voltage rail too.
390 */
391 if (power_on) {
Kishon Vijay Abraham I1d17f302015-08-27 14:44:06 +0530392 ret = omap_hsmmc_enable_supply(mmc);
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530393 if (ret)
394 return ret;
Kishon Vijay Abraham I97fe7e52015-08-27 14:44:02 +0530395
396 ret = omap_hsmmc_set_pbias(host, true, vdd);
397 if (ret)
398 goto err_set_voltage;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800399 } else {
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530400 ret = omap_hsmmc_disable_supply(mmc);
401 if (ret)
402 return ret;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800403 }
404
Andreas Fenkart326119c2014-11-08 15:33:14 +0100405 if (mmc_pdata(host)->after_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100406 mmc_pdata(host)->after_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800407
Kishon Vijay Abraham I229f3292015-08-27 14:43:59 +0530408 return 0;
409
410err_set_voltage:
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530411 omap_hsmmc_disable_supply(mmc);
Kishon Vijay Abraham I229f3292015-08-27 14:43:59 +0530412
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800413 return ret;
414}
415
Kishon Vijay Abraham Ic8518ef2015-08-27 14:44:03 +0530416static int omap_hsmmc_disable_boot_regulator(struct regulator *reg)
417{
418 int ret;
419
420 if (!reg)
421 return 0;
422
423 if (regulator_is_enabled(reg)) {
424 ret = regulator_enable(reg);
425 if (ret)
426 return ret;
427
428 ret = regulator_disable(reg);
429 if (ret)
430 return ret;
431 }
432
433 return 0;
434}
435
436static int omap_hsmmc_disable_boot_regulators(struct omap_hsmmc_host *host)
437{
438 struct mmc_host *mmc = host->mmc;
439 int ret;
440
441 /*
442 * disable regulators enabled during boot and get the usecount
443 * right so that regulators can be enabled/disabled by checking
444 * the return value of regulator_is_enabled
445 */
446 ret = omap_hsmmc_disable_boot_regulator(mmc->supply.vmmc);
447 if (ret) {
448 dev_err(host->dev, "fail to disable boot enabled vmmc reg\n");
449 return ret;
450 }
451
452 ret = omap_hsmmc_disable_boot_regulator(mmc->supply.vqmmc);
453 if (ret) {
454 dev_err(host->dev,
455 "fail to disable boot enabled vmmc_aux reg\n");
456 return ret;
457 }
458
459 ret = omap_hsmmc_disable_boot_regulator(host->pbias);
460 if (ret) {
461 dev_err(host->dev,
462 "failed to disable boot enabled pbias reg\n");
463 return ret;
464 }
465
466 return 0;
467}
468
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800469static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
470{
kishore kadiyala64be9782010-10-01 16:35:28 -0700471 int ocr_value = 0;
Kishon Vijay Abraham I7d607f92015-08-27 14:43:53 +0530472 int ret;
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530473 struct mmc_host *mmc = host->mmc;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800474
Andreas Fenkartf7f0f032015-07-07 20:38:43 +0200475 if (mmc_pdata(host)->set_power)
476 return 0;
477
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530478 mmc->supply.vmmc = devm_regulator_get_optional(host->dev, "vmmc");
479 if (IS_ERR(mmc->supply.vmmc)) {
480 ret = PTR_ERR(mmc->supply.vmmc);
Tony Lindgren123e20b2015-10-07 06:22:25 -0700481 if ((ret != -ENODEV) && host->dev->of_node)
Kishon Vijay Abraham I7d607f92015-08-27 14:43:53 +0530482 return ret;
Kishon Vijay Abraham I7d607f92015-08-27 14:43:53 +0530483 dev_dbg(host->dev, "unable to get vmmc regulator %ld\n",
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530484 PTR_ERR(mmc->supply.vmmc));
485 mmc->supply.vmmc = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800486 } else {
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530487 ocr_value = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
Kishon Vijay Abraham Ib49069f2015-08-27 14:43:56 +0530488 if (ocr_value > 0)
Andreas Fenkart326119c2014-11-08 15:33:14 +0100489 mmc_pdata(host)->ocr_mask = ocr_value;
Balaji T K987fd492014-02-19 20:26:40 +0530490 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800491
Balaji T K987fd492014-02-19 20:26:40 +0530492 /* Allow an aux regulator */
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530493 mmc->supply.vqmmc = devm_regulator_get_optional(host->dev, "vmmc_aux");
494 if (IS_ERR(mmc->supply.vqmmc)) {
495 ret = PTR_ERR(mmc->supply.vqmmc);
Tony Lindgren123e20b2015-10-07 06:22:25 -0700496 if ((ret != -ENODEV) && host->dev->of_node)
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530497 return ret;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530498 dev_dbg(host->dev, "unable to get vmmc_aux regulator %ld\n",
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530499 PTR_ERR(mmc->supply.vqmmc));
500 mmc->supply.vqmmc = NULL;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530501 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800502
Kishon Vijay Abraham Ic299dc32015-08-27 14:43:55 +0530503 host->pbias = devm_regulator_get_optional(host->dev, "pbias");
504 if (IS_ERR(host->pbias)) {
505 ret = PTR_ERR(host->pbias);
Kishon Vijay Abraham I91437572016-01-14 14:45:20 +0530506 if ((ret != -ENODEV) && host->dev->of_node) {
507 dev_err(host->dev,
508 "SD card detect fail? enable CONFIG_REGULATOR_PBIAS\n");
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530509 return ret;
Kishon Vijay Abraham I91437572016-01-14 14:45:20 +0530510 }
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530511 dev_dbg(host->dev, "unable to get pbias regulator %ld\n",
Kishon Vijay Abraham Ic299dc32015-08-27 14:43:55 +0530512 PTR_ERR(host->pbias));
513 host->pbias = NULL;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530514 }
Balaji T Ke99448f2014-02-19 20:26:40 +0530515
Balaji T K987fd492014-02-19 20:26:40 +0530516 /* For eMMC do not power off when not in sleep state */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100517 if (mmc_pdata(host)->no_regulator_off_init)
Balaji T K987fd492014-02-19 20:26:40 +0530518 return 0;
Adrian Huntere840ce12011-05-06 12:14:10 +0300519
Kishon Vijay Abraham Ic8518ef2015-08-27 14:44:03 +0530520 ret = omap_hsmmc_disable_boot_regulators(host);
521 if (ret)
522 return ret;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800523
524 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800525}
526
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100527static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id);
NeilBrown41afa3142015-01-13 08:23:18 +1300528
529static int omap_hsmmc_gpio_init(struct mmc_host *mmc,
530 struct omap_hsmmc_host *host,
Andreas Fenkart1e363e32014-11-08 15:33:15 +0100531 struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800532{
533 int ret;
534
Andreas Fenkartb7a56462015-03-20 15:53:54 +0100535 if (gpio_is_valid(pdata->gpio_cod)) {
536 ret = mmc_gpio_request_cd(mmc, pdata->gpio_cod, 0);
Adrian Hunterb702b102010-02-15 10:03:35 -0800537 if (ret)
538 return ret;
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100539
540 host->get_cover_state = omap_hsmmc_get_cover_state;
541 mmc_gpio_set_cd_isr(mmc, omap_hsmmc_cover_irq);
Andreas Fenkartb7a56462015-03-20 15:53:54 +0100542 } else if (gpio_is_valid(pdata->gpio_cd)) {
543 ret = mmc_gpio_request_cd(mmc, pdata->gpio_cd, 0);
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100544 if (ret)
545 return ret;
546
547 host->card_detect = omap_hsmmc_card_detect;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100548 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800549
Andreas Fenkart326119c2014-11-08 15:33:14 +0100550 if (gpio_is_valid(pdata->gpio_wp)) {
NeilBrown41afa3142015-01-13 08:23:18 +1300551 ret = mmc_gpio_request_ro(mmc, pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800552 if (ret)
NeilBrown41afa3142015-01-13 08:23:18 +1300553 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100554 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800555
556 return 0;
Adrian Hunterb702b102010-02-15 10:03:35 -0800557}
558
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100559/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300560 * Start clock to the card
561 */
562static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
563{
564 OMAP_HSMMC_WRITE(host->base, SYSCTL,
565 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
566}
567
568/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100569 * Stop clock to the card
570 */
Denis Karpov70a33412009-09-22 16:44:59 -0700571static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100572{
573 OMAP_HSMMC_WRITE(host->base, SYSCTL,
574 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
575 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900576 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100577}
578
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700579static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
580 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700581{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200582 u32 irq_mask = INT_EN_MASK;
583 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700584
585 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200586 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700587
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700588 /* Disable timeout for erases */
589 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530590 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700591
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200592 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700593 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
594 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200595
596 /* latch pending CIRQ, but don't signal MMC core */
597 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
598 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700599 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200600 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700601}
602
603static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
604{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200605 u32 irq_mask = 0;
606 unsigned long flags;
607
608 spin_lock_irqsave(&host->irq_lock, flags);
609 /* no transfer running but need to keep cirq if enabled */
610 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
611 irq_mask |= CIRQ_EN;
612 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
613 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700614 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200615 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700616}
617
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300618/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530619static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300620{
621 u16 dsor = 0;
622
623 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530624 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530625 if (dsor > CLKD_MAX)
626 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300627 }
628
629 return dsor;
630}
631
Andy Shevchenko5934df22011-05-06 12:14:06 +0300632static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
633{
634 struct mmc_ios *ios = &host->mmc->ios;
635 unsigned long regval;
636 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530637 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300638
Venkatraman S8986d312012-08-07 19:10:38 +0530639 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300640
641 omap_hsmmc_stop_clock(host);
642
643 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
644 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530645 clkdiv = calc_divisor(host, ios);
646 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300647 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
648 OMAP_HSMMC_WRITE(host->base, SYSCTL,
649 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
650
651 /* Wait till the ICS bit is set */
652 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
653 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
654 && time_before(jiffies, timeout))
655 cpu_relax();
656
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530657 /*
658 * Enable High-Speed Support
659 * Pre-Requisites
660 * - Controller should support High-Speed-Enable Bit
661 * - Controller should not be using DDR Mode
662 * - Controller should advertise that it supports High Speed
663 * in capabilities register
664 * - MMC/SD clock coming out of controller > 25MHz
665 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100666 if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900667 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Ulf Hansson903101a2014-11-25 13:05:13 +0100668 (ios->timing != MMC_TIMING_UHS_DDR50) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530669 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
670 regval = OMAP_HSMMC_READ(host->base, HCTL);
671 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
672 regval |= HSPE;
673 else
674 regval &= ~HSPE;
675
676 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
677 }
678
Andy Shevchenko5934df22011-05-06 12:14:06 +0300679 omap_hsmmc_start_clock(host);
680}
681
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400682static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
683{
684 struct mmc_ios *ios = &host->mmc->ios;
685 u32 con;
686
687 con = OMAP_HSMMC_READ(host->base, CON);
Ulf Hansson903101a2014-11-25 13:05:13 +0100688 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
689 ios->timing == MMC_TIMING_UHS_DDR50)
Balaji T K03b5d922012-04-09 12:08:33 +0530690 con |= DDR; /* configure in DDR mode */
691 else
692 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400693 switch (ios->bus_width) {
694 case MMC_BUS_WIDTH_8:
695 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
696 break;
697 case MMC_BUS_WIDTH_4:
698 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
699 OMAP_HSMMC_WRITE(host->base, HCTL,
700 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
701 break;
702 case MMC_BUS_WIDTH_1:
703 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
704 OMAP_HSMMC_WRITE(host->base, HCTL,
705 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
706 break;
707 }
708}
709
710static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
711{
712 struct mmc_ios *ios = &host->mmc->ios;
713 u32 con;
714
715 con = OMAP_HSMMC_READ(host->base, CON);
716 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
717 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
718 else
719 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
720}
721
Denis Karpov11dd62a2009-09-22 16:44:43 -0700722#ifdef CONFIG_PM
723
724/*
725 * Restore the MMC host context, if it was lost as result of a
726 * power state change.
727 */
Denis Karpov70a33412009-09-22 16:44:59 -0700728static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700729{
730 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400731 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700732 unsigned long timeout;
733
Tony Lindgren0a82e062013-10-21 00:25:19 +0530734 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
735 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
736 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
737 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
738 return 0;
739
740 host->context_loss++;
741
Balaji T Kc2200ef2012-03-07 09:55:30 -0500742 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700743 if (host->power_mode != MMC_POWER_OFF &&
744 (1 << ios->vdd) <= MMC_VDD_23_24)
745 hctl = SDVS18;
746 else
747 hctl = SDVS30;
748 capa = VS30 | VS18;
749 } else {
750 hctl = SDVS18;
751 capa = VS18;
752 }
753
Balaji T K5a52b082014-05-29 10:28:02 +0200754 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
755 hctl |= IWE;
756
Denis Karpov11dd62a2009-09-22 16:44:43 -0700757 OMAP_HSMMC_WRITE(host->base, HCTL,
758 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
759
760 OMAP_HSMMC_WRITE(host->base, CAPA,
761 OMAP_HSMMC_READ(host->base, CAPA) | capa);
762
763 OMAP_HSMMC_WRITE(host->base, HCTL,
764 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
765
766 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
767 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
768 && time_before(jiffies, timeout))
769 ;
770
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200771 OMAP_HSMMC_WRITE(host->base, ISE, 0);
772 OMAP_HSMMC_WRITE(host->base, IE, 0);
773 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700774
775 /* Do not initialize card-specific things if the power is off */
776 if (host->power_mode == MMC_POWER_OFF)
777 goto out;
778
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400779 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700780
Andy Shevchenko5934df22011-05-06 12:14:06 +0300781 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700782
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400783 omap_hsmmc_set_bus_mode(host);
784
Denis Karpov11dd62a2009-09-22 16:44:43 -0700785out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530786 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
787 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700788 return 0;
789}
790
791/*
792 * Save the MMC host context (store the number of power state changes so far).
793 */
Denis Karpov70a33412009-09-22 16:44:59 -0700794static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700795{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530796 host->con = OMAP_HSMMC_READ(host->base, CON);
797 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
798 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
799 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700800}
801
802#else
803
Denis Karpov70a33412009-09-22 16:44:59 -0700804static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700805{
806 return 0;
807}
808
Denis Karpov70a33412009-09-22 16:44:59 -0700809static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700810{
811}
812
813#endif
814
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100815/*
816 * Send init stream sequence to card
817 * before sending IDLE command
818 */
Denis Karpov70a33412009-09-22 16:44:59 -0700819static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100820{
821 int reg = 0;
822 unsigned long timeout;
823
Adrian Hunterb62f6222009-09-22 16:45:01 -0700824 if (host->protect_card)
825 return;
826
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100827 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700828
829 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100830 OMAP_HSMMC_WRITE(host->base, CON,
831 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
832 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
833
834 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530835 while ((reg != CC_EN) && time_before(jiffies, timeout))
836 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100837
838 OMAP_HSMMC_WRITE(host->base, CON,
839 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700840
841 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
842 OMAP_HSMMC_READ(host->base, STAT);
843
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100844 enable_irq(host->irq);
845}
846
847static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700848int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100849{
850 int r = 1;
851
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100852 if (host->get_cover_state)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100853 r = host->get_cover_state(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100854 return r;
855}
856
857static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700858omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100859 char *buf)
860{
861 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700862 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100863
Denis Karpov70a33412009-09-22 16:44:59 -0700864 return sprintf(buf, "%s\n",
865 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100866}
867
Denis Karpov70a33412009-09-22 16:44:59 -0700868static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100869
870static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700871omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100872 char *buf)
873{
874 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700875 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100876
Andreas Fenkart326119c2014-11-08 15:33:14 +0100877 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100878}
879
Denis Karpov70a33412009-09-22 16:44:59 -0700880static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100881
882/*
883 * Configure the response type and send the cmd.
884 */
885static void
Denis Karpov70a33412009-09-22 16:44:59 -0700886omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100887 struct mmc_data *data)
888{
889 int cmdreg = 0, resptype = 0, cmdtype = 0;
890
Venkatraman S8986d312012-08-07 19:10:38 +0530891 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100892 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
893 host->cmd = cmd;
894
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700895 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100896
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200897 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100898 if (cmd->flags & MMC_RSP_PRESENT) {
899 if (cmd->flags & MMC_RSP_136)
900 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200901 else if (cmd->flags & MMC_RSP_BUSY) {
902 resptype = 3;
903 host->response_busy = 1;
904 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100905 resptype = 2;
906 }
907
908 /*
909 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
910 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
911 * a val of 0x3, rest 0x0.
912 */
913 if (cmd == host->mrq->stop)
914 cmdtype = 0x3;
915
916 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
917
Balaji T Ka2e77152014-01-21 19:54:42 +0530918 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
919 host->mrq->sbc) {
920 cmdreg |= ACEN_ACMD23;
921 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
922 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100923 if (data) {
924 cmdreg |= DP_SELECT | MSBS | BCE;
925 if (data->flags & MMC_DATA_READ)
926 cmdreg |= DDIR;
927 else
928 cmdreg &= ~(DDIR);
929 }
930
931 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530932 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100933
Adrian Hunterb4175772010-05-26 14:42:06 -0700934 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700935
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100936 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
937 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
938}
939
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200940static int
Denis Karpov70a33412009-09-22 16:44:59 -0700941omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200942{
943 if (data->flags & MMC_DATA_WRITE)
944 return DMA_TO_DEVICE;
945 else
946 return DMA_FROM_DEVICE;
947}
948
Russell Kingc5c98922012-04-13 12:14:39 +0100949static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
950 struct mmc_data *data)
951{
952 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
953}
954
Adrian Hunterb4175772010-05-26 14:42:06 -0700955static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
956{
957 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530958 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700959
Venkatraman S31463b12012-04-09 12:08:34 +0530960 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700961 host->req_in_progress = 0;
962 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530963 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700964
965 omap_hsmmc_disable_irq(host);
966 /* Do not complete the request if DMA is still in progress */
967 if (mrq->data && host->use_dma && dma_ch != -1)
968 return;
969 host->mrq = NULL;
970 mmc_request_done(host->mmc, mrq);
NeilBrownf57ba4c2015-03-26 12:18:23 +1100971 pm_runtime_mark_last_busy(host->dev);
972 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -0700973}
974
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100975/*
976 * Notify the transfer complete to MMC core
977 */
978static void
Denis Karpov70a33412009-09-22 16:44:59 -0700979omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100980{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200981 if (!data) {
982 struct mmc_request *mrq = host->mrq;
983
Adrian Hunter23050102009-09-22 16:44:57 -0700984 /* TC before CC from CMD6 - don't know why, but it happens */
985 if (host->cmd && host->cmd->opcode == 6 &&
986 host->response_busy) {
987 host->response_busy = 0;
988 return;
989 }
990
Adrian Hunterb4175772010-05-26 14:42:06 -0700991 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200992 return;
993 }
994
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100995 host->data = NULL;
996
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100997 if (!data->error)
998 data->bytes_xfered += data->blocks * (data->blksz);
999 else
1000 data->bytes_xfered = 0;
1001
Balaji T Kbf129e12014-01-21 19:54:42 +05301002 if (data->stop && (data->error || !host->mrq->sbc))
1003 omap_hsmmc_start_command(host, data->stop, NULL);
1004 else
Adrian Hunterb4175772010-05-26 14:42:06 -07001005 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001006}
1007
1008/*
1009 * Notify the core about command completion
1010 */
1011static void
Denis Karpov70a33412009-09-22 16:44:59 -07001012omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001013{
Balaji T Kbf129e12014-01-21 19:54:42 +05301014 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +05301015 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +05301016 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +05301017 omap_hsmmc_start_dma_transfer(host);
1018 omap_hsmmc_start_command(host, host->mrq->cmd,
1019 host->mrq->data);
1020 return;
1021 }
1022
Balaji T K2177fa92014-05-09 22:16:52 +05301023 host->cmd = NULL;
1024
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001025 if (cmd->flags & MMC_RSP_PRESENT) {
1026 if (cmd->flags & MMC_RSP_136) {
1027 /* response type 2 */
1028 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
1029 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
1030 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
1031 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
1032 } else {
1033 /* response types 1, 1b, 3, 4, 5, 6 */
1034 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
1035 }
1036 }
Adrian Hunterb4175772010-05-26 14:42:06 -07001037 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +05301038 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001039}
1040
1041/*
1042 * DMA clean up for command errors
1043 */
Denis Karpov70a33412009-09-22 16:44:59 -07001044static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001045{
Adrian Hunterb4175772010-05-26 14:42:06 -07001046 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +05301047 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -07001048
Jarkko Lavinen82788ff2008-12-05 12:31:46 +02001049 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001050
Venkatraman S31463b12012-04-09 12:08:34 +05301051 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001052 dma_ch = host->dma_ch;
1053 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +05301054 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001055
1056 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +01001057 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
1058
1059 dmaengine_terminate_all(chan);
1060 dma_unmap_sg(chan->device->dev,
1061 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -07001062 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +01001063
Per Forlin053bf342011-11-07 21:55:11 +05301064 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001065 }
1066 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001067}
1068
1069/*
1070 * Readable error output
1071 */
1072#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +03001073static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001074{
1075 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -07001076 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +03001077 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
1078 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
1079 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
1080 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001081 };
1082 char res[256];
1083 char *buf = res;
1084 int len, i;
1085
1086 len = sprintf(buf, "MMC IRQ 0x%x :", status);
1087 buf += len;
1088
Denis Karpov70a33412009-09-22 16:44:59 -07001089 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001090 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -07001091 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001092 buf += len;
1093 }
1094
Venkatraman S8986d312012-08-07 19:10:38 +05301095 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001096}
Adrian Hunter699b9582011-05-06 12:14:01 +03001097#else
1098static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1099 u32 status)
1100{
1101}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001102#endif /* CONFIG_MMC_DEBUG */
1103
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001104/*
1105 * MMC controller internal state machines reset
1106 *
1107 * Used to reset command or data internal state machines, using respectively
1108 * SRC or SRD bit of SYSCTL register
1109 * Can be called from interrupt context
1110 */
Denis Karpov70a33412009-09-22 16:44:59 -07001111static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1112 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001113{
1114 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301115 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001116
1117 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1118 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1119
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001120 /*
1121 * OMAP4 ES2 and greater has an updated reset logic.
1122 * Monitor a 0->1 transition first
1123 */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001124 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001125 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001126 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301127 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001128 }
1129 i = 0;
1130
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001131 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1132 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301133 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001134
1135 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1136 dev_err(mmc_dev(host->mmc),
1137 "Timeout waiting on controller reset in %s\n",
1138 __func__);
1139}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001140
Balaji T K25e18972012-11-19 21:59:55 +05301141static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1142 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301143{
Balaji T K25e18972012-11-19 21:59:55 +05301144 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301145 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301146 if (host->cmd)
1147 host->cmd->error = err;
1148 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301149
1150 if (host->data) {
1151 omap_hsmmc_reset_controller_fsm(host, SRD);
1152 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301153 } else if (host->mrq && host->mrq->cmd)
1154 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301155}
1156
Adrian Hunterb4175772010-05-26 14:42:06 -07001157static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001158{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001159 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001160 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301161 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001162
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001163 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301164 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001165
Venkatraman Sa7e96872012-11-19 22:00:01 +05301166 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001167 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001168
Venkatraman Sa7e96872012-11-19 22:00:01 +05301169 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301170 end_cmd = 1;
Kishon Vijay Abraham I408806f2015-06-16 16:07:17 +05301171 if (host->data || host->response_busy) {
1172 end_trans = !end_cmd;
1173 host->response_busy = 0;
1174 }
Venkatraman Sa7e96872012-11-19 22:00:01 +05301175 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301176 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Vignesh R5027cd12015-06-16 16:07:18 +05301177 else if (status & (CCRC_EN | DCRC_EN | DEB_EN | CEB_EN |
1178 BADA_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301179 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1180
Balaji T Ka2e77152014-01-21 19:54:42 +05301181 if (status & ACE_EN) {
1182 u32 ac12;
1183 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1184 if (!(ac12 & ACNE) && host->mrq->sbc) {
1185 end_cmd = 1;
1186 if (ac12 & ACTO)
1187 error = -ETIMEDOUT;
1188 else if (ac12 & (ACCE | ACEB | ACIE))
1189 error = -EILSEQ;
1190 host->mrq->sbc->error = error;
1191 hsmmc_command_incomplete(host, error, end_cmd);
1192 }
1193 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1194 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001195 }
1196
Francesco Lavra7472bab2013-06-29 08:25:12 +02001197 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301198 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001199 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301200 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001201 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001202}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001203
Adrian Hunterb4175772010-05-26 14:42:06 -07001204/*
1205 * MMC controller IRQ handler
1206 */
1207static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1208{
1209 struct omap_hsmmc_host *host = dev_id;
1210 int status;
1211
1212 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001213 while (status & (INT_EN_MASK | CIRQ_EN)) {
1214 if (host->req_in_progress)
1215 omap_hsmmc_do_irq(host, status);
1216
1217 if (status & CIRQ_EN)
1218 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301219
Adrian Hunterb4175772010-05-26 14:42:06 -07001220 /* Flush posted write */
1221 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301222 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001223
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001224 return IRQ_HANDLED;
1225}
1226
Denis Karpov70a33412009-09-22 16:44:59 -07001227static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001228{
1229 unsigned long i;
1230
1231 OMAP_HSMMC_WRITE(host->base, HCTL,
1232 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1233 for (i = 0; i < loops_per_jiffy; i++) {
1234 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1235 break;
1236 cpu_relax();
1237 }
1238}
1239
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001240/*
David Brownelleb250822009-02-17 14:49:01 -08001241 * Switch MMC interface voltage ... only relevant for MMC1.
1242 *
1243 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1244 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1245 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001246 */
Denis Karpov70a33412009-09-22 16:44:59 -07001247static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001248{
1249 u32 reg_val = 0;
1250 int ret;
1251
1252 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301253 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301254 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301255 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001256
1257 /* Turn the power off */
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001258 ret = omap_hsmmc_set_power(host->dev, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001259
1260 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001261 if (!ret)
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001262 ret = omap_hsmmc_set_power(host->dev, 1, vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301263 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301264 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301265 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001266
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001267 if (ret != 0)
1268 goto err;
1269
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001270 OMAP_HSMMC_WRITE(host->base, HCTL,
1271 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1272 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001273
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001274 /*
1275 * If a MMC dual voltage card is detected, the set_ios fn calls
1276 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001277 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001278 *
David Brownelleb250822009-02-17 14:49:01 -08001279 * Cope with a bit of slop in the range ... per data sheets:
1280 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1281 * but recommended values are 1.71V to 1.89V
1282 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1283 * but recommended values are 2.7V to 3.3V
1284 *
1285 * Board setup code shouldn't permit anything very out-of-range.
1286 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1287 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001288 */
David Brownelleb250822009-02-17 14:49:01 -08001289 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001290 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001291 else
1292 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001293
1294 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001295 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001296
1297 return 0;
1298err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301299 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001300 return ret;
1301}
1302
Adrian Hunterb62f6222009-09-22 16:45:01 -07001303/* Protect the card while the cover is open */
1304static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1305{
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001306 if (!host->get_cover_state)
Adrian Hunterb62f6222009-09-22 16:45:01 -07001307 return;
1308
1309 host->reqs_blocked = 0;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001310 if (host->get_cover_state(host->dev)) {
Adrian Hunterb62f6222009-09-22 16:45:01 -07001311 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301312 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001313 "card is now accessible\n",
1314 mmc_hostname(host->mmc));
1315 host->protect_card = 0;
1316 }
1317 } else {
1318 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301319 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001320 "card is now inaccessible\n",
1321 mmc_hostname(host->mmc));
1322 host->protect_card = 1;
1323 }
1324 }
1325}
1326
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001327/*
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001328 * irq handler when (cell-phone) cover is mounted/removed
1329 */
1330static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id)
1331{
1332 struct omap_hsmmc_host *host = dev_id;
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001333
1334 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
1335
Andreas Fenkart11227d12015-03-03 13:28:17 +01001336 omap_hsmmc_protect_card(host);
1337 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001338 return IRQ_HANDLED;
1339}
1340
Russell Kingc5c98922012-04-13 12:14:39 +01001341static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001342{
Russell Kingc5c98922012-04-13 12:14:39 +01001343 struct omap_hsmmc_host *host = param;
1344 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001345 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001346 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001347
Russell Kingc5c98922012-04-13 12:14:39 +01001348 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001349 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001350 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001351 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001352 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001353
Adrian Hunter770d7432011-05-06 12:14:11 +03001354 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001355 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001356 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001357 dma_unmap_sg(chan->device->dev,
1358 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001359 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001360
1361 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001362 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001363 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001364
1365 /* If DMA has finished after TC, complete the request */
1366 if (!req_in_progress) {
1367 struct mmc_request *mrq = host->mrq;
1368
1369 host->mrq = NULL;
1370 mmc_request_done(host->mmc, mrq);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001371 pm_runtime_mark_last_busy(host->dev);
1372 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001373 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001374}
1375
Per Forlin9782aff2011-07-01 18:55:23 +02001376static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1377 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001378 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001379 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001380{
1381 int dma_len;
1382
1383 if (!next && data->host_cookie &&
1384 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301385 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001386 " host->next_data.cookie %d\n",
1387 __func__, data->host_cookie, host->next_data.cookie);
1388 data->host_cookie = 0;
1389 }
1390
1391 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001392 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001393 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001394 omap_hsmmc_get_dma_dir(host, data));
1395
1396 } else {
1397 dma_len = host->next_data.dma_len;
1398 host->next_data.dma_len = 0;
1399 }
1400
1401
1402 if (dma_len == 0)
1403 return -EINVAL;
1404
1405 if (next) {
1406 next->dma_len = dma_len;
1407 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1408 } else
1409 host->dma_len = dma_len;
1410
1411 return 0;
1412}
1413
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001414/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001415 * Routine to configure and start DMA for the MMC card
1416 */
Balaji T K9d025332014-01-21 19:54:42 +05301417static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001418 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001419{
Russell King26b88522012-04-13 12:27:37 +01001420 struct dma_slave_config cfg;
1421 struct dma_async_tx_descriptor *tx;
1422 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001423 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001424 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001425
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001426 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001427 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001428 struct scatterlist *sgl;
1429
1430 sgl = data->sg + i;
1431 if (sgl->length % data->blksz)
1432 return -EINVAL;
1433 }
1434 if ((data->blksz % 4) != 0)
1435 /* REVISIT: The MMC buffer increments only when MSB is written.
1436 * Return error for blksz which is non multiple of four.
1437 */
1438 return -EINVAL;
1439
Adrian Hunterb4175772010-05-26 14:42:06 -07001440 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001441
Russell Kingc5c98922012-04-13 12:14:39 +01001442 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001443
Russell King26b88522012-04-13 12:27:37 +01001444 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1445 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1446 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1447 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1448 cfg.src_maxburst = data->blksz / 4;
1449 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001450
Russell King26b88522012-04-13 12:27:37 +01001451 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001452 if (ret)
1453 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001454
Russell King26b88522012-04-13 12:27:37 +01001455 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1456 if (ret)
1457 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001458
Russell King26b88522012-04-13 12:27:37 +01001459 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1460 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1461 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1462 if (!tx) {
1463 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1464 /* FIXME: cleanup */
1465 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001466 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001467
Russell King26b88522012-04-13 12:27:37 +01001468 tx->callback = omap_hsmmc_dma_callback;
1469 tx->callback_param = host;
1470
1471 /* Does not fail */
1472 dmaengine_submit(tx);
1473
1474 host->dma_ch = 1;
1475
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001476 return 0;
1477}
1478
Denis Karpov70a33412009-09-22 16:44:59 -07001479static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001480 unsigned int timeout_ns,
1481 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001482{
1483 unsigned int timeout, cycle_ns;
1484 uint32_t reg, clkd, dto = 0;
1485
1486 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1487 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1488 if (clkd == 0)
1489 clkd = 1;
1490
Balaji T K6e3076c2014-01-21 19:54:42 +05301491 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001492 timeout = timeout_ns / cycle_ns;
1493 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001494 if (timeout) {
1495 while ((timeout & 0x80000000) == 0) {
1496 dto += 1;
1497 timeout <<= 1;
1498 }
1499 dto = 31 - dto;
1500 timeout <<= 1;
1501 if (timeout && dto)
1502 dto += 1;
1503 if (dto >= 13)
1504 dto -= 13;
1505 else
1506 dto = 0;
1507 if (dto > 14)
1508 dto = 14;
1509 }
1510
1511 reg &= ~DTO_MASK;
1512 reg |= dto << DTO_SHIFT;
1513 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1514}
1515
Balaji T K9d025332014-01-21 19:54:42 +05301516static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1517{
1518 struct mmc_request *req = host->mrq;
1519 struct dma_chan *chan;
1520
1521 if (!req->data)
1522 return;
1523 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1524 | (req->data->blocks << 16));
1525 set_data_timeout(host, req->data->timeout_ns,
1526 req->data->timeout_clks);
1527 chan = omap_hsmmc_get_dma_chan(host, req->data);
1528 dma_async_issue_pending(chan);
1529}
1530
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001531/*
1532 * Configure block length for MMC/SD cards and initiate the transfer.
1533 */
1534static int
Denis Karpov70a33412009-09-22 16:44:59 -07001535omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001536{
1537 int ret;
1538 host->data = req->data;
1539
1540 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001541 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001542 /*
1543 * Set an arbitrary 100ms data timeout for commands with
1544 * busy signal.
1545 */
1546 if (req->cmd->flags & MMC_RSP_BUSY)
1547 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001548 return 0;
1549 }
1550
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001551 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301552 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001553 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301554 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001555 return ret;
1556 }
1557 }
1558 return 0;
1559}
1560
Per Forlin9782aff2011-07-01 18:55:23 +02001561static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1562 int err)
1563{
1564 struct omap_hsmmc_host *host = mmc_priv(mmc);
1565 struct mmc_data *data = mrq->data;
1566
Russell King26b88522012-04-13 12:27:37 +01001567 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001568 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001569
Russell King26b88522012-04-13 12:27:37 +01001570 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1571 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001572 data->host_cookie = 0;
1573 }
1574}
1575
1576static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1577 bool is_first_req)
1578{
1579 struct omap_hsmmc_host *host = mmc_priv(mmc);
1580
1581 if (mrq->data->host_cookie) {
1582 mrq->data->host_cookie = 0;
1583 return ;
1584 }
1585
Russell Kingc5c98922012-04-13 12:14:39 +01001586 if (host->use_dma) {
1587 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001588
Per Forlin9782aff2011-07-01 18:55:23 +02001589 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001590 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001591 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001592 }
Per Forlin9782aff2011-07-01 18:55:23 +02001593}
1594
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001595/*
1596 * Request function. for read/write operation
1597 */
Denis Karpov70a33412009-09-22 16:44:59 -07001598static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001599{
Denis Karpov70a33412009-09-22 16:44:59 -07001600 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001601 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001602
Adrian Hunterb4175772010-05-26 14:42:06 -07001603 BUG_ON(host->req_in_progress);
1604 BUG_ON(host->dma_ch != -1);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001605 pm_runtime_get_sync(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001606 if (host->protect_card) {
1607 if (host->reqs_blocked < 3) {
1608 /*
1609 * Ensure the controller is left in a consistent
1610 * state by resetting the command and data state
1611 * machines.
1612 */
1613 omap_hsmmc_reset_controller_fsm(host, SRD);
1614 omap_hsmmc_reset_controller_fsm(host, SRC);
1615 host->reqs_blocked += 1;
1616 }
1617 req->cmd->error = -EBADF;
1618 if (req->data)
1619 req->data->error = -EBADF;
1620 req->cmd->retries = 0;
1621 mmc_request_done(mmc, req);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001622 pm_runtime_mark_last_busy(host->dev);
1623 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001624 return;
1625 } else if (host->reqs_blocked)
1626 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001627 WARN_ON(host->mrq != NULL);
1628 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301629 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001630 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001631 if (err) {
1632 req->cmd->error = err;
1633 if (req->data)
1634 req->data->error = err;
1635 host->mrq = NULL;
1636 mmc_request_done(mmc, req);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001637 pm_runtime_mark_last_busy(host->dev);
1638 pm_runtime_put_autosuspend(host->dev);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001639 return;
1640 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301641 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301642 omap_hsmmc_start_command(host, req->sbc, NULL);
1643 return;
1644 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001645
Balaji T K9d025332014-01-21 19:54:42 +05301646 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001647 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001648}
1649
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001650/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001651static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001652{
Denis Karpov70a33412009-09-22 16:44:59 -07001653 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001654 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001655
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301656 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001657
Adrian Huntera3621462009-09-22 16:44:42 -07001658 if (ios->power_mode != host->power_mode) {
1659 switch (ios->power_mode) {
1660 case MMC_POWER_OFF:
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001661 omap_hsmmc_set_power(host->dev, 0, 0);
Adrian Huntera3621462009-09-22 16:44:42 -07001662 break;
1663 case MMC_POWER_UP:
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001664 omap_hsmmc_set_power(host->dev, 1, ios->vdd);
Adrian Huntera3621462009-09-22 16:44:42 -07001665 break;
1666 case MMC_POWER_ON:
1667 do_send_init_stream = 1;
1668 break;
1669 }
1670 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001671 }
1672
Denis Karpovdd498ef2009-09-22 16:44:49 -07001673 /* FIXME: set registers based only on changes to ios */
1674
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001675 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001676
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301677 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001678 /* Only MMC1 can interface at 3V without some flavor
1679 * of external transceiver; but they all handle 1.8V.
1680 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001681 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301682 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001683 /*
1684 * The mmc_select_voltage fn of the core does
1685 * not seem to set the power_mode to
1686 * MMC_POWER_UP upon recalculating the voltage.
1687 * vdd 1.8v.
1688 */
Denis Karpov70a33412009-09-22 16:44:59 -07001689 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1690 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001691 "Switch operation failed\n");
1692 }
1693 }
1694
Andy Shevchenko5934df22011-05-06 12:14:06 +03001695 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001696
Adrian Huntera3621462009-09-22 16:44:42 -07001697 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001698 send_init_stream(host);
1699
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001700 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001701
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301702 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001703}
1704
1705static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1706{
Denis Karpov70a33412009-09-22 16:44:59 -07001707 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001708
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001709 if (!host->card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001710 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001711 return host->card_detect(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001712}
1713
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001714static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1715{
1716 struct omap_hsmmc_host *host = mmc_priv(mmc);
1717
Andreas Fenkart326119c2014-11-08 15:33:14 +01001718 if (mmc_pdata(host)->init_card)
1719 mmc_pdata(host)->init_card(card);
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001720}
1721
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001722static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1723{
1724 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001725 u32 irq_mask, con;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001726 unsigned long flags;
1727
1728 spin_lock_irqsave(&host->irq_lock, flags);
1729
Balaji T K5a52b082014-05-29 10:28:02 +02001730 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001731 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1732 if (enable) {
1733 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1734 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001735 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001736 } else {
1737 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1738 irq_mask &= ~CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001739 con &= ~(CTPL | CLKEXTFREE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001740 }
Balaji T K5a52b082014-05-29 10:28:02 +02001741 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001742 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1743
1744 /*
1745 * if enable, piggy back detection on current request
1746 * but always disable immediately
1747 */
1748 if (!host->req_in_progress || !enable)
1749 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1750
1751 /* flush posted write */
1752 OMAP_HSMMC_READ(host->base, IE);
1753
1754 spin_unlock_irqrestore(&host->irq_lock, flags);
1755}
1756
1757static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1758{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001759 int ret;
1760
1761 /*
1762 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1763 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1764 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1765 * with functional clock disabled.
1766 */
1767 if (!host->dev->of_node || !host->wake_irq)
1768 return -ENODEV;
1769
Tony Lindgren5b83b222015-05-21 15:51:52 -07001770 ret = dev_pm_set_dedicated_wake_irq(host->dev, host->wake_irq);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001771 if (ret) {
1772 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1773 goto err;
1774 }
1775
1776 /*
1777 * Some omaps don't have wake-up path from deeper idle states
1778 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1779 */
1780 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001781 struct pinctrl *p = devm_pinctrl_get(host->dev);
1782 if (!p) {
1783 ret = -ENODEV;
1784 goto err_free_irq;
1785 }
1786 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1787 dev_info(host->dev, "missing default pinctrl state\n");
1788 devm_pinctrl_put(p);
1789 ret = -EINVAL;
1790 goto err_free_irq;
1791 }
1792
1793 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1794 dev_info(host->dev, "missing idle pinctrl state\n");
1795 devm_pinctrl_put(p);
1796 ret = -EINVAL;
1797 goto err_free_irq;
1798 }
1799 devm_pinctrl_put(p);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001800 }
1801
Balaji T K5a52b082014-05-29 10:28:02 +02001802 OMAP_HSMMC_WRITE(host->base, HCTL,
1803 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001804 return 0;
1805
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001806err_free_irq:
Tony Lindgren5b83b222015-05-21 15:51:52 -07001807 dev_pm_clear_wake_irq(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001808err:
1809 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1810 host->wake_irq = 0;
1811 return ret;
1812}
1813
Denis Karpov70a33412009-09-22 16:44:59 -07001814static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001815{
1816 u32 hctl, capa, value;
1817
1818 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301819 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001820 hctl = SDVS30;
1821 capa = VS30 | VS18;
1822 } else {
1823 hctl = SDVS18;
1824 capa = VS18;
1825 }
1826
1827 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1828 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1829
1830 value = OMAP_HSMMC_READ(host->base, CAPA);
1831 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1832
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001833 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001834 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001835}
1836
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07001837static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1838 unsigned int direction, int blk_size)
1839{
1840 /* This controller can't do multiblock reads due to hw bugs */
1841 if (direction == MMC_DATA_READ)
1842 return 1;
1843
1844 return blk_size;
1845}
1846
1847static struct mmc_host_ops omap_hsmmc_ops = {
Per Forlin9782aff2011-07-01 18:55:23 +02001848 .post_req = omap_hsmmc_post_req,
1849 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001850 .request = omap_hsmmc_request,
1851 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001852 .get_cd = omap_hsmmc_get_cd,
Andreas Fenkarta49d8352015-03-03 13:28:14 +01001853 .get_ro = mmc_gpio_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001854 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001855 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001856};
1857
Denis Karpovd900f712009-09-22 16:44:38 -07001858#ifdef CONFIG_DEBUG_FS
1859
Denis Karpov70a33412009-09-22 16:44:59 -07001860static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001861{
1862 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001863 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001864
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001865 seq_printf(s, "mmc%d:\n", mmc->index);
1866 seq_printf(s, "sdio irq mode\t%s\n",
1867 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1868
1869 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1870 seq_printf(s, "sdio irq \t%s\n",
1871 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1872 : "disabled");
1873 }
1874 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001875
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301876 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001877 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001878 seq_printf(s, "CON:\t\t0x%08x\n",
1879 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001880 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1881 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001882 seq_printf(s, "HCTL:\t\t0x%08x\n",
1883 OMAP_HSMMC_READ(host->base, HCTL));
1884 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1885 OMAP_HSMMC_READ(host->base, SYSCTL));
1886 seq_printf(s, "IE:\t\t0x%08x\n",
1887 OMAP_HSMMC_READ(host->base, IE));
1888 seq_printf(s, "ISE:\t\t0x%08x\n",
1889 OMAP_HSMMC_READ(host->base, ISE));
1890 seq_printf(s, "CAPA:\t\t0x%08x\n",
1891 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001892
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301893 pm_runtime_mark_last_busy(host->dev);
1894 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001895
Denis Karpovd900f712009-09-22 16:44:38 -07001896 return 0;
1897}
1898
Denis Karpov70a33412009-09-22 16:44:59 -07001899static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001900{
Denis Karpov70a33412009-09-22 16:44:59 -07001901 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001902}
1903
1904static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001905 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001906 .read = seq_read,
1907 .llseek = seq_lseek,
1908 .release = single_release,
1909};
1910
Denis Karpov70a33412009-09-22 16:44:59 -07001911static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001912{
1913 if (mmc->debugfs_root)
1914 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1915 mmc, &mmc_regs_fops);
1916}
1917
1918#else
1919
Denis Karpov70a33412009-09-22 16:44:59 -07001920static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001921{
1922}
1923
1924#endif
1925
Rajendra Nayak46856a62012-03-12 20:32:37 +05301926#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001927static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1928 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1929 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1930};
1931
1932static const struct omap_mmc_of_data omap4_mmc_of_data = {
1933 .reg_offset = 0x100,
1934};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001935static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1936 .reg_offset = 0x100,
1937 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1938};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301939
1940static const struct of_device_id omap_mmc_of_match[] = {
1941 {
1942 .compatible = "ti,omap2-hsmmc",
1943 },
1944 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001945 .compatible = "ti,omap3-pre-es3-hsmmc",
1946 .data = &omap3_pre_es3_mmc_of_data,
1947 },
1948 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301949 .compatible = "ti,omap3-hsmmc",
1950 },
1951 {
1952 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001953 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301954 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001955 {
1956 .compatible = "ti,am33xx-hsmmc",
1957 .data = &am33xx_mmc_of_data,
1958 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301959 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001960};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301961MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1962
Andreas Fenkart551434382014-11-08 15:33:09 +01001963static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
Rajendra Nayak46856a62012-03-12 20:32:37 +05301964{
Andreas Fenkart551434382014-11-08 15:33:09 +01001965 struct omap_hsmmc_platform_data *pdata;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301966 struct device_node *np = dev->of_node;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301967
1968 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1969 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301970 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301971
1972 if (of_find_property(np, "ti,dual-volt", NULL))
1973 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1974
Andreas Fenkartb7a56462015-03-20 15:53:54 +01001975 pdata->gpio_cd = -EINVAL;
1976 pdata->gpio_cod = -EINVAL;
NeilBrownfdb9de12015-01-13 08:23:18 +13001977 pdata->gpio_wp = -EINVAL;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301978
1979 if (of_find_property(np, "ti,non-removable", NULL)) {
Andreas Fenkart326119c2014-11-08 15:33:14 +01001980 pdata->nonremovable = true;
1981 pdata->no_regulator_off_init = true;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301982 }
Rajendra Nayak46856a62012-03-12 20:32:37 +05301983
1984 if (of_find_property(np, "ti,needs-special-reset", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001985 pdata->features |= HSMMC_HAS_UPDATED_RESET;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301986
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301987 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001988 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301989
Rajendra Nayak46856a62012-03-12 20:32:37 +05301990 return pdata;
1991}
1992#else
Andreas Fenkart551434382014-11-08 15:33:09 +01001993static inline struct omap_hsmmc_platform_data
Rajendra Nayak46856a62012-03-12 20:32:37 +05301994 *of_get_hsmmc_pdata(struct device *dev)
1995{
Balaji T K19df45b2014-02-28 19:08:18 +05301996 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301997}
1998#endif
1999
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05002000static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002001{
Andreas Fenkart551434382014-11-08 15:33:09 +01002002 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002003 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07002004 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002005 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002006 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302007 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01002008 dma_cap_mask_t mask;
2009 unsigned tx_req, rx_req;
Nishanth Menon59445b12014-02-13 23:45:48 -06002010 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05302011 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302012
2013 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
2014 if (match) {
2015 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01002016
2017 if (IS_ERR(pdata))
2018 return PTR_ERR(pdata);
2019
Rajendra Nayak46856a62012-03-12 20:32:37 +05302020 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06002021 data = match->data;
2022 pdata->reg_offset = data->reg_offset;
2023 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302024 }
2025 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002026
2027 if (pdata == NULL) {
2028 dev_err(&pdev->dev, "Platform Data is missing\n");
2029 return -ENXIO;
2030 }
2031
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002032 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2033 irq = platform_get_irq(pdev, 0);
2034 if (res == NULL || irq < 0)
2035 return -ENXIO;
2036
Balaji T K77fae212014-05-09 22:16:51 +05302037 base = devm_ioremap_resource(&pdev->dev, res);
2038 if (IS_ERR(base))
2039 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002040
Denis Karpov70a33412009-09-22 16:44:59 -07002041 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002042 if (!mmc) {
2043 ret = -ENOMEM;
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002044 goto err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002045 }
2046
NeilBrownfdb9de12015-01-13 08:23:18 +13002047 ret = mmc_of_parse(mmc);
2048 if (ret)
2049 goto err1;
2050
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002051 host = mmc_priv(mmc);
2052 host->mmc = mmc;
2053 host->pdata = pdata;
2054 host->dev = &pdev->dev;
2055 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002056 host->dma_ch = -1;
2057 host->irq = irq;
Balaji T Kfc307df2012-04-02 12:26:47 +05302058 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05302059 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08002060 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002061 host->next_data.cookie = 1;
Tony Lindgrenbb2726b2015-10-07 06:22:24 -07002062 host->pbias_enabled = 0;
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +05302063 host->vqmmc_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002064
NeilBrown41afa3142015-01-13 08:23:18 +13002065 ret = omap_hsmmc_gpio_init(mmc, host, pdata);
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002066 if (ret)
2067 goto err_gpio;
2068
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002069 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002070
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002071 if (pdev->dev.of_node)
2072 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2073
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302074 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002075
Daniel Mackd418ed82012-02-19 13:20:33 +01002076 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2077
2078 if (pdata->max_freq > 0)
2079 mmc->f_max = pdata->max_freq;
NeilBrownfdb9de12015-01-13 08:23:18 +13002080 else if (mmc->f_max == 0)
Daniel Mackd418ed82012-02-19 13:20:33 +01002081 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002082
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002083 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002084
Balaji T K96181952014-05-09 22:16:48 +05302085 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002086 if (IS_ERR(host->fclk)) {
2087 ret = PTR_ERR(host->fclk);
2088 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002089 goto err1;
2090 }
2091
Paul Walmsley9b682562011-10-06 14:50:35 -06002092 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2093 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07002094 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
Paul Walmsley9b682562011-10-06 14:50:35 -06002095 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002096
Tony Lindgren5b83b222015-05-21 15:51:52 -07002097 device_init_wakeup(&pdev->dev, true);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302098 pm_runtime_enable(host->dev);
2099 pm_runtime_get_sync(host->dev);
2100 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2101 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002102
Balaji T K92a3aeb2012-02-24 21:14:34 +05302103 omap_hsmmc_context_save(host);
2104
Balaji T K96181952014-05-09 22:16:48 +05302105 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302106 /*
2107 * MMC can still work without debounce clock.
2108 */
2109 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302110 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302111 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302112 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302113 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002114 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002115
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002116 /* Since we do only SG emulation, we can have as many segs
2117 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002118 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002119
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002120 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2121 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2122 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2123 mmc->max_seg_size = mmc->max_req_size;
2124
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002125 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002126 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002127
Andreas Fenkart326119c2014-11-08 15:33:14 +01002128 mmc->caps |= mmc_pdata(host)->caps;
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002129 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002130 mmc->caps |= MMC_CAP_4_BIT_DATA;
2131
Andreas Fenkart326119c2014-11-08 15:33:14 +01002132 if (mmc_pdata(host)->nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002133 mmc->caps |= MMC_CAP_NONREMOVABLE;
2134
NeilBrownfdb9de12015-01-13 08:23:18 +13002135 mmc->pm_caps |= mmc_pdata(host)->pm_caps;
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002136
Denis Karpov70a33412009-09-22 16:44:59 -07002137 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002138
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302139 if (!pdev->dev.of_node) {
2140 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2141 if (!res) {
2142 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2143 ret = -ENXIO;
2144 goto err_irq;
2145 }
2146 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002147
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302148 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2149 if (!res) {
2150 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2151 ret = -ENXIO;
2152 goto err_irq;
2153 }
2154 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002155 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002156
Russell King26b88522012-04-13 12:27:37 +01002157 dma_cap_zero(mask);
2158 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002159
Matt Porterd272fbf2013-05-10 17:42:34 +05302160 host->rx_chan =
2161 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2162 &rx_req, &pdev->dev, "rx");
2163
Russell King26b88522012-04-13 12:27:37 +01002164 if (!host->rx_chan) {
2165 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002166 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002167 goto err_irq;
2168 }
2169
Matt Porterd272fbf2013-05-10 17:42:34 +05302170 host->tx_chan =
2171 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2172 &tx_req, &pdev->dev, "tx");
2173
Russell King26b88522012-04-13 12:27:37 +01002174 if (!host->tx_chan) {
2175 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002176 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002177 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002178 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002179
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002180 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302181 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002182 mmc_hostname(mmc), host);
2183 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302184 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002185 goto err_irq;
2186 }
2187
Kishon Vijay Abraham I987e05c2015-08-27 14:44:07 +05302188 ret = omap_hsmmc_reg_get(host);
2189 if (ret)
2190 goto err_irq;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002191
Andreas Fenkart326119c2014-11-08 15:33:14 +01002192 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002193
Adrian Hunterb4175772010-05-26 14:42:06 -07002194 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002195
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002196 /*
2197 * For now, only support SDIO interrupt if we have a separate
2198 * wake-up interrupt configured from device tree. This is because
2199 * the wake-up interrupt is needed for idle state and some
2200 * platforms need special quirks. And we don't want to add new
2201 * legacy mux platform init code callbacks any longer as we
2202 * are moving to DT based booting anyways.
2203 */
2204 ret = omap_hsmmc_configure_wake_irq(host);
2205 if (!ret)
2206 mmc->caps |= MMC_CAP_SDIO_IRQ;
2207
Adrian Hunterb62f6222009-09-22 16:45:01 -07002208 omap_hsmmc_protect_card(host);
2209
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002210 mmc_add_host(mmc);
2211
Andreas Fenkart326119c2014-11-08 15:33:14 +01002212 if (mmc_pdata(host)->name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002213 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2214 if (ret < 0)
2215 goto err_slot_name;
2216 }
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002217 if (host->get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002218 ret = device_create_file(&mmc->class_dev,
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002219 &dev_attr_cover_switch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002220 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002221 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002222 }
2223
Denis Karpov70a33412009-09-22 16:44:59 -07002224 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302225 pm_runtime_mark_last_busy(host->dev);
2226 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002227
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002228 return 0;
2229
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002230err_slot_name:
2231 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002232err_irq:
Tony Lindgren5b83b222015-05-21 15:51:52 -07002233 device_init_wakeup(&pdev->dev, false);
Russell Kingc5c98922012-04-13 12:14:39 +01002234 if (host->tx_chan)
2235 dma_release_channel(host->tx_chan);
2236 if (host->rx_chan)
2237 dma_release_channel(host->rx_chan);
Tony Lindgren814a3c02016-02-10 15:02:44 -08002238 pm_runtime_dont_use_autosuspend(host->dev);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302239 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002240 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302241 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302242 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002243err1:
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002244err_gpio:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002245 mmc_free_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002246err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002247 return ret;
2248}
2249
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002250static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002251{
Denis Karpov70a33412009-09-22 16:44:59 -07002252 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002253
Felipe Balbi927ce942012-03-14 11:18:27 +02002254 pm_runtime_get_sync(host->dev);
2255 mmc_remove_host(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002256
Peter Ujfalusidc285622015-11-03 13:37:31 +02002257 dma_release_channel(host->tx_chan);
2258 dma_release_channel(host->rx_chan);
Russell Kingc5c98922012-04-13 12:14:39 +01002259
Tony Lindgren814a3c02016-02-10 15:02:44 -08002260 pm_runtime_dont_use_autosuspend(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002261 pm_runtime_put_sync(host->dev);
2262 pm_runtime_disable(host->dev);
Tony Lindgren5b83b222015-05-21 15:51:52 -07002263 device_init_wakeup(&pdev->dev, false);
Balaji T K96181952014-05-09 22:16:48 +05302264 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302265 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002266
Balaji T K9d1f0282012-10-15 21:35:07 +05302267 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002268
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002269 return 0;
2270}
2271
Russ Dill3d3bbfb2015-02-27 13:24:34 +02002272#ifdef CONFIG_PM_SLEEP
Kevin Hilmana791daa2010-05-26 14:42:07 -07002273static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002274{
Felipe Balbi927ce942012-03-14 11:18:27 +02002275 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2276
2277 if (!host)
2278 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002279
Felipe Balbi927ce942012-03-14 11:18:27 +02002280 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002281
2282 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002283 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2284 OMAP_HSMMC_WRITE(host->base, IE, 0);
2285 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002286 OMAP_HSMMC_WRITE(host->base, HCTL,
2287 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2288 }
2289
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302290 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302291 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002292
Eliad Peller31f9d462011-11-22 16:02:17 +02002293 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002294 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002295}
2296
2297/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002298static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002299{
Felipe Balbi927ce942012-03-14 11:18:27 +02002300 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2301
2302 if (!host)
2303 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002304
Felipe Balbi927ce942012-03-14 11:18:27 +02002305 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002306
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302307 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302308 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002309
Felipe Balbi927ce942012-03-14 11:18:27 +02002310 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2311 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002312
Felipe Balbi927ce942012-03-14 11:18:27 +02002313 omap_hsmmc_protect_card(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002314 pm_runtime_mark_last_busy(host->dev);
2315 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002316 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002317}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002318#endif
2319
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302320static int omap_hsmmc_runtime_suspend(struct device *dev)
2321{
2322 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002323 unsigned long flags;
Andreas Fenkartf9459012014-05-29 10:28:03 +02002324 int ret = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302325
2326 host = platform_get_drvdata(to_platform_device(dev));
2327 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002328 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302329
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002330 spin_lock_irqsave(&host->irq_lock, flags);
2331 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2332 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2333 /* disable sdio irq handling to prevent race */
2334 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2335 OMAP_HSMMC_WRITE(host->base, IE, 0);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002336
2337 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2338 /*
2339 * dat1 line low, pending sdio irq
2340 * race condition: possible irq handler running on
2341 * multi-core, abort
2342 */
2343 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2344 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2345 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2346 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2347 pm_runtime_mark_last_busy(dev);
2348 ret = -EBUSY;
2349 goto abort;
2350 }
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002351
Andreas Fenkart97978a42014-05-29 10:28:04 +02002352 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002353 } else {
2354 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002355 }
Andreas Fenkart97978a42014-05-29 10:28:04 +02002356
Andreas Fenkartf9459012014-05-29 10:28:03 +02002357abort:
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002358 spin_unlock_irqrestore(&host->irq_lock, flags);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002359 return ret;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302360}
2361
2362static int omap_hsmmc_runtime_resume(struct device *dev)
2363{
2364 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002365 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302366
2367 host = platform_get_drvdata(to_platform_device(dev));
2368 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002369 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302370
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002371 spin_lock_irqsave(&host->irq_lock, flags);
2372 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2373 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002374
Andreas Fenkart97978a42014-05-29 10:28:04 +02002375 pinctrl_pm_select_default_state(host->dev);
2376
2377 /* irq lost, if pinmux incorrect */
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002378 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2379 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2380 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002381 } else {
2382 pinctrl_pm_select_default_state(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002383 }
2384 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302385 return 0;
2386}
2387
Kevin Hilmana791daa2010-05-26 14:42:07 -07002388static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Russ Dill3d3bbfb2015-02-27 13:24:34 +02002389 SET_SYSTEM_SLEEP_PM_OPS(omap_hsmmc_suspend, omap_hsmmc_resume)
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302390 .runtime_suspend = omap_hsmmc_runtime_suspend,
2391 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002392};
2393
2394static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002395 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002396 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002397 .driver = {
2398 .name = DRIVER_NAME,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002399 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302400 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002401 },
2402};
2403
Felipe Balbib7964502012-03-14 11:18:32 +02002404module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002405MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2406MODULE_LICENSE("GPL");
2407MODULE_ALIAS("platform:" DRIVER_NAME);
2408MODULE_AUTHOR("Texas Instruments Inc");