blob: ad3a2acd366499e94ecbf41f56159b54a70246bd [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>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010039#include <linux/io.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020040#include <linux/irq.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080041#include <linux/gpio.h>
42#include <linux/regulator/consumer.h>
Daniel Mack46b76032012-10-15 21:35:05 +053043#include <linux/pinctrl/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053044#include <linux/pm_runtime.h>
Tony Lindgren68f39e72012-10-15 12:09:43 -070045#include <linux/platform_data/mmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010046
47/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070048#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010049#define OMAP_HSMMC_CON 0x002C
Balaji T Ka2e77152014-01-21 19:54:42 +053050#define OMAP_HSMMC_SDMASA 0x0100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010051#define OMAP_HSMMC_BLK 0x0104
52#define OMAP_HSMMC_ARG 0x0108
53#define OMAP_HSMMC_CMD 0x010C
54#define OMAP_HSMMC_RSP10 0x0110
55#define OMAP_HSMMC_RSP32 0x0114
56#define OMAP_HSMMC_RSP54 0x0118
57#define OMAP_HSMMC_RSP76 0x011C
58#define OMAP_HSMMC_DATA 0x0120
Andreas Fenkartbb0635f2014-05-29 10:28:01 +020059#define OMAP_HSMMC_PSTATE 0x0124
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010060#define OMAP_HSMMC_HCTL 0x0128
61#define OMAP_HSMMC_SYSCTL 0x012C
62#define OMAP_HSMMC_STAT 0x0130
63#define OMAP_HSMMC_IE 0x0134
64#define OMAP_HSMMC_ISE 0x0138
Balaji T Ka2e77152014-01-21 19:54:42 +053065#define OMAP_HSMMC_AC12 0x013C
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010066#define OMAP_HSMMC_CAPA 0x0140
67
68#define VS18 (1 << 26)
69#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053070#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010071#define SDVS18 (0x5 << 9)
72#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080073#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010074#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010075#define SDVSCLR 0xFFFFF1FF
76#define SDVSDET 0x00000400
77#define AUTOIDLE 0x1
78#define SDBP (1 << 8)
79#define DTO 0xe
80#define ICE 0x1
81#define ICS 0x2
82#define CEN (1 << 2)
Balaji T Ked164182013-10-21 00:25:21 +053083#define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010084#define CLKD_MASK 0x0000FFC0
85#define CLKD_SHIFT 6
86#define DTO_MASK 0x000F0000
87#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010088#define INIT_STREAM (1 << 1)
Balaji T Ka2e77152014-01-21 19:54:42 +053089#define ACEN_ACMD23 (2 << 2)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010090#define DP_SELECT (1 << 21)
91#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053092#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010093#define MSBS (1 << 5)
94#define BCE (1 << 1)
95#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053096#define HSPE (1 << 2)
Balaji T K03b5d922012-04-09 12:08:33 +053097#define DDR (1 << 19)
Jarkko Lavinen73153012008-11-21 16:49:54 +020098#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010099#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100100#define STAT_CLEAR 0xFFFFFFFF
101#define INIT_STREAM_CMD 0x00000000
102#define DUAL_VOLT_OCR_BIT 7
103#define SRC (1 << 25)
104#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700105#define SOFTRESET (1 << 1)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100106
Venkatraman Sa7e96872012-11-19 22:00:01 +0530107/* Interrupt masks for IE and ISE register */
108#define CC_EN (1 << 0)
109#define TC_EN (1 << 1)
110#define BWR_EN (1 << 4)
111#define BRR_EN (1 << 5)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200112#define CIRQ_EN (1 << 8)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530113#define ERR_EN (1 << 15)
114#define CTO_EN (1 << 16)
115#define CCRC_EN (1 << 17)
116#define CEB_EN (1 << 18)
117#define CIE_EN (1 << 19)
118#define DTO_EN (1 << 20)
119#define DCRC_EN (1 << 21)
120#define DEB_EN (1 << 22)
Balaji T Ka2e77152014-01-21 19:54:42 +0530121#define ACE_EN (1 << 24)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530122#define CERR_EN (1 << 28)
123#define BADA_EN (1 << 29)
124
Balaji T Ka2e77152014-01-21 19:54:42 +0530125#define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
Venkatraman Sa7e96872012-11-19 22:00:01 +0530126 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
127 BRR_EN | BWR_EN | TC_EN | CC_EN)
128
Balaji T Ka2e77152014-01-21 19:54:42 +0530129#define CNI (1 << 7)
130#define ACIE (1 << 4)
131#define ACEB (1 << 3)
132#define ACCE (1 << 2)
133#define ACTO (1 << 1)
134#define ACNE (1 << 0)
135
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530136#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530137#define MMC_TIMEOUT_MS 20 /* 20 mSec */
138#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400139#define OMAP_MMC_MIN_CLOCK 400000
140#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530141#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100142
Balaji T Ke99448f2014-02-19 20:26:40 +0530143#define VDD_1V8 1800000 /* 180000 uV */
144#define VDD_3V0 3000000 /* 300000 uV */
145#define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
146
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100147/*
148 * One controller can have multiple slots, like on some omap boards using
149 * omap.c controller driver. Luckily this is not currently done on any known
150 * omap_hsmmc.c device.
151 */
152#define mmc_slot(host) (host->pdata->slots[host->slot_id])
153
154/*
155 * MMC Host controller read/write API's
156 */
157#define OMAP_HSMMC_READ(base, reg) \
158 __raw_readl((base) + OMAP_HSMMC_##reg)
159
160#define OMAP_HSMMC_WRITE(base, reg, val) \
161 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
162
Per Forlin9782aff2011-07-01 18:55:23 +0200163struct omap_hsmmc_next {
164 unsigned int dma_len;
165 s32 cookie;
166};
167
Denis Karpov70a33412009-09-22 16:44:59 -0700168struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100169 struct device *dev;
170 struct mmc_host *mmc;
171 struct mmc_request *mrq;
172 struct mmc_command *cmd;
173 struct mmc_data *data;
174 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100175 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800176 /*
177 * vcc == configured supply
178 * vcc_aux == optional
179 * - MMC1, supply for DAT4..DAT7
180 * - MMC2/MMC2, external level shifter voltage supply, for
181 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
182 */
183 struct regulator *vcc;
184 struct regulator *vcc_aux;
Balaji T Ke99448f2014-02-19 20:26:40 +0530185 struct regulator *pbias;
186 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100187 void __iomem *base;
188 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;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100204 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200205 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700206 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700207 int protect_card;
208 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800209 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700210 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530211 unsigned long clk_rate;
Balaji T Ka2e77152014-01-21 19:54:42 +0530212 unsigned int flags;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200213#define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
214#define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
215#define HSMMC_WAKE_IRQ_ENABLED (1 << 2)
Per Forlin9782aff2011-07-01 18:55:23 +0200216 struct omap_hsmmc_next next_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100217 struct omap_mmc_platform_data *pdata;
218};
219
Nishanth Menon59445b12014-02-13 23:45:48 -0600220struct omap_mmc_of_data {
221 u32 reg_offset;
222 u8 controller_flags;
223};
224
Balaji T Kbf129e12014-01-21 19:54:42 +0530225static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
226
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800227static int omap_hsmmc_card_detect(struct device *dev, int slot)
228{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530229 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
230 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800231
232 /* NOTE: assumes card detect signal is active-low */
233 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
234}
235
236static int omap_hsmmc_get_wp(struct device *dev, int slot)
237{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530238 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
239 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800240
241 /* NOTE: assumes write protect signal is active-high */
242 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
243}
244
245static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
246{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530247 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
248 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800249
250 /* NOTE: assumes card detect signal is active-low */
251 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
252}
253
254#ifdef CONFIG_PM
255
256static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
257{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530258 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
259 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800260
261 disable_irq(mmc->slots[0].card_detect_irq);
262 return 0;
263}
264
265static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
266{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530267 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
268 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800269
270 enable_irq(mmc->slots[0].card_detect_irq);
271 return 0;
272}
273
274#else
275
276#define omap_hsmmc_suspend_cdirq NULL
277#define omap_hsmmc_resume_cdirq NULL
278
279#endif
280
Adrian Hunterb702b102010-02-15 10:03:35 -0800281#ifdef CONFIG_REGULATOR
282
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500283static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800284 int vdd)
285{
286 struct omap_hsmmc_host *host =
287 platform_get_drvdata(to_platform_device(dev));
288 int ret = 0;
289
290 /*
291 * If we don't see a Vcc regulator, assume it's a fixed
292 * voltage always-on regulator.
293 */
294 if (!host->vcc)
295 return 0;
296
297 if (mmc_slot(host).before_set_reg)
298 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
299
Balaji T Ke99448f2014-02-19 20:26:40 +0530300 if (host->pbias) {
301 if (host->pbias_enabled == 1) {
302 ret = regulator_disable(host->pbias);
303 if (!ret)
304 host->pbias_enabled = 0;
305 }
306 regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
307 }
308
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800309 /*
310 * Assume Vcc regulator is used only to power the card ... OMAP
311 * VDDS is used to power the pins, optionally with a transceiver to
312 * support cards using voltages other than VDDS (1.8V nominal). When a
313 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
314 *
315 * In some cases this regulator won't support enable/disable;
316 * e.g. it's a fixed rail for a WLAN chip.
317 *
318 * In other cases vcc_aux switches interface power. Example, for
319 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
320 * chips/cards need an interface voltage rail too.
321 */
322 if (power_on) {
Balaji T K987fd492014-02-19 20:26:40 +0530323 if (host->vcc)
324 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800325 /* Enable interface voltage rail, if needed */
326 if (ret == 0 && host->vcc_aux) {
327 ret = regulator_enable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530328 if (ret < 0 && host->vcc)
Linus Walleij99fc5132010-09-29 01:08:27 -0400329 ret = mmc_regulator_set_ocr(host->mmc,
330 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800331 }
332 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400333 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800334 if (host->vcc_aux)
335 ret = regulator_disable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530336 if (host->vcc) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400337 /* Then proceed to shut down the local regulator */
338 ret = mmc_regulator_set_ocr(host->mmc,
339 host->vcc, 0);
340 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800341 }
342
Balaji T Ke99448f2014-02-19 20:26:40 +0530343 if (host->pbias) {
344 if (vdd <= VDD_165_195)
345 ret = regulator_set_voltage(host->pbias, VDD_1V8,
346 VDD_1V8);
347 else
348 ret = regulator_set_voltage(host->pbias, VDD_3V0,
349 VDD_3V0);
350 if (ret < 0)
351 goto error_set_power;
352
353 if (host->pbias_enabled == 0) {
354 ret = regulator_enable(host->pbias);
355 if (!ret)
356 host->pbias_enabled = 1;
357 }
358 }
359
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800360 if (mmc_slot(host).after_set_reg)
361 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
362
Balaji T Ke99448f2014-02-19 20:26:40 +0530363error_set_power:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800364 return ret;
365}
366
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800367static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
368{
369 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700370 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800371
Balaji T Kf2ddc1d2014-02-19 20:26:40 +0530372 reg = devm_regulator_get(host->dev, "vmmc");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800373 if (IS_ERR(reg)) {
Balaji T K987fd492014-02-19 20:26:40 +0530374 dev_err(host->dev, "unable to get vmmc regulator %ld\n",
375 PTR_ERR(reg));
NeilBrown1fdc90f2012-08-08 00:06:00 -0400376 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800377 } else {
378 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700379 ocr_value = mmc_regulator_get_ocrmask(reg);
380 if (!mmc_slot(host).ocr_mask) {
381 mmc_slot(host).ocr_mask = ocr_value;
382 } else {
383 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530384 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500385 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700386 mmc_slot(host).ocr_mask = 0;
387 return -EINVAL;
388 }
389 }
Balaji T K987fd492014-02-19 20:26:40 +0530390 }
391 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800392
Balaji T K987fd492014-02-19 20:26:40 +0530393 /* Allow an aux regulator */
394 reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
395 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800396
Balaji T Ke99448f2014-02-19 20:26:40 +0530397 reg = devm_regulator_get_optional(host->dev, "pbias");
398 host->pbias = IS_ERR(reg) ? NULL : reg;
399
Balaji T K987fd492014-02-19 20:26:40 +0530400 /* For eMMC do not power off when not in sleep state */
401 if (mmc_slot(host).no_regulator_off_init)
402 return 0;
403 /*
404 * To disable boot_on regulator, enable regulator
405 * to increase usecount and then disable it.
406 */
407 if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
408 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
409 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
Adrian Huntere840ce12011-05-06 12:14:10 +0300410
Balaji T K987fd492014-02-19 20:26:40 +0530411 mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
412 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800413 }
414
415 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800416}
417
418static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
419{
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800420 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800421}
422
Adrian Hunterb702b102010-02-15 10:03:35 -0800423static inline int omap_hsmmc_have_reg(void)
424{
425 return 1;
426}
427
428#else
429
430static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
431{
432 return -EINVAL;
433}
434
435static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
436{
437}
438
439static inline int omap_hsmmc_have_reg(void)
440{
441 return 0;
442}
443
444#endif
445
446static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
447{
448 int ret;
449
450 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800451 if (pdata->slots[0].cover)
452 pdata->slots[0].get_cover_state =
453 omap_hsmmc_get_cover_state;
454 else
455 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
456 pdata->slots[0].card_detect_irq =
457 gpio_to_irq(pdata->slots[0].switch_pin);
458 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
459 if (ret)
460 return ret;
461 ret = gpio_direction_input(pdata->slots[0].switch_pin);
462 if (ret)
463 goto err_free_sp;
464 } else
465 pdata->slots[0].switch_pin = -EINVAL;
466
467 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
468 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
469 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
470 if (ret)
471 goto err_free_cd;
472 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
473 if (ret)
474 goto err_free_wp;
475 } else
476 pdata->slots[0].gpio_wp = -EINVAL;
477
478 return 0;
479
480err_free_wp:
481 gpio_free(pdata->slots[0].gpio_wp);
482err_free_cd:
483 if (gpio_is_valid(pdata->slots[0].switch_pin))
484err_free_sp:
485 gpio_free(pdata->slots[0].switch_pin);
486 return ret;
487}
488
489static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
490{
491 if (gpio_is_valid(pdata->slots[0].gpio_wp))
492 gpio_free(pdata->slots[0].gpio_wp);
493 if (gpio_is_valid(pdata->slots[0].switch_pin))
494 gpio_free(pdata->slots[0].switch_pin);
495}
496
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100497/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300498 * Start clock to the card
499 */
500static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
501{
502 OMAP_HSMMC_WRITE(host->base, SYSCTL,
503 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
504}
505
506/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100507 * Stop clock to the card
508 */
Denis Karpov70a33412009-09-22 16:44:59 -0700509static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100510{
511 OMAP_HSMMC_WRITE(host->base, SYSCTL,
512 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
513 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900514 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100515}
516
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700517static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
518 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700519{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200520 u32 irq_mask = INT_EN_MASK;
521 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700522
523 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200524 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700525
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700526 /* Disable timeout for erases */
527 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530528 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700529
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200530 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700531 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
532 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200533
534 /* latch pending CIRQ, but don't signal MMC core */
535 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
536 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700537 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200538 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700539}
540
541static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
542{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200543 u32 irq_mask = 0;
544 unsigned long flags;
545
546 spin_lock_irqsave(&host->irq_lock, flags);
547 /* no transfer running but need to keep cirq if enabled */
548 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
549 irq_mask |= CIRQ_EN;
550 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
551 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700552 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200553 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700554}
555
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300556/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530557static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300558{
559 u16 dsor = 0;
560
561 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530562 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530563 if (dsor > CLKD_MAX)
564 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300565 }
566
567 return dsor;
568}
569
Andy Shevchenko5934df22011-05-06 12:14:06 +0300570static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
571{
572 struct mmc_ios *ios = &host->mmc->ios;
573 unsigned long regval;
574 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530575 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300576
Venkatraman S8986d312012-08-07 19:10:38 +0530577 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300578
579 omap_hsmmc_stop_clock(host);
580
581 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
582 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530583 clkdiv = calc_divisor(host, ios);
584 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300585 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
586 OMAP_HSMMC_WRITE(host->base, SYSCTL,
587 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
588
589 /* Wait till the ICS bit is set */
590 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
591 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
592 && time_before(jiffies, timeout))
593 cpu_relax();
594
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530595 /*
596 * Enable High-Speed Support
597 * Pre-Requisites
598 * - Controller should support High-Speed-Enable Bit
599 * - Controller should not be using DDR Mode
600 * - Controller should advertise that it supports High Speed
601 * in capabilities register
602 * - MMC/SD clock coming out of controller > 25MHz
603 */
604 if ((mmc_slot(host).features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900605 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530606 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
607 regval = OMAP_HSMMC_READ(host->base, HCTL);
608 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
609 regval |= HSPE;
610 else
611 regval &= ~HSPE;
612
613 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
614 }
615
Andy Shevchenko5934df22011-05-06 12:14:06 +0300616 omap_hsmmc_start_clock(host);
617}
618
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400619static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
620{
621 struct mmc_ios *ios = &host->mmc->ios;
622 u32 con;
623
624 con = OMAP_HSMMC_READ(host->base, CON);
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900625 if (ios->timing == MMC_TIMING_MMC_DDR52)
Balaji T K03b5d922012-04-09 12:08:33 +0530626 con |= DDR; /* configure in DDR mode */
627 else
628 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400629 switch (ios->bus_width) {
630 case MMC_BUS_WIDTH_8:
631 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
632 break;
633 case MMC_BUS_WIDTH_4:
634 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
635 OMAP_HSMMC_WRITE(host->base, HCTL,
636 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
637 break;
638 case MMC_BUS_WIDTH_1:
639 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
640 OMAP_HSMMC_WRITE(host->base, HCTL,
641 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
642 break;
643 }
644}
645
646static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
647{
648 struct mmc_ios *ios = &host->mmc->ios;
649 u32 con;
650
651 con = OMAP_HSMMC_READ(host->base, CON);
652 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
653 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
654 else
655 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
656}
657
Denis Karpov11dd62a2009-09-22 16:44:43 -0700658#ifdef CONFIG_PM
659
660/*
661 * Restore the MMC host context, if it was lost as result of a
662 * power state change.
663 */
Denis Karpov70a33412009-09-22 16:44:59 -0700664static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700665{
666 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400667 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700668 unsigned long timeout;
669
Tony Lindgren0a82e062013-10-21 00:25:19 +0530670 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
671 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
672 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
673 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
674 return 0;
675
676 host->context_loss++;
677
Balaji T Kc2200ef2012-03-07 09:55:30 -0500678 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700679 if (host->power_mode != MMC_POWER_OFF &&
680 (1 << ios->vdd) <= MMC_VDD_23_24)
681 hctl = SDVS18;
682 else
683 hctl = SDVS30;
684 capa = VS30 | VS18;
685 } else {
686 hctl = SDVS18;
687 capa = VS18;
688 }
689
690 OMAP_HSMMC_WRITE(host->base, HCTL,
691 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
692
693 OMAP_HSMMC_WRITE(host->base, CAPA,
694 OMAP_HSMMC_READ(host->base, CAPA) | capa);
695
696 OMAP_HSMMC_WRITE(host->base, HCTL,
697 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
698
699 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
700 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
701 && time_before(jiffies, timeout))
702 ;
703
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200704 OMAP_HSMMC_WRITE(host->base, ISE, 0);
705 OMAP_HSMMC_WRITE(host->base, IE, 0);
706 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700707
708 /* Do not initialize card-specific things if the power is off */
709 if (host->power_mode == MMC_POWER_OFF)
710 goto out;
711
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400712 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700713
Andy Shevchenko5934df22011-05-06 12:14:06 +0300714 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700715
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400716 omap_hsmmc_set_bus_mode(host);
717
Denis Karpov11dd62a2009-09-22 16:44:43 -0700718out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530719 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
720 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700721 return 0;
722}
723
724/*
725 * Save the MMC host context (store the number of power state changes so far).
726 */
Denis Karpov70a33412009-09-22 16:44:59 -0700727static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700728{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530729 host->con = OMAP_HSMMC_READ(host->base, CON);
730 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
731 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
732 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700733}
734
735#else
736
Denis Karpov70a33412009-09-22 16:44:59 -0700737static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700738{
739 return 0;
740}
741
Denis Karpov70a33412009-09-22 16:44:59 -0700742static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700743{
744}
745
746#endif
747
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100748/*
749 * Send init stream sequence to card
750 * before sending IDLE command
751 */
Denis Karpov70a33412009-09-22 16:44:59 -0700752static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100753{
754 int reg = 0;
755 unsigned long timeout;
756
Adrian Hunterb62f6222009-09-22 16:45:01 -0700757 if (host->protect_card)
758 return;
759
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100760 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700761
762 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100763 OMAP_HSMMC_WRITE(host->base, CON,
764 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
765 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
766
767 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530768 while ((reg != CC_EN) && time_before(jiffies, timeout))
769 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100770
771 OMAP_HSMMC_WRITE(host->base, CON,
772 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700773
774 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
775 OMAP_HSMMC_READ(host->base, STAT);
776
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100777 enable_irq(host->irq);
778}
779
780static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700781int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100782{
783 int r = 1;
784
Denis Karpov191d1f12009-09-22 16:44:55 -0700785 if (mmc_slot(host).get_cover_state)
786 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100787 return r;
788}
789
790static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700791omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100792 char *buf)
793{
794 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700795 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100796
Denis Karpov70a33412009-09-22 16:44:59 -0700797 return sprintf(buf, "%s\n",
798 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100799}
800
Denis Karpov70a33412009-09-22 16:44:59 -0700801static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100802
803static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700804omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100805 char *buf)
806{
807 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700808 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100809
Denis Karpov191d1f12009-09-22 16:44:55 -0700810 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100811}
812
Denis Karpov70a33412009-09-22 16:44:59 -0700813static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100814
815/*
816 * Configure the response type and send the cmd.
817 */
818static void
Denis Karpov70a33412009-09-22 16:44:59 -0700819omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100820 struct mmc_data *data)
821{
822 int cmdreg = 0, resptype = 0, cmdtype = 0;
823
Venkatraman S8986d312012-08-07 19:10:38 +0530824 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100825 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
826 host->cmd = cmd;
827
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700828 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100829
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200830 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100831 if (cmd->flags & MMC_RSP_PRESENT) {
832 if (cmd->flags & MMC_RSP_136)
833 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200834 else if (cmd->flags & MMC_RSP_BUSY) {
835 resptype = 3;
836 host->response_busy = 1;
837 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100838 resptype = 2;
839 }
840
841 /*
842 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
843 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
844 * a val of 0x3, rest 0x0.
845 */
846 if (cmd == host->mrq->stop)
847 cmdtype = 0x3;
848
849 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
850
Balaji T Ka2e77152014-01-21 19:54:42 +0530851 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
852 host->mrq->sbc) {
853 cmdreg |= ACEN_ACMD23;
854 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
855 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100856 if (data) {
857 cmdreg |= DP_SELECT | MSBS | BCE;
858 if (data->flags & MMC_DATA_READ)
859 cmdreg |= DDIR;
860 else
861 cmdreg &= ~(DDIR);
862 }
863
864 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530865 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100866
Adrian Hunterb4175772010-05-26 14:42:06 -0700867 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700868
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100869 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
870 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
871}
872
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200873static int
Denis Karpov70a33412009-09-22 16:44:59 -0700874omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200875{
876 if (data->flags & MMC_DATA_WRITE)
877 return DMA_TO_DEVICE;
878 else
879 return DMA_FROM_DEVICE;
880}
881
Russell Kingc5c98922012-04-13 12:14:39 +0100882static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
883 struct mmc_data *data)
884{
885 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
886}
887
Adrian Hunterb4175772010-05-26 14:42:06 -0700888static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
889{
890 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530891 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700892
Venkatraman S31463b12012-04-09 12:08:34 +0530893 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700894 host->req_in_progress = 0;
895 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530896 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700897
898 omap_hsmmc_disable_irq(host);
899 /* Do not complete the request if DMA is still in progress */
900 if (mrq->data && host->use_dma && dma_ch != -1)
901 return;
902 host->mrq = NULL;
903 mmc_request_done(host->mmc, mrq);
904}
905
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100906/*
907 * Notify the transfer complete to MMC core
908 */
909static void
Denis Karpov70a33412009-09-22 16:44:59 -0700910omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100911{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200912 if (!data) {
913 struct mmc_request *mrq = host->mrq;
914
Adrian Hunter23050102009-09-22 16:44:57 -0700915 /* TC before CC from CMD6 - don't know why, but it happens */
916 if (host->cmd && host->cmd->opcode == 6 &&
917 host->response_busy) {
918 host->response_busy = 0;
919 return;
920 }
921
Adrian Hunterb4175772010-05-26 14:42:06 -0700922 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200923 return;
924 }
925
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100926 host->data = NULL;
927
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100928 if (!data->error)
929 data->bytes_xfered += data->blocks * (data->blksz);
930 else
931 data->bytes_xfered = 0;
932
Balaji T Kbf129e12014-01-21 19:54:42 +0530933 if (data->stop && (data->error || !host->mrq->sbc))
934 omap_hsmmc_start_command(host, data->stop, NULL);
935 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700936 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100937}
938
939/*
940 * Notify the core about command completion
941 */
942static void
Denis Karpov70a33412009-09-22 16:44:59 -0700943omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100944{
Balaji T Kbf129e12014-01-21 19:54:42 +0530945 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +0530946 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +0530947 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +0530948 omap_hsmmc_start_dma_transfer(host);
949 omap_hsmmc_start_command(host, host->mrq->cmd,
950 host->mrq->data);
951 return;
952 }
953
Balaji T K2177fa92014-05-09 22:16:52 +0530954 host->cmd = NULL;
955
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100956 if (cmd->flags & MMC_RSP_PRESENT) {
957 if (cmd->flags & MMC_RSP_136) {
958 /* response type 2 */
959 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
960 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
961 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
962 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
963 } else {
964 /* response types 1, 1b, 3, 4, 5, 6 */
965 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
966 }
967 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700968 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530969 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100970}
971
972/*
973 * DMA clean up for command errors
974 */
Denis Karpov70a33412009-09-22 16:44:59 -0700975static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100976{
Adrian Hunterb4175772010-05-26 14:42:06 -0700977 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530978 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700979
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200980 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100981
Venkatraman S31463b12012-04-09 12:08:34 +0530982 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700983 dma_ch = host->dma_ch;
984 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530985 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700986
987 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100988 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
989
990 dmaengine_terminate_all(chan);
991 dma_unmap_sg(chan->device->dev,
992 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700993 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100994
Per Forlin053bf342011-11-07 21:55:11 +0530995 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100996 }
997 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100998}
999
1000/*
1001 * Readable error output
1002 */
1003#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +03001004static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001005{
1006 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -07001007 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +03001008 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
1009 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
1010 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
1011 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001012 };
1013 char res[256];
1014 char *buf = res;
1015 int len, i;
1016
1017 len = sprintf(buf, "MMC IRQ 0x%x :", status);
1018 buf += len;
1019
Denis Karpov70a33412009-09-22 16:44:59 -07001020 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001021 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -07001022 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001023 buf += len;
1024 }
1025
Venkatraman S8986d312012-08-07 19:10:38 +05301026 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001027}
Adrian Hunter699b9582011-05-06 12:14:01 +03001028#else
1029static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1030 u32 status)
1031{
1032}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001033#endif /* CONFIG_MMC_DEBUG */
1034
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001035/*
1036 * MMC controller internal state machines reset
1037 *
1038 * Used to reset command or data internal state machines, using respectively
1039 * SRC or SRD bit of SYSCTL register
1040 * Can be called from interrupt context
1041 */
Denis Karpov70a33412009-09-22 16:44:59 -07001042static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1043 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001044{
1045 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301046 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001047
1048 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1049 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1050
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001051 /*
1052 * OMAP4 ES2 and greater has an updated reset logic.
1053 * Monitor a 0->1 transition first
1054 */
1055 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001056 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001057 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301058 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001059 }
1060 i = 0;
1061
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001062 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1063 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301064 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001065
1066 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1067 dev_err(mmc_dev(host->mmc),
1068 "Timeout waiting on controller reset in %s\n",
1069 __func__);
1070}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001071
Balaji T K25e18972012-11-19 21:59:55 +05301072static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1073 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301074{
Balaji T K25e18972012-11-19 21:59:55 +05301075 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301076 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301077 if (host->cmd)
1078 host->cmd->error = err;
1079 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301080
1081 if (host->data) {
1082 omap_hsmmc_reset_controller_fsm(host, SRD);
1083 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301084 } else if (host->mrq && host->mrq->cmd)
1085 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301086}
1087
Adrian Hunterb4175772010-05-26 14:42:06 -07001088static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001089{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001090 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001091 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301092 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001093
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001094 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301095 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001096
Venkatraman Sa7e96872012-11-19 22:00:01 +05301097 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001098 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001099
Venkatraman Sa7e96872012-11-19 22:00:01 +05301100 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301101 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301102 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301103 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301104 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301105 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1106
Balaji T Ka2e77152014-01-21 19:54:42 +05301107 if (status & ACE_EN) {
1108 u32 ac12;
1109 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1110 if (!(ac12 & ACNE) && host->mrq->sbc) {
1111 end_cmd = 1;
1112 if (ac12 & ACTO)
1113 error = -ETIMEDOUT;
1114 else if (ac12 & (ACCE | ACEB | ACIE))
1115 error = -EILSEQ;
1116 host->mrq->sbc->error = error;
1117 hsmmc_command_incomplete(host, error, end_cmd);
1118 }
1119 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1120 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301121 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301122 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301123 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001124 }
1125 }
1126
Francesco Lavra7472bab2013-06-29 08:25:12 +02001127 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301128 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001129 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301130 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001131 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001132}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001133
Adrian Hunterb4175772010-05-26 14:42:06 -07001134/*
1135 * MMC controller IRQ handler
1136 */
1137static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1138{
1139 struct omap_hsmmc_host *host = dev_id;
1140 int status;
1141
1142 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001143 while (status & (INT_EN_MASK | CIRQ_EN)) {
1144 if (host->req_in_progress)
1145 omap_hsmmc_do_irq(host, status);
1146
1147 if (status & CIRQ_EN)
1148 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301149
Adrian Hunterb4175772010-05-26 14:42:06 -07001150 /* Flush posted write */
1151 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301152 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001153
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001154 return IRQ_HANDLED;
1155}
1156
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001157static irqreturn_t omap_hsmmc_wake_irq(int irq, void *dev_id)
1158{
1159 struct omap_hsmmc_host *host = dev_id;
1160
1161 /* cirq is level triggered, disable to avoid infinite loop */
1162 spin_lock(&host->irq_lock);
1163 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
1164 disable_irq_nosync(host->wake_irq);
1165 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
1166 }
1167 spin_unlock(&host->irq_lock);
1168 pm_request_resume(host->dev); /* no use counter */
1169
1170 return IRQ_HANDLED;
1171}
1172
Denis Karpov70a33412009-09-22 16:44:59 -07001173static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001174{
1175 unsigned long i;
1176
1177 OMAP_HSMMC_WRITE(host->base, HCTL,
1178 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1179 for (i = 0; i < loops_per_jiffy; i++) {
1180 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1181 break;
1182 cpu_relax();
1183 }
1184}
1185
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001186/*
David Brownelleb250822009-02-17 14:49:01 -08001187 * Switch MMC interface voltage ... only relevant for MMC1.
1188 *
1189 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1190 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1191 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001192 */
Denis Karpov70a33412009-09-22 16:44:59 -07001193static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001194{
1195 u32 reg_val = 0;
1196 int ret;
1197
1198 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301199 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301200 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301201 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001202
1203 /* Turn the power off */
1204 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001205
1206 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001207 if (!ret)
1208 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1209 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301210 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301211 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301212 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001213
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001214 if (ret != 0)
1215 goto err;
1216
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001217 OMAP_HSMMC_WRITE(host->base, HCTL,
1218 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1219 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001220
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001221 /*
1222 * If a MMC dual voltage card is detected, the set_ios fn calls
1223 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001224 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001225 *
David Brownelleb250822009-02-17 14:49:01 -08001226 * Cope with a bit of slop in the range ... per data sheets:
1227 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1228 * but recommended values are 1.71V to 1.89V
1229 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1230 * but recommended values are 2.7V to 3.3V
1231 *
1232 * Board setup code shouldn't permit anything very out-of-range.
1233 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1234 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001235 */
David Brownelleb250822009-02-17 14:49:01 -08001236 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001237 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001238 else
1239 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001240
1241 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001242 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001243
1244 return 0;
1245err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301246 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001247 return ret;
1248}
1249
Adrian Hunterb62f6222009-09-22 16:45:01 -07001250/* Protect the card while the cover is open */
1251static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1252{
1253 if (!mmc_slot(host).get_cover_state)
1254 return;
1255
1256 host->reqs_blocked = 0;
1257 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1258 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301259 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001260 "card is now accessible\n",
1261 mmc_hostname(host->mmc));
1262 host->protect_card = 0;
1263 }
1264 } else {
1265 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301266 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001267 "card is now inaccessible\n",
1268 mmc_hostname(host->mmc));
1269 host->protect_card = 1;
1270 }
1271 }
1272}
1273
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001274/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001275 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001276 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001277static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001278{
NeilBrown7efab4f2011-12-30 12:35:13 +11001279 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001280 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001281 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001282
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001283 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001284
Denis Karpov191d1f12009-09-22 16:44:55 -07001285 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001286 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001287 else {
1288 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001289 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001290 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001291
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001292 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001293 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001294 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001295 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001296 return IRQ_HANDLED;
1297}
1298
Russell Kingc5c98922012-04-13 12:14:39 +01001299static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001300{
Russell Kingc5c98922012-04-13 12:14:39 +01001301 struct omap_hsmmc_host *host = param;
1302 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001303 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001304 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001305
Russell Kingc5c98922012-04-13 12:14:39 +01001306 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001307 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001308 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001309 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001310 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001311
Adrian Hunter770d7432011-05-06 12:14:11 +03001312 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001313 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001314 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001315 dma_unmap_sg(chan->device->dev,
1316 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001317 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001318
1319 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001320 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001321 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001322
1323 /* If DMA has finished after TC, complete the request */
1324 if (!req_in_progress) {
1325 struct mmc_request *mrq = host->mrq;
1326
1327 host->mrq = NULL;
1328 mmc_request_done(host->mmc, mrq);
1329 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001330}
1331
Per Forlin9782aff2011-07-01 18:55:23 +02001332static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1333 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001334 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001335 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001336{
1337 int dma_len;
1338
1339 if (!next && data->host_cookie &&
1340 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301341 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001342 " host->next_data.cookie %d\n",
1343 __func__, data->host_cookie, host->next_data.cookie);
1344 data->host_cookie = 0;
1345 }
1346
1347 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001348 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001349 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001350 omap_hsmmc_get_dma_dir(host, data));
1351
1352 } else {
1353 dma_len = host->next_data.dma_len;
1354 host->next_data.dma_len = 0;
1355 }
1356
1357
1358 if (dma_len == 0)
1359 return -EINVAL;
1360
1361 if (next) {
1362 next->dma_len = dma_len;
1363 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1364 } else
1365 host->dma_len = dma_len;
1366
1367 return 0;
1368}
1369
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001370/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001371 * Routine to configure and start DMA for the MMC card
1372 */
Balaji T K9d025332014-01-21 19:54:42 +05301373static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001374 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001375{
Russell King26b88522012-04-13 12:27:37 +01001376 struct dma_slave_config cfg;
1377 struct dma_async_tx_descriptor *tx;
1378 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001379 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001380 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001381
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001382 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001383 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001384 struct scatterlist *sgl;
1385
1386 sgl = data->sg + i;
1387 if (sgl->length % data->blksz)
1388 return -EINVAL;
1389 }
1390 if ((data->blksz % 4) != 0)
1391 /* REVISIT: The MMC buffer increments only when MSB is written.
1392 * Return error for blksz which is non multiple of four.
1393 */
1394 return -EINVAL;
1395
Adrian Hunterb4175772010-05-26 14:42:06 -07001396 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001397
Russell Kingc5c98922012-04-13 12:14:39 +01001398 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001399
Russell King26b88522012-04-13 12:27:37 +01001400 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1401 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1402 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1403 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1404 cfg.src_maxburst = data->blksz / 4;
1405 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001406
Russell King26b88522012-04-13 12:27:37 +01001407 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001408 if (ret)
1409 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001410
Russell King26b88522012-04-13 12:27:37 +01001411 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1412 if (ret)
1413 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001414
Russell King26b88522012-04-13 12:27:37 +01001415 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1416 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1417 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1418 if (!tx) {
1419 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1420 /* FIXME: cleanup */
1421 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001422 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001423
Russell King26b88522012-04-13 12:27:37 +01001424 tx->callback = omap_hsmmc_dma_callback;
1425 tx->callback_param = host;
1426
1427 /* Does not fail */
1428 dmaengine_submit(tx);
1429
1430 host->dma_ch = 1;
1431
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001432 return 0;
1433}
1434
Denis Karpov70a33412009-09-22 16:44:59 -07001435static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001436 unsigned int timeout_ns,
1437 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001438{
1439 unsigned int timeout, cycle_ns;
1440 uint32_t reg, clkd, dto = 0;
1441
1442 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1443 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1444 if (clkd == 0)
1445 clkd = 1;
1446
Balaji T K6e3076c2014-01-21 19:54:42 +05301447 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001448 timeout = timeout_ns / cycle_ns;
1449 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001450 if (timeout) {
1451 while ((timeout & 0x80000000) == 0) {
1452 dto += 1;
1453 timeout <<= 1;
1454 }
1455 dto = 31 - dto;
1456 timeout <<= 1;
1457 if (timeout && dto)
1458 dto += 1;
1459 if (dto >= 13)
1460 dto -= 13;
1461 else
1462 dto = 0;
1463 if (dto > 14)
1464 dto = 14;
1465 }
1466
1467 reg &= ~DTO_MASK;
1468 reg |= dto << DTO_SHIFT;
1469 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1470}
1471
Balaji T K9d025332014-01-21 19:54:42 +05301472static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1473{
1474 struct mmc_request *req = host->mrq;
1475 struct dma_chan *chan;
1476
1477 if (!req->data)
1478 return;
1479 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1480 | (req->data->blocks << 16));
1481 set_data_timeout(host, req->data->timeout_ns,
1482 req->data->timeout_clks);
1483 chan = omap_hsmmc_get_dma_chan(host, req->data);
1484 dma_async_issue_pending(chan);
1485}
1486
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001487/*
1488 * Configure block length for MMC/SD cards and initiate the transfer.
1489 */
1490static int
Denis Karpov70a33412009-09-22 16:44:59 -07001491omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001492{
1493 int ret;
1494 host->data = req->data;
1495
1496 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001497 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001498 /*
1499 * Set an arbitrary 100ms data timeout for commands with
1500 * busy signal.
1501 */
1502 if (req->cmd->flags & MMC_RSP_BUSY)
1503 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001504 return 0;
1505 }
1506
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001507 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301508 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001509 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301510 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001511 return ret;
1512 }
1513 }
1514 return 0;
1515}
1516
Per Forlin9782aff2011-07-01 18:55:23 +02001517static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1518 int err)
1519{
1520 struct omap_hsmmc_host *host = mmc_priv(mmc);
1521 struct mmc_data *data = mrq->data;
1522
Russell King26b88522012-04-13 12:27:37 +01001523 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001524 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001525
Russell King26b88522012-04-13 12:27:37 +01001526 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1527 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001528 data->host_cookie = 0;
1529 }
1530}
1531
1532static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1533 bool is_first_req)
1534{
1535 struct omap_hsmmc_host *host = mmc_priv(mmc);
1536
1537 if (mrq->data->host_cookie) {
1538 mrq->data->host_cookie = 0;
1539 return ;
1540 }
1541
Russell Kingc5c98922012-04-13 12:14:39 +01001542 if (host->use_dma) {
1543 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001544
Per Forlin9782aff2011-07-01 18:55:23 +02001545 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001546 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001547 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001548 }
Per Forlin9782aff2011-07-01 18:55:23 +02001549}
1550
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001551/*
1552 * Request function. for read/write operation
1553 */
Denis Karpov70a33412009-09-22 16:44:59 -07001554static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001555{
Denis Karpov70a33412009-09-22 16:44:59 -07001556 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001557 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001558
Adrian Hunterb4175772010-05-26 14:42:06 -07001559 BUG_ON(host->req_in_progress);
1560 BUG_ON(host->dma_ch != -1);
1561 if (host->protect_card) {
1562 if (host->reqs_blocked < 3) {
1563 /*
1564 * Ensure the controller is left in a consistent
1565 * state by resetting the command and data state
1566 * machines.
1567 */
1568 omap_hsmmc_reset_controller_fsm(host, SRD);
1569 omap_hsmmc_reset_controller_fsm(host, SRC);
1570 host->reqs_blocked += 1;
1571 }
1572 req->cmd->error = -EBADF;
1573 if (req->data)
1574 req->data->error = -EBADF;
1575 req->cmd->retries = 0;
1576 mmc_request_done(mmc, req);
1577 return;
1578 } else if (host->reqs_blocked)
1579 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001580 WARN_ON(host->mrq != NULL);
1581 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301582 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001583 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001584 if (err) {
1585 req->cmd->error = err;
1586 if (req->data)
1587 req->data->error = err;
1588 host->mrq = NULL;
1589 mmc_request_done(mmc, req);
1590 return;
1591 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301592 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301593 omap_hsmmc_start_command(host, req->sbc, NULL);
1594 return;
1595 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001596
Balaji T K9d025332014-01-21 19:54:42 +05301597 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001598 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001599}
1600
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001601/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001602static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001603{
Denis Karpov70a33412009-09-22 16:44:59 -07001604 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001605 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001606
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301607 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001608
Adrian Huntera3621462009-09-22 16:44:42 -07001609 if (ios->power_mode != host->power_mode) {
1610 switch (ios->power_mode) {
1611 case MMC_POWER_OFF:
1612 mmc_slot(host).set_power(host->dev, host->slot_id,
1613 0, 0);
1614 break;
1615 case MMC_POWER_UP:
1616 mmc_slot(host).set_power(host->dev, host->slot_id,
1617 1, ios->vdd);
1618 break;
1619 case MMC_POWER_ON:
1620 do_send_init_stream = 1;
1621 break;
1622 }
1623 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001624 }
1625
Denis Karpovdd498ef2009-09-22 16:44:49 -07001626 /* FIXME: set registers based only on changes to ios */
1627
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001628 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001629
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301630 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001631 /* Only MMC1 can interface at 3V without some flavor
1632 * of external transceiver; but they all handle 1.8V.
1633 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001634 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301635 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001636 /*
1637 * The mmc_select_voltage fn of the core does
1638 * not seem to set the power_mode to
1639 * MMC_POWER_UP upon recalculating the voltage.
1640 * vdd 1.8v.
1641 */
Denis Karpov70a33412009-09-22 16:44:59 -07001642 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1643 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001644 "Switch operation failed\n");
1645 }
1646 }
1647
Andy Shevchenko5934df22011-05-06 12:14:06 +03001648 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001649
Adrian Huntera3621462009-09-22 16:44:42 -07001650 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001651 send_init_stream(host);
1652
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001653 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001654
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301655 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001656}
1657
1658static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1659{
Denis Karpov70a33412009-09-22 16:44:59 -07001660 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001661
Denis Karpov191d1f12009-09-22 16:44:55 -07001662 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001663 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001664 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001665}
1666
1667static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1668{
Denis Karpov70a33412009-09-22 16:44:59 -07001669 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001670
Denis Karpov191d1f12009-09-22 16:44:55 -07001671 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001672 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001673 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001674}
1675
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001676static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1677{
1678 struct omap_hsmmc_host *host = mmc_priv(mmc);
1679
1680 if (mmc_slot(host).init_card)
1681 mmc_slot(host).init_card(card);
1682}
1683
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001684static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1685{
1686 struct omap_hsmmc_host *host = mmc_priv(mmc);
1687 u32 irq_mask;
1688 unsigned long flags;
1689
1690 spin_lock_irqsave(&host->irq_lock, flags);
1691
1692 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1693 if (enable) {
1694 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1695 irq_mask |= CIRQ_EN;
1696 } else {
1697 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1698 irq_mask &= ~CIRQ_EN;
1699 }
1700 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1701
1702 /*
1703 * if enable, piggy back detection on current request
1704 * but always disable immediately
1705 */
1706 if (!host->req_in_progress || !enable)
1707 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1708
1709 /* flush posted write */
1710 OMAP_HSMMC_READ(host->base, IE);
1711
1712 spin_unlock_irqrestore(&host->irq_lock, flags);
1713}
1714
1715static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1716{
1717 struct mmc_host *mmc = host->mmc;
1718 int ret;
1719
1720 /*
1721 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1722 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1723 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1724 * with functional clock disabled.
1725 */
1726 if (!host->dev->of_node || !host->wake_irq)
1727 return -ENODEV;
1728
1729 /* Prevent auto-enabling of IRQ */
1730 irq_set_status_flags(host->wake_irq, IRQ_NOAUTOEN);
1731 ret = devm_request_irq(host->dev, host->wake_irq, omap_hsmmc_wake_irq,
1732 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1733 mmc_hostname(mmc), host);
1734 if (ret) {
1735 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1736 goto err;
1737 }
1738
1739 /*
1740 * Some omaps don't have wake-up path from deeper idle states
1741 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1742 */
1743 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
1744 ret = -ENODEV;
1745 devm_free_irq(host->dev, host->wake_irq, host);
1746 goto err;
1747 }
1748
1749 return 0;
1750
1751err:
1752 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1753 host->wake_irq = 0;
1754 return ret;
1755}
1756
Denis Karpov70a33412009-09-22 16:44:59 -07001757static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001758{
1759 u32 hctl, capa, value;
1760
1761 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301762 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001763 hctl = SDVS30;
1764 capa = VS30 | VS18;
1765 } else {
1766 hctl = SDVS18;
1767 capa = VS18;
1768 }
1769
1770 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1771 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1772
1773 value = OMAP_HSMMC_READ(host->base, CAPA);
1774 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1775
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001776 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001777 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001778}
1779
Denis Karpov70a33412009-09-22 16:44:59 -07001780static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001781{
Denis Karpov70a33412009-09-22 16:44:59 -07001782 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001783
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301784 pm_runtime_get_sync(host->dev);
1785
Denis Karpovdd498ef2009-09-22 16:44:49 -07001786 return 0;
1787}
1788
Adrian Hunter907d2e72012-02-29 09:17:21 +02001789static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001790{
Denis Karpov70a33412009-09-22 16:44:59 -07001791 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001792
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301793 pm_runtime_mark_last_busy(host->dev);
1794 pm_runtime_put_autosuspend(host->dev);
1795
Denis Karpovdd498ef2009-09-22 16:44:49 -07001796 return 0;
1797}
1798
Denis Karpov70a33412009-09-22 16:44:59 -07001799static const struct mmc_host_ops omap_hsmmc_ops = {
1800 .enable = omap_hsmmc_enable_fclk,
1801 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001802 .post_req = omap_hsmmc_post_req,
1803 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001804 .request = omap_hsmmc_request,
1805 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001806 .get_cd = omap_hsmmc_get_cd,
1807 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001808 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001809 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001810};
1811
Denis Karpovd900f712009-09-22 16:44:38 -07001812#ifdef CONFIG_DEBUG_FS
1813
Denis Karpov70a33412009-09-22 16:44:59 -07001814static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001815{
1816 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001817 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001818
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001819 seq_printf(s, "mmc%d:\n", mmc->index);
1820 seq_printf(s, "sdio irq mode\t%s\n",
1821 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1822
1823 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1824 seq_printf(s, "sdio irq \t%s\n",
1825 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1826 : "disabled");
1827 }
1828 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001829
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301830 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001831 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001832 seq_printf(s, "CON:\t\t0x%08x\n",
1833 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001834 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1835 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001836 seq_printf(s, "HCTL:\t\t0x%08x\n",
1837 OMAP_HSMMC_READ(host->base, HCTL));
1838 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1839 OMAP_HSMMC_READ(host->base, SYSCTL));
1840 seq_printf(s, "IE:\t\t0x%08x\n",
1841 OMAP_HSMMC_READ(host->base, IE));
1842 seq_printf(s, "ISE:\t\t0x%08x\n",
1843 OMAP_HSMMC_READ(host->base, ISE));
1844 seq_printf(s, "CAPA:\t\t0x%08x\n",
1845 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001846
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301847 pm_runtime_mark_last_busy(host->dev);
1848 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001849
Denis Karpovd900f712009-09-22 16:44:38 -07001850 return 0;
1851}
1852
Denis Karpov70a33412009-09-22 16:44:59 -07001853static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001854{
Denis Karpov70a33412009-09-22 16:44:59 -07001855 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001856}
1857
1858static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001859 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001860 .read = seq_read,
1861 .llseek = seq_lseek,
1862 .release = single_release,
1863};
1864
Denis Karpov70a33412009-09-22 16:44:59 -07001865static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001866{
1867 if (mmc->debugfs_root)
1868 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1869 mmc, &mmc_regs_fops);
1870}
1871
1872#else
1873
Denis Karpov70a33412009-09-22 16:44:59 -07001874static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001875{
1876}
1877
1878#endif
1879
Rajendra Nayak46856a62012-03-12 20:32:37 +05301880#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001881static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1882 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1883 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1884};
1885
1886static const struct omap_mmc_of_data omap4_mmc_of_data = {
1887 .reg_offset = 0x100,
1888};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001889static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1890 .reg_offset = 0x100,
1891 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1892};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301893
1894static const struct of_device_id omap_mmc_of_match[] = {
1895 {
1896 .compatible = "ti,omap2-hsmmc",
1897 },
1898 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001899 .compatible = "ti,omap3-pre-es3-hsmmc",
1900 .data = &omap3_pre_es3_mmc_of_data,
1901 },
1902 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301903 .compatible = "ti,omap3-hsmmc",
1904 },
1905 {
1906 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001907 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301908 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001909 {
1910 .compatible = "ti,am33xx-hsmmc",
1911 .data = &am33xx_mmc_of_data,
1912 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301913 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001914};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301915MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1916
1917static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1918{
1919 struct omap_mmc_platform_data *pdata;
1920 struct device_node *np = dev->of_node;
Daniel Mackd8714e82012-10-15 21:35:06 +05301921 u32 bus_width, max_freq;
Jan Luebbedc642c22013-01-30 10:07:17 +01001922 int cd_gpio, wp_gpio;
1923
1924 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
1925 wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1926 if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
1927 return ERR_PTR(-EPROBE_DEFER);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301928
1929 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1930 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301931 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301932
1933 if (of_find_property(np, "ti,dual-volt", NULL))
1934 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1935
1936 /* This driver only supports 1 slot */
1937 pdata->nr_slots = 1;
Jan Luebbedc642c22013-01-30 10:07:17 +01001938 pdata->slots[0].switch_pin = cd_gpio;
1939 pdata->slots[0].gpio_wp = wp_gpio;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301940
1941 if (of_find_property(np, "ti,non-removable", NULL)) {
1942 pdata->slots[0].nonremovable = true;
1943 pdata->slots[0].no_regulator_off_init = true;
1944 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001945 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301946 if (bus_width == 4)
1947 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1948 else if (bus_width == 8)
1949 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1950
1951 if (of_find_property(np, "ti,needs-special-reset", NULL))
1952 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1953
Daniel Mackd8714e82012-10-15 21:35:06 +05301954 if (!of_property_read_u32(np, "max-frequency", &max_freq))
1955 pdata->max_freq = max_freq;
1956
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301957 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
1958 pdata->slots[0].features |= HSMMC_HAS_HSPE_SUPPORT;
1959
Daniel Mackc9ae64d2014-02-17 12:36:33 +01001960 if (of_find_property(np, "keep-power-in-suspend", NULL))
1961 pdata->slots[0].pm_caps |= MMC_PM_KEEP_POWER;
1962
1963 if (of_find_property(np, "enable-sdio-wakeup", NULL))
1964 pdata->slots[0].pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
1965
Rajendra Nayak46856a62012-03-12 20:32:37 +05301966 return pdata;
1967}
1968#else
1969static inline struct omap_mmc_platform_data
1970 *of_get_hsmmc_pdata(struct device *dev)
1971{
Balaji T K19df45b2014-02-28 19:08:18 +05301972 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301973}
1974#endif
1975
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001976static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001977{
1978 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1979 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001980 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001981 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001982 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301983 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001984 dma_cap_mask_t mask;
1985 unsigned tx_req, rx_req;
Daniel Mack46b76032012-10-15 21:35:05 +05301986 struct pinctrl *pinctrl;
Nishanth Menon59445b12014-02-13 23:45:48 -06001987 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05301988 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301989
1990 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1991 if (match) {
1992 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01001993
1994 if (IS_ERR(pdata))
1995 return PTR_ERR(pdata);
1996
Rajendra Nayak46856a62012-03-12 20:32:37 +05301997 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06001998 data = match->data;
1999 pdata->reg_offset = data->reg_offset;
2000 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302001 }
2002 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002003
2004 if (pdata == NULL) {
2005 dev_err(&pdev->dev, "Platform Data is missing\n");
2006 return -ENXIO;
2007 }
2008
2009 if (pdata->nr_slots == 0) {
2010 dev_err(&pdev->dev, "No Slots\n");
2011 return -ENXIO;
2012 }
2013
2014 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2015 irq = platform_get_irq(pdev, 0);
2016 if (res == NULL || irq < 0)
2017 return -ENXIO;
2018
Balaji T K77fae212014-05-09 22:16:51 +05302019 base = devm_ioremap_resource(&pdev->dev, res);
2020 if (IS_ERR(base))
2021 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002022
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002023 ret = omap_hsmmc_gpio_init(pdata);
2024 if (ret)
2025 goto err;
2026
Denis Karpov70a33412009-09-22 16:44:59 -07002027 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002028 if (!mmc) {
2029 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002030 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002031 }
2032
2033 host = mmc_priv(mmc);
2034 host->mmc = mmc;
2035 host->pdata = pdata;
2036 host->dev = &pdev->dev;
2037 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002038 host->dma_ch = -1;
2039 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002040 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05302041 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05302042 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08002043 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002044 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05302045 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002046
2047 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002048
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002049 if (pdev->dev.of_node)
2050 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2051
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302052 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002053
Daniel Mackd418ed82012-02-19 13:20:33 +01002054 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2055
2056 if (pdata->max_freq > 0)
2057 mmc->f_max = pdata->max_freq;
2058 else
2059 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002060
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002061 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002062
Balaji T K96181952014-05-09 22:16:48 +05302063 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002064 if (IS_ERR(host->fclk)) {
2065 ret = PTR_ERR(host->fclk);
2066 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002067 goto err1;
2068 }
2069
Paul Walmsley9b682562011-10-06 14:50:35 -06002070 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2071 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
2072 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
2073 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002074
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302075 pm_runtime_enable(host->dev);
2076 pm_runtime_get_sync(host->dev);
2077 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2078 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002079
Balaji T K92a3aeb2012-02-24 21:14:34 +05302080 omap_hsmmc_context_save(host);
2081
Balaji T K96181952014-05-09 22:16:48 +05302082 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302083 /*
2084 * MMC can still work without debounce clock.
2085 */
2086 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302087 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302088 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302089 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302090 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002091 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002092
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002093 /* Since we do only SG emulation, we can have as many segs
2094 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002095 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002096
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002097 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2098 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2099 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2100 mmc->max_seg_size = mmc->max_req_size;
2101
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002102 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002103 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002104
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002105 mmc->caps |= mmc_slot(host).caps;
2106 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002107 mmc->caps |= MMC_CAP_4_BIT_DATA;
2108
Denis Karpov191d1f12009-09-22 16:44:55 -07002109 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002110 mmc->caps |= MMC_CAP_NONREMOVABLE;
2111
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002112 mmc->pm_caps = mmc_slot(host).pm_caps;
2113
Denis Karpov70a33412009-09-22 16:44:59 -07002114 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002115
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302116 if (!pdev->dev.of_node) {
2117 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2118 if (!res) {
2119 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2120 ret = -ENXIO;
2121 goto err_irq;
2122 }
2123 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002124
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302125 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2126 if (!res) {
2127 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2128 ret = -ENXIO;
2129 goto err_irq;
2130 }
2131 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002132 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002133
Russell King26b88522012-04-13 12:27:37 +01002134 dma_cap_zero(mask);
2135 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002136
Matt Porterd272fbf2013-05-10 17:42:34 +05302137 host->rx_chan =
2138 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2139 &rx_req, &pdev->dev, "rx");
2140
Russell King26b88522012-04-13 12:27:37 +01002141 if (!host->rx_chan) {
2142 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002143 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002144 goto err_irq;
2145 }
2146
Matt Porterd272fbf2013-05-10 17:42:34 +05302147 host->tx_chan =
2148 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2149 &tx_req, &pdev->dev, "tx");
2150
Russell King26b88522012-04-13 12:27:37 +01002151 if (!host->tx_chan) {
2152 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002153 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002154 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002155 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002156
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002157 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302158 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002159 mmc_hostname(mmc), host);
2160 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302161 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002162 goto err_irq;
2163 }
2164
2165 if (pdata->init != NULL) {
2166 if (pdata->init(&pdev->dev) != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302167 dev_err(mmc_dev(host->mmc),
Denis Karpov70a33412009-09-22 16:44:59 -07002168 "Unable to configure MMC IRQs\n");
Balaji T Ke1538ed2014-05-09 22:16:49 +05302169 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002170 }
2171 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002172
Adrian Hunterb702b102010-02-15 10:03:35 -08002173 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002174 ret = omap_hsmmc_reg_get(host);
2175 if (ret)
2176 goto err_reg;
2177 host->use_reg = 1;
2178 }
2179
David Brownellb583f262009-05-28 14:04:03 -07002180 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002181
2182 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002183 if ((mmc_slot(host).card_detect_irq)) {
Balaji T K9fa0e052014-05-09 22:16:50 +05302184 ret = devm_request_threaded_irq(&pdev->dev,
2185 mmc_slot(host).card_detect_irq,
2186 NULL, omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08002187 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11002188 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002189 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302190 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002191 "Unable to grab MMC CD IRQ\n");
2192 goto err_irq_cd;
2193 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00002194 pdata->suspend = omap_hsmmc_suspend_cdirq;
2195 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002196 }
2197
Adrian Hunterb4175772010-05-26 14:42:06 -07002198 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002199
Daniel Mack46b76032012-10-15 21:35:05 +05302200 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
2201 if (IS_ERR(pinctrl))
2202 dev_warn(&pdev->dev,
2203 "pins are not configured from the driver\n");
2204
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002205 /*
2206 * For now, only support SDIO interrupt if we have a separate
2207 * wake-up interrupt configured from device tree. This is because
2208 * the wake-up interrupt is needed for idle state and some
2209 * platforms need special quirks. And we don't want to add new
2210 * legacy mux platform init code callbacks any longer as we
2211 * are moving to DT based booting anyways.
2212 */
2213 ret = omap_hsmmc_configure_wake_irq(host);
2214 if (!ret)
2215 mmc->caps |= MMC_CAP_SDIO_IRQ;
2216
Adrian Hunterb62f6222009-09-22 16:45:01 -07002217 omap_hsmmc_protect_card(host);
2218
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002219 mmc_add_host(mmc);
2220
Denis Karpov191d1f12009-09-22 16:44:55 -07002221 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002222 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2223 if (ret < 0)
2224 goto err_slot_name;
2225 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002226 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002227 ret = device_create_file(&mmc->class_dev,
2228 &dev_attr_cover_switch);
2229 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002230 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002231 }
2232
Denis Karpov70a33412009-09-22 16:44:59 -07002233 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302234 pm_runtime_mark_last_busy(host->dev);
2235 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002236
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002237 return 0;
2238
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002239err_slot_name:
2240 mmc_remove_host(mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002241err_irq_cd:
2242 if (host->use_reg)
2243 omap_hsmmc_reg_put(host);
2244err_reg:
2245 if (host->pdata->cleanup)
2246 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002247err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002248 if (host->tx_chan)
2249 dma_release_channel(host->tx_chan);
2250 if (host->rx_chan)
2251 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302252 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002253 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302254 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302255 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002256err1:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002257 mmc_free_host(mmc);
2258err_alloc:
2259 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002260err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002261 return ret;
2262}
2263
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002264static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002265{
Denis Karpov70a33412009-09-22 16:44:59 -07002266 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002267
Felipe Balbi927ce942012-03-14 11:18:27 +02002268 pm_runtime_get_sync(host->dev);
2269 mmc_remove_host(host->mmc);
2270 if (host->use_reg)
2271 omap_hsmmc_reg_put(host);
2272 if (host->pdata->cleanup)
2273 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002274
Russell Kingc5c98922012-04-13 12:14:39 +01002275 if (host->tx_chan)
2276 dma_release_channel(host->tx_chan);
2277 if (host->rx_chan)
2278 dma_release_channel(host->rx_chan);
2279
Felipe Balbi927ce942012-03-14 11:18:27 +02002280 pm_runtime_put_sync(host->dev);
2281 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302282 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302283 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002284
Balaji T K9ea28ec2012-10-15 21:35:08 +05302285 omap_hsmmc_gpio_free(host->pdata);
Balaji T K9d1f0282012-10-15 21:35:07 +05302286 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002287
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002288 return 0;
2289}
2290
2291#ifdef CONFIG_PM
Felipe Balbia48ce882012-11-19 21:59:59 +05302292static int omap_hsmmc_prepare(struct device *dev)
2293{
2294 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2295
2296 if (host->pdata->suspend)
2297 return host->pdata->suspend(dev, host->slot_id);
2298
2299 return 0;
2300}
2301
2302static void omap_hsmmc_complete(struct device *dev)
2303{
2304 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2305
2306 if (host->pdata->resume)
2307 host->pdata->resume(dev, host->slot_id);
2308
2309}
2310
Kevin Hilmana791daa2010-05-26 14:42:07 -07002311static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002312{
Felipe Balbi927ce942012-03-14 11:18:27 +02002313 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2314
2315 if (!host)
2316 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002317
Felipe Balbi927ce942012-03-14 11:18:27 +02002318 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002319
2320 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002321 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2322 OMAP_HSMMC_WRITE(host->base, IE, 0);
2323 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002324 OMAP_HSMMC_WRITE(host->base, HCTL,
2325 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2326 }
2327
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002328 /* do not wake up due to sdio irq */
2329 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2330 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2331 disable_irq(host->wake_irq);
2332
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302333 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302334 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002335
Eliad Peller31f9d462011-11-22 16:02:17 +02002336 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002337 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002338}
2339
2340/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002341static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002342{
Felipe Balbi927ce942012-03-14 11:18:27 +02002343 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2344
2345 if (!host)
2346 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002347
Felipe Balbi927ce942012-03-14 11:18:27 +02002348 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002349
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302350 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302351 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002352
Felipe Balbi927ce942012-03-14 11:18:27 +02002353 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2354 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002355
Felipe Balbi927ce942012-03-14 11:18:27 +02002356 omap_hsmmc_protect_card(host);
2357
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002358 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2359 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2360 enable_irq(host->wake_irq);
2361
Felipe Balbi927ce942012-03-14 11:18:27 +02002362 pm_runtime_mark_last_busy(host->dev);
2363 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002364 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002365}
2366
2367#else
Felipe Balbia48ce882012-11-19 21:59:59 +05302368#define omap_hsmmc_prepare NULL
2369#define omap_hsmmc_complete NULL
Denis Karpov70a33412009-09-22 16:44:59 -07002370#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302371#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002372#endif
2373
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302374static int omap_hsmmc_runtime_suspend(struct device *dev)
2375{
2376 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002377 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302378
2379 host = platform_get_drvdata(to_platform_device(dev));
2380 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002381 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302382
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002383 spin_lock_irqsave(&host->irq_lock, flags);
2384 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2385 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2386 /* disable sdio irq handling to prevent race */
2387 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2388 OMAP_HSMMC_WRITE(host->base, IE, 0);
2389 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2390
2391 WARN_ON(host->flags & HSMMC_WAKE_IRQ_ENABLED);
2392 enable_irq(host->wake_irq);
2393 host->flags |= HSMMC_WAKE_IRQ_ENABLED;
2394 }
2395 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302396 return 0;
2397}
2398
2399static int omap_hsmmc_runtime_resume(struct device *dev)
2400{
2401 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002402 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302403
2404 host = platform_get_drvdata(to_platform_device(dev));
2405 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002406 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302407
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002408 spin_lock_irqsave(&host->irq_lock, flags);
2409 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2410 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2411 /* sdio irq flag can't change while in runtime suspend */
2412 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
2413 disable_irq_nosync(host->wake_irq);
2414 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
2415 }
2416
2417 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2418 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2419 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2420 }
2421 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302422 return 0;
2423}
2424
Kevin Hilmana791daa2010-05-26 14:42:07 -07002425static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002426 .suspend = omap_hsmmc_suspend,
2427 .resume = omap_hsmmc_resume,
Felipe Balbia48ce882012-11-19 21:59:59 +05302428 .prepare = omap_hsmmc_prepare,
2429 .complete = omap_hsmmc_complete,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302430 .runtime_suspend = omap_hsmmc_runtime_suspend,
2431 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002432};
2433
2434static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002435 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002436 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002437 .driver = {
2438 .name = DRIVER_NAME,
2439 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002440 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302441 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002442 },
2443};
2444
Felipe Balbib7964502012-03-14 11:18:32 +02002445module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002446MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2447MODULE_LICENSE("GPL");
2448MODULE_ALIAS("platform:" DRIVER_NAME);
2449MODULE_AUTHOR("Texas Instruments Inc");