blob: 58683b3c55b6409b0dbdfcb00cba03954168e75b [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 K03b5d9242012-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;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100185 void __iomem *base;
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530186 int vqmmc_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100187 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700188 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100189 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200190 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100191 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700192 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100193 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530194 u32 con;
195 u32 hctl;
196 u32 sysctl;
197 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100198 int irq;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200199 int wake_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100200 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100201 struct dma_chan *tx_chan;
202 struct dma_chan *rx_chan;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200203 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700204 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700205 int protect_card;
206 int reqs_blocked;
Adrian Hunterb4175772010-05-26 14:42:06 -0700207 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530208 unsigned long clk_rate;
Balaji T Ka2e77152014-01-21 19:54:42 +0530209 unsigned int flags;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200210#define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
211#define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
Per Forlin9782aff2011-07-01 18:55:23 +0200212 struct omap_hsmmc_next next_data;
Andreas Fenkart551434382014-11-08 15:33:09 +0100213 struct omap_hsmmc_platform_data *pdata;
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100214
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100215 /* return MMC cover switch state, can be NULL if not supported.
216 *
217 * possible return values:
218 * 0 - closed
219 * 1 - open
220 */
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100221 int (*get_cover_state)(struct device *dev);
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100222
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100223 int (*card_detect)(struct device *dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100224};
225
Nishanth Menon59445b12014-02-13 23:45:48 -0600226struct omap_mmc_of_data {
227 u32 reg_offset;
228 u8 controller_flags;
229};
230
Balaji T Kbf129e12014-01-21 19:54:42 +0530231static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
232
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100233static int omap_hsmmc_card_detect(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800234{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530235 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800236
NeilBrown41afa3142015-01-13 08:23:18 +1300237 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800238}
239
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100240static int omap_hsmmc_get_cover_state(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800241{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530242 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800243
NeilBrown41afa3142015-01-13 08:23:18 +1300244 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800245}
246
Adrian Hunterb702b102010-02-15 10:03:35 -0800247#ifdef CONFIG_REGULATOR
248
Kishon Vijay Abraham I1d17f302015-08-27 14:44:06 +0530249static int omap_hsmmc_enable_supply(struct mmc_host *mmc)
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530250{
251 int ret;
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530252 struct omap_hsmmc_host *host = mmc_priv(mmc);
Kishon Vijay Abraham I1d17f302015-08-27 14:44:06 +0530253 struct mmc_ios *ios = &mmc->ios;
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530254
255 if (mmc->supply.vmmc) {
Kishon Vijay Abraham I1d17f302015-08-27 14:44:06 +0530256 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530257 if (ret)
258 return ret;
259 }
260
261 /* Enable interface voltage rail, if needed */
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530262 if (mmc->supply.vqmmc && !host->vqmmc_enabled) {
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530263 ret = regulator_enable(mmc->supply.vqmmc);
264 if (ret) {
265 dev_err(mmc_dev(mmc), "vmmc_aux reg enable failed\n");
266 goto err_vqmmc;
267 }
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530268 host->vqmmc_enabled = 1;
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530269 }
270
271 return 0;
272
273err_vqmmc:
274 if (mmc->supply.vmmc)
275 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
276
277 return ret;
278}
279
280static int omap_hsmmc_disable_supply(struct mmc_host *mmc)
281{
282 int ret;
283 int status;
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530284 struct omap_hsmmc_host *host = mmc_priv(mmc);
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530285
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530286 if (mmc->supply.vqmmc && host->vqmmc_enabled) {
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530287 ret = regulator_disable(mmc->supply.vqmmc);
288 if (ret) {
289 dev_err(mmc_dev(mmc), "vmmc_aux reg disable failed\n");
290 return ret;
291 }
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530292 host->vqmmc_enabled = 0;
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530293 }
294
295 if (mmc->supply.vmmc) {
296 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
297 if (ret)
298 goto err_set_ocr;
299 }
300
301 return 0;
302
303err_set_ocr:
304 if (mmc->supply.vqmmc) {
305 status = regulator_enable(mmc->supply.vqmmc);
306 if (status)
307 dev_err(mmc_dev(mmc), "vmmc_aux re-enable failed\n");
308 }
309
310 return ret;
311}
312
Kishon Vijay Abraham Iec85c952015-08-27 14:44:01 +0530313static int omap_hsmmc_set_pbias(struct omap_hsmmc_host *host, bool power_on,
314 int vdd)
315{
316 int ret;
317
318 if (!host->pbias)
319 return 0;
320
321 if (power_on) {
322 if (vdd <= VDD_165_195)
323 ret = regulator_set_voltage(host->pbias, VDD_1V8,
324 VDD_1V8);
325 else
326 ret = regulator_set_voltage(host->pbias, VDD_3V0,
327 VDD_3V0);
328 if (ret < 0) {
329 dev_err(host->dev, "pbias set voltage fail\n");
330 return ret;
331 }
332
Kishon Vijay Abraham Ic55d7a02015-08-27 14:44:05 +0530333 if (!regulator_is_enabled(host->pbias)) {
Kishon Vijay Abraham Iec85c952015-08-27 14:44:01 +0530334 ret = regulator_enable(host->pbias);
335 if (ret) {
336 dev_err(host->dev, "pbias reg enable fail\n");
337 return ret;
338 }
Kishon Vijay Abraham Iec85c952015-08-27 14:44:01 +0530339 }
340 } else {
Kishon Vijay Abraham Ic55d7a02015-08-27 14:44:05 +0530341 if (regulator_is_enabled(host->pbias)) {
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 }
Kishon Vijay Abraham Iec85c952015-08-27 14:44:01 +0530347 }
348 }
349
350 return 0;
351}
352
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100353static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800354{
355 struct omap_hsmmc_host *host =
356 platform_get_drvdata(to_platform_device(dev));
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530357 struct mmc_host *mmc = host->mmc;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800358 int ret = 0;
359
Andreas Fenkartf7f0f032015-07-07 20:38:43 +0200360 if (mmc_pdata(host)->set_power)
361 return mmc_pdata(host)->set_power(dev, power_on, vdd);
362
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800363 /*
364 * If we don't see a Vcc regulator, assume it's a fixed
365 * voltage always-on regulator.
366 */
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530367 if (!mmc->supply.vmmc)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800368 return 0;
369
Andreas Fenkart326119c2014-11-08 15:33:14 +0100370 if (mmc_pdata(host)->before_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100371 mmc_pdata(host)->before_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800372
Kishon Vijay Abraham Iec85c952015-08-27 14:44:01 +0530373 ret = omap_hsmmc_set_pbias(host, false, 0);
374 if (ret)
375 return ret;
Balaji T Ke99448f2014-02-19 20:26:40 +0530376
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800377 /*
378 * Assume Vcc regulator is used only to power the card ... OMAP
379 * VDDS is used to power the pins, optionally with a transceiver to
380 * support cards using voltages other than VDDS (1.8V nominal). When a
381 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
382 *
383 * In some cases this regulator won't support enable/disable;
384 * e.g. it's a fixed rail for a WLAN chip.
385 *
386 * In other cases vcc_aux switches interface power. Example, for
387 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
388 * chips/cards need an interface voltage rail too.
389 */
390 if (power_on) {
Kishon Vijay Abraham I1d17f302015-08-27 14:44:06 +0530391 ret = omap_hsmmc_enable_supply(mmc);
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530392 if (ret)
393 return ret;
Kishon Vijay Abraham I97fe7e52015-08-27 14:44:02 +0530394
395 ret = omap_hsmmc_set_pbias(host, true, vdd);
396 if (ret)
397 goto err_set_voltage;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800398 } else {
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530399 ret = omap_hsmmc_disable_supply(mmc);
400 if (ret)
401 return ret;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800402 }
403
Andreas Fenkart326119c2014-11-08 15:33:14 +0100404 if (mmc_pdata(host)->after_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100405 mmc_pdata(host)->after_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800406
Kishon Vijay Abraham I229f3292015-08-27 14:43:59 +0530407 return 0;
408
409err_set_voltage:
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530410 omap_hsmmc_disable_supply(mmc);
Kishon Vijay Abraham I229f3292015-08-27 14:43:59 +0530411
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800412 return ret;
413}
414
Kishon Vijay Abraham Ic8518ef2015-08-27 14:44:03 +0530415static int omap_hsmmc_disable_boot_regulator(struct regulator *reg)
416{
417 int ret;
418
419 if (!reg)
420 return 0;
421
422 if (regulator_is_enabled(reg)) {
423 ret = regulator_enable(reg);
424 if (ret)
425 return ret;
426
427 ret = regulator_disable(reg);
428 if (ret)
429 return ret;
430 }
431
432 return 0;
433}
434
435static int omap_hsmmc_disable_boot_regulators(struct omap_hsmmc_host *host)
436{
437 struct mmc_host *mmc = host->mmc;
438 int ret;
439
440 /*
441 * disable regulators enabled during boot and get the usecount
442 * right so that regulators can be enabled/disabled by checking
443 * the return value of regulator_is_enabled
444 */
445 ret = omap_hsmmc_disable_boot_regulator(mmc->supply.vmmc);
446 if (ret) {
447 dev_err(host->dev, "fail to disable boot enabled vmmc reg\n");
448 return ret;
449 }
450
451 ret = omap_hsmmc_disable_boot_regulator(mmc->supply.vqmmc);
452 if (ret) {
453 dev_err(host->dev,
454 "fail to disable boot enabled vmmc_aux reg\n");
455 return ret;
456 }
457
458 ret = omap_hsmmc_disable_boot_regulator(host->pbias);
459 if (ret) {
460 dev_err(host->dev,
461 "failed to disable boot enabled pbias reg\n");
462 return ret;
463 }
464
465 return 0;
466}
467
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800468static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
469{
kishore kadiyala64be9782010-10-01 16:35:28 -0700470 int ocr_value = 0;
Kishon Vijay Abraham I7d607f92015-08-27 14:43:53 +0530471 int ret;
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530472 struct mmc_host *mmc = host->mmc;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800473
Andreas Fenkartf7f0f032015-07-07 20:38:43 +0200474 if (mmc_pdata(host)->set_power)
475 return 0;
476
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530477 mmc->supply.vmmc = devm_regulator_get_optional(host->dev, "vmmc");
478 if (IS_ERR(mmc->supply.vmmc)) {
479 ret = PTR_ERR(mmc->supply.vmmc);
Kishon Vijay Abraham I7d607f92015-08-27 14:43:53 +0530480 if (ret != -ENODEV)
481 return ret;
Kishon Vijay Abraham I7d607f92015-08-27 14:43:53 +0530482 dev_dbg(host->dev, "unable to get vmmc regulator %ld\n",
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530483 PTR_ERR(mmc->supply.vmmc));
484 mmc->supply.vmmc = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800485 } else {
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530486 ocr_value = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
Kishon Vijay Abraham Ib49069f2015-08-27 14:43:56 +0530487 if (ocr_value > 0)
Andreas Fenkart326119c2014-11-08 15:33:14 +0100488 mmc_pdata(host)->ocr_mask = ocr_value;
Balaji T K987fd492014-02-19 20:26:40 +0530489 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800490
Balaji T K987fd492014-02-19 20:26:40 +0530491 /* Allow an aux regulator */
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530492 mmc->supply.vqmmc = devm_regulator_get_optional(host->dev, "vmmc_aux");
493 if (IS_ERR(mmc->supply.vqmmc)) {
494 ret = PTR_ERR(mmc->supply.vqmmc);
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530495 if (ret != -ENODEV)
496 return ret;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530497 dev_dbg(host->dev, "unable to get vmmc_aux regulator %ld\n",
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530498 PTR_ERR(mmc->supply.vqmmc));
499 mmc->supply.vqmmc = NULL;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530500 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800501
Kishon Vijay Abraham Ic299dc32015-08-27 14:43:55 +0530502 host->pbias = devm_regulator_get_optional(host->dev, "pbias");
503 if (IS_ERR(host->pbias)) {
504 ret = PTR_ERR(host->pbias);
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530505 if (ret != -ENODEV)
506 return ret;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530507 dev_dbg(host->dev, "unable to get pbias regulator %ld\n",
Kishon Vijay Abraham Ic299dc32015-08-27 14:43:55 +0530508 PTR_ERR(host->pbias));
509 host->pbias = NULL;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530510 }
Balaji T Ke99448f2014-02-19 20:26:40 +0530511
Balaji T K987fd492014-02-19 20:26:40 +0530512 /* For eMMC do not power off when not in sleep state */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100513 if (mmc_pdata(host)->no_regulator_off_init)
Balaji T K987fd492014-02-19 20:26:40 +0530514 return 0;
Adrian Huntere840ce12011-05-06 12:14:10 +0300515
Kishon Vijay Abraham Ic8518ef2015-08-27 14:44:03 +0530516 ret = omap_hsmmc_disable_boot_regulators(host);
517 if (ret)
518 return ret;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800519
520 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800521}
522
Adrian Hunterb702b102010-02-15 10:03:35 -0800523static inline int omap_hsmmc_have_reg(void)
524{
525 return 1;
526}
527
528#else
529
Andreas Fenkartf7f0f032015-07-07 20:38:43 +0200530static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
531{
532 return 0;
533}
534
Adrian Hunterb702b102010-02-15 10:03:35 -0800535static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
536{
537 return -EINVAL;
538}
539
Adrian Hunterb702b102010-02-15 10:03:35 -0800540static inline int omap_hsmmc_have_reg(void)
541{
542 return 0;
543}
544
545#endif
546
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100547static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id);
NeilBrown41afa3142015-01-13 08:23:18 +1300548
549static int omap_hsmmc_gpio_init(struct mmc_host *mmc,
550 struct omap_hsmmc_host *host,
Andreas Fenkart1e363e32014-11-08 15:33:15 +0100551 struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800552{
553 int ret;
554
Andreas Fenkartb7a56462015-03-20 15:53:54 +0100555 if (gpio_is_valid(pdata->gpio_cod)) {
556 ret = mmc_gpio_request_cd(mmc, pdata->gpio_cod, 0);
Adrian Hunterb702b102010-02-15 10:03:35 -0800557 if (ret)
558 return ret;
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100559
560 host->get_cover_state = omap_hsmmc_get_cover_state;
561 mmc_gpio_set_cd_isr(mmc, omap_hsmmc_cover_irq);
Andreas Fenkartb7a56462015-03-20 15:53:54 +0100562 } else if (gpio_is_valid(pdata->gpio_cd)) {
563 ret = mmc_gpio_request_cd(mmc, pdata->gpio_cd, 0);
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100564 if (ret)
565 return ret;
566
567 host->card_detect = omap_hsmmc_card_detect;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100568 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800569
Andreas Fenkart326119c2014-11-08 15:33:14 +0100570 if (gpio_is_valid(pdata->gpio_wp)) {
NeilBrown41afa3142015-01-13 08:23:18 +1300571 ret = mmc_gpio_request_ro(mmc, pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800572 if (ret)
NeilBrown41afa3142015-01-13 08:23:18 +1300573 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100574 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800575
576 return 0;
Adrian Hunterb702b102010-02-15 10:03:35 -0800577}
578
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100579/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300580 * Start clock to the card
581 */
582static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
583{
584 OMAP_HSMMC_WRITE(host->base, SYSCTL,
585 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
586}
587
588/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100589 * Stop clock to the card
590 */
Denis Karpov70a33412009-09-22 16:44:59 -0700591static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100592{
593 OMAP_HSMMC_WRITE(host->base, SYSCTL,
594 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
595 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900596 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100597}
598
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700599static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
600 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700601{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200602 u32 irq_mask = INT_EN_MASK;
603 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700604
605 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200606 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700607
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700608 /* Disable timeout for erases */
609 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530610 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700611
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200612 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700613 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
614 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200615
616 /* latch pending CIRQ, but don't signal MMC core */
617 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
618 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700619 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200620 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700621}
622
623static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
624{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200625 u32 irq_mask = 0;
626 unsigned long flags;
627
628 spin_lock_irqsave(&host->irq_lock, flags);
629 /* no transfer running but need to keep cirq if enabled */
630 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
631 irq_mask |= CIRQ_EN;
632 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
633 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700634 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200635 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700636}
637
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300638/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530639static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300640{
641 u16 dsor = 0;
642
643 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530644 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530645 if (dsor > CLKD_MAX)
646 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300647 }
648
649 return dsor;
650}
651
Andy Shevchenko5934df22011-05-06 12:14:06 +0300652static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
653{
654 struct mmc_ios *ios = &host->mmc->ios;
655 unsigned long regval;
656 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530657 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300658
Venkatraman S8986d312012-08-07 19:10:38 +0530659 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300660
661 omap_hsmmc_stop_clock(host);
662
663 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
664 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530665 clkdiv = calc_divisor(host, ios);
666 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300667 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
668 OMAP_HSMMC_WRITE(host->base, SYSCTL,
669 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
670
671 /* Wait till the ICS bit is set */
672 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
673 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
674 && time_before(jiffies, timeout))
675 cpu_relax();
676
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530677 /*
678 * Enable High-Speed Support
679 * Pre-Requisites
680 * - Controller should support High-Speed-Enable Bit
681 * - Controller should not be using DDR Mode
682 * - Controller should advertise that it supports High Speed
683 * in capabilities register
684 * - MMC/SD clock coming out of controller > 25MHz
685 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100686 if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900687 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Ulf Hansson903101a2014-11-25 13:05:13 +0100688 (ios->timing != MMC_TIMING_UHS_DDR50) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530689 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
690 regval = OMAP_HSMMC_READ(host->base, HCTL);
691 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
692 regval |= HSPE;
693 else
694 regval &= ~HSPE;
695
696 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
697 }
698
Andy Shevchenko5934df22011-05-06 12:14:06 +0300699 omap_hsmmc_start_clock(host);
700}
701
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400702static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
703{
704 struct mmc_ios *ios = &host->mmc->ios;
705 u32 con;
706
707 con = OMAP_HSMMC_READ(host->base, CON);
Ulf Hansson903101a2014-11-25 13:05:13 +0100708 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
709 ios->timing == MMC_TIMING_UHS_DDR50)
Balaji T K03b5d9242012-04-09 12:08:33 +0530710 con |= DDR; /* configure in DDR mode */
711 else
712 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400713 switch (ios->bus_width) {
714 case MMC_BUS_WIDTH_8:
715 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
716 break;
717 case MMC_BUS_WIDTH_4:
718 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
719 OMAP_HSMMC_WRITE(host->base, HCTL,
720 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
721 break;
722 case MMC_BUS_WIDTH_1:
723 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
724 OMAP_HSMMC_WRITE(host->base, HCTL,
725 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
726 break;
727 }
728}
729
730static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
731{
732 struct mmc_ios *ios = &host->mmc->ios;
733 u32 con;
734
735 con = OMAP_HSMMC_READ(host->base, CON);
736 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
737 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
738 else
739 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
740}
741
Denis Karpov11dd62a2009-09-22 16:44:43 -0700742#ifdef CONFIG_PM
743
744/*
745 * Restore the MMC host context, if it was lost as result of a
746 * power state change.
747 */
Denis Karpov70a33412009-09-22 16:44:59 -0700748static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700749{
750 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400751 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700752 unsigned long timeout;
753
Tony Lindgren0a82e062013-10-21 00:25:19 +0530754 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
755 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
756 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
757 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
758 return 0;
759
760 host->context_loss++;
761
Balaji T Kc2200ef2012-03-07 09:55:30 -0500762 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700763 if (host->power_mode != MMC_POWER_OFF &&
764 (1 << ios->vdd) <= MMC_VDD_23_24)
765 hctl = SDVS18;
766 else
767 hctl = SDVS30;
768 capa = VS30 | VS18;
769 } else {
770 hctl = SDVS18;
771 capa = VS18;
772 }
773
Balaji T K5a52b082014-05-29 10:28:02 +0200774 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
775 hctl |= IWE;
776
Denis Karpov11dd62a2009-09-22 16:44:43 -0700777 OMAP_HSMMC_WRITE(host->base, HCTL,
778 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
779
780 OMAP_HSMMC_WRITE(host->base, CAPA,
781 OMAP_HSMMC_READ(host->base, CAPA) | capa);
782
783 OMAP_HSMMC_WRITE(host->base, HCTL,
784 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
785
786 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
787 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
788 && time_before(jiffies, timeout))
789 ;
790
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200791 OMAP_HSMMC_WRITE(host->base, ISE, 0);
792 OMAP_HSMMC_WRITE(host->base, IE, 0);
793 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700794
795 /* Do not initialize card-specific things if the power is off */
796 if (host->power_mode == MMC_POWER_OFF)
797 goto out;
798
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400799 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700800
Andy Shevchenko5934df22011-05-06 12:14:06 +0300801 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700802
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400803 omap_hsmmc_set_bus_mode(host);
804
Denis Karpov11dd62a2009-09-22 16:44:43 -0700805out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530806 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
807 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700808 return 0;
809}
810
811/*
812 * Save the MMC host context (store the number of power state changes so far).
813 */
Denis Karpov70a33412009-09-22 16:44:59 -0700814static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700815{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530816 host->con = OMAP_HSMMC_READ(host->base, CON);
817 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
818 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
819 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700820}
821
822#else
823
Denis Karpov70a33412009-09-22 16:44:59 -0700824static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700825{
826 return 0;
827}
828
Denis Karpov70a33412009-09-22 16:44:59 -0700829static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700830{
831}
832
833#endif
834
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100835/*
836 * Send init stream sequence to card
837 * before sending IDLE command
838 */
Denis Karpov70a33412009-09-22 16:44:59 -0700839static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100840{
841 int reg = 0;
842 unsigned long timeout;
843
Adrian Hunterb62f6222009-09-22 16:45:01 -0700844 if (host->protect_card)
845 return;
846
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100847 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700848
849 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100850 OMAP_HSMMC_WRITE(host->base, CON,
851 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
852 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
853
854 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530855 while ((reg != CC_EN) && time_before(jiffies, timeout))
856 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100857
858 OMAP_HSMMC_WRITE(host->base, CON,
859 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700860
861 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
862 OMAP_HSMMC_READ(host->base, STAT);
863
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100864 enable_irq(host->irq);
865}
866
867static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700868int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100869{
870 int r = 1;
871
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100872 if (host->get_cover_state)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100873 r = host->get_cover_state(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100874 return r;
875}
876
877static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700878omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100879 char *buf)
880{
881 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700882 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100883
Denis Karpov70a33412009-09-22 16:44:59 -0700884 return sprintf(buf, "%s\n",
885 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100886}
887
Denis Karpov70a33412009-09-22 16:44:59 -0700888static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100889
890static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700891omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100892 char *buf)
893{
894 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700895 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100896
Andreas Fenkart326119c2014-11-08 15:33:14 +0100897 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100898}
899
Denis Karpov70a33412009-09-22 16:44:59 -0700900static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100901
902/*
903 * Configure the response type and send the cmd.
904 */
905static void
Denis Karpov70a33412009-09-22 16:44:59 -0700906omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100907 struct mmc_data *data)
908{
909 int cmdreg = 0, resptype = 0, cmdtype = 0;
910
Venkatraman S8986d312012-08-07 19:10:38 +0530911 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100912 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
913 host->cmd = cmd;
914
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700915 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100916
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200917 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100918 if (cmd->flags & MMC_RSP_PRESENT) {
919 if (cmd->flags & MMC_RSP_136)
920 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200921 else if (cmd->flags & MMC_RSP_BUSY) {
922 resptype = 3;
923 host->response_busy = 1;
924 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100925 resptype = 2;
926 }
927
928 /*
929 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
930 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
931 * a val of 0x3, rest 0x0.
932 */
933 if (cmd == host->mrq->stop)
934 cmdtype = 0x3;
935
936 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
937
Balaji T Ka2e77152014-01-21 19:54:42 +0530938 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
939 host->mrq->sbc) {
940 cmdreg |= ACEN_ACMD23;
941 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
942 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100943 if (data) {
944 cmdreg |= DP_SELECT | MSBS | BCE;
945 if (data->flags & MMC_DATA_READ)
946 cmdreg |= DDIR;
947 else
948 cmdreg &= ~(DDIR);
949 }
950
951 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530952 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100953
Adrian Hunterb4175772010-05-26 14:42:06 -0700954 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700955
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100956 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
957 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
958}
959
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200960static int
Denis Karpov70a33412009-09-22 16:44:59 -0700961omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200962{
963 if (data->flags & MMC_DATA_WRITE)
964 return DMA_TO_DEVICE;
965 else
966 return DMA_FROM_DEVICE;
967}
968
Russell Kingc5c98922012-04-13 12:14:39 +0100969static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
970 struct mmc_data *data)
971{
972 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
973}
974
Adrian Hunterb4175772010-05-26 14:42:06 -0700975static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
976{
977 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530978 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700979
Venkatraman S31463b12012-04-09 12:08:34 +0530980 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700981 host->req_in_progress = 0;
982 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530983 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700984
985 omap_hsmmc_disable_irq(host);
986 /* Do not complete the request if DMA is still in progress */
987 if (mrq->data && host->use_dma && dma_ch != -1)
988 return;
989 host->mrq = NULL;
990 mmc_request_done(host->mmc, mrq);
NeilBrownf57ba4c2015-03-26 12:18:23 +1100991 pm_runtime_mark_last_busy(host->dev);
992 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -0700993}
994
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100995/*
996 * Notify the transfer complete to MMC core
997 */
998static void
Denis Karpov70a33412009-09-22 16:44:59 -0700999omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001000{
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001001 if (!data) {
1002 struct mmc_request *mrq = host->mrq;
1003
Adrian Hunter23050102009-09-22 16:44:57 -07001004 /* TC before CC from CMD6 - don't know why, but it happens */
1005 if (host->cmd && host->cmd->opcode == 6 &&
1006 host->response_busy) {
1007 host->response_busy = 0;
1008 return;
1009 }
1010
Adrian Hunterb4175772010-05-26 14:42:06 -07001011 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001012 return;
1013 }
1014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001015 host->data = NULL;
1016
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001017 if (!data->error)
1018 data->bytes_xfered += data->blocks * (data->blksz);
1019 else
1020 data->bytes_xfered = 0;
1021
Balaji T Kbf129e12014-01-21 19:54:42 +05301022 if (data->stop && (data->error || !host->mrq->sbc))
1023 omap_hsmmc_start_command(host, data->stop, NULL);
1024 else
Adrian Hunterb4175772010-05-26 14:42:06 -07001025 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001026}
1027
1028/*
1029 * Notify the core about command completion
1030 */
1031static void
Denis Karpov70a33412009-09-22 16:44:59 -07001032omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001033{
Balaji T Kbf129e12014-01-21 19:54:42 +05301034 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +05301035 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +05301036 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +05301037 omap_hsmmc_start_dma_transfer(host);
1038 omap_hsmmc_start_command(host, host->mrq->cmd,
1039 host->mrq->data);
1040 return;
1041 }
1042
Balaji T K2177fa92014-05-09 22:16:52 +05301043 host->cmd = NULL;
1044
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001045 if (cmd->flags & MMC_RSP_PRESENT) {
1046 if (cmd->flags & MMC_RSP_136) {
1047 /* response type 2 */
1048 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
1049 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
1050 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
1051 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
1052 } else {
1053 /* response types 1, 1b, 3, 4, 5, 6 */
1054 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
1055 }
1056 }
Adrian Hunterb4175772010-05-26 14:42:06 -07001057 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +05301058 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001059}
1060
1061/*
1062 * DMA clean up for command errors
1063 */
Denis Karpov70a33412009-09-22 16:44:59 -07001064static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001065{
Adrian Hunterb4175772010-05-26 14:42:06 -07001066 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +05301067 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -07001068
Jarkko Lavinen82788ff2008-12-05 12:31:46 +02001069 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001070
Venkatraman S31463b12012-04-09 12:08:34 +05301071 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001072 dma_ch = host->dma_ch;
1073 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +05301074 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001075
1076 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +01001077 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
1078
1079 dmaengine_terminate_all(chan);
1080 dma_unmap_sg(chan->device->dev,
1081 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -07001082 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +01001083
Per Forlin053bf342011-11-07 21:55:11 +05301084 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001085 }
1086 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001087}
1088
1089/*
1090 * Readable error output
1091 */
1092#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +03001093static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001094{
1095 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -07001096 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +03001097 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
1098 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
1099 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
1100 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001101 };
1102 char res[256];
1103 char *buf = res;
1104 int len, i;
1105
1106 len = sprintf(buf, "MMC IRQ 0x%x :", status);
1107 buf += len;
1108
Denis Karpov70a33412009-09-22 16:44:59 -07001109 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001110 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -07001111 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001112 buf += len;
1113 }
1114
Venkatraman S8986d312012-08-07 19:10:38 +05301115 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001116}
Adrian Hunter699b9582011-05-06 12:14:01 +03001117#else
1118static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1119 u32 status)
1120{
1121}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001122#endif /* CONFIG_MMC_DEBUG */
1123
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001124/*
1125 * MMC controller internal state machines reset
1126 *
1127 * Used to reset command or data internal state machines, using respectively
1128 * SRC or SRD bit of SYSCTL register
1129 * Can be called from interrupt context
1130 */
Denis Karpov70a33412009-09-22 16:44:59 -07001131static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1132 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001133{
1134 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301135 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001136
1137 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1138 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1139
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001140 /*
1141 * OMAP4 ES2 and greater has an updated reset logic.
1142 * Monitor a 0->1 transition first
1143 */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001144 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001145 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001146 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301147 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001148 }
1149 i = 0;
1150
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001151 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1152 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301153 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001154
1155 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1156 dev_err(mmc_dev(host->mmc),
1157 "Timeout waiting on controller reset in %s\n",
1158 __func__);
1159}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001160
Balaji T K25e18972012-11-19 21:59:55 +05301161static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1162 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301163{
Balaji T K25e18972012-11-19 21:59:55 +05301164 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301165 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301166 if (host->cmd)
1167 host->cmd->error = err;
1168 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301169
1170 if (host->data) {
1171 omap_hsmmc_reset_controller_fsm(host, SRD);
1172 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301173 } else if (host->mrq && host->mrq->cmd)
1174 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301175}
1176
Adrian Hunterb4175772010-05-26 14:42:06 -07001177static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001178{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001179 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001180 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301181 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001182
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001183 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301184 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001185
Venkatraman Sa7e96872012-11-19 22:00:01 +05301186 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001187 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001188
Venkatraman Sa7e96872012-11-19 22:00:01 +05301189 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301190 end_cmd = 1;
Kishon Vijay Abraham I408806f2015-06-16 16:07:17 +05301191 if (host->data || host->response_busy) {
1192 end_trans = !end_cmd;
1193 host->response_busy = 0;
1194 }
Venkatraman Sa7e96872012-11-19 22:00:01 +05301195 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301196 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Vignesh R5027cd12015-06-16 16:07:18 +05301197 else if (status & (CCRC_EN | DCRC_EN | DEB_EN | CEB_EN |
1198 BADA_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301199 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1200
Balaji T Ka2e77152014-01-21 19:54:42 +05301201 if (status & ACE_EN) {
1202 u32 ac12;
1203 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1204 if (!(ac12 & ACNE) && host->mrq->sbc) {
1205 end_cmd = 1;
1206 if (ac12 & ACTO)
1207 error = -ETIMEDOUT;
1208 else if (ac12 & (ACCE | ACEB | ACIE))
1209 error = -EILSEQ;
1210 host->mrq->sbc->error = error;
1211 hsmmc_command_incomplete(host, error, end_cmd);
1212 }
1213 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1214 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001215 }
1216
Francesco Lavra7472bab2013-06-29 08:25:12 +02001217 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301218 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001219 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301220 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001221 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001222}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001223
Adrian Hunterb4175772010-05-26 14:42:06 -07001224/*
1225 * MMC controller IRQ handler
1226 */
1227static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1228{
1229 struct omap_hsmmc_host *host = dev_id;
1230 int status;
1231
1232 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001233 while (status & (INT_EN_MASK | CIRQ_EN)) {
1234 if (host->req_in_progress)
1235 omap_hsmmc_do_irq(host, status);
1236
1237 if (status & CIRQ_EN)
1238 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301239
Adrian Hunterb4175772010-05-26 14:42:06 -07001240 /* Flush posted write */
1241 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301242 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001243
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001244 return IRQ_HANDLED;
1245}
1246
Denis Karpov70a33412009-09-22 16:44:59 -07001247static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001248{
1249 unsigned long i;
1250
1251 OMAP_HSMMC_WRITE(host->base, HCTL,
1252 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1253 for (i = 0; i < loops_per_jiffy; i++) {
1254 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1255 break;
1256 cpu_relax();
1257 }
1258}
1259
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001260/*
David Brownelleb250822009-02-17 14:49:01 -08001261 * Switch MMC interface voltage ... only relevant for MMC1.
1262 *
1263 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1264 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1265 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001266 */
Denis Karpov70a33412009-09-22 16:44:59 -07001267static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001268{
1269 u32 reg_val = 0;
1270 int ret;
1271
1272 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301273 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301274 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301275 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001276
1277 /* Turn the power off */
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001278 ret = omap_hsmmc_set_power(host->dev, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001279
1280 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001281 if (!ret)
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001282 ret = omap_hsmmc_set_power(host->dev, 1, vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301283 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301284 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301285 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001286
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001287 if (ret != 0)
1288 goto err;
1289
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001290 OMAP_HSMMC_WRITE(host->base, HCTL,
1291 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1292 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001293
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001294 /*
1295 * If a MMC dual voltage card is detected, the set_ios fn calls
1296 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001297 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001298 *
David Brownelleb250822009-02-17 14:49:01 -08001299 * Cope with a bit of slop in the range ... per data sheets:
1300 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1301 * but recommended values are 1.71V to 1.89V
1302 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1303 * but recommended values are 2.7V to 3.3V
1304 *
1305 * Board setup code shouldn't permit anything very out-of-range.
1306 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1307 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001308 */
David Brownelleb250822009-02-17 14:49:01 -08001309 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001310 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001311 else
1312 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001313
1314 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001315 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001316
1317 return 0;
1318err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301319 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001320 return ret;
1321}
1322
Adrian Hunterb62f6222009-09-22 16:45:01 -07001323/* Protect the card while the cover is open */
1324static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1325{
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001326 if (!host->get_cover_state)
Adrian Hunterb62f6222009-09-22 16:45:01 -07001327 return;
1328
1329 host->reqs_blocked = 0;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001330 if (host->get_cover_state(host->dev)) {
Adrian Hunterb62f6222009-09-22 16:45:01 -07001331 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301332 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001333 "card is now accessible\n",
1334 mmc_hostname(host->mmc));
1335 host->protect_card = 0;
1336 }
1337 } else {
1338 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301339 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001340 "card is now inaccessible\n",
1341 mmc_hostname(host->mmc));
1342 host->protect_card = 1;
1343 }
1344 }
1345}
1346
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001347/*
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001348 * irq handler when (cell-phone) cover is mounted/removed
1349 */
1350static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id)
1351{
1352 struct omap_hsmmc_host *host = dev_id;
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001353
1354 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
1355
Andreas Fenkart11227d12015-03-03 13:28:17 +01001356 omap_hsmmc_protect_card(host);
1357 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001358 return IRQ_HANDLED;
1359}
1360
Russell Kingc5c98922012-04-13 12:14:39 +01001361static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001362{
Russell Kingc5c98922012-04-13 12:14:39 +01001363 struct omap_hsmmc_host *host = param;
1364 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001365 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001366 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001367
Russell Kingc5c98922012-04-13 12:14:39 +01001368 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001369 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001370 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001371 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001372 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001373
Adrian Hunter770d7432011-05-06 12:14:11 +03001374 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001375 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001376 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001377 dma_unmap_sg(chan->device->dev,
1378 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001379 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001380
1381 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001382 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001383 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001384
1385 /* If DMA has finished after TC, complete the request */
1386 if (!req_in_progress) {
1387 struct mmc_request *mrq = host->mrq;
1388
1389 host->mrq = NULL;
1390 mmc_request_done(host->mmc, mrq);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001391 pm_runtime_mark_last_busy(host->dev);
1392 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001393 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001394}
1395
Per Forlin9782aff2011-07-01 18:55:23 +02001396static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1397 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001398 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001399 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001400{
1401 int dma_len;
1402
1403 if (!next && data->host_cookie &&
1404 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301405 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001406 " host->next_data.cookie %d\n",
1407 __func__, data->host_cookie, host->next_data.cookie);
1408 data->host_cookie = 0;
1409 }
1410
1411 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001412 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001413 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001414 omap_hsmmc_get_dma_dir(host, data));
1415
1416 } else {
1417 dma_len = host->next_data.dma_len;
1418 host->next_data.dma_len = 0;
1419 }
1420
1421
1422 if (dma_len == 0)
1423 return -EINVAL;
1424
1425 if (next) {
1426 next->dma_len = dma_len;
1427 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1428 } else
1429 host->dma_len = dma_len;
1430
1431 return 0;
1432}
1433
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001434/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001435 * Routine to configure and start DMA for the MMC card
1436 */
Balaji T K9d025332014-01-21 19:54:42 +05301437static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001438 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001439{
Russell King26b88522012-04-13 12:27:37 +01001440 struct dma_slave_config cfg;
1441 struct dma_async_tx_descriptor *tx;
1442 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001443 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001444 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001445
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001446 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001447 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001448 struct scatterlist *sgl;
1449
1450 sgl = data->sg + i;
1451 if (sgl->length % data->blksz)
1452 return -EINVAL;
1453 }
1454 if ((data->blksz % 4) != 0)
1455 /* REVISIT: The MMC buffer increments only when MSB is written.
1456 * Return error for blksz which is non multiple of four.
1457 */
1458 return -EINVAL;
1459
Adrian Hunterb4175772010-05-26 14:42:06 -07001460 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001461
Russell Kingc5c98922012-04-13 12:14:39 +01001462 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001463
Russell King26b88522012-04-13 12:27:37 +01001464 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1465 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1466 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1467 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1468 cfg.src_maxburst = data->blksz / 4;
1469 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001470
Russell King26b88522012-04-13 12:27:37 +01001471 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001472 if (ret)
1473 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001474
Russell King26b88522012-04-13 12:27:37 +01001475 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1476 if (ret)
1477 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001478
Russell King26b88522012-04-13 12:27:37 +01001479 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1480 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1481 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1482 if (!tx) {
1483 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1484 /* FIXME: cleanup */
1485 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001486 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001487
Russell King26b88522012-04-13 12:27:37 +01001488 tx->callback = omap_hsmmc_dma_callback;
1489 tx->callback_param = host;
1490
1491 /* Does not fail */
1492 dmaengine_submit(tx);
1493
1494 host->dma_ch = 1;
1495
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001496 return 0;
1497}
1498
Denis Karpov70a33412009-09-22 16:44:59 -07001499static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001500 unsigned int timeout_ns,
1501 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001502{
1503 unsigned int timeout, cycle_ns;
1504 uint32_t reg, clkd, dto = 0;
1505
1506 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1507 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1508 if (clkd == 0)
1509 clkd = 1;
1510
Balaji T K6e3076c2014-01-21 19:54:42 +05301511 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001512 timeout = timeout_ns / cycle_ns;
1513 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001514 if (timeout) {
1515 while ((timeout & 0x80000000) == 0) {
1516 dto += 1;
1517 timeout <<= 1;
1518 }
1519 dto = 31 - dto;
1520 timeout <<= 1;
1521 if (timeout && dto)
1522 dto += 1;
1523 if (dto >= 13)
1524 dto -= 13;
1525 else
1526 dto = 0;
1527 if (dto > 14)
1528 dto = 14;
1529 }
1530
1531 reg &= ~DTO_MASK;
1532 reg |= dto << DTO_SHIFT;
1533 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1534}
1535
Balaji T K9d025332014-01-21 19:54:42 +05301536static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1537{
1538 struct mmc_request *req = host->mrq;
1539 struct dma_chan *chan;
1540
1541 if (!req->data)
1542 return;
1543 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1544 | (req->data->blocks << 16));
1545 set_data_timeout(host, req->data->timeout_ns,
1546 req->data->timeout_clks);
1547 chan = omap_hsmmc_get_dma_chan(host, req->data);
1548 dma_async_issue_pending(chan);
1549}
1550
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001551/*
1552 * Configure block length for MMC/SD cards and initiate the transfer.
1553 */
1554static int
Denis Karpov70a33412009-09-22 16:44:59 -07001555omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001556{
1557 int ret;
1558 host->data = req->data;
1559
1560 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001561 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001562 /*
1563 * Set an arbitrary 100ms data timeout for commands with
1564 * busy signal.
1565 */
1566 if (req->cmd->flags & MMC_RSP_BUSY)
1567 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001568 return 0;
1569 }
1570
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001571 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301572 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001573 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301574 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001575 return ret;
1576 }
1577 }
1578 return 0;
1579}
1580
Per Forlin9782aff2011-07-01 18:55:23 +02001581static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1582 int err)
1583{
1584 struct omap_hsmmc_host *host = mmc_priv(mmc);
1585 struct mmc_data *data = mrq->data;
1586
Russell King26b88522012-04-13 12:27:37 +01001587 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001588 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001589
Russell King26b88522012-04-13 12:27:37 +01001590 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1591 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001592 data->host_cookie = 0;
1593 }
1594}
1595
1596static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1597 bool is_first_req)
1598{
1599 struct omap_hsmmc_host *host = mmc_priv(mmc);
1600
1601 if (mrq->data->host_cookie) {
1602 mrq->data->host_cookie = 0;
1603 return ;
1604 }
1605
Russell Kingc5c98922012-04-13 12:14:39 +01001606 if (host->use_dma) {
1607 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001608
Per Forlin9782aff2011-07-01 18:55:23 +02001609 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001610 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001611 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001612 }
Per Forlin9782aff2011-07-01 18:55:23 +02001613}
1614
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001615/*
1616 * Request function. for read/write operation
1617 */
Denis Karpov70a33412009-09-22 16:44:59 -07001618static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001619{
Denis Karpov70a33412009-09-22 16:44:59 -07001620 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001621 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001622
Adrian Hunterb4175772010-05-26 14:42:06 -07001623 BUG_ON(host->req_in_progress);
1624 BUG_ON(host->dma_ch != -1);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001625 pm_runtime_get_sync(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001626 if (host->protect_card) {
1627 if (host->reqs_blocked < 3) {
1628 /*
1629 * Ensure the controller is left in a consistent
1630 * state by resetting the command and data state
1631 * machines.
1632 */
1633 omap_hsmmc_reset_controller_fsm(host, SRD);
1634 omap_hsmmc_reset_controller_fsm(host, SRC);
1635 host->reqs_blocked += 1;
1636 }
1637 req->cmd->error = -EBADF;
1638 if (req->data)
1639 req->data->error = -EBADF;
1640 req->cmd->retries = 0;
1641 mmc_request_done(mmc, req);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001642 pm_runtime_mark_last_busy(host->dev);
1643 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001644 return;
1645 } else if (host->reqs_blocked)
1646 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001647 WARN_ON(host->mrq != NULL);
1648 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301649 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001650 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001651 if (err) {
1652 req->cmd->error = err;
1653 if (req->data)
1654 req->data->error = err;
1655 host->mrq = NULL;
1656 mmc_request_done(mmc, req);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001657 pm_runtime_mark_last_busy(host->dev);
1658 pm_runtime_put_autosuspend(host->dev);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001659 return;
1660 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301661 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301662 omap_hsmmc_start_command(host, req->sbc, NULL);
1663 return;
1664 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001665
Balaji T K9d025332014-01-21 19:54:42 +05301666 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001667 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001668}
1669
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001670/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001671static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001672{
Denis Karpov70a33412009-09-22 16:44:59 -07001673 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001674 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001675
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301676 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001677
Adrian Huntera3621462009-09-22 16:44:42 -07001678 if (ios->power_mode != host->power_mode) {
1679 switch (ios->power_mode) {
1680 case MMC_POWER_OFF:
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001681 omap_hsmmc_set_power(host->dev, 0, 0);
Adrian Huntera3621462009-09-22 16:44:42 -07001682 break;
1683 case MMC_POWER_UP:
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001684 omap_hsmmc_set_power(host->dev, 1, ios->vdd);
Adrian Huntera3621462009-09-22 16:44:42 -07001685 break;
1686 case MMC_POWER_ON:
1687 do_send_init_stream = 1;
1688 break;
1689 }
1690 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001691 }
1692
Denis Karpovdd498ef2009-09-22 16:44:49 -07001693 /* FIXME: set registers based only on changes to ios */
1694
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001695 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001696
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301697 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001698 /* Only MMC1 can interface at 3V without some flavor
1699 * of external transceiver; but they all handle 1.8V.
1700 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001701 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301702 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001703 /*
1704 * The mmc_select_voltage fn of the core does
1705 * not seem to set the power_mode to
1706 * MMC_POWER_UP upon recalculating the voltage.
1707 * vdd 1.8v.
1708 */
Denis Karpov70a33412009-09-22 16:44:59 -07001709 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1710 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001711 "Switch operation failed\n");
1712 }
1713 }
1714
Andy Shevchenko5934df22011-05-06 12:14:06 +03001715 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001716
Adrian Huntera3621462009-09-22 16:44:42 -07001717 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001718 send_init_stream(host);
1719
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001720 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001721
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301722 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001723}
1724
1725static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1726{
Denis Karpov70a33412009-09-22 16:44:59 -07001727 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001728
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001729 if (!host->card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001730 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001731 return host->card_detect(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001732}
1733
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001734static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1735{
1736 struct omap_hsmmc_host *host = mmc_priv(mmc);
1737
Andreas Fenkart326119c2014-11-08 15:33:14 +01001738 if (mmc_pdata(host)->init_card)
1739 mmc_pdata(host)->init_card(card);
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001740}
1741
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001742static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1743{
1744 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001745 u32 irq_mask, con;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001746 unsigned long flags;
1747
1748 spin_lock_irqsave(&host->irq_lock, flags);
1749
Balaji T K5a52b082014-05-29 10:28:02 +02001750 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001751 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1752 if (enable) {
1753 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1754 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001755 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001756 } else {
1757 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1758 irq_mask &= ~CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001759 con &= ~(CTPL | CLKEXTFREE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001760 }
Balaji T K5a52b082014-05-29 10:28:02 +02001761 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001762 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1763
1764 /*
1765 * if enable, piggy back detection on current request
1766 * but always disable immediately
1767 */
1768 if (!host->req_in_progress || !enable)
1769 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1770
1771 /* flush posted write */
1772 OMAP_HSMMC_READ(host->base, IE);
1773
1774 spin_unlock_irqrestore(&host->irq_lock, flags);
1775}
1776
1777static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1778{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001779 int ret;
1780
1781 /*
1782 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1783 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1784 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1785 * with functional clock disabled.
1786 */
1787 if (!host->dev->of_node || !host->wake_irq)
1788 return -ENODEV;
1789
Tony Lindgren5b83b222015-05-21 15:51:52 -07001790 ret = dev_pm_set_dedicated_wake_irq(host->dev, host->wake_irq);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001791 if (ret) {
1792 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1793 goto err;
1794 }
1795
1796 /*
1797 * Some omaps don't have wake-up path from deeper idle states
1798 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1799 */
1800 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001801 struct pinctrl *p = devm_pinctrl_get(host->dev);
1802 if (!p) {
1803 ret = -ENODEV;
1804 goto err_free_irq;
1805 }
1806 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1807 dev_info(host->dev, "missing default pinctrl state\n");
1808 devm_pinctrl_put(p);
1809 ret = -EINVAL;
1810 goto err_free_irq;
1811 }
1812
1813 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1814 dev_info(host->dev, "missing idle pinctrl state\n");
1815 devm_pinctrl_put(p);
1816 ret = -EINVAL;
1817 goto err_free_irq;
1818 }
1819 devm_pinctrl_put(p);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001820 }
1821
Balaji T K5a52b082014-05-29 10:28:02 +02001822 OMAP_HSMMC_WRITE(host->base, HCTL,
1823 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001824 return 0;
1825
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001826err_free_irq:
Tony Lindgren5b83b222015-05-21 15:51:52 -07001827 dev_pm_clear_wake_irq(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001828err:
1829 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1830 host->wake_irq = 0;
1831 return ret;
1832}
1833
Denis Karpov70a33412009-09-22 16:44:59 -07001834static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001835{
1836 u32 hctl, capa, value;
1837
1838 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301839 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001840 hctl = SDVS30;
1841 capa = VS30 | VS18;
1842 } else {
1843 hctl = SDVS18;
1844 capa = VS18;
1845 }
1846
1847 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1848 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1849
1850 value = OMAP_HSMMC_READ(host->base, CAPA);
1851 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1852
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001853 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001854 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001855}
1856
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07001857static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1858 unsigned int direction, int blk_size)
1859{
1860 /* This controller can't do multiblock reads due to hw bugs */
1861 if (direction == MMC_DATA_READ)
1862 return 1;
1863
1864 return blk_size;
1865}
1866
1867static struct mmc_host_ops omap_hsmmc_ops = {
Per Forlin9782aff2011-07-01 18:55:23 +02001868 .post_req = omap_hsmmc_post_req,
1869 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001870 .request = omap_hsmmc_request,
1871 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001872 .get_cd = omap_hsmmc_get_cd,
Andreas Fenkarta49d8352015-03-03 13:28:14 +01001873 .get_ro = mmc_gpio_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001874 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001875 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001876};
1877
Denis Karpovd900f712009-09-22 16:44:38 -07001878#ifdef CONFIG_DEBUG_FS
1879
Denis Karpov70a33412009-09-22 16:44:59 -07001880static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001881{
1882 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001883 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001884
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001885 seq_printf(s, "mmc%d:\n", mmc->index);
1886 seq_printf(s, "sdio irq mode\t%s\n",
1887 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1888
1889 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1890 seq_printf(s, "sdio irq \t%s\n",
1891 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1892 : "disabled");
1893 }
1894 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001895
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301896 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001897 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001898 seq_printf(s, "CON:\t\t0x%08x\n",
1899 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001900 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1901 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001902 seq_printf(s, "HCTL:\t\t0x%08x\n",
1903 OMAP_HSMMC_READ(host->base, HCTL));
1904 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1905 OMAP_HSMMC_READ(host->base, SYSCTL));
1906 seq_printf(s, "IE:\t\t0x%08x\n",
1907 OMAP_HSMMC_READ(host->base, IE));
1908 seq_printf(s, "ISE:\t\t0x%08x\n",
1909 OMAP_HSMMC_READ(host->base, ISE));
1910 seq_printf(s, "CAPA:\t\t0x%08x\n",
1911 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001912
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301913 pm_runtime_mark_last_busy(host->dev);
1914 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001915
Denis Karpovd900f712009-09-22 16:44:38 -07001916 return 0;
1917}
1918
Denis Karpov70a33412009-09-22 16:44:59 -07001919static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001920{
Denis Karpov70a33412009-09-22 16:44:59 -07001921 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001922}
1923
1924static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001925 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001926 .read = seq_read,
1927 .llseek = seq_lseek,
1928 .release = single_release,
1929};
1930
Denis Karpov70a33412009-09-22 16:44:59 -07001931static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001932{
1933 if (mmc->debugfs_root)
1934 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1935 mmc, &mmc_regs_fops);
1936}
1937
1938#else
1939
Denis Karpov70a33412009-09-22 16:44:59 -07001940static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001941{
1942}
1943
1944#endif
1945
Rajendra Nayak46856a62012-03-12 20:32:37 +05301946#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001947static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1948 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1949 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1950};
1951
1952static const struct omap_mmc_of_data omap4_mmc_of_data = {
1953 .reg_offset = 0x100,
1954};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001955static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1956 .reg_offset = 0x100,
1957 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1958};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301959
1960static const struct of_device_id omap_mmc_of_match[] = {
1961 {
1962 .compatible = "ti,omap2-hsmmc",
1963 },
1964 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001965 .compatible = "ti,omap3-pre-es3-hsmmc",
1966 .data = &omap3_pre_es3_mmc_of_data,
1967 },
1968 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301969 .compatible = "ti,omap3-hsmmc",
1970 },
1971 {
1972 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001973 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301974 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001975 {
1976 .compatible = "ti,am33xx-hsmmc",
1977 .data = &am33xx_mmc_of_data,
1978 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301979 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001980};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301981MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1982
Andreas Fenkart551434382014-11-08 15:33:09 +01001983static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
Rajendra Nayak46856a62012-03-12 20:32:37 +05301984{
Andreas Fenkart551434382014-11-08 15:33:09 +01001985 struct omap_hsmmc_platform_data *pdata;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301986 struct device_node *np = dev->of_node;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301987
1988 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1989 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301990 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301991
1992 if (of_find_property(np, "ti,dual-volt", NULL))
1993 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1994
Andreas Fenkartb7a56462015-03-20 15:53:54 +01001995 pdata->gpio_cd = -EINVAL;
1996 pdata->gpio_cod = -EINVAL;
NeilBrownfdb9de12015-01-13 08:23:18 +13001997 pdata->gpio_wp = -EINVAL;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301998
1999 if (of_find_property(np, "ti,non-removable", NULL)) {
Andreas Fenkart326119c2014-11-08 15:33:14 +01002000 pdata->nonremovable = true;
2001 pdata->no_regulator_off_init = true;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302002 }
Rajendra Nayak46856a62012-03-12 20:32:37 +05302003
2004 if (of_find_property(np, "ti,needs-special-reset", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002005 pdata->features |= HSMMC_HAS_UPDATED_RESET;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302006
Hebbar, Gururajacd587092012-11-19 21:59:58 +05302007 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002008 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
Hebbar, Gururajacd587092012-11-19 21:59:58 +05302009
Rajendra Nayak46856a62012-03-12 20:32:37 +05302010 return pdata;
2011}
2012#else
Andreas Fenkart551434382014-11-08 15:33:09 +01002013static inline struct omap_hsmmc_platform_data
Rajendra Nayak46856a62012-03-12 20:32:37 +05302014 *of_get_hsmmc_pdata(struct device *dev)
2015{
Balaji T K19df45b2014-02-28 19:08:18 +05302016 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05302017}
2018#endif
2019
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05002020static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002021{
Andreas Fenkart551434382014-11-08 15:33:09 +01002022 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002023 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07002024 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002025 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002026 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302027 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01002028 dma_cap_mask_t mask;
2029 unsigned tx_req, rx_req;
Nishanth Menon59445b12014-02-13 23:45:48 -06002030 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05302031 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302032
2033 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
2034 if (match) {
2035 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01002036
2037 if (IS_ERR(pdata))
2038 return PTR_ERR(pdata);
2039
Rajendra Nayak46856a62012-03-12 20:32:37 +05302040 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06002041 data = match->data;
2042 pdata->reg_offset = data->reg_offset;
2043 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302044 }
2045 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002046
2047 if (pdata == NULL) {
2048 dev_err(&pdev->dev, "Platform Data is missing\n");
2049 return -ENXIO;
2050 }
2051
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002052 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2053 irq = platform_get_irq(pdev, 0);
2054 if (res == NULL || irq < 0)
2055 return -ENXIO;
2056
Balaji T K77fae212014-05-09 22:16:51 +05302057 base = devm_ioremap_resource(&pdev->dev, res);
2058 if (IS_ERR(base))
2059 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002060
Denis Karpov70a33412009-09-22 16:44:59 -07002061 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002062 if (!mmc) {
2063 ret = -ENOMEM;
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002064 goto err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002065 }
2066
NeilBrownfdb9de12015-01-13 08:23:18 +13002067 ret = mmc_of_parse(mmc);
2068 if (ret)
2069 goto err1;
2070
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002071 host = mmc_priv(mmc);
2072 host->mmc = mmc;
2073 host->pdata = pdata;
2074 host->dev = &pdev->dev;
2075 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002076 host->dma_ch = -1;
2077 host->irq = irq;
Balaji T Kfc307df2012-04-02 12:26:47 +05302078 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05302079 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08002080 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002081 host->next_data.cookie = 1;
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +05302082 host->vqmmc_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002083
NeilBrown41afa3142015-01-13 08:23:18 +13002084 ret = omap_hsmmc_gpio_init(mmc, host, pdata);
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002085 if (ret)
2086 goto err_gpio;
2087
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002088 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002089
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002090 if (pdev->dev.of_node)
2091 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2092
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302093 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002094
Daniel Mackd418ed82012-02-19 13:20:33 +01002095 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2096
2097 if (pdata->max_freq > 0)
2098 mmc->f_max = pdata->max_freq;
NeilBrownfdb9de12015-01-13 08:23:18 +13002099 else if (mmc->f_max == 0)
Daniel Mackd418ed82012-02-19 13:20:33 +01002100 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002101
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002102 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002103
Balaji T K96181952014-05-09 22:16:48 +05302104 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002105 if (IS_ERR(host->fclk)) {
2106 ret = PTR_ERR(host->fclk);
2107 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002108 goto err1;
2109 }
2110
Paul Walmsley9b682562011-10-06 14:50:35 -06002111 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2112 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 -07002113 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
Paul Walmsley9b682562011-10-06 14:50:35 -06002114 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002115
Tony Lindgren5b83b222015-05-21 15:51:52 -07002116 device_init_wakeup(&pdev->dev, true);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302117 pm_runtime_enable(host->dev);
2118 pm_runtime_get_sync(host->dev);
2119 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2120 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002121
Balaji T K92a3aeb2012-02-24 21:14:34 +05302122 omap_hsmmc_context_save(host);
2123
Balaji T K96181952014-05-09 22:16:48 +05302124 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302125 /*
2126 * MMC can still work without debounce clock.
2127 */
2128 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302129 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302130 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302131 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302132 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002133 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002134
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002135 /* Since we do only SG emulation, we can have as many segs
2136 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002137 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002138
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002139 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2140 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2141 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2142 mmc->max_seg_size = mmc->max_req_size;
2143
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002144 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002145 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002146
Andreas Fenkart326119c2014-11-08 15:33:14 +01002147 mmc->caps |= mmc_pdata(host)->caps;
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002148 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002149 mmc->caps |= MMC_CAP_4_BIT_DATA;
2150
Andreas Fenkart326119c2014-11-08 15:33:14 +01002151 if (mmc_pdata(host)->nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002152 mmc->caps |= MMC_CAP_NONREMOVABLE;
2153
NeilBrownfdb9de12015-01-13 08:23:18 +13002154 mmc->pm_caps |= mmc_pdata(host)->pm_caps;
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002155
Denis Karpov70a33412009-09-22 16:44:59 -07002156 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002157
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302158 if (!pdev->dev.of_node) {
2159 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2160 if (!res) {
2161 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2162 ret = -ENXIO;
2163 goto err_irq;
2164 }
2165 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002166
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302167 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2168 if (!res) {
2169 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2170 ret = -ENXIO;
2171 goto err_irq;
2172 }
2173 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002174 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002175
Russell King26b88522012-04-13 12:27:37 +01002176 dma_cap_zero(mask);
2177 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002178
Matt Porterd272fbf2013-05-10 17:42:34 +05302179 host->rx_chan =
2180 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2181 &rx_req, &pdev->dev, "rx");
2182
Russell King26b88522012-04-13 12:27:37 +01002183 if (!host->rx_chan) {
2184 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002185 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002186 goto err_irq;
2187 }
2188
Matt Porterd272fbf2013-05-10 17:42:34 +05302189 host->tx_chan =
2190 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2191 &tx_req, &pdev->dev, "tx");
2192
Russell King26b88522012-04-13 12:27:37 +01002193 if (!host->tx_chan) {
2194 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002195 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002196 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002197 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002198
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002199 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302200 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002201 mmc_hostname(mmc), host);
2202 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302203 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002204 goto err_irq;
2205 }
2206
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02002207 if (omap_hsmmc_have_reg()) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002208 ret = omap_hsmmc_reg_get(host);
2209 if (ret)
Andreas Fenkartbb09d152014-11-08 15:33:11 +01002210 goto err_irq;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002211 }
2212
Andreas Fenkart326119c2014-11-08 15:33:14 +01002213 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002214
Adrian Hunterb4175772010-05-26 14:42:06 -07002215 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002216
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002217 /*
2218 * For now, only support SDIO interrupt if we have a separate
2219 * wake-up interrupt configured from device tree. This is because
2220 * the wake-up interrupt is needed for idle state and some
2221 * platforms need special quirks. And we don't want to add new
2222 * legacy mux platform init code callbacks any longer as we
2223 * are moving to DT based booting anyways.
2224 */
2225 ret = omap_hsmmc_configure_wake_irq(host);
2226 if (!ret)
2227 mmc->caps |= MMC_CAP_SDIO_IRQ;
2228
Adrian Hunterb62f6222009-09-22 16:45:01 -07002229 omap_hsmmc_protect_card(host);
2230
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002231 mmc_add_host(mmc);
2232
Andreas Fenkart326119c2014-11-08 15:33:14 +01002233 if (mmc_pdata(host)->name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002234 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2235 if (ret < 0)
2236 goto err_slot_name;
2237 }
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002238 if (host->get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002239 ret = device_create_file(&mmc->class_dev,
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002240 &dev_attr_cover_switch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002241 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002242 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002243 }
2244
Denis Karpov70a33412009-09-22 16:44:59 -07002245 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302246 pm_runtime_mark_last_busy(host->dev);
2247 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002248
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002249 return 0;
2250
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002251err_slot_name:
2252 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002253err_irq:
Tony Lindgren5b83b222015-05-21 15:51:52 -07002254 device_init_wakeup(&pdev->dev, false);
Russell Kingc5c98922012-04-13 12:14:39 +01002255 if (host->tx_chan)
2256 dma_release_channel(host->tx_chan);
2257 if (host->rx_chan)
2258 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302259 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002260 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302261 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302262 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002263err1:
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002264err_gpio:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002265 mmc_free_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002266err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002267 return ret;
2268}
2269
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002270static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002271{
Denis Karpov70a33412009-09-22 16:44:59 -07002272 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002273
Felipe Balbi927ce942012-03-14 11:18:27 +02002274 pm_runtime_get_sync(host->dev);
2275 mmc_remove_host(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002276
Russell Kingc5c98922012-04-13 12:14:39 +01002277 if (host->tx_chan)
2278 dma_release_channel(host->tx_chan);
2279 if (host->rx_chan)
2280 dma_release_channel(host->rx_chan);
2281
Felipe Balbi927ce942012-03-14 11:18:27 +02002282 pm_runtime_put_sync(host->dev);
2283 pm_runtime_disable(host->dev);
Tony Lindgren5b83b222015-05-21 15:51:52 -07002284 device_init_wakeup(&pdev->dev, false);
Balaji T K96181952014-05-09 22:16:48 +05302285 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302286 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002287
Balaji T K9d1f0282012-10-15 21:35:07 +05302288 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002289
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002290 return 0;
2291}
2292
Russ Dill3d3bbfb2015-02-27 13:24:34 +02002293#ifdef CONFIG_PM_SLEEP
Kevin Hilmana791daa2010-05-26 14:42:07 -07002294static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002295{
Felipe Balbi927ce942012-03-14 11:18:27 +02002296 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2297
2298 if (!host)
2299 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002300
Felipe Balbi927ce942012-03-14 11:18:27 +02002301 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002302
2303 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002304 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2305 OMAP_HSMMC_WRITE(host->base, IE, 0);
2306 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002307 OMAP_HSMMC_WRITE(host->base, HCTL,
2308 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2309 }
2310
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302311 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302312 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002313
Eliad Peller31f9d462011-11-22 16:02:17 +02002314 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002315 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002316}
2317
2318/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002319static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002320{
Felipe Balbi927ce942012-03-14 11:18:27 +02002321 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2322
2323 if (!host)
2324 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002325
Felipe Balbi927ce942012-03-14 11:18:27 +02002326 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002327
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302328 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302329 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002330
Felipe Balbi927ce942012-03-14 11:18:27 +02002331 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2332 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002333
Felipe Balbi927ce942012-03-14 11:18:27 +02002334 omap_hsmmc_protect_card(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002335 pm_runtime_mark_last_busy(host->dev);
2336 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002337 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002338}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002339#endif
2340
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302341static int omap_hsmmc_runtime_suspend(struct device *dev)
2342{
2343 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002344 unsigned long flags;
Andreas Fenkartf9459012014-05-29 10:28:03 +02002345 int ret = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302346
2347 host = platform_get_drvdata(to_platform_device(dev));
2348 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002349 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302350
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002351 spin_lock_irqsave(&host->irq_lock, flags);
2352 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2353 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2354 /* disable sdio irq handling to prevent race */
2355 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2356 OMAP_HSMMC_WRITE(host->base, IE, 0);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002357
2358 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2359 /*
2360 * dat1 line low, pending sdio irq
2361 * race condition: possible irq handler running on
2362 * multi-core, abort
2363 */
2364 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2365 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2366 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2367 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2368 pm_runtime_mark_last_busy(dev);
2369 ret = -EBUSY;
2370 goto abort;
2371 }
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002372
Andreas Fenkart97978a42014-05-29 10:28:04 +02002373 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002374 } else {
2375 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002376 }
Andreas Fenkart97978a42014-05-29 10:28:04 +02002377
Andreas Fenkartf9459012014-05-29 10:28:03 +02002378abort:
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002379 spin_unlock_irqrestore(&host->irq_lock, flags);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002380 return ret;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302381}
2382
2383static int omap_hsmmc_runtime_resume(struct device *dev)
2384{
2385 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002386 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302387
2388 host = platform_get_drvdata(to_platform_device(dev));
2389 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002390 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302391
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002392 spin_lock_irqsave(&host->irq_lock, flags);
2393 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2394 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002395
Andreas Fenkart97978a42014-05-29 10:28:04 +02002396 pinctrl_pm_select_default_state(host->dev);
2397
2398 /* irq lost, if pinmux incorrect */
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002399 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2400 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2401 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002402 } else {
2403 pinctrl_pm_select_default_state(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002404 }
2405 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302406 return 0;
2407}
2408
Kevin Hilmana791daa2010-05-26 14:42:07 -07002409static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Russ Dill3d3bbfb2015-02-27 13:24:34 +02002410 SET_SYSTEM_SLEEP_PM_OPS(omap_hsmmc_suspend, omap_hsmmc_resume)
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302411 .runtime_suspend = omap_hsmmc_runtime_suspend,
2412 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002413};
2414
2415static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002416 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002417 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002418 .driver = {
2419 .name = DRIVER_NAME,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002420 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302421 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002422 },
2423};
2424
Felipe Balbib7964502012-03-14 11:18:32 +02002425module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002426MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2427MODULE_LICENSE("GPL");
2428MODULE_ALIAS("platform:" DRIVER_NAME);
2429MODULE_AUTHOR("Texas Instruments Inc");