blob: b6639ea0bf18dbd37ccf0d639073119353b418b6 [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);
Tony Lindgren123e20b2015-10-07 06:22:25 -0700506 if ((ret != -ENODEV) && host->dev->of_node)
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530507 return ret;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530508 dev_dbg(host->dev, "unable to get pbias regulator %ld\n",
Kishon Vijay Abraham Ic299dc32015-08-27 14:43:55 +0530509 PTR_ERR(host->pbias));
510 host->pbias = NULL;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530511 }
Balaji T Ke99448f2014-02-19 20:26:40 +0530512
Balaji T K987fd492014-02-19 20:26:40 +0530513 /* For eMMC do not power off when not in sleep state */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100514 if (mmc_pdata(host)->no_regulator_off_init)
Balaji T K987fd492014-02-19 20:26:40 +0530515 return 0;
Adrian Huntere840ce12011-05-06 12:14:10 +0300516
Kishon Vijay Abraham Ic8518ef2015-08-27 14:44:03 +0530517 ret = omap_hsmmc_disable_boot_regulators(host);
518 if (ret)
519 return ret;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800520
521 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800522}
523
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100524static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id);
NeilBrown41afa3142015-01-13 08:23:18 +1300525
526static int omap_hsmmc_gpio_init(struct mmc_host *mmc,
527 struct omap_hsmmc_host *host,
Andreas Fenkart1e363e32014-11-08 15:33:15 +0100528 struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800529{
530 int ret;
531
Andreas Fenkartb7a56462015-03-20 15:53:54 +0100532 if (gpio_is_valid(pdata->gpio_cod)) {
533 ret = mmc_gpio_request_cd(mmc, pdata->gpio_cod, 0);
Adrian Hunterb702b102010-02-15 10:03:35 -0800534 if (ret)
535 return ret;
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100536
537 host->get_cover_state = omap_hsmmc_get_cover_state;
538 mmc_gpio_set_cd_isr(mmc, omap_hsmmc_cover_irq);
Andreas Fenkartb7a56462015-03-20 15:53:54 +0100539 } else if (gpio_is_valid(pdata->gpio_cd)) {
540 ret = mmc_gpio_request_cd(mmc, pdata->gpio_cd, 0);
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100541 if (ret)
542 return ret;
543
544 host->card_detect = omap_hsmmc_card_detect;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100545 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800546
Andreas Fenkart326119c2014-11-08 15:33:14 +0100547 if (gpio_is_valid(pdata->gpio_wp)) {
NeilBrown41afa3142015-01-13 08:23:18 +1300548 ret = mmc_gpio_request_ro(mmc, pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800549 if (ret)
NeilBrown41afa3142015-01-13 08:23:18 +1300550 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100551 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800552
553 return 0;
Adrian Hunterb702b102010-02-15 10:03:35 -0800554}
555
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100556/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300557 * Start clock to the card
558 */
559static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
560{
561 OMAP_HSMMC_WRITE(host->base, SYSCTL,
562 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
563}
564
565/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100566 * Stop clock to the card
567 */
Denis Karpov70a33412009-09-22 16:44:59 -0700568static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100569{
570 OMAP_HSMMC_WRITE(host->base, SYSCTL,
571 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
572 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900573 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100574}
575
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700576static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
577 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700578{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200579 u32 irq_mask = INT_EN_MASK;
580 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700581
582 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200583 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700584
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700585 /* Disable timeout for erases */
586 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530587 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700588
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200589 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700590 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
591 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200592
593 /* latch pending CIRQ, but don't signal MMC core */
594 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
595 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700596 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200597 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700598}
599
600static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
601{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200602 u32 irq_mask = 0;
603 unsigned long flags;
604
605 spin_lock_irqsave(&host->irq_lock, flags);
606 /* no transfer running but need to keep cirq if enabled */
607 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
608 irq_mask |= CIRQ_EN;
609 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
610 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700611 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200612 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700613}
614
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300615/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530616static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300617{
618 u16 dsor = 0;
619
620 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530621 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530622 if (dsor > CLKD_MAX)
623 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300624 }
625
626 return dsor;
627}
628
Andy Shevchenko5934df22011-05-06 12:14:06 +0300629static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
630{
631 struct mmc_ios *ios = &host->mmc->ios;
632 unsigned long regval;
633 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530634 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300635
Venkatraman S8986d312012-08-07 19:10:38 +0530636 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300637
638 omap_hsmmc_stop_clock(host);
639
640 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
641 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530642 clkdiv = calc_divisor(host, ios);
643 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300644 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
645 OMAP_HSMMC_WRITE(host->base, SYSCTL,
646 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
647
648 /* Wait till the ICS bit is set */
649 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
650 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
651 && time_before(jiffies, timeout))
652 cpu_relax();
653
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530654 /*
655 * Enable High-Speed Support
656 * Pre-Requisites
657 * - Controller should support High-Speed-Enable Bit
658 * - Controller should not be using DDR Mode
659 * - Controller should advertise that it supports High Speed
660 * in capabilities register
661 * - MMC/SD clock coming out of controller > 25MHz
662 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100663 if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900664 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Ulf Hansson903101a2014-11-25 13:05:13 +0100665 (ios->timing != MMC_TIMING_UHS_DDR50) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530666 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
667 regval = OMAP_HSMMC_READ(host->base, HCTL);
668 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
669 regval |= HSPE;
670 else
671 regval &= ~HSPE;
672
673 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
674 }
675
Andy Shevchenko5934df22011-05-06 12:14:06 +0300676 omap_hsmmc_start_clock(host);
677}
678
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400679static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
680{
681 struct mmc_ios *ios = &host->mmc->ios;
682 u32 con;
683
684 con = OMAP_HSMMC_READ(host->base, CON);
Ulf Hansson903101a2014-11-25 13:05:13 +0100685 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
686 ios->timing == MMC_TIMING_UHS_DDR50)
Balaji T K03b5d922012-04-09 12:08:33 +0530687 con |= DDR; /* configure in DDR mode */
688 else
689 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400690 switch (ios->bus_width) {
691 case MMC_BUS_WIDTH_8:
692 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
693 break;
694 case MMC_BUS_WIDTH_4:
695 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
696 OMAP_HSMMC_WRITE(host->base, HCTL,
697 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
698 break;
699 case MMC_BUS_WIDTH_1:
700 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
701 OMAP_HSMMC_WRITE(host->base, HCTL,
702 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
703 break;
704 }
705}
706
707static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
708{
709 struct mmc_ios *ios = &host->mmc->ios;
710 u32 con;
711
712 con = OMAP_HSMMC_READ(host->base, CON);
713 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
714 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
715 else
716 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
717}
718
Denis Karpov11dd62a2009-09-22 16:44:43 -0700719#ifdef CONFIG_PM
720
721/*
722 * Restore the MMC host context, if it was lost as result of a
723 * power state change.
724 */
Denis Karpov70a33412009-09-22 16:44:59 -0700725static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700726{
727 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400728 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700729 unsigned long timeout;
730
Tony Lindgren0a82e062013-10-21 00:25:19 +0530731 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
732 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
733 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
734 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
735 return 0;
736
737 host->context_loss++;
738
Balaji T Kc2200ef2012-03-07 09:55:30 -0500739 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700740 if (host->power_mode != MMC_POWER_OFF &&
741 (1 << ios->vdd) <= MMC_VDD_23_24)
742 hctl = SDVS18;
743 else
744 hctl = SDVS30;
745 capa = VS30 | VS18;
746 } else {
747 hctl = SDVS18;
748 capa = VS18;
749 }
750
Balaji T K5a52b082014-05-29 10:28:02 +0200751 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
752 hctl |= IWE;
753
Denis Karpov11dd62a2009-09-22 16:44:43 -0700754 OMAP_HSMMC_WRITE(host->base, HCTL,
755 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
756
757 OMAP_HSMMC_WRITE(host->base, CAPA,
758 OMAP_HSMMC_READ(host->base, CAPA) | capa);
759
760 OMAP_HSMMC_WRITE(host->base, HCTL,
761 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
762
763 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
764 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
765 && time_before(jiffies, timeout))
766 ;
767
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200768 OMAP_HSMMC_WRITE(host->base, ISE, 0);
769 OMAP_HSMMC_WRITE(host->base, IE, 0);
770 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700771
772 /* Do not initialize card-specific things if the power is off */
773 if (host->power_mode == MMC_POWER_OFF)
774 goto out;
775
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400776 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700777
Andy Shevchenko5934df22011-05-06 12:14:06 +0300778 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700779
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400780 omap_hsmmc_set_bus_mode(host);
781
Denis Karpov11dd62a2009-09-22 16:44:43 -0700782out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530783 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
784 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700785 return 0;
786}
787
788/*
789 * Save the MMC host context (store the number of power state changes so far).
790 */
Denis Karpov70a33412009-09-22 16:44:59 -0700791static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700792{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530793 host->con = OMAP_HSMMC_READ(host->base, CON);
794 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
795 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
796 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700797}
798
799#else
800
Denis Karpov70a33412009-09-22 16:44:59 -0700801static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700802{
803 return 0;
804}
805
Denis Karpov70a33412009-09-22 16:44:59 -0700806static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700807{
808}
809
810#endif
811
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100812/*
813 * Send init stream sequence to card
814 * before sending IDLE command
815 */
Denis Karpov70a33412009-09-22 16:44:59 -0700816static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100817{
818 int reg = 0;
819 unsigned long timeout;
820
Adrian Hunterb62f6222009-09-22 16:45:01 -0700821 if (host->protect_card)
822 return;
823
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100824 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700825
826 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100827 OMAP_HSMMC_WRITE(host->base, CON,
828 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
829 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
830
831 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530832 while ((reg != CC_EN) && time_before(jiffies, timeout))
833 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100834
835 OMAP_HSMMC_WRITE(host->base, CON,
836 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700837
838 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
839 OMAP_HSMMC_READ(host->base, STAT);
840
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100841 enable_irq(host->irq);
842}
843
844static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700845int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100846{
847 int r = 1;
848
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100849 if (host->get_cover_state)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100850 r = host->get_cover_state(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100851 return r;
852}
853
854static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700855omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100856 char *buf)
857{
858 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700859 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100860
Denis Karpov70a33412009-09-22 16:44:59 -0700861 return sprintf(buf, "%s\n",
862 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100863}
864
Denis Karpov70a33412009-09-22 16:44:59 -0700865static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100866
867static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700868omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100869 char *buf)
870{
871 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700872 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100873
Andreas Fenkart326119c2014-11-08 15:33:14 +0100874 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100875}
876
Denis Karpov70a33412009-09-22 16:44:59 -0700877static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100878
879/*
880 * Configure the response type and send the cmd.
881 */
882static void
Denis Karpov70a33412009-09-22 16:44:59 -0700883omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100884 struct mmc_data *data)
885{
886 int cmdreg = 0, resptype = 0, cmdtype = 0;
887
Venkatraman S8986d312012-08-07 19:10:38 +0530888 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100889 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
890 host->cmd = cmd;
891
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700892 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100893
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200894 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100895 if (cmd->flags & MMC_RSP_PRESENT) {
896 if (cmd->flags & MMC_RSP_136)
897 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200898 else if (cmd->flags & MMC_RSP_BUSY) {
899 resptype = 3;
900 host->response_busy = 1;
901 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100902 resptype = 2;
903 }
904
905 /*
906 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
907 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
908 * a val of 0x3, rest 0x0.
909 */
910 if (cmd == host->mrq->stop)
911 cmdtype = 0x3;
912
913 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
914
Balaji T Ka2e77152014-01-21 19:54:42 +0530915 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
916 host->mrq->sbc) {
917 cmdreg |= ACEN_ACMD23;
918 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
919 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100920 if (data) {
921 cmdreg |= DP_SELECT | MSBS | BCE;
922 if (data->flags & MMC_DATA_READ)
923 cmdreg |= DDIR;
924 else
925 cmdreg &= ~(DDIR);
926 }
927
928 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530929 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100930
Adrian Hunterb4175772010-05-26 14:42:06 -0700931 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700932
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100933 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
934 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
935}
936
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200937static int
Denis Karpov70a33412009-09-22 16:44:59 -0700938omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200939{
940 if (data->flags & MMC_DATA_WRITE)
941 return DMA_TO_DEVICE;
942 else
943 return DMA_FROM_DEVICE;
944}
945
Russell Kingc5c98922012-04-13 12:14:39 +0100946static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
947 struct mmc_data *data)
948{
949 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
950}
951
Adrian Hunterb4175772010-05-26 14:42:06 -0700952static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
953{
954 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530955 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700956
Venkatraman S31463b12012-04-09 12:08:34 +0530957 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700958 host->req_in_progress = 0;
959 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530960 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700961
962 omap_hsmmc_disable_irq(host);
963 /* Do not complete the request if DMA is still in progress */
964 if (mrq->data && host->use_dma && dma_ch != -1)
965 return;
966 host->mrq = NULL;
967 mmc_request_done(host->mmc, mrq);
NeilBrownf57ba4c2015-03-26 12:18:23 +1100968 pm_runtime_mark_last_busy(host->dev);
969 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -0700970}
971
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100972/*
973 * Notify the transfer complete to MMC core
974 */
975static void
Denis Karpov70a33412009-09-22 16:44:59 -0700976omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100977{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200978 if (!data) {
979 struct mmc_request *mrq = host->mrq;
980
Adrian Hunter23050102009-09-22 16:44:57 -0700981 /* TC before CC from CMD6 - don't know why, but it happens */
982 if (host->cmd && host->cmd->opcode == 6 &&
983 host->response_busy) {
984 host->response_busy = 0;
985 return;
986 }
987
Adrian Hunterb4175772010-05-26 14:42:06 -0700988 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200989 return;
990 }
991
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100992 host->data = NULL;
993
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100994 if (!data->error)
995 data->bytes_xfered += data->blocks * (data->blksz);
996 else
997 data->bytes_xfered = 0;
998
Balaji T Kbf129e12014-01-21 19:54:42 +0530999 if (data->stop && (data->error || !host->mrq->sbc))
1000 omap_hsmmc_start_command(host, data->stop, NULL);
1001 else
Adrian Hunterb4175772010-05-26 14:42:06 -07001002 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001003}
1004
1005/*
1006 * Notify the core about command completion
1007 */
1008static void
Denis Karpov70a33412009-09-22 16:44:59 -07001009omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001010{
Balaji T Kbf129e12014-01-21 19:54:42 +05301011 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +05301012 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +05301013 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +05301014 omap_hsmmc_start_dma_transfer(host);
1015 omap_hsmmc_start_command(host, host->mrq->cmd,
1016 host->mrq->data);
1017 return;
1018 }
1019
Balaji T K2177fa92014-05-09 22:16:52 +05301020 host->cmd = NULL;
1021
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001022 if (cmd->flags & MMC_RSP_PRESENT) {
1023 if (cmd->flags & MMC_RSP_136) {
1024 /* response type 2 */
1025 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
1026 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
1027 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
1028 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
1029 } else {
1030 /* response types 1, 1b, 3, 4, 5, 6 */
1031 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
1032 }
1033 }
Adrian Hunterb4175772010-05-26 14:42:06 -07001034 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +05301035 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001036}
1037
1038/*
1039 * DMA clean up for command errors
1040 */
Denis Karpov70a33412009-09-22 16:44:59 -07001041static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001042{
Adrian Hunterb4175772010-05-26 14:42:06 -07001043 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +05301044 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -07001045
Jarkko Lavinen82788ff2008-12-05 12:31:46 +02001046 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001047
Venkatraman S31463b12012-04-09 12:08:34 +05301048 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001049 dma_ch = host->dma_ch;
1050 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +05301051 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001052
1053 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +01001054 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
1055
1056 dmaengine_terminate_all(chan);
1057 dma_unmap_sg(chan->device->dev,
1058 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -07001059 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +01001060
Per Forlin053bf342011-11-07 21:55:11 +05301061 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001062 }
1063 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001064}
1065
1066/*
1067 * Readable error output
1068 */
1069#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +03001070static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001071{
1072 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -07001073 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +03001074 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
1075 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
1076 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
1077 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001078 };
1079 char res[256];
1080 char *buf = res;
1081 int len, i;
1082
1083 len = sprintf(buf, "MMC IRQ 0x%x :", status);
1084 buf += len;
1085
Denis Karpov70a33412009-09-22 16:44:59 -07001086 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001087 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -07001088 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001089 buf += len;
1090 }
1091
Venkatraman S8986d312012-08-07 19:10:38 +05301092 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001093}
Adrian Hunter699b9582011-05-06 12:14:01 +03001094#else
1095static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1096 u32 status)
1097{
1098}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001099#endif /* CONFIG_MMC_DEBUG */
1100
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001101/*
1102 * MMC controller internal state machines reset
1103 *
1104 * Used to reset command or data internal state machines, using respectively
1105 * SRC or SRD bit of SYSCTL register
1106 * Can be called from interrupt context
1107 */
Denis Karpov70a33412009-09-22 16:44:59 -07001108static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1109 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001110{
1111 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301112 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001113
1114 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1115 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1116
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001117 /*
1118 * OMAP4 ES2 and greater has an updated reset logic.
1119 * Monitor a 0->1 transition first
1120 */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001121 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001122 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001123 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301124 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001125 }
1126 i = 0;
1127
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001128 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1129 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301130 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001131
1132 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1133 dev_err(mmc_dev(host->mmc),
1134 "Timeout waiting on controller reset in %s\n",
1135 __func__);
1136}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001137
Balaji T K25e18972012-11-19 21:59:55 +05301138static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1139 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301140{
Balaji T K25e18972012-11-19 21:59:55 +05301141 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301142 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301143 if (host->cmd)
1144 host->cmd->error = err;
1145 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301146
1147 if (host->data) {
1148 omap_hsmmc_reset_controller_fsm(host, SRD);
1149 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301150 } else if (host->mrq && host->mrq->cmd)
1151 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301152}
1153
Adrian Hunterb4175772010-05-26 14:42:06 -07001154static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001155{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001156 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001157 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301158 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001159
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001160 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301161 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001162
Venkatraman Sa7e96872012-11-19 22:00:01 +05301163 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001164 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001165
Venkatraman Sa7e96872012-11-19 22:00:01 +05301166 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301167 end_cmd = 1;
Kishon Vijay Abraham I408806f2015-06-16 16:07:17 +05301168 if (host->data || host->response_busy) {
1169 end_trans = !end_cmd;
1170 host->response_busy = 0;
1171 }
Venkatraman Sa7e96872012-11-19 22:00:01 +05301172 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301173 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Vignesh R5027cd12015-06-16 16:07:18 +05301174 else if (status & (CCRC_EN | DCRC_EN | DEB_EN | CEB_EN |
1175 BADA_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301176 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1177
Balaji T Ka2e77152014-01-21 19:54:42 +05301178 if (status & ACE_EN) {
1179 u32 ac12;
1180 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1181 if (!(ac12 & ACNE) && host->mrq->sbc) {
1182 end_cmd = 1;
1183 if (ac12 & ACTO)
1184 error = -ETIMEDOUT;
1185 else if (ac12 & (ACCE | ACEB | ACIE))
1186 error = -EILSEQ;
1187 host->mrq->sbc->error = error;
1188 hsmmc_command_incomplete(host, error, end_cmd);
1189 }
1190 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1191 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001192 }
1193
Francesco Lavra7472bab2013-06-29 08:25:12 +02001194 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301195 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001196 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301197 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001198 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001199}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001200
Adrian Hunterb4175772010-05-26 14:42:06 -07001201/*
1202 * MMC controller IRQ handler
1203 */
1204static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1205{
1206 struct omap_hsmmc_host *host = dev_id;
1207 int status;
1208
1209 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001210 while (status & (INT_EN_MASK | CIRQ_EN)) {
1211 if (host->req_in_progress)
1212 omap_hsmmc_do_irq(host, status);
1213
1214 if (status & CIRQ_EN)
1215 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301216
Adrian Hunterb4175772010-05-26 14:42:06 -07001217 /* Flush posted write */
1218 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301219 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001220
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001221 return IRQ_HANDLED;
1222}
1223
Denis Karpov70a33412009-09-22 16:44:59 -07001224static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001225{
1226 unsigned long i;
1227
1228 OMAP_HSMMC_WRITE(host->base, HCTL,
1229 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1230 for (i = 0; i < loops_per_jiffy; i++) {
1231 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1232 break;
1233 cpu_relax();
1234 }
1235}
1236
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001237/*
David Brownelleb250822009-02-17 14:49:01 -08001238 * Switch MMC interface voltage ... only relevant for MMC1.
1239 *
1240 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1241 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1242 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001243 */
Denis Karpov70a33412009-09-22 16:44:59 -07001244static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001245{
1246 u32 reg_val = 0;
1247 int ret;
1248
1249 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301250 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301251 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301252 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001253
1254 /* Turn the power off */
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001255 ret = omap_hsmmc_set_power(host->dev, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001256
1257 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001258 if (!ret)
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001259 ret = omap_hsmmc_set_power(host->dev, 1, vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301260 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301261 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301262 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001263
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001264 if (ret != 0)
1265 goto err;
1266
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001267 OMAP_HSMMC_WRITE(host->base, HCTL,
1268 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1269 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001270
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001271 /*
1272 * If a MMC dual voltage card is detected, the set_ios fn calls
1273 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001274 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001275 *
David Brownelleb250822009-02-17 14:49:01 -08001276 * Cope with a bit of slop in the range ... per data sheets:
1277 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1278 * but recommended values are 1.71V to 1.89V
1279 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1280 * but recommended values are 2.7V to 3.3V
1281 *
1282 * Board setup code shouldn't permit anything very out-of-range.
1283 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1284 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001285 */
David Brownelleb250822009-02-17 14:49:01 -08001286 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001287 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001288 else
1289 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001290
1291 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001292 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001293
1294 return 0;
1295err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301296 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001297 return ret;
1298}
1299
Adrian Hunterb62f6222009-09-22 16:45:01 -07001300/* Protect the card while the cover is open */
1301static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1302{
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001303 if (!host->get_cover_state)
Adrian Hunterb62f6222009-09-22 16:45:01 -07001304 return;
1305
1306 host->reqs_blocked = 0;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001307 if (host->get_cover_state(host->dev)) {
Adrian Hunterb62f6222009-09-22 16:45:01 -07001308 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301309 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001310 "card is now accessible\n",
1311 mmc_hostname(host->mmc));
1312 host->protect_card = 0;
1313 }
1314 } else {
1315 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301316 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001317 "card is now inaccessible\n",
1318 mmc_hostname(host->mmc));
1319 host->protect_card = 1;
1320 }
1321 }
1322}
1323
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001324/*
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001325 * irq handler when (cell-phone) cover is mounted/removed
1326 */
1327static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id)
1328{
1329 struct omap_hsmmc_host *host = dev_id;
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001330
1331 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
1332
Andreas Fenkart11227d12015-03-03 13:28:17 +01001333 omap_hsmmc_protect_card(host);
1334 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001335 return IRQ_HANDLED;
1336}
1337
Russell Kingc5c98922012-04-13 12:14:39 +01001338static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001339{
Russell Kingc5c98922012-04-13 12:14:39 +01001340 struct omap_hsmmc_host *host = param;
1341 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001342 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001343 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001344
Russell Kingc5c98922012-04-13 12:14:39 +01001345 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001346 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001347 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001348 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001349 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001350
Adrian Hunter770d7432011-05-06 12:14:11 +03001351 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001352 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001353 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001354 dma_unmap_sg(chan->device->dev,
1355 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001356 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001357
1358 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001359 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001360 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001361
1362 /* If DMA has finished after TC, complete the request */
1363 if (!req_in_progress) {
1364 struct mmc_request *mrq = host->mrq;
1365
1366 host->mrq = NULL;
1367 mmc_request_done(host->mmc, mrq);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001368 pm_runtime_mark_last_busy(host->dev);
1369 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001370 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001371}
1372
Per Forlin9782aff2011-07-01 18:55:23 +02001373static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1374 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001375 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001376 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001377{
1378 int dma_len;
1379
1380 if (!next && data->host_cookie &&
1381 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301382 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001383 " host->next_data.cookie %d\n",
1384 __func__, data->host_cookie, host->next_data.cookie);
1385 data->host_cookie = 0;
1386 }
1387
1388 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001389 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001390 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001391 omap_hsmmc_get_dma_dir(host, data));
1392
1393 } else {
1394 dma_len = host->next_data.dma_len;
1395 host->next_data.dma_len = 0;
1396 }
1397
1398
1399 if (dma_len == 0)
1400 return -EINVAL;
1401
1402 if (next) {
1403 next->dma_len = dma_len;
1404 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1405 } else
1406 host->dma_len = dma_len;
1407
1408 return 0;
1409}
1410
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001411/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001412 * Routine to configure and start DMA for the MMC card
1413 */
Balaji T K9d025332014-01-21 19:54:42 +05301414static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001415 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001416{
Russell King26b88522012-04-13 12:27:37 +01001417 struct dma_slave_config cfg;
1418 struct dma_async_tx_descriptor *tx;
1419 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001420 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001421 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001422
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001423 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001424 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001425 struct scatterlist *sgl;
1426
1427 sgl = data->sg + i;
1428 if (sgl->length % data->blksz)
1429 return -EINVAL;
1430 }
1431 if ((data->blksz % 4) != 0)
1432 /* REVISIT: The MMC buffer increments only when MSB is written.
1433 * Return error for blksz which is non multiple of four.
1434 */
1435 return -EINVAL;
1436
Adrian Hunterb4175772010-05-26 14:42:06 -07001437 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001438
Russell Kingc5c98922012-04-13 12:14:39 +01001439 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001440
Russell King26b88522012-04-13 12:27:37 +01001441 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1442 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1443 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1444 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1445 cfg.src_maxburst = data->blksz / 4;
1446 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001447
Russell King26b88522012-04-13 12:27:37 +01001448 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001449 if (ret)
1450 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001451
Russell King26b88522012-04-13 12:27:37 +01001452 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1453 if (ret)
1454 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001455
Russell King26b88522012-04-13 12:27:37 +01001456 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1457 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1458 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1459 if (!tx) {
1460 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1461 /* FIXME: cleanup */
1462 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001463 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001464
Russell King26b88522012-04-13 12:27:37 +01001465 tx->callback = omap_hsmmc_dma_callback;
1466 tx->callback_param = host;
1467
1468 /* Does not fail */
1469 dmaengine_submit(tx);
1470
1471 host->dma_ch = 1;
1472
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001473 return 0;
1474}
1475
Denis Karpov70a33412009-09-22 16:44:59 -07001476static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001477 unsigned int timeout_ns,
1478 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001479{
1480 unsigned int timeout, cycle_ns;
1481 uint32_t reg, clkd, dto = 0;
1482
1483 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1484 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1485 if (clkd == 0)
1486 clkd = 1;
1487
Balaji T K6e3076c2014-01-21 19:54:42 +05301488 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001489 timeout = timeout_ns / cycle_ns;
1490 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001491 if (timeout) {
1492 while ((timeout & 0x80000000) == 0) {
1493 dto += 1;
1494 timeout <<= 1;
1495 }
1496 dto = 31 - dto;
1497 timeout <<= 1;
1498 if (timeout && dto)
1499 dto += 1;
1500 if (dto >= 13)
1501 dto -= 13;
1502 else
1503 dto = 0;
1504 if (dto > 14)
1505 dto = 14;
1506 }
1507
1508 reg &= ~DTO_MASK;
1509 reg |= dto << DTO_SHIFT;
1510 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1511}
1512
Balaji T K9d025332014-01-21 19:54:42 +05301513static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1514{
1515 struct mmc_request *req = host->mrq;
1516 struct dma_chan *chan;
1517
1518 if (!req->data)
1519 return;
1520 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1521 | (req->data->blocks << 16));
1522 set_data_timeout(host, req->data->timeout_ns,
1523 req->data->timeout_clks);
1524 chan = omap_hsmmc_get_dma_chan(host, req->data);
1525 dma_async_issue_pending(chan);
1526}
1527
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001528/*
1529 * Configure block length for MMC/SD cards and initiate the transfer.
1530 */
1531static int
Denis Karpov70a33412009-09-22 16:44:59 -07001532omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001533{
1534 int ret;
1535 host->data = req->data;
1536
1537 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001538 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001539 /*
1540 * Set an arbitrary 100ms data timeout for commands with
1541 * busy signal.
1542 */
1543 if (req->cmd->flags & MMC_RSP_BUSY)
1544 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001545 return 0;
1546 }
1547
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001548 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301549 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001550 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301551 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001552 return ret;
1553 }
1554 }
1555 return 0;
1556}
1557
Per Forlin9782aff2011-07-01 18:55:23 +02001558static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1559 int err)
1560{
1561 struct omap_hsmmc_host *host = mmc_priv(mmc);
1562 struct mmc_data *data = mrq->data;
1563
Russell King26b88522012-04-13 12:27:37 +01001564 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001565 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001566
Russell King26b88522012-04-13 12:27:37 +01001567 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1568 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001569 data->host_cookie = 0;
1570 }
1571}
1572
1573static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1574 bool is_first_req)
1575{
1576 struct omap_hsmmc_host *host = mmc_priv(mmc);
1577
1578 if (mrq->data->host_cookie) {
1579 mrq->data->host_cookie = 0;
1580 return ;
1581 }
1582
Russell Kingc5c98922012-04-13 12:14:39 +01001583 if (host->use_dma) {
1584 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001585
Per Forlin9782aff2011-07-01 18:55:23 +02001586 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001587 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001588 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001589 }
Per Forlin9782aff2011-07-01 18:55:23 +02001590}
1591
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001592/*
1593 * Request function. for read/write operation
1594 */
Denis Karpov70a33412009-09-22 16:44:59 -07001595static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001596{
Denis Karpov70a33412009-09-22 16:44:59 -07001597 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001598 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001599
Adrian Hunterb4175772010-05-26 14:42:06 -07001600 BUG_ON(host->req_in_progress);
1601 BUG_ON(host->dma_ch != -1);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001602 pm_runtime_get_sync(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001603 if (host->protect_card) {
1604 if (host->reqs_blocked < 3) {
1605 /*
1606 * Ensure the controller is left in a consistent
1607 * state by resetting the command and data state
1608 * machines.
1609 */
1610 omap_hsmmc_reset_controller_fsm(host, SRD);
1611 omap_hsmmc_reset_controller_fsm(host, SRC);
1612 host->reqs_blocked += 1;
1613 }
1614 req->cmd->error = -EBADF;
1615 if (req->data)
1616 req->data->error = -EBADF;
1617 req->cmd->retries = 0;
1618 mmc_request_done(mmc, req);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001619 pm_runtime_mark_last_busy(host->dev);
1620 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001621 return;
1622 } else if (host->reqs_blocked)
1623 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001624 WARN_ON(host->mrq != NULL);
1625 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301626 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001627 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001628 if (err) {
1629 req->cmd->error = err;
1630 if (req->data)
1631 req->data->error = err;
1632 host->mrq = NULL;
1633 mmc_request_done(mmc, req);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001634 pm_runtime_mark_last_busy(host->dev);
1635 pm_runtime_put_autosuspend(host->dev);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001636 return;
1637 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301638 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301639 omap_hsmmc_start_command(host, req->sbc, NULL);
1640 return;
1641 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001642
Balaji T K9d025332014-01-21 19:54:42 +05301643 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001644 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001645}
1646
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001647/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001648static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001649{
Denis Karpov70a33412009-09-22 16:44:59 -07001650 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001651 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001652
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301653 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001654
Adrian Huntera3621462009-09-22 16:44:42 -07001655 if (ios->power_mode != host->power_mode) {
1656 switch (ios->power_mode) {
1657 case MMC_POWER_OFF:
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001658 omap_hsmmc_set_power(host->dev, 0, 0);
Adrian Huntera3621462009-09-22 16:44:42 -07001659 break;
1660 case MMC_POWER_UP:
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001661 omap_hsmmc_set_power(host->dev, 1, ios->vdd);
Adrian Huntera3621462009-09-22 16:44:42 -07001662 break;
1663 case MMC_POWER_ON:
1664 do_send_init_stream = 1;
1665 break;
1666 }
1667 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001668 }
1669
Denis Karpovdd498ef2009-09-22 16:44:49 -07001670 /* FIXME: set registers based only on changes to ios */
1671
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001672 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001673
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301674 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001675 /* Only MMC1 can interface at 3V without some flavor
1676 * of external transceiver; but they all handle 1.8V.
1677 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001678 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301679 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001680 /*
1681 * The mmc_select_voltage fn of the core does
1682 * not seem to set the power_mode to
1683 * MMC_POWER_UP upon recalculating the voltage.
1684 * vdd 1.8v.
1685 */
Denis Karpov70a33412009-09-22 16:44:59 -07001686 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1687 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001688 "Switch operation failed\n");
1689 }
1690 }
1691
Andy Shevchenko5934df22011-05-06 12:14:06 +03001692 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001693
Adrian Huntera3621462009-09-22 16:44:42 -07001694 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001695 send_init_stream(host);
1696
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001697 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001698
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301699 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001700}
1701
1702static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1703{
Denis Karpov70a33412009-09-22 16:44:59 -07001704 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001705
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001706 if (!host->card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001707 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001708 return host->card_detect(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001709}
1710
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001711static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1712{
1713 struct omap_hsmmc_host *host = mmc_priv(mmc);
1714
Andreas Fenkart326119c2014-11-08 15:33:14 +01001715 if (mmc_pdata(host)->init_card)
1716 mmc_pdata(host)->init_card(card);
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001717}
1718
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001719static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1720{
1721 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001722 u32 irq_mask, con;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001723 unsigned long flags;
1724
1725 spin_lock_irqsave(&host->irq_lock, flags);
1726
Balaji T K5a52b082014-05-29 10:28:02 +02001727 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001728 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1729 if (enable) {
1730 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1731 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001732 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001733 } else {
1734 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1735 irq_mask &= ~CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001736 con &= ~(CTPL | CLKEXTFREE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001737 }
Balaji T K5a52b082014-05-29 10:28:02 +02001738 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001739 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1740
1741 /*
1742 * if enable, piggy back detection on current request
1743 * but always disable immediately
1744 */
1745 if (!host->req_in_progress || !enable)
1746 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1747
1748 /* flush posted write */
1749 OMAP_HSMMC_READ(host->base, IE);
1750
1751 spin_unlock_irqrestore(&host->irq_lock, flags);
1752}
1753
1754static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1755{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001756 int ret;
1757
1758 /*
1759 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1760 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1761 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1762 * with functional clock disabled.
1763 */
1764 if (!host->dev->of_node || !host->wake_irq)
1765 return -ENODEV;
1766
Tony Lindgren5b83b222015-05-21 15:51:52 -07001767 ret = dev_pm_set_dedicated_wake_irq(host->dev, host->wake_irq);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001768 if (ret) {
1769 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1770 goto err;
1771 }
1772
1773 /*
1774 * Some omaps don't have wake-up path from deeper idle states
1775 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1776 */
1777 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001778 struct pinctrl *p = devm_pinctrl_get(host->dev);
1779 if (!p) {
1780 ret = -ENODEV;
1781 goto err_free_irq;
1782 }
1783 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1784 dev_info(host->dev, "missing default pinctrl state\n");
1785 devm_pinctrl_put(p);
1786 ret = -EINVAL;
1787 goto err_free_irq;
1788 }
1789
1790 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1791 dev_info(host->dev, "missing idle pinctrl state\n");
1792 devm_pinctrl_put(p);
1793 ret = -EINVAL;
1794 goto err_free_irq;
1795 }
1796 devm_pinctrl_put(p);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001797 }
1798
Balaji T K5a52b082014-05-29 10:28:02 +02001799 OMAP_HSMMC_WRITE(host->base, HCTL,
1800 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001801 return 0;
1802
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001803err_free_irq:
Tony Lindgren5b83b222015-05-21 15:51:52 -07001804 dev_pm_clear_wake_irq(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001805err:
1806 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1807 host->wake_irq = 0;
1808 return ret;
1809}
1810
Denis Karpov70a33412009-09-22 16:44:59 -07001811static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001812{
1813 u32 hctl, capa, value;
1814
1815 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301816 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001817 hctl = SDVS30;
1818 capa = VS30 | VS18;
1819 } else {
1820 hctl = SDVS18;
1821 capa = VS18;
1822 }
1823
1824 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1825 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1826
1827 value = OMAP_HSMMC_READ(host->base, CAPA);
1828 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1829
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001830 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001831 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001832}
1833
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07001834static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1835 unsigned int direction, int blk_size)
1836{
1837 /* This controller can't do multiblock reads due to hw bugs */
1838 if (direction == MMC_DATA_READ)
1839 return 1;
1840
1841 return blk_size;
1842}
1843
1844static struct mmc_host_ops omap_hsmmc_ops = {
Per Forlin9782aff2011-07-01 18:55:23 +02001845 .post_req = omap_hsmmc_post_req,
1846 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001847 .request = omap_hsmmc_request,
1848 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001849 .get_cd = omap_hsmmc_get_cd,
Andreas Fenkarta49d8352015-03-03 13:28:14 +01001850 .get_ro = mmc_gpio_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001851 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001852 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001853};
1854
Denis Karpovd900f712009-09-22 16:44:38 -07001855#ifdef CONFIG_DEBUG_FS
1856
Denis Karpov70a33412009-09-22 16:44:59 -07001857static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001858{
1859 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001860 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001861
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001862 seq_printf(s, "mmc%d:\n", mmc->index);
1863 seq_printf(s, "sdio irq mode\t%s\n",
1864 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1865
1866 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1867 seq_printf(s, "sdio irq \t%s\n",
1868 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1869 : "disabled");
1870 }
1871 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001872
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301873 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001874 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001875 seq_printf(s, "CON:\t\t0x%08x\n",
1876 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001877 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1878 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001879 seq_printf(s, "HCTL:\t\t0x%08x\n",
1880 OMAP_HSMMC_READ(host->base, HCTL));
1881 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1882 OMAP_HSMMC_READ(host->base, SYSCTL));
1883 seq_printf(s, "IE:\t\t0x%08x\n",
1884 OMAP_HSMMC_READ(host->base, IE));
1885 seq_printf(s, "ISE:\t\t0x%08x\n",
1886 OMAP_HSMMC_READ(host->base, ISE));
1887 seq_printf(s, "CAPA:\t\t0x%08x\n",
1888 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001889
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301890 pm_runtime_mark_last_busy(host->dev);
1891 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001892
Denis Karpovd900f712009-09-22 16:44:38 -07001893 return 0;
1894}
1895
Denis Karpov70a33412009-09-22 16:44:59 -07001896static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001897{
Denis Karpov70a33412009-09-22 16:44:59 -07001898 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001899}
1900
1901static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001902 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001903 .read = seq_read,
1904 .llseek = seq_lseek,
1905 .release = single_release,
1906};
1907
Denis Karpov70a33412009-09-22 16:44:59 -07001908static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001909{
1910 if (mmc->debugfs_root)
1911 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1912 mmc, &mmc_regs_fops);
1913}
1914
1915#else
1916
Denis Karpov70a33412009-09-22 16:44:59 -07001917static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001918{
1919}
1920
1921#endif
1922
Rajendra Nayak46856a62012-03-12 20:32:37 +05301923#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001924static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1925 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1926 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1927};
1928
1929static const struct omap_mmc_of_data omap4_mmc_of_data = {
1930 .reg_offset = 0x100,
1931};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001932static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1933 .reg_offset = 0x100,
1934 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1935};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301936
1937static const struct of_device_id omap_mmc_of_match[] = {
1938 {
1939 .compatible = "ti,omap2-hsmmc",
1940 },
1941 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001942 .compatible = "ti,omap3-pre-es3-hsmmc",
1943 .data = &omap3_pre_es3_mmc_of_data,
1944 },
1945 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301946 .compatible = "ti,omap3-hsmmc",
1947 },
1948 {
1949 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001950 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301951 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001952 {
1953 .compatible = "ti,am33xx-hsmmc",
1954 .data = &am33xx_mmc_of_data,
1955 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301956 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001957};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301958MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1959
Andreas Fenkart551434382014-11-08 15:33:09 +01001960static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
Rajendra Nayak46856a62012-03-12 20:32:37 +05301961{
Andreas Fenkart551434382014-11-08 15:33:09 +01001962 struct omap_hsmmc_platform_data *pdata;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301963 struct device_node *np = dev->of_node;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301964
1965 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1966 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301967 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301968
1969 if (of_find_property(np, "ti,dual-volt", NULL))
1970 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1971
Andreas Fenkartb7a56462015-03-20 15:53:54 +01001972 pdata->gpio_cd = -EINVAL;
1973 pdata->gpio_cod = -EINVAL;
NeilBrownfdb9de12015-01-13 08:23:18 +13001974 pdata->gpio_wp = -EINVAL;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301975
1976 if (of_find_property(np, "ti,non-removable", NULL)) {
Andreas Fenkart326119c2014-11-08 15:33:14 +01001977 pdata->nonremovable = true;
1978 pdata->no_regulator_off_init = true;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301979 }
Rajendra Nayak46856a62012-03-12 20:32:37 +05301980
1981 if (of_find_property(np, "ti,needs-special-reset", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001982 pdata->features |= HSMMC_HAS_UPDATED_RESET;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301983
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301984 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001985 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301986
Rajendra Nayak46856a62012-03-12 20:32:37 +05301987 return pdata;
1988}
1989#else
Andreas Fenkart551434382014-11-08 15:33:09 +01001990static inline struct omap_hsmmc_platform_data
Rajendra Nayak46856a62012-03-12 20:32:37 +05301991 *of_get_hsmmc_pdata(struct device *dev)
1992{
Balaji T K19df45b2014-02-28 19:08:18 +05301993 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301994}
1995#endif
1996
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001997static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001998{
Andreas Fenkart551434382014-11-08 15:33:09 +01001999 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002000 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07002001 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002002 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002003 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302004 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01002005 dma_cap_mask_t mask;
2006 unsigned tx_req, rx_req;
Nishanth Menon59445b12014-02-13 23:45:48 -06002007 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05302008 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302009
2010 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
2011 if (match) {
2012 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01002013
2014 if (IS_ERR(pdata))
2015 return PTR_ERR(pdata);
2016
Rajendra Nayak46856a62012-03-12 20:32:37 +05302017 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06002018 data = match->data;
2019 pdata->reg_offset = data->reg_offset;
2020 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302021 }
2022 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002023
2024 if (pdata == NULL) {
2025 dev_err(&pdev->dev, "Platform Data is missing\n");
2026 return -ENXIO;
2027 }
2028
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002029 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2030 irq = platform_get_irq(pdev, 0);
2031 if (res == NULL || irq < 0)
2032 return -ENXIO;
2033
Balaji T K77fae212014-05-09 22:16:51 +05302034 base = devm_ioremap_resource(&pdev->dev, res);
2035 if (IS_ERR(base))
2036 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002037
Denis Karpov70a33412009-09-22 16:44:59 -07002038 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002039 if (!mmc) {
2040 ret = -ENOMEM;
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002041 goto err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002042 }
2043
NeilBrownfdb9de12015-01-13 08:23:18 +13002044 ret = mmc_of_parse(mmc);
2045 if (ret)
2046 goto err1;
2047
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002048 host = mmc_priv(mmc);
2049 host->mmc = mmc;
2050 host->pdata = pdata;
2051 host->dev = &pdev->dev;
2052 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002053 host->dma_ch = -1;
2054 host->irq = irq;
Balaji T Kfc307df2012-04-02 12:26:47 +05302055 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05302056 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08002057 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002058 host->next_data.cookie = 1;
Tony Lindgrenbb2726b2015-10-07 06:22:24 -07002059 host->pbias_enabled = 0;
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +05302060 host->vqmmc_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002061
NeilBrown41afa3142015-01-13 08:23:18 +13002062 ret = omap_hsmmc_gpio_init(mmc, host, pdata);
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002063 if (ret)
2064 goto err_gpio;
2065
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002066 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002067
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002068 if (pdev->dev.of_node)
2069 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2070
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302071 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002072
Daniel Mackd418ed82012-02-19 13:20:33 +01002073 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2074
2075 if (pdata->max_freq > 0)
2076 mmc->f_max = pdata->max_freq;
NeilBrownfdb9de12015-01-13 08:23:18 +13002077 else if (mmc->f_max == 0)
Daniel Mackd418ed82012-02-19 13:20:33 +01002078 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002079
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002080 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002081
Balaji T K96181952014-05-09 22:16:48 +05302082 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002083 if (IS_ERR(host->fclk)) {
2084 ret = PTR_ERR(host->fclk);
2085 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002086 goto err1;
2087 }
2088
Paul Walmsley9b682562011-10-06 14:50:35 -06002089 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2090 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 -07002091 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
Paul Walmsley9b682562011-10-06 14:50:35 -06002092 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002093
Tony Lindgren5b83b222015-05-21 15:51:52 -07002094 device_init_wakeup(&pdev->dev, true);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302095 pm_runtime_enable(host->dev);
2096 pm_runtime_get_sync(host->dev);
2097 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2098 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002099
Balaji T K92a3aeb2012-02-24 21:14:34 +05302100 omap_hsmmc_context_save(host);
2101
Balaji T K96181952014-05-09 22:16:48 +05302102 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302103 /*
2104 * MMC can still work without debounce clock.
2105 */
2106 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302107 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302108 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302109 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302110 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002111 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002112
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002113 /* Since we do only SG emulation, we can have as many segs
2114 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002115 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002116
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002117 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2118 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2119 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2120 mmc->max_seg_size = mmc->max_req_size;
2121
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002122 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002123 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002124
Andreas Fenkart326119c2014-11-08 15:33:14 +01002125 mmc->caps |= mmc_pdata(host)->caps;
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002126 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002127 mmc->caps |= MMC_CAP_4_BIT_DATA;
2128
Andreas Fenkart326119c2014-11-08 15:33:14 +01002129 if (mmc_pdata(host)->nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002130 mmc->caps |= MMC_CAP_NONREMOVABLE;
2131
NeilBrownfdb9de12015-01-13 08:23:18 +13002132 mmc->pm_caps |= mmc_pdata(host)->pm_caps;
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002133
Denis Karpov70a33412009-09-22 16:44:59 -07002134 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002135
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302136 if (!pdev->dev.of_node) {
2137 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2138 if (!res) {
2139 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2140 ret = -ENXIO;
2141 goto err_irq;
2142 }
2143 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002144
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302145 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2146 if (!res) {
2147 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2148 ret = -ENXIO;
2149 goto err_irq;
2150 }
2151 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002152 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002153
Russell King26b88522012-04-13 12:27:37 +01002154 dma_cap_zero(mask);
2155 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002156
Matt Porterd272fbf2013-05-10 17:42:34 +05302157 host->rx_chan =
2158 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2159 &rx_req, &pdev->dev, "rx");
2160
Russell King26b88522012-04-13 12:27:37 +01002161 if (!host->rx_chan) {
2162 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002163 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002164 goto err_irq;
2165 }
2166
Matt Porterd272fbf2013-05-10 17:42:34 +05302167 host->tx_chan =
2168 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2169 &tx_req, &pdev->dev, "tx");
2170
Russell King26b88522012-04-13 12:27:37 +01002171 if (!host->tx_chan) {
2172 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002173 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002174 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002175 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002176
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002177 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302178 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002179 mmc_hostname(mmc), host);
2180 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302181 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002182 goto err_irq;
2183 }
2184
Kishon Vijay Abraham I987e05c2015-08-27 14:44:07 +05302185 ret = omap_hsmmc_reg_get(host);
2186 if (ret)
2187 goto err_irq;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002188
Andreas Fenkart326119c2014-11-08 15:33:14 +01002189 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002190
Adrian Hunterb4175772010-05-26 14:42:06 -07002191 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002192
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002193 /*
2194 * For now, only support SDIO interrupt if we have a separate
2195 * wake-up interrupt configured from device tree. This is because
2196 * the wake-up interrupt is needed for idle state and some
2197 * platforms need special quirks. And we don't want to add new
2198 * legacy mux platform init code callbacks any longer as we
2199 * are moving to DT based booting anyways.
2200 */
2201 ret = omap_hsmmc_configure_wake_irq(host);
2202 if (!ret)
2203 mmc->caps |= MMC_CAP_SDIO_IRQ;
2204
Adrian Hunterb62f6222009-09-22 16:45:01 -07002205 omap_hsmmc_protect_card(host);
2206
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002207 mmc_add_host(mmc);
2208
Andreas Fenkart326119c2014-11-08 15:33:14 +01002209 if (mmc_pdata(host)->name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002210 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2211 if (ret < 0)
2212 goto err_slot_name;
2213 }
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002214 if (host->get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002215 ret = device_create_file(&mmc->class_dev,
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002216 &dev_attr_cover_switch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002217 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002218 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002219 }
2220
Denis Karpov70a33412009-09-22 16:44:59 -07002221 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302222 pm_runtime_mark_last_busy(host->dev);
2223 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002224
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002225 return 0;
2226
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002227err_slot_name:
2228 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002229err_irq:
Tony Lindgren5b83b222015-05-21 15:51:52 -07002230 device_init_wakeup(&pdev->dev, false);
Russell Kingc5c98922012-04-13 12:14:39 +01002231 if (host->tx_chan)
2232 dma_release_channel(host->tx_chan);
2233 if (host->rx_chan)
2234 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302235 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002236 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302237 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302238 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002239err1:
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002240err_gpio:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002241 mmc_free_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002242err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002243 return ret;
2244}
2245
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002246static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002247{
Denis Karpov70a33412009-09-22 16:44:59 -07002248 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002249
Felipe Balbi927ce942012-03-14 11:18:27 +02002250 pm_runtime_get_sync(host->dev);
2251 mmc_remove_host(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002252
Peter Ujfalusidc285622015-11-03 13:37:31 +02002253 dma_release_channel(host->tx_chan);
2254 dma_release_channel(host->rx_chan);
Russell Kingc5c98922012-04-13 12:14:39 +01002255
Felipe Balbi927ce942012-03-14 11:18:27 +02002256 pm_runtime_put_sync(host->dev);
2257 pm_runtime_disable(host->dev);
Tony Lindgren5b83b222015-05-21 15:51:52 -07002258 device_init_wakeup(&pdev->dev, false);
Balaji T K96181952014-05-09 22:16:48 +05302259 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302260 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002261
Balaji T K9d1f0282012-10-15 21:35:07 +05302262 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002263
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002264 return 0;
2265}
2266
Russ Dill3d3bbfb2015-02-27 13:24:34 +02002267#ifdef CONFIG_PM_SLEEP
Kevin Hilmana791daa2010-05-26 14:42:07 -07002268static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002269{
Felipe Balbi927ce942012-03-14 11:18:27 +02002270 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2271
2272 if (!host)
2273 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002274
Felipe Balbi927ce942012-03-14 11:18:27 +02002275 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002276
2277 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002278 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2279 OMAP_HSMMC_WRITE(host->base, IE, 0);
2280 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002281 OMAP_HSMMC_WRITE(host->base, HCTL,
2282 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2283 }
2284
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302285 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302286 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002287
Eliad Peller31f9d462011-11-22 16:02:17 +02002288 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002289 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002290}
2291
2292/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002293static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002294{
Felipe Balbi927ce942012-03-14 11:18:27 +02002295 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2296
2297 if (!host)
2298 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002299
Felipe Balbi927ce942012-03-14 11:18:27 +02002300 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002301
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302302 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302303 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002304
Felipe Balbi927ce942012-03-14 11:18:27 +02002305 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2306 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002307
Felipe Balbi927ce942012-03-14 11:18:27 +02002308 omap_hsmmc_protect_card(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002309 pm_runtime_mark_last_busy(host->dev);
2310 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002311 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002312}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002313#endif
2314
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302315static int omap_hsmmc_runtime_suspend(struct device *dev)
2316{
2317 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002318 unsigned long flags;
Andreas Fenkartf9459012014-05-29 10:28:03 +02002319 int ret = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302320
2321 host = platform_get_drvdata(to_platform_device(dev));
2322 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002323 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302324
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002325 spin_lock_irqsave(&host->irq_lock, flags);
2326 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2327 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2328 /* disable sdio irq handling to prevent race */
2329 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2330 OMAP_HSMMC_WRITE(host->base, IE, 0);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002331
2332 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2333 /*
2334 * dat1 line low, pending sdio irq
2335 * race condition: possible irq handler running on
2336 * multi-core, abort
2337 */
2338 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2339 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2340 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2341 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2342 pm_runtime_mark_last_busy(dev);
2343 ret = -EBUSY;
2344 goto abort;
2345 }
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002346
Andreas Fenkart97978a42014-05-29 10:28:04 +02002347 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002348 } else {
2349 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002350 }
Andreas Fenkart97978a42014-05-29 10:28:04 +02002351
Andreas Fenkartf9459012014-05-29 10:28:03 +02002352abort:
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002353 spin_unlock_irqrestore(&host->irq_lock, flags);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002354 return ret;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302355}
2356
2357static int omap_hsmmc_runtime_resume(struct device *dev)
2358{
2359 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002360 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302361
2362 host = platform_get_drvdata(to_platform_device(dev));
2363 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002364 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302365
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002366 spin_lock_irqsave(&host->irq_lock, flags);
2367 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2368 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002369
Andreas Fenkart97978a42014-05-29 10:28:04 +02002370 pinctrl_pm_select_default_state(host->dev);
2371
2372 /* irq lost, if pinmux incorrect */
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002373 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2374 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2375 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002376 } else {
2377 pinctrl_pm_select_default_state(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002378 }
2379 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302380 return 0;
2381}
2382
Kevin Hilmana791daa2010-05-26 14:42:07 -07002383static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Russ Dill3d3bbfb2015-02-27 13:24:34 +02002384 SET_SYSTEM_SLEEP_PM_OPS(omap_hsmmc_suspend, omap_hsmmc_resume)
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302385 .runtime_suspend = omap_hsmmc_runtime_suspend,
2386 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002387};
2388
2389static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002390 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002391 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002392 .driver = {
2393 .name = DRIVER_NAME,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002394 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302395 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002396 },
2397};
2398
Felipe Balbib7964502012-03-14 11:18:32 +02002399module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002400MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2401MODULE_LICENSE("GPL");
2402MODULE_ALIAS("platform:" DRIVER_NAME);
2403MODULE_AUTHOR("Texas Instruments Inc");