blob: 5a5946a7f2df880d2c77c97b45b05d9afc3fa578 [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;
185 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100186 void __iomem *base;
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530187 int vqmmc_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100188 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700189 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100190 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200191 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100192 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700193 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100194 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530195 u32 con;
196 u32 hctl;
197 u32 sysctl;
198 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100199 int irq;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200200 int wake_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100201 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100202 struct dma_chan *tx_chan;
203 struct dma_chan *rx_chan;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200204 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700205 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700206 int protect_card;
207 int reqs_blocked;
Adrian Hunterb4175772010-05-26 14:42:06 -0700208 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530209 unsigned long clk_rate;
Balaji T Ka2e77152014-01-21 19:54:42 +0530210 unsigned int flags;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200211#define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
212#define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
Per Forlin9782aff2011-07-01 18:55:23 +0200213 struct omap_hsmmc_next next_data;
Andreas Fenkart551434382014-11-08 15:33:09 +0100214 struct omap_hsmmc_platform_data *pdata;
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100215
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100216 /* return MMC cover switch state, can be NULL if not supported.
217 *
218 * possible return values:
219 * 0 - closed
220 * 1 - open
221 */
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100222 int (*get_cover_state)(struct device *dev);
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100223
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100224 int (*card_detect)(struct device *dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100225};
226
Nishanth Menon59445b12014-02-13 23:45:48 -0600227struct omap_mmc_of_data {
228 u32 reg_offset;
229 u8 controller_flags;
230};
231
Balaji T Kbf129e12014-01-21 19:54:42 +0530232static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
233
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100234static int omap_hsmmc_card_detect(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800235{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530236 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800237
NeilBrown41afa3142015-01-13 08:23:18 +1300238 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800239}
240
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100241static int omap_hsmmc_get_cover_state(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800242{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530243 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800244
NeilBrown41afa3142015-01-13 08:23:18 +1300245 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800246}
247
Adrian Hunterb702b102010-02-15 10:03:35 -0800248#ifdef CONFIG_REGULATOR
249
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530250static int omap_hsmmc_enable_supply(struct mmc_host *mmc, int vdd)
251{
252 int ret;
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +0530253 struct omap_hsmmc_host *host = mmc_priv(mmc);
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530254
255 if (mmc->supply.vmmc) {
256 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
257 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
333 if (host->pbias_enabled == 0) {
334 ret = regulator_enable(host->pbias);
335 if (ret) {
336 dev_err(host->dev, "pbias reg enable fail\n");
337 return ret;
338 }
339 host->pbias_enabled = 1;
340 }
341 } else {
342 if (host->pbias_enabled == 1) {
343 ret = regulator_disable(host->pbias);
344 if (ret) {
345 dev_err(host->dev, "pbias reg disable fail\n");
346 return ret;
347 }
348 host->pbias_enabled = 0;
349 }
350 }
351
352 return 0;
353}
354
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100355static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800356{
357 struct omap_hsmmc_host *host =
358 platform_get_drvdata(to_platform_device(dev));
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530359 struct mmc_host *mmc = host->mmc;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800360 int ret = 0;
361
Andreas Fenkartf7f0f032015-07-07 20:38:43 +0200362 if (mmc_pdata(host)->set_power)
363 return mmc_pdata(host)->set_power(dev, power_on, vdd);
364
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800365 /*
366 * If we don't see a Vcc regulator, assume it's a fixed
367 * voltage always-on regulator.
368 */
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530369 if (!mmc->supply.vmmc)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800370 return 0;
371
Andreas Fenkart326119c2014-11-08 15:33:14 +0100372 if (mmc_pdata(host)->before_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100373 mmc_pdata(host)->before_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800374
Kishon Vijay Abraham Iec85c952015-08-27 14:44:01 +0530375 ret = omap_hsmmc_set_pbias(host, false, 0);
376 if (ret)
377 return ret;
Balaji T Ke99448f2014-02-19 20:26:40 +0530378
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800379 /*
380 * Assume Vcc regulator is used only to power the card ... OMAP
381 * VDDS is used to power the pins, optionally with a transceiver to
382 * support cards using voltages other than VDDS (1.8V nominal). When a
383 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
384 *
385 * In some cases this regulator won't support enable/disable;
386 * e.g. it's a fixed rail for a WLAN chip.
387 *
388 * In other cases vcc_aux switches interface power. Example, for
389 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
390 * chips/cards need an interface voltage rail too.
391 */
392 if (power_on) {
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530393 ret = omap_hsmmc_enable_supply(mmc, vdd);
394 if (ret)
395 return ret;
Kishon Vijay Abraham I97fe7e52015-08-27 14:44:02 +0530396
397 ret = omap_hsmmc_set_pbias(host, true, vdd);
398 if (ret)
399 goto err_set_voltage;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800400 } else {
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530401 ret = omap_hsmmc_disable_supply(mmc);
402 if (ret)
403 return ret;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800404 }
405
Andreas Fenkart326119c2014-11-08 15:33:14 +0100406 if (mmc_pdata(host)->after_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100407 mmc_pdata(host)->after_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800408
Kishon Vijay Abraham I229f3292015-08-27 14:43:59 +0530409 return 0;
410
411err_set_voltage:
Kishon Vijay Abraham I2a17f842015-08-27 14:44:00 +0530412 omap_hsmmc_disable_supply(mmc);
Kishon Vijay Abraham I229f3292015-08-27 14:43:59 +0530413
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800414 return ret;
415}
416
Kishon Vijay Abraham Ic8518ef2015-08-27 14:44:03 +0530417static int omap_hsmmc_disable_boot_regulator(struct regulator *reg)
418{
419 int ret;
420
421 if (!reg)
422 return 0;
423
424 if (regulator_is_enabled(reg)) {
425 ret = regulator_enable(reg);
426 if (ret)
427 return ret;
428
429 ret = regulator_disable(reg);
430 if (ret)
431 return ret;
432 }
433
434 return 0;
435}
436
437static int omap_hsmmc_disable_boot_regulators(struct omap_hsmmc_host *host)
438{
439 struct mmc_host *mmc = host->mmc;
440 int ret;
441
442 /*
443 * disable regulators enabled during boot and get the usecount
444 * right so that regulators can be enabled/disabled by checking
445 * the return value of regulator_is_enabled
446 */
447 ret = omap_hsmmc_disable_boot_regulator(mmc->supply.vmmc);
448 if (ret) {
449 dev_err(host->dev, "fail to disable boot enabled vmmc reg\n");
450 return ret;
451 }
452
453 ret = omap_hsmmc_disable_boot_regulator(mmc->supply.vqmmc);
454 if (ret) {
455 dev_err(host->dev,
456 "fail to disable boot enabled vmmc_aux reg\n");
457 return ret;
458 }
459
460 ret = omap_hsmmc_disable_boot_regulator(host->pbias);
461 if (ret) {
462 dev_err(host->dev,
463 "failed to disable boot enabled pbias reg\n");
464 return ret;
465 }
466
467 return 0;
468}
469
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800470static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
471{
kishore kadiyala64be9782010-10-01 16:35:28 -0700472 int ocr_value = 0;
Kishon Vijay Abraham I7d607f92015-08-27 14:43:53 +0530473 int ret;
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530474 struct mmc_host *mmc = host->mmc;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800475
Andreas Fenkartf7f0f032015-07-07 20:38:43 +0200476 if (mmc_pdata(host)->set_power)
477 return 0;
478
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530479 mmc->supply.vmmc = devm_regulator_get_optional(host->dev, "vmmc");
480 if (IS_ERR(mmc->supply.vmmc)) {
481 ret = PTR_ERR(mmc->supply.vmmc);
Kishon Vijay Abraham I7d607f92015-08-27 14:43:53 +0530482 if (ret != -ENODEV)
483 return ret;
Kishon Vijay Abraham I7d607f92015-08-27 14:43:53 +0530484 dev_dbg(host->dev, "unable to get vmmc regulator %ld\n",
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530485 PTR_ERR(mmc->supply.vmmc));
486 mmc->supply.vmmc = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800487 } else {
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530488 ocr_value = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
Kishon Vijay Abraham Ib49069f2015-08-27 14:43:56 +0530489 if (ocr_value > 0)
Andreas Fenkart326119c2014-11-08 15:33:14 +0100490 mmc_pdata(host)->ocr_mask = ocr_value;
Balaji T K987fd492014-02-19 20:26:40 +0530491 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800492
Balaji T K987fd492014-02-19 20:26:40 +0530493 /* Allow an aux regulator */
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530494 mmc->supply.vqmmc = devm_regulator_get_optional(host->dev, "vmmc_aux");
495 if (IS_ERR(mmc->supply.vqmmc)) {
496 ret = PTR_ERR(mmc->supply.vqmmc);
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530497 if (ret != -ENODEV)
498 return ret;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530499 dev_dbg(host->dev, "unable to get vmmc_aux regulator %ld\n",
Kishon Vijay Abraham Iaa9a6802015-08-27 14:43:57 +0530500 PTR_ERR(mmc->supply.vqmmc));
501 mmc->supply.vqmmc = NULL;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530502 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800503
Kishon Vijay Abraham Ic299dc32015-08-27 14:43:55 +0530504 host->pbias = devm_regulator_get_optional(host->dev, "pbias");
505 if (IS_ERR(host->pbias)) {
506 ret = PTR_ERR(host->pbias);
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530507 if (ret != -ENODEV)
508 return ret;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530509 dev_dbg(host->dev, "unable to get pbias regulator %ld\n",
Kishon Vijay Abraham Ic299dc32015-08-27 14:43:55 +0530510 PTR_ERR(host->pbias));
511 host->pbias = NULL;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530512 }
Balaji T Ke99448f2014-02-19 20:26:40 +0530513
Balaji T K987fd492014-02-19 20:26:40 +0530514 /* For eMMC do not power off when not in sleep state */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100515 if (mmc_pdata(host)->no_regulator_off_init)
Balaji T K987fd492014-02-19 20:26:40 +0530516 return 0;
Adrian Huntere840ce12011-05-06 12:14:10 +0300517
Kishon Vijay Abraham Ic8518ef2015-08-27 14:44:03 +0530518 ret = omap_hsmmc_disable_boot_regulators(host);
519 if (ret)
520 return ret;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800521
522 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800523}
524
Adrian Hunterb702b102010-02-15 10:03:35 -0800525static inline int omap_hsmmc_have_reg(void)
526{
527 return 1;
528}
529
530#else
531
Andreas Fenkartf7f0f032015-07-07 20:38:43 +0200532static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
533{
534 return 0;
535}
536
Adrian Hunterb702b102010-02-15 10:03:35 -0800537static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
538{
539 return -EINVAL;
540}
541
Adrian Hunterb702b102010-02-15 10:03:35 -0800542static inline int omap_hsmmc_have_reg(void)
543{
544 return 0;
545}
546
547#endif
548
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100549static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id);
NeilBrown41afa3142015-01-13 08:23:18 +1300550
551static int omap_hsmmc_gpio_init(struct mmc_host *mmc,
552 struct omap_hsmmc_host *host,
Andreas Fenkart1e363e32014-11-08 15:33:15 +0100553 struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800554{
555 int ret;
556
Andreas Fenkartb7a56462015-03-20 15:53:54 +0100557 if (gpio_is_valid(pdata->gpio_cod)) {
558 ret = mmc_gpio_request_cd(mmc, pdata->gpio_cod, 0);
Adrian Hunterb702b102010-02-15 10:03:35 -0800559 if (ret)
560 return ret;
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100561
562 host->get_cover_state = omap_hsmmc_get_cover_state;
563 mmc_gpio_set_cd_isr(mmc, omap_hsmmc_cover_irq);
Andreas Fenkartb7a56462015-03-20 15:53:54 +0100564 } else if (gpio_is_valid(pdata->gpio_cd)) {
565 ret = mmc_gpio_request_cd(mmc, pdata->gpio_cd, 0);
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100566 if (ret)
567 return ret;
568
569 host->card_detect = omap_hsmmc_card_detect;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100570 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800571
Andreas Fenkart326119c2014-11-08 15:33:14 +0100572 if (gpio_is_valid(pdata->gpio_wp)) {
NeilBrown41afa3142015-01-13 08:23:18 +1300573 ret = mmc_gpio_request_ro(mmc, pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800574 if (ret)
NeilBrown41afa3142015-01-13 08:23:18 +1300575 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100576 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800577
578 return 0;
Adrian Hunterb702b102010-02-15 10:03:35 -0800579}
580
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100581/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300582 * Start clock to the card
583 */
584static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
585{
586 OMAP_HSMMC_WRITE(host->base, SYSCTL,
587 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
588}
589
590/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100591 * Stop clock to the card
592 */
Denis Karpov70a33412009-09-22 16:44:59 -0700593static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100594{
595 OMAP_HSMMC_WRITE(host->base, SYSCTL,
596 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
597 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900598 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100599}
600
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700601static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
602 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700603{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200604 u32 irq_mask = INT_EN_MASK;
605 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700606
607 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200608 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700609
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700610 /* Disable timeout for erases */
611 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530612 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700613
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200614 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700615 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
616 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200617
618 /* latch pending CIRQ, but don't signal MMC core */
619 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
620 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700621 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200622 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700623}
624
625static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
626{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200627 u32 irq_mask = 0;
628 unsigned long flags;
629
630 spin_lock_irqsave(&host->irq_lock, flags);
631 /* no transfer running but need to keep cirq if enabled */
632 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
633 irq_mask |= CIRQ_EN;
634 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
635 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700636 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200637 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700638}
639
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300640/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530641static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300642{
643 u16 dsor = 0;
644
645 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530646 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530647 if (dsor > CLKD_MAX)
648 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300649 }
650
651 return dsor;
652}
653
Andy Shevchenko5934df22011-05-06 12:14:06 +0300654static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
655{
656 struct mmc_ios *ios = &host->mmc->ios;
657 unsigned long regval;
658 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530659 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300660
Venkatraman S8986d312012-08-07 19:10:38 +0530661 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300662
663 omap_hsmmc_stop_clock(host);
664
665 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
666 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530667 clkdiv = calc_divisor(host, ios);
668 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300669 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
670 OMAP_HSMMC_WRITE(host->base, SYSCTL,
671 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
672
673 /* Wait till the ICS bit is set */
674 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
675 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
676 && time_before(jiffies, timeout))
677 cpu_relax();
678
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530679 /*
680 * Enable High-Speed Support
681 * Pre-Requisites
682 * - Controller should support High-Speed-Enable Bit
683 * - Controller should not be using DDR Mode
684 * - Controller should advertise that it supports High Speed
685 * in capabilities register
686 * - MMC/SD clock coming out of controller > 25MHz
687 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100688 if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900689 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Ulf Hansson903101a2014-11-25 13:05:13 +0100690 (ios->timing != MMC_TIMING_UHS_DDR50) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530691 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
692 regval = OMAP_HSMMC_READ(host->base, HCTL);
693 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
694 regval |= HSPE;
695 else
696 regval &= ~HSPE;
697
698 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
699 }
700
Andy Shevchenko5934df22011-05-06 12:14:06 +0300701 omap_hsmmc_start_clock(host);
702}
703
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400704static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
705{
706 struct mmc_ios *ios = &host->mmc->ios;
707 u32 con;
708
709 con = OMAP_HSMMC_READ(host->base, CON);
Ulf Hansson903101a2014-11-25 13:05:13 +0100710 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
711 ios->timing == MMC_TIMING_UHS_DDR50)
Balaji T K03b5d9242012-04-09 12:08:33 +0530712 con |= DDR; /* configure in DDR mode */
713 else
714 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400715 switch (ios->bus_width) {
716 case MMC_BUS_WIDTH_8:
717 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
718 break;
719 case MMC_BUS_WIDTH_4:
720 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
721 OMAP_HSMMC_WRITE(host->base, HCTL,
722 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
723 break;
724 case MMC_BUS_WIDTH_1:
725 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
726 OMAP_HSMMC_WRITE(host->base, HCTL,
727 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
728 break;
729 }
730}
731
732static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
733{
734 struct mmc_ios *ios = &host->mmc->ios;
735 u32 con;
736
737 con = OMAP_HSMMC_READ(host->base, CON);
738 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
739 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
740 else
741 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
742}
743
Denis Karpov11dd62a2009-09-22 16:44:43 -0700744#ifdef CONFIG_PM
745
746/*
747 * Restore the MMC host context, if it was lost as result of a
748 * power state change.
749 */
Denis Karpov70a33412009-09-22 16:44:59 -0700750static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700751{
752 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400753 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700754 unsigned long timeout;
755
Tony Lindgren0a82e062013-10-21 00:25:19 +0530756 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
757 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
758 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
759 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
760 return 0;
761
762 host->context_loss++;
763
Balaji T Kc2200ef2012-03-07 09:55:30 -0500764 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700765 if (host->power_mode != MMC_POWER_OFF &&
766 (1 << ios->vdd) <= MMC_VDD_23_24)
767 hctl = SDVS18;
768 else
769 hctl = SDVS30;
770 capa = VS30 | VS18;
771 } else {
772 hctl = SDVS18;
773 capa = VS18;
774 }
775
Balaji T K5a52b082014-05-29 10:28:02 +0200776 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
777 hctl |= IWE;
778
Denis Karpov11dd62a2009-09-22 16:44:43 -0700779 OMAP_HSMMC_WRITE(host->base, HCTL,
780 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
781
782 OMAP_HSMMC_WRITE(host->base, CAPA,
783 OMAP_HSMMC_READ(host->base, CAPA) | capa);
784
785 OMAP_HSMMC_WRITE(host->base, HCTL,
786 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
787
788 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
789 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
790 && time_before(jiffies, timeout))
791 ;
792
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200793 OMAP_HSMMC_WRITE(host->base, ISE, 0);
794 OMAP_HSMMC_WRITE(host->base, IE, 0);
795 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700796
797 /* Do not initialize card-specific things if the power is off */
798 if (host->power_mode == MMC_POWER_OFF)
799 goto out;
800
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400801 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700802
Andy Shevchenko5934df22011-05-06 12:14:06 +0300803 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700804
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400805 omap_hsmmc_set_bus_mode(host);
806
Denis Karpov11dd62a2009-09-22 16:44:43 -0700807out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530808 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
809 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700810 return 0;
811}
812
813/*
814 * Save the MMC host context (store the number of power state changes so far).
815 */
Denis Karpov70a33412009-09-22 16:44:59 -0700816static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700817{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530818 host->con = OMAP_HSMMC_READ(host->base, CON);
819 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
820 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
821 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700822}
823
824#else
825
Denis Karpov70a33412009-09-22 16:44:59 -0700826static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700827{
828 return 0;
829}
830
Denis Karpov70a33412009-09-22 16:44:59 -0700831static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700832{
833}
834
835#endif
836
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100837/*
838 * Send init stream sequence to card
839 * before sending IDLE command
840 */
Denis Karpov70a33412009-09-22 16:44:59 -0700841static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100842{
843 int reg = 0;
844 unsigned long timeout;
845
Adrian Hunterb62f6222009-09-22 16:45:01 -0700846 if (host->protect_card)
847 return;
848
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100849 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700850
851 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100852 OMAP_HSMMC_WRITE(host->base, CON,
853 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
854 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
855
856 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530857 while ((reg != CC_EN) && time_before(jiffies, timeout))
858 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100859
860 OMAP_HSMMC_WRITE(host->base, CON,
861 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700862
863 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
864 OMAP_HSMMC_READ(host->base, STAT);
865
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100866 enable_irq(host->irq);
867}
868
869static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700870int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100871{
872 int r = 1;
873
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100874 if (host->get_cover_state)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100875 r = host->get_cover_state(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100876 return r;
877}
878
879static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700880omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100881 char *buf)
882{
883 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700884 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100885
Denis Karpov70a33412009-09-22 16:44:59 -0700886 return sprintf(buf, "%s\n",
887 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100888}
889
Denis Karpov70a33412009-09-22 16:44:59 -0700890static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100891
892static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700893omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100894 char *buf)
895{
896 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700897 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100898
Andreas Fenkart326119c2014-11-08 15:33:14 +0100899 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100900}
901
Denis Karpov70a33412009-09-22 16:44:59 -0700902static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100903
904/*
905 * Configure the response type and send the cmd.
906 */
907static void
Denis Karpov70a33412009-09-22 16:44:59 -0700908omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100909 struct mmc_data *data)
910{
911 int cmdreg = 0, resptype = 0, cmdtype = 0;
912
Venkatraman S8986d312012-08-07 19:10:38 +0530913 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100914 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
915 host->cmd = cmd;
916
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700917 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100918
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200919 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100920 if (cmd->flags & MMC_RSP_PRESENT) {
921 if (cmd->flags & MMC_RSP_136)
922 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200923 else if (cmd->flags & MMC_RSP_BUSY) {
924 resptype = 3;
925 host->response_busy = 1;
926 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100927 resptype = 2;
928 }
929
930 /*
931 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
932 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
933 * a val of 0x3, rest 0x0.
934 */
935 if (cmd == host->mrq->stop)
936 cmdtype = 0x3;
937
938 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
939
Balaji T Ka2e77152014-01-21 19:54:42 +0530940 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
941 host->mrq->sbc) {
942 cmdreg |= ACEN_ACMD23;
943 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
944 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100945 if (data) {
946 cmdreg |= DP_SELECT | MSBS | BCE;
947 if (data->flags & MMC_DATA_READ)
948 cmdreg |= DDIR;
949 else
950 cmdreg &= ~(DDIR);
951 }
952
953 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530954 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100955
Adrian Hunterb4175772010-05-26 14:42:06 -0700956 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700957
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100958 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
959 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
960}
961
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200962static int
Denis Karpov70a33412009-09-22 16:44:59 -0700963omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200964{
965 if (data->flags & MMC_DATA_WRITE)
966 return DMA_TO_DEVICE;
967 else
968 return DMA_FROM_DEVICE;
969}
970
Russell Kingc5c98922012-04-13 12:14:39 +0100971static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
972 struct mmc_data *data)
973{
974 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
975}
976
Adrian Hunterb4175772010-05-26 14:42:06 -0700977static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
978{
979 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530980 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700981
Venkatraman S31463b12012-04-09 12:08:34 +0530982 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700983 host->req_in_progress = 0;
984 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530985 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700986
987 omap_hsmmc_disable_irq(host);
988 /* Do not complete the request if DMA is still in progress */
989 if (mrq->data && host->use_dma && dma_ch != -1)
990 return;
991 host->mrq = NULL;
992 mmc_request_done(host->mmc, mrq);
NeilBrownf57ba4c2015-03-26 12:18:23 +1100993 pm_runtime_mark_last_busy(host->dev);
994 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -0700995}
996
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100997/*
998 * Notify the transfer complete to MMC core
999 */
1000static void
Denis Karpov70a33412009-09-22 16:44:59 -07001001omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001002{
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001003 if (!data) {
1004 struct mmc_request *mrq = host->mrq;
1005
Adrian Hunter23050102009-09-22 16:44:57 -07001006 /* TC before CC from CMD6 - don't know why, but it happens */
1007 if (host->cmd && host->cmd->opcode == 6 &&
1008 host->response_busy) {
1009 host->response_busy = 0;
1010 return;
1011 }
1012
Adrian Hunterb4175772010-05-26 14:42:06 -07001013 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001014 return;
1015 }
1016
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001017 host->data = NULL;
1018
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001019 if (!data->error)
1020 data->bytes_xfered += data->blocks * (data->blksz);
1021 else
1022 data->bytes_xfered = 0;
1023
Balaji T Kbf129e12014-01-21 19:54:42 +05301024 if (data->stop && (data->error || !host->mrq->sbc))
1025 omap_hsmmc_start_command(host, data->stop, NULL);
1026 else
Adrian Hunterb4175772010-05-26 14:42:06 -07001027 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001028}
1029
1030/*
1031 * Notify the core about command completion
1032 */
1033static void
Denis Karpov70a33412009-09-22 16:44:59 -07001034omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001035{
Balaji T Kbf129e12014-01-21 19:54:42 +05301036 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +05301037 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +05301038 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +05301039 omap_hsmmc_start_dma_transfer(host);
1040 omap_hsmmc_start_command(host, host->mrq->cmd,
1041 host->mrq->data);
1042 return;
1043 }
1044
Balaji T K2177fa92014-05-09 22:16:52 +05301045 host->cmd = NULL;
1046
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001047 if (cmd->flags & MMC_RSP_PRESENT) {
1048 if (cmd->flags & MMC_RSP_136) {
1049 /* response type 2 */
1050 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
1051 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
1052 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
1053 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
1054 } else {
1055 /* response types 1, 1b, 3, 4, 5, 6 */
1056 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
1057 }
1058 }
Adrian Hunterb4175772010-05-26 14:42:06 -07001059 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +05301060 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001061}
1062
1063/*
1064 * DMA clean up for command errors
1065 */
Denis Karpov70a33412009-09-22 16:44:59 -07001066static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001067{
Adrian Hunterb4175772010-05-26 14:42:06 -07001068 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +05301069 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -07001070
Jarkko Lavinen82788ff2008-12-05 12:31:46 +02001071 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001072
Venkatraman S31463b12012-04-09 12:08:34 +05301073 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001074 dma_ch = host->dma_ch;
1075 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +05301076 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001077
1078 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +01001079 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
1080
1081 dmaengine_terminate_all(chan);
1082 dma_unmap_sg(chan->device->dev,
1083 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -07001084 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +01001085
Per Forlin053bf342011-11-07 21:55:11 +05301086 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001087 }
1088 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001089}
1090
1091/*
1092 * Readable error output
1093 */
1094#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +03001095static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001096{
1097 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -07001098 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +03001099 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
1100 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
1101 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
1102 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001103 };
1104 char res[256];
1105 char *buf = res;
1106 int len, i;
1107
1108 len = sprintf(buf, "MMC IRQ 0x%x :", status);
1109 buf += len;
1110
Denis Karpov70a33412009-09-22 16:44:59 -07001111 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001112 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -07001113 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001114 buf += len;
1115 }
1116
Venkatraman S8986d312012-08-07 19:10:38 +05301117 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001118}
Adrian Hunter699b9582011-05-06 12:14:01 +03001119#else
1120static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1121 u32 status)
1122{
1123}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001124#endif /* CONFIG_MMC_DEBUG */
1125
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001126/*
1127 * MMC controller internal state machines reset
1128 *
1129 * Used to reset command or data internal state machines, using respectively
1130 * SRC or SRD bit of SYSCTL register
1131 * Can be called from interrupt context
1132 */
Denis Karpov70a33412009-09-22 16:44:59 -07001133static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1134 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001135{
1136 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301137 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001138
1139 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1140 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1141
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001142 /*
1143 * OMAP4 ES2 and greater has an updated reset logic.
1144 * Monitor a 0->1 transition first
1145 */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001146 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001147 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001148 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301149 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001150 }
1151 i = 0;
1152
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001153 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1154 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301155 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001156
1157 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1158 dev_err(mmc_dev(host->mmc),
1159 "Timeout waiting on controller reset in %s\n",
1160 __func__);
1161}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001162
Balaji T K25e18972012-11-19 21:59:55 +05301163static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1164 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301165{
Balaji T K25e18972012-11-19 21:59:55 +05301166 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301167 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301168 if (host->cmd)
1169 host->cmd->error = err;
1170 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301171
1172 if (host->data) {
1173 omap_hsmmc_reset_controller_fsm(host, SRD);
1174 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301175 } else if (host->mrq && host->mrq->cmd)
1176 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301177}
1178
Adrian Hunterb4175772010-05-26 14:42:06 -07001179static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001180{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001181 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001182 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301183 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001184
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001185 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301186 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001187
Venkatraman Sa7e96872012-11-19 22:00:01 +05301188 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001189 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001190
Venkatraman Sa7e96872012-11-19 22:00:01 +05301191 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301192 end_cmd = 1;
Kishon Vijay Abraham I408806f2015-06-16 16:07:17 +05301193 if (host->data || host->response_busy) {
1194 end_trans = !end_cmd;
1195 host->response_busy = 0;
1196 }
Venkatraman Sa7e96872012-11-19 22:00:01 +05301197 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301198 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Vignesh R5027cd12015-06-16 16:07:18 +05301199 else if (status & (CCRC_EN | DCRC_EN | DEB_EN | CEB_EN |
1200 BADA_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301201 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1202
Balaji T Ka2e77152014-01-21 19:54:42 +05301203 if (status & ACE_EN) {
1204 u32 ac12;
1205 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1206 if (!(ac12 & ACNE) && host->mrq->sbc) {
1207 end_cmd = 1;
1208 if (ac12 & ACTO)
1209 error = -ETIMEDOUT;
1210 else if (ac12 & (ACCE | ACEB | ACIE))
1211 error = -EILSEQ;
1212 host->mrq->sbc->error = error;
1213 hsmmc_command_incomplete(host, error, end_cmd);
1214 }
1215 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1216 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001217 }
1218
Francesco Lavra7472bab2013-06-29 08:25:12 +02001219 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301220 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001221 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301222 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001223 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001224}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001225
Adrian Hunterb4175772010-05-26 14:42:06 -07001226/*
1227 * MMC controller IRQ handler
1228 */
1229static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1230{
1231 struct omap_hsmmc_host *host = dev_id;
1232 int status;
1233
1234 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001235 while (status & (INT_EN_MASK | CIRQ_EN)) {
1236 if (host->req_in_progress)
1237 omap_hsmmc_do_irq(host, status);
1238
1239 if (status & CIRQ_EN)
1240 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301241
Adrian Hunterb4175772010-05-26 14:42:06 -07001242 /* Flush posted write */
1243 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301244 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001245
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001246 return IRQ_HANDLED;
1247}
1248
Denis Karpov70a33412009-09-22 16:44:59 -07001249static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001250{
1251 unsigned long i;
1252
1253 OMAP_HSMMC_WRITE(host->base, HCTL,
1254 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1255 for (i = 0; i < loops_per_jiffy; i++) {
1256 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1257 break;
1258 cpu_relax();
1259 }
1260}
1261
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001262/*
David Brownelleb250822009-02-17 14:49:01 -08001263 * Switch MMC interface voltage ... only relevant for MMC1.
1264 *
1265 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1266 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1267 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001268 */
Denis Karpov70a33412009-09-22 16:44:59 -07001269static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001270{
1271 u32 reg_val = 0;
1272 int ret;
1273
1274 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301275 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301276 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301277 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001278
1279 /* Turn the power off */
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001280 ret = omap_hsmmc_set_power(host->dev, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001281
1282 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001283 if (!ret)
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001284 ret = omap_hsmmc_set_power(host->dev, 1, vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301285 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301286 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301287 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001288
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001289 if (ret != 0)
1290 goto err;
1291
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001292 OMAP_HSMMC_WRITE(host->base, HCTL,
1293 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1294 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001295
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001296 /*
1297 * If a MMC dual voltage card is detected, the set_ios fn calls
1298 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001299 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001300 *
David Brownelleb250822009-02-17 14:49:01 -08001301 * Cope with a bit of slop in the range ... per data sheets:
1302 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1303 * but recommended values are 1.71V to 1.89V
1304 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1305 * but recommended values are 2.7V to 3.3V
1306 *
1307 * Board setup code shouldn't permit anything very out-of-range.
1308 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1309 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001310 */
David Brownelleb250822009-02-17 14:49:01 -08001311 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001312 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001313 else
1314 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001315
1316 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001317 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001318
1319 return 0;
1320err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301321 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001322 return ret;
1323}
1324
Adrian Hunterb62f6222009-09-22 16:45:01 -07001325/* Protect the card while the cover is open */
1326static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1327{
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001328 if (!host->get_cover_state)
Adrian Hunterb62f6222009-09-22 16:45:01 -07001329 return;
1330
1331 host->reqs_blocked = 0;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001332 if (host->get_cover_state(host->dev)) {
Adrian Hunterb62f6222009-09-22 16:45:01 -07001333 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301334 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001335 "card is now accessible\n",
1336 mmc_hostname(host->mmc));
1337 host->protect_card = 0;
1338 }
1339 } else {
1340 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301341 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001342 "card is now inaccessible\n",
1343 mmc_hostname(host->mmc));
1344 host->protect_card = 1;
1345 }
1346 }
1347}
1348
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001349/*
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001350 * irq handler when (cell-phone) cover is mounted/removed
1351 */
1352static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id)
1353{
1354 struct omap_hsmmc_host *host = dev_id;
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001355
1356 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
1357
Andreas Fenkart11227d12015-03-03 13:28:17 +01001358 omap_hsmmc_protect_card(host);
1359 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001360 return IRQ_HANDLED;
1361}
1362
Russell Kingc5c98922012-04-13 12:14:39 +01001363static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001364{
Russell Kingc5c98922012-04-13 12:14:39 +01001365 struct omap_hsmmc_host *host = param;
1366 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001367 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001368 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001369
Russell Kingc5c98922012-04-13 12:14:39 +01001370 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001371 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001372 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001373 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001374 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001375
Adrian Hunter770d7432011-05-06 12:14:11 +03001376 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001377 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001378 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001379 dma_unmap_sg(chan->device->dev,
1380 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001381 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001382
1383 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001384 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001385 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001386
1387 /* If DMA has finished after TC, complete the request */
1388 if (!req_in_progress) {
1389 struct mmc_request *mrq = host->mrq;
1390
1391 host->mrq = NULL;
1392 mmc_request_done(host->mmc, mrq);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001393 pm_runtime_mark_last_busy(host->dev);
1394 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001395 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001396}
1397
Per Forlin9782aff2011-07-01 18:55:23 +02001398static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1399 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001400 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001401 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001402{
1403 int dma_len;
1404
1405 if (!next && data->host_cookie &&
1406 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301407 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001408 " host->next_data.cookie %d\n",
1409 __func__, data->host_cookie, host->next_data.cookie);
1410 data->host_cookie = 0;
1411 }
1412
1413 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001414 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001415 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001416 omap_hsmmc_get_dma_dir(host, data));
1417
1418 } else {
1419 dma_len = host->next_data.dma_len;
1420 host->next_data.dma_len = 0;
1421 }
1422
1423
1424 if (dma_len == 0)
1425 return -EINVAL;
1426
1427 if (next) {
1428 next->dma_len = dma_len;
1429 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1430 } else
1431 host->dma_len = dma_len;
1432
1433 return 0;
1434}
1435
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001436/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001437 * Routine to configure and start DMA for the MMC card
1438 */
Balaji T K9d025332014-01-21 19:54:42 +05301439static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001440 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001441{
Russell King26b88522012-04-13 12:27:37 +01001442 struct dma_slave_config cfg;
1443 struct dma_async_tx_descriptor *tx;
1444 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001445 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001446 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001447
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001448 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001449 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001450 struct scatterlist *sgl;
1451
1452 sgl = data->sg + i;
1453 if (sgl->length % data->blksz)
1454 return -EINVAL;
1455 }
1456 if ((data->blksz % 4) != 0)
1457 /* REVISIT: The MMC buffer increments only when MSB is written.
1458 * Return error for blksz which is non multiple of four.
1459 */
1460 return -EINVAL;
1461
Adrian Hunterb4175772010-05-26 14:42:06 -07001462 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001463
Russell Kingc5c98922012-04-13 12:14:39 +01001464 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001465
Russell King26b88522012-04-13 12:27:37 +01001466 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1467 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1468 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1469 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1470 cfg.src_maxburst = data->blksz / 4;
1471 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001472
Russell King26b88522012-04-13 12:27:37 +01001473 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001474 if (ret)
1475 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001476
Russell King26b88522012-04-13 12:27:37 +01001477 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1478 if (ret)
1479 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001480
Russell King26b88522012-04-13 12:27:37 +01001481 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1482 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1483 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1484 if (!tx) {
1485 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1486 /* FIXME: cleanup */
1487 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001488 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001489
Russell King26b88522012-04-13 12:27:37 +01001490 tx->callback = omap_hsmmc_dma_callback;
1491 tx->callback_param = host;
1492
1493 /* Does not fail */
1494 dmaengine_submit(tx);
1495
1496 host->dma_ch = 1;
1497
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001498 return 0;
1499}
1500
Denis Karpov70a33412009-09-22 16:44:59 -07001501static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001502 unsigned int timeout_ns,
1503 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001504{
1505 unsigned int timeout, cycle_ns;
1506 uint32_t reg, clkd, dto = 0;
1507
1508 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1509 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1510 if (clkd == 0)
1511 clkd = 1;
1512
Balaji T K6e3076c2014-01-21 19:54:42 +05301513 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001514 timeout = timeout_ns / cycle_ns;
1515 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001516 if (timeout) {
1517 while ((timeout & 0x80000000) == 0) {
1518 dto += 1;
1519 timeout <<= 1;
1520 }
1521 dto = 31 - dto;
1522 timeout <<= 1;
1523 if (timeout && dto)
1524 dto += 1;
1525 if (dto >= 13)
1526 dto -= 13;
1527 else
1528 dto = 0;
1529 if (dto > 14)
1530 dto = 14;
1531 }
1532
1533 reg &= ~DTO_MASK;
1534 reg |= dto << DTO_SHIFT;
1535 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1536}
1537
Balaji T K9d025332014-01-21 19:54:42 +05301538static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1539{
1540 struct mmc_request *req = host->mrq;
1541 struct dma_chan *chan;
1542
1543 if (!req->data)
1544 return;
1545 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1546 | (req->data->blocks << 16));
1547 set_data_timeout(host, req->data->timeout_ns,
1548 req->data->timeout_clks);
1549 chan = omap_hsmmc_get_dma_chan(host, req->data);
1550 dma_async_issue_pending(chan);
1551}
1552
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001553/*
1554 * Configure block length for MMC/SD cards and initiate the transfer.
1555 */
1556static int
Denis Karpov70a33412009-09-22 16:44:59 -07001557omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001558{
1559 int ret;
1560 host->data = req->data;
1561
1562 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001563 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001564 /*
1565 * Set an arbitrary 100ms data timeout for commands with
1566 * busy signal.
1567 */
1568 if (req->cmd->flags & MMC_RSP_BUSY)
1569 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001570 return 0;
1571 }
1572
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001573 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301574 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001575 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301576 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001577 return ret;
1578 }
1579 }
1580 return 0;
1581}
1582
Per Forlin9782aff2011-07-01 18:55:23 +02001583static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1584 int err)
1585{
1586 struct omap_hsmmc_host *host = mmc_priv(mmc);
1587 struct mmc_data *data = mrq->data;
1588
Russell King26b88522012-04-13 12:27:37 +01001589 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001590 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001591
Russell King26b88522012-04-13 12:27:37 +01001592 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1593 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001594 data->host_cookie = 0;
1595 }
1596}
1597
1598static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1599 bool is_first_req)
1600{
1601 struct omap_hsmmc_host *host = mmc_priv(mmc);
1602
1603 if (mrq->data->host_cookie) {
1604 mrq->data->host_cookie = 0;
1605 return ;
1606 }
1607
Russell Kingc5c98922012-04-13 12:14:39 +01001608 if (host->use_dma) {
1609 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001610
Per Forlin9782aff2011-07-01 18:55:23 +02001611 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001612 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001613 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001614 }
Per Forlin9782aff2011-07-01 18:55:23 +02001615}
1616
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001617/*
1618 * Request function. for read/write operation
1619 */
Denis Karpov70a33412009-09-22 16:44:59 -07001620static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001621{
Denis Karpov70a33412009-09-22 16:44:59 -07001622 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001623 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001624
Adrian Hunterb4175772010-05-26 14:42:06 -07001625 BUG_ON(host->req_in_progress);
1626 BUG_ON(host->dma_ch != -1);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001627 pm_runtime_get_sync(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001628 if (host->protect_card) {
1629 if (host->reqs_blocked < 3) {
1630 /*
1631 * Ensure the controller is left in a consistent
1632 * state by resetting the command and data state
1633 * machines.
1634 */
1635 omap_hsmmc_reset_controller_fsm(host, SRD);
1636 omap_hsmmc_reset_controller_fsm(host, SRC);
1637 host->reqs_blocked += 1;
1638 }
1639 req->cmd->error = -EBADF;
1640 if (req->data)
1641 req->data->error = -EBADF;
1642 req->cmd->retries = 0;
1643 mmc_request_done(mmc, req);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001644 pm_runtime_mark_last_busy(host->dev);
1645 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001646 return;
1647 } else if (host->reqs_blocked)
1648 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001649 WARN_ON(host->mrq != NULL);
1650 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301651 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001652 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001653 if (err) {
1654 req->cmd->error = err;
1655 if (req->data)
1656 req->data->error = err;
1657 host->mrq = NULL;
1658 mmc_request_done(mmc, req);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001659 pm_runtime_mark_last_busy(host->dev);
1660 pm_runtime_put_autosuspend(host->dev);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001661 return;
1662 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301663 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301664 omap_hsmmc_start_command(host, req->sbc, NULL);
1665 return;
1666 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001667
Balaji T K9d025332014-01-21 19:54:42 +05301668 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001669 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001670}
1671
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001672/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001673static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001674{
Denis Karpov70a33412009-09-22 16:44:59 -07001675 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001676 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001677
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301678 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001679
Adrian Huntera3621462009-09-22 16:44:42 -07001680 if (ios->power_mode != host->power_mode) {
1681 switch (ios->power_mode) {
1682 case MMC_POWER_OFF:
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001683 omap_hsmmc_set_power(host->dev, 0, 0);
Adrian Huntera3621462009-09-22 16:44:42 -07001684 break;
1685 case MMC_POWER_UP:
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001686 omap_hsmmc_set_power(host->dev, 1, ios->vdd);
Adrian Huntera3621462009-09-22 16:44:42 -07001687 break;
1688 case MMC_POWER_ON:
1689 do_send_init_stream = 1;
1690 break;
1691 }
1692 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001693 }
1694
Denis Karpovdd498ef2009-09-22 16:44:49 -07001695 /* FIXME: set registers based only on changes to ios */
1696
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001697 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001698
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301699 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001700 /* Only MMC1 can interface at 3V without some flavor
1701 * of external transceiver; but they all handle 1.8V.
1702 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001703 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301704 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001705 /*
1706 * The mmc_select_voltage fn of the core does
1707 * not seem to set the power_mode to
1708 * MMC_POWER_UP upon recalculating the voltage.
1709 * vdd 1.8v.
1710 */
Denis Karpov70a33412009-09-22 16:44:59 -07001711 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1712 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001713 "Switch operation failed\n");
1714 }
1715 }
1716
Andy Shevchenko5934df22011-05-06 12:14:06 +03001717 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001718
Adrian Huntera3621462009-09-22 16:44:42 -07001719 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001720 send_init_stream(host);
1721
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001722 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001723
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301724 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001725}
1726
1727static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1728{
Denis Karpov70a33412009-09-22 16:44:59 -07001729 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001730
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001731 if (!host->card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001732 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001733 return host->card_detect(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001734}
1735
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001736static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1737{
1738 struct omap_hsmmc_host *host = mmc_priv(mmc);
1739
Andreas Fenkart326119c2014-11-08 15:33:14 +01001740 if (mmc_pdata(host)->init_card)
1741 mmc_pdata(host)->init_card(card);
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001742}
1743
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001744static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1745{
1746 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001747 u32 irq_mask, con;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001748 unsigned long flags;
1749
1750 spin_lock_irqsave(&host->irq_lock, flags);
1751
Balaji T K5a52b082014-05-29 10:28:02 +02001752 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001753 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1754 if (enable) {
1755 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1756 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001757 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001758 } else {
1759 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1760 irq_mask &= ~CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001761 con &= ~(CTPL | CLKEXTFREE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001762 }
Balaji T K5a52b082014-05-29 10:28:02 +02001763 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001764 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1765
1766 /*
1767 * if enable, piggy back detection on current request
1768 * but always disable immediately
1769 */
1770 if (!host->req_in_progress || !enable)
1771 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1772
1773 /* flush posted write */
1774 OMAP_HSMMC_READ(host->base, IE);
1775
1776 spin_unlock_irqrestore(&host->irq_lock, flags);
1777}
1778
1779static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1780{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001781 int ret;
1782
1783 /*
1784 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1785 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1786 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1787 * with functional clock disabled.
1788 */
1789 if (!host->dev->of_node || !host->wake_irq)
1790 return -ENODEV;
1791
Tony Lindgren5b83b222015-05-21 15:51:52 -07001792 ret = dev_pm_set_dedicated_wake_irq(host->dev, host->wake_irq);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001793 if (ret) {
1794 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1795 goto err;
1796 }
1797
1798 /*
1799 * Some omaps don't have wake-up path from deeper idle states
1800 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1801 */
1802 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001803 struct pinctrl *p = devm_pinctrl_get(host->dev);
1804 if (!p) {
1805 ret = -ENODEV;
1806 goto err_free_irq;
1807 }
1808 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1809 dev_info(host->dev, "missing default pinctrl state\n");
1810 devm_pinctrl_put(p);
1811 ret = -EINVAL;
1812 goto err_free_irq;
1813 }
1814
1815 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1816 dev_info(host->dev, "missing idle pinctrl state\n");
1817 devm_pinctrl_put(p);
1818 ret = -EINVAL;
1819 goto err_free_irq;
1820 }
1821 devm_pinctrl_put(p);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001822 }
1823
Balaji T K5a52b082014-05-29 10:28:02 +02001824 OMAP_HSMMC_WRITE(host->base, HCTL,
1825 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001826 return 0;
1827
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001828err_free_irq:
Tony Lindgren5b83b222015-05-21 15:51:52 -07001829 dev_pm_clear_wake_irq(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001830err:
1831 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1832 host->wake_irq = 0;
1833 return ret;
1834}
1835
Denis Karpov70a33412009-09-22 16:44:59 -07001836static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001837{
1838 u32 hctl, capa, value;
1839
1840 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301841 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001842 hctl = SDVS30;
1843 capa = VS30 | VS18;
1844 } else {
1845 hctl = SDVS18;
1846 capa = VS18;
1847 }
1848
1849 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1850 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1851
1852 value = OMAP_HSMMC_READ(host->base, CAPA);
1853 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1854
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001855 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001856 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001857}
1858
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07001859static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1860 unsigned int direction, int blk_size)
1861{
1862 /* This controller can't do multiblock reads due to hw bugs */
1863 if (direction == MMC_DATA_READ)
1864 return 1;
1865
1866 return blk_size;
1867}
1868
1869static struct mmc_host_ops omap_hsmmc_ops = {
Per Forlin9782aff2011-07-01 18:55:23 +02001870 .post_req = omap_hsmmc_post_req,
1871 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001872 .request = omap_hsmmc_request,
1873 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001874 .get_cd = omap_hsmmc_get_cd,
Andreas Fenkarta49d8352015-03-03 13:28:14 +01001875 .get_ro = mmc_gpio_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001876 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001877 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001878};
1879
Denis Karpovd900f712009-09-22 16:44:38 -07001880#ifdef CONFIG_DEBUG_FS
1881
Denis Karpov70a33412009-09-22 16:44:59 -07001882static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001883{
1884 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001885 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001886
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001887 seq_printf(s, "mmc%d:\n", mmc->index);
1888 seq_printf(s, "sdio irq mode\t%s\n",
1889 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1890
1891 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1892 seq_printf(s, "sdio irq \t%s\n",
1893 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1894 : "disabled");
1895 }
1896 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001897
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301898 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001899 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001900 seq_printf(s, "CON:\t\t0x%08x\n",
1901 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001902 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1903 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001904 seq_printf(s, "HCTL:\t\t0x%08x\n",
1905 OMAP_HSMMC_READ(host->base, HCTL));
1906 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1907 OMAP_HSMMC_READ(host->base, SYSCTL));
1908 seq_printf(s, "IE:\t\t0x%08x\n",
1909 OMAP_HSMMC_READ(host->base, IE));
1910 seq_printf(s, "ISE:\t\t0x%08x\n",
1911 OMAP_HSMMC_READ(host->base, ISE));
1912 seq_printf(s, "CAPA:\t\t0x%08x\n",
1913 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001914
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301915 pm_runtime_mark_last_busy(host->dev);
1916 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001917
Denis Karpovd900f712009-09-22 16:44:38 -07001918 return 0;
1919}
1920
Denis Karpov70a33412009-09-22 16:44:59 -07001921static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001922{
Denis Karpov70a33412009-09-22 16:44:59 -07001923 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001924}
1925
1926static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001927 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001928 .read = seq_read,
1929 .llseek = seq_lseek,
1930 .release = single_release,
1931};
1932
Denis Karpov70a33412009-09-22 16:44:59 -07001933static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001934{
1935 if (mmc->debugfs_root)
1936 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1937 mmc, &mmc_regs_fops);
1938}
1939
1940#else
1941
Denis Karpov70a33412009-09-22 16:44:59 -07001942static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001943{
1944}
1945
1946#endif
1947
Rajendra Nayak46856a62012-03-12 20:32:37 +05301948#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001949static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1950 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1951 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1952};
1953
1954static const struct omap_mmc_of_data omap4_mmc_of_data = {
1955 .reg_offset = 0x100,
1956};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001957static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1958 .reg_offset = 0x100,
1959 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1960};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301961
1962static const struct of_device_id omap_mmc_of_match[] = {
1963 {
1964 .compatible = "ti,omap2-hsmmc",
1965 },
1966 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001967 .compatible = "ti,omap3-pre-es3-hsmmc",
1968 .data = &omap3_pre_es3_mmc_of_data,
1969 },
1970 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301971 .compatible = "ti,omap3-hsmmc",
1972 },
1973 {
1974 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001975 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301976 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001977 {
1978 .compatible = "ti,am33xx-hsmmc",
1979 .data = &am33xx_mmc_of_data,
1980 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301981 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001982};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301983MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1984
Andreas Fenkart551434382014-11-08 15:33:09 +01001985static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
Rajendra Nayak46856a62012-03-12 20:32:37 +05301986{
Andreas Fenkart551434382014-11-08 15:33:09 +01001987 struct omap_hsmmc_platform_data *pdata;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301988 struct device_node *np = dev->of_node;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301989
1990 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1991 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301992 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301993
1994 if (of_find_property(np, "ti,dual-volt", NULL))
1995 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1996
Andreas Fenkartb7a56462015-03-20 15:53:54 +01001997 pdata->gpio_cd = -EINVAL;
1998 pdata->gpio_cod = -EINVAL;
NeilBrownfdb9de12015-01-13 08:23:18 +13001999 pdata->gpio_wp = -EINVAL;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302000
2001 if (of_find_property(np, "ti,non-removable", NULL)) {
Andreas Fenkart326119c2014-11-08 15:33:14 +01002002 pdata->nonremovable = true;
2003 pdata->no_regulator_off_init = true;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302004 }
Rajendra Nayak46856a62012-03-12 20:32:37 +05302005
2006 if (of_find_property(np, "ti,needs-special-reset", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002007 pdata->features |= HSMMC_HAS_UPDATED_RESET;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302008
Hebbar, Gururajacd587092012-11-19 21:59:58 +05302009 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002010 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
Hebbar, Gururajacd587092012-11-19 21:59:58 +05302011
Rajendra Nayak46856a62012-03-12 20:32:37 +05302012 return pdata;
2013}
2014#else
Andreas Fenkart551434382014-11-08 15:33:09 +01002015static inline struct omap_hsmmc_platform_data
Rajendra Nayak46856a62012-03-12 20:32:37 +05302016 *of_get_hsmmc_pdata(struct device *dev)
2017{
Balaji T K19df45b2014-02-28 19:08:18 +05302018 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05302019}
2020#endif
2021
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05002022static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002023{
Andreas Fenkart551434382014-11-08 15:33:09 +01002024 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002025 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07002026 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002027 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002028 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302029 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01002030 dma_cap_mask_t mask;
2031 unsigned tx_req, rx_req;
Nishanth Menon59445b12014-02-13 23:45:48 -06002032 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05302033 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302034
2035 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
2036 if (match) {
2037 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01002038
2039 if (IS_ERR(pdata))
2040 return PTR_ERR(pdata);
2041
Rajendra Nayak46856a62012-03-12 20:32:37 +05302042 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06002043 data = match->data;
2044 pdata->reg_offset = data->reg_offset;
2045 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302046 }
2047 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002048
2049 if (pdata == NULL) {
2050 dev_err(&pdev->dev, "Platform Data is missing\n");
2051 return -ENXIO;
2052 }
2053
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002054 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2055 irq = platform_get_irq(pdev, 0);
2056 if (res == NULL || irq < 0)
2057 return -ENXIO;
2058
Balaji T K77fae212014-05-09 22:16:51 +05302059 base = devm_ioremap_resource(&pdev->dev, res);
2060 if (IS_ERR(base))
2061 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002062
Denis Karpov70a33412009-09-22 16:44:59 -07002063 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002064 if (!mmc) {
2065 ret = -ENOMEM;
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002066 goto err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002067 }
2068
NeilBrownfdb9de12015-01-13 08:23:18 +13002069 ret = mmc_of_parse(mmc);
2070 if (ret)
2071 goto err1;
2072
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002073 host = mmc_priv(mmc);
2074 host->mmc = mmc;
2075 host->pdata = pdata;
2076 host->dev = &pdev->dev;
2077 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002078 host->dma_ch = -1;
2079 host->irq = irq;
Balaji T Kfc307df2012-04-02 12:26:47 +05302080 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05302081 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08002082 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002083 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05302084 host->pbias_enabled = 0;
Kishon Vijay Abraham I3f77f702015-08-27 14:44:04 +05302085 host->vqmmc_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002086
NeilBrown41afa3142015-01-13 08:23:18 +13002087 ret = omap_hsmmc_gpio_init(mmc, host, pdata);
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002088 if (ret)
2089 goto err_gpio;
2090
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002091 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002092
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002093 if (pdev->dev.of_node)
2094 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2095
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302096 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002097
Daniel Mackd418ed82012-02-19 13:20:33 +01002098 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2099
2100 if (pdata->max_freq > 0)
2101 mmc->f_max = pdata->max_freq;
NeilBrownfdb9de12015-01-13 08:23:18 +13002102 else if (mmc->f_max == 0)
Daniel Mackd418ed82012-02-19 13:20:33 +01002103 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002104
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002105 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002106
Balaji T K96181952014-05-09 22:16:48 +05302107 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002108 if (IS_ERR(host->fclk)) {
2109 ret = PTR_ERR(host->fclk);
2110 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002111 goto err1;
2112 }
2113
Paul Walmsley9b682562011-10-06 14:50:35 -06002114 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2115 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 -07002116 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
Paul Walmsley9b682562011-10-06 14:50:35 -06002117 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002118
Tony Lindgren5b83b222015-05-21 15:51:52 -07002119 device_init_wakeup(&pdev->dev, true);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302120 pm_runtime_enable(host->dev);
2121 pm_runtime_get_sync(host->dev);
2122 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2123 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002124
Balaji T K92a3aeb2012-02-24 21:14:34 +05302125 omap_hsmmc_context_save(host);
2126
Balaji T K96181952014-05-09 22:16:48 +05302127 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302128 /*
2129 * MMC can still work without debounce clock.
2130 */
2131 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302132 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302133 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302134 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302135 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002136 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002137
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002138 /* Since we do only SG emulation, we can have as many segs
2139 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002140 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002141
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002142 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2143 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2144 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2145 mmc->max_seg_size = mmc->max_req_size;
2146
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002147 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002148 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002149
Andreas Fenkart326119c2014-11-08 15:33:14 +01002150 mmc->caps |= mmc_pdata(host)->caps;
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002151 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002152 mmc->caps |= MMC_CAP_4_BIT_DATA;
2153
Andreas Fenkart326119c2014-11-08 15:33:14 +01002154 if (mmc_pdata(host)->nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002155 mmc->caps |= MMC_CAP_NONREMOVABLE;
2156
NeilBrownfdb9de12015-01-13 08:23:18 +13002157 mmc->pm_caps |= mmc_pdata(host)->pm_caps;
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002158
Denis Karpov70a33412009-09-22 16:44:59 -07002159 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002160
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302161 if (!pdev->dev.of_node) {
2162 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2163 if (!res) {
2164 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2165 ret = -ENXIO;
2166 goto err_irq;
2167 }
2168 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002169
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302170 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2171 if (!res) {
2172 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2173 ret = -ENXIO;
2174 goto err_irq;
2175 }
2176 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002177 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002178
Russell King26b88522012-04-13 12:27:37 +01002179 dma_cap_zero(mask);
2180 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002181
Matt Porterd272fbf2013-05-10 17:42:34 +05302182 host->rx_chan =
2183 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2184 &rx_req, &pdev->dev, "rx");
2185
Russell King26b88522012-04-13 12:27:37 +01002186 if (!host->rx_chan) {
2187 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002188 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002189 goto err_irq;
2190 }
2191
Matt Porterd272fbf2013-05-10 17:42:34 +05302192 host->tx_chan =
2193 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2194 &tx_req, &pdev->dev, "tx");
2195
Russell King26b88522012-04-13 12:27:37 +01002196 if (!host->tx_chan) {
2197 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002198 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002199 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002200 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002201
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002202 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302203 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002204 mmc_hostname(mmc), host);
2205 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302206 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002207 goto err_irq;
2208 }
2209
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02002210 if (omap_hsmmc_have_reg()) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002211 ret = omap_hsmmc_reg_get(host);
2212 if (ret)
Andreas Fenkartbb09d152014-11-08 15:33:11 +01002213 goto err_irq;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002214 }
2215
Andreas Fenkart326119c2014-11-08 15:33:14 +01002216 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002217
Adrian Hunterb4175772010-05-26 14:42:06 -07002218 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002219
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002220 /*
2221 * For now, only support SDIO interrupt if we have a separate
2222 * wake-up interrupt configured from device tree. This is because
2223 * the wake-up interrupt is needed for idle state and some
2224 * platforms need special quirks. And we don't want to add new
2225 * legacy mux platform init code callbacks any longer as we
2226 * are moving to DT based booting anyways.
2227 */
2228 ret = omap_hsmmc_configure_wake_irq(host);
2229 if (!ret)
2230 mmc->caps |= MMC_CAP_SDIO_IRQ;
2231
Adrian Hunterb62f6222009-09-22 16:45:01 -07002232 omap_hsmmc_protect_card(host);
2233
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002234 mmc_add_host(mmc);
2235
Andreas Fenkart326119c2014-11-08 15:33:14 +01002236 if (mmc_pdata(host)->name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002237 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2238 if (ret < 0)
2239 goto err_slot_name;
2240 }
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002241 if (host->get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002242 ret = device_create_file(&mmc->class_dev,
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002243 &dev_attr_cover_switch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002244 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002245 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002246 }
2247
Denis Karpov70a33412009-09-22 16:44:59 -07002248 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302249 pm_runtime_mark_last_busy(host->dev);
2250 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002251
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002252 return 0;
2253
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002254err_slot_name:
2255 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002256err_irq:
Tony Lindgren5b83b222015-05-21 15:51:52 -07002257 device_init_wakeup(&pdev->dev, false);
Russell Kingc5c98922012-04-13 12:14:39 +01002258 if (host->tx_chan)
2259 dma_release_channel(host->tx_chan);
2260 if (host->rx_chan)
2261 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302262 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002263 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302264 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302265 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002266err1:
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002267err_gpio:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002268 mmc_free_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002269err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002270 return ret;
2271}
2272
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002273static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002274{
Denis Karpov70a33412009-09-22 16:44:59 -07002275 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002276
Felipe Balbi927ce942012-03-14 11:18:27 +02002277 pm_runtime_get_sync(host->dev);
2278 mmc_remove_host(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002279
Russell Kingc5c98922012-04-13 12:14:39 +01002280 if (host->tx_chan)
2281 dma_release_channel(host->tx_chan);
2282 if (host->rx_chan)
2283 dma_release_channel(host->rx_chan);
2284
Felipe Balbi927ce942012-03-14 11:18:27 +02002285 pm_runtime_put_sync(host->dev);
2286 pm_runtime_disable(host->dev);
Tony Lindgren5b83b222015-05-21 15:51:52 -07002287 device_init_wakeup(&pdev->dev, false);
Balaji T K96181952014-05-09 22:16:48 +05302288 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302289 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002290
Balaji T K9d1f0282012-10-15 21:35:07 +05302291 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002292
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002293 return 0;
2294}
2295
Russ Dill3d3bbfb2015-02-27 13:24:34 +02002296#ifdef CONFIG_PM_SLEEP
Kevin Hilmana791daa2010-05-26 14:42:07 -07002297static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002298{
Felipe Balbi927ce942012-03-14 11:18:27 +02002299 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2300
2301 if (!host)
2302 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002303
Felipe Balbi927ce942012-03-14 11:18:27 +02002304 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002305
2306 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002307 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2308 OMAP_HSMMC_WRITE(host->base, IE, 0);
2309 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002310 OMAP_HSMMC_WRITE(host->base, HCTL,
2311 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2312 }
2313
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302314 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302315 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002316
Eliad Peller31f9d462011-11-22 16:02:17 +02002317 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002318 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002319}
2320
2321/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002322static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002323{
Felipe Balbi927ce942012-03-14 11:18:27 +02002324 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2325
2326 if (!host)
2327 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002328
Felipe Balbi927ce942012-03-14 11:18:27 +02002329 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002330
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302331 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302332 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002333
Felipe Balbi927ce942012-03-14 11:18:27 +02002334 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2335 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002336
Felipe Balbi927ce942012-03-14 11:18:27 +02002337 omap_hsmmc_protect_card(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002338 pm_runtime_mark_last_busy(host->dev);
2339 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002340 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002341}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002342#endif
2343
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302344static int omap_hsmmc_runtime_suspend(struct device *dev)
2345{
2346 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002347 unsigned long flags;
Andreas Fenkartf9459012014-05-29 10:28:03 +02002348 int ret = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302349
2350 host = platform_get_drvdata(to_platform_device(dev));
2351 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002352 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302353
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002354 spin_lock_irqsave(&host->irq_lock, flags);
2355 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2356 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2357 /* disable sdio irq handling to prevent race */
2358 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2359 OMAP_HSMMC_WRITE(host->base, IE, 0);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002360
2361 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2362 /*
2363 * dat1 line low, pending sdio irq
2364 * race condition: possible irq handler running on
2365 * multi-core, abort
2366 */
2367 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2368 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2369 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2370 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2371 pm_runtime_mark_last_busy(dev);
2372 ret = -EBUSY;
2373 goto abort;
2374 }
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002375
Andreas Fenkart97978a42014-05-29 10:28:04 +02002376 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002377 } else {
2378 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002379 }
Andreas Fenkart97978a42014-05-29 10:28:04 +02002380
Andreas Fenkartf9459012014-05-29 10:28:03 +02002381abort:
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002382 spin_unlock_irqrestore(&host->irq_lock, flags);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002383 return ret;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302384}
2385
2386static int omap_hsmmc_runtime_resume(struct device *dev)
2387{
2388 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002389 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302390
2391 host = platform_get_drvdata(to_platform_device(dev));
2392 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002393 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302394
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002395 spin_lock_irqsave(&host->irq_lock, flags);
2396 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2397 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002398
Andreas Fenkart97978a42014-05-29 10:28:04 +02002399 pinctrl_pm_select_default_state(host->dev);
2400
2401 /* irq lost, if pinmux incorrect */
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002402 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2403 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2404 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002405 } else {
2406 pinctrl_pm_select_default_state(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002407 }
2408 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302409 return 0;
2410}
2411
Kevin Hilmana791daa2010-05-26 14:42:07 -07002412static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Russ Dill3d3bbfb2015-02-27 13:24:34 +02002413 SET_SYSTEM_SLEEP_PM_OPS(omap_hsmmc_suspend, omap_hsmmc_resume)
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302414 .runtime_suspend = omap_hsmmc_runtime_suspend,
2415 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002416};
2417
2418static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002419 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002420 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002421 .driver = {
2422 .name = DRIVER_NAME,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002423 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302424 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002425 },
2426};
2427
Felipe Balbib7964502012-03-14 11:18:32 +02002428module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002429MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2430MODULE_LICENSE("GPL");
2431MODULE_ALIAS("platform:" DRIVER_NAME);
2432MODULE_AUTHOR("Texas Instruments Inc");