blob: 9446010a5dd95df6d09f895352fb12f041d9b8f6 [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
59#define OMAP_HSMMC_HCTL 0x0128
60#define OMAP_HSMMC_SYSCTL 0x012C
61#define OMAP_HSMMC_STAT 0x0130
62#define OMAP_HSMMC_IE 0x0134
63#define OMAP_HSMMC_ISE 0x0138
Balaji T Ka2e77152014-01-21 19:54:42 +053064#define OMAP_HSMMC_AC12 0x013C
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010065#define OMAP_HSMMC_CAPA 0x0140
66
67#define VS18 (1 << 26)
68#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053069#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010070#define SDVS18 (0x5 << 9)
71#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080072#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010073#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010074#define SDVSCLR 0xFFFFF1FF
75#define SDVSDET 0x00000400
76#define AUTOIDLE 0x1
77#define SDBP (1 << 8)
78#define DTO 0xe
79#define ICE 0x1
80#define ICS 0x2
81#define CEN (1 << 2)
Balaji T Ked164182013-10-21 00:25:21 +053082#define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010083#define CLKD_MASK 0x0000FFC0
84#define CLKD_SHIFT 6
85#define DTO_MASK 0x000F0000
86#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010087#define INIT_STREAM (1 << 1)
Balaji T Ka2e77152014-01-21 19:54:42 +053088#define ACEN_ACMD23 (2 << 2)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010089#define DP_SELECT (1 << 21)
90#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053091#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010092#define MSBS (1 << 5)
93#define BCE (1 << 1)
94#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053095#define HSPE (1 << 2)
Balaji T K03b5d922012-04-09 12:08:33 +053096#define DDR (1 << 19)
Jarkko Lavinen73153012008-11-21 16:49:54 +020097#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010098#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010099#define STAT_CLEAR 0xFFFFFFFF
100#define INIT_STREAM_CMD 0x00000000
101#define DUAL_VOLT_OCR_BIT 7
102#define SRC (1 << 25)
103#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700104#define SOFTRESET (1 << 1)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100105
Venkatraman Sa7e96872012-11-19 22:00:01 +0530106/* Interrupt masks for IE and ISE register */
107#define CC_EN (1 << 0)
108#define TC_EN (1 << 1)
109#define BWR_EN (1 << 4)
110#define BRR_EN (1 << 5)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200111#define CIRQ_EN (1 << 8)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530112#define ERR_EN (1 << 15)
113#define CTO_EN (1 << 16)
114#define CCRC_EN (1 << 17)
115#define CEB_EN (1 << 18)
116#define CIE_EN (1 << 19)
117#define DTO_EN (1 << 20)
118#define DCRC_EN (1 << 21)
119#define DEB_EN (1 << 22)
Balaji T Ka2e77152014-01-21 19:54:42 +0530120#define ACE_EN (1 << 24)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530121#define CERR_EN (1 << 28)
122#define BADA_EN (1 << 29)
123
Balaji T Ka2e77152014-01-21 19:54:42 +0530124#define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
Venkatraman Sa7e96872012-11-19 22:00:01 +0530125 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
126 BRR_EN | BWR_EN | TC_EN | CC_EN)
127
Balaji T Ka2e77152014-01-21 19:54:42 +0530128#define CNI (1 << 7)
129#define ACIE (1 << 4)
130#define ACEB (1 << 3)
131#define ACCE (1 << 2)
132#define ACTO (1 << 1)
133#define ACNE (1 << 0)
134
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530135#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530136#define MMC_TIMEOUT_MS 20 /* 20 mSec */
137#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400138#define OMAP_MMC_MIN_CLOCK 400000
139#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530140#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100141
Balaji T Ke99448f2014-02-19 20:26:40 +0530142#define VDD_1V8 1800000 /* 180000 uV */
143#define VDD_3V0 3000000 /* 300000 uV */
144#define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
145
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100146/*
147 * One controller can have multiple slots, like on some omap boards using
148 * omap.c controller driver. Luckily this is not currently done on any known
149 * omap_hsmmc.c device.
150 */
151#define mmc_slot(host) (host->pdata->slots[host->slot_id])
152
153/*
154 * MMC Host controller read/write API's
155 */
156#define OMAP_HSMMC_READ(base, reg) \
157 __raw_readl((base) + OMAP_HSMMC_##reg)
158
159#define OMAP_HSMMC_WRITE(base, reg, val) \
160 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
161
Per Forlin9782aff2011-07-01 18:55:23 +0200162struct omap_hsmmc_next {
163 unsigned int dma_len;
164 s32 cookie;
165};
166
Denis Karpov70a33412009-09-22 16:44:59 -0700167struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100168 struct device *dev;
169 struct mmc_host *mmc;
170 struct mmc_request *mrq;
171 struct mmc_command *cmd;
172 struct mmc_data *data;
173 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100174 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800175 /*
176 * vcc == configured supply
177 * vcc_aux == optional
178 * - MMC1, supply for DAT4..DAT7
179 * - MMC2/MMC2, external level shifter voltage supply, for
180 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
181 */
182 struct regulator *vcc;
183 struct regulator *vcc_aux;
Balaji T Ke99448f2014-02-19 20:26:40 +0530184 struct regulator *pbias;
185 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100186 void __iomem *base;
187 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700188 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100189 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200190 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100191 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700192 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100193 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530194 u32 con;
195 u32 hctl;
196 u32 sysctl;
197 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100198 int irq;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200199 int wake_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100200 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100201 struct dma_chan *tx_chan;
202 struct dma_chan *rx_chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100203 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200204 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700205 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700206 int protect_card;
207 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800208 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700209 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530210 unsigned long clk_rate;
Balaji T Ka2e77152014-01-21 19:54:42 +0530211 unsigned int flags;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200212#define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
213#define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
214#define HSMMC_WAKE_IRQ_ENABLED (1 << 2)
Per Forlin9782aff2011-07-01 18:55:23 +0200215 struct omap_hsmmc_next next_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100216 struct omap_mmc_platform_data *pdata;
217};
218
Nishanth Menon59445b12014-02-13 23:45:48 -0600219struct omap_mmc_of_data {
220 u32 reg_offset;
221 u8 controller_flags;
222};
223
Balaji T Kbf129e12014-01-21 19:54:42 +0530224static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
225
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800226static int omap_hsmmc_card_detect(struct device *dev, int slot)
227{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530228 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
229 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800230
231 /* NOTE: assumes card detect signal is active-low */
232 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
233}
234
235static int omap_hsmmc_get_wp(struct device *dev, int slot)
236{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530237 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
238 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800239
240 /* NOTE: assumes write protect signal is active-high */
241 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
242}
243
244static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
245{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530246 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
247 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800248
249 /* NOTE: assumes card detect signal is active-low */
250 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
251}
252
253#ifdef CONFIG_PM
254
255static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
256{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530257 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
258 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800259
260 disable_irq(mmc->slots[0].card_detect_irq);
261 return 0;
262}
263
264static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
265{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530266 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
267 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800268
269 enable_irq(mmc->slots[0].card_detect_irq);
270 return 0;
271}
272
273#else
274
275#define omap_hsmmc_suspend_cdirq NULL
276#define omap_hsmmc_resume_cdirq NULL
277
278#endif
279
Adrian Hunterb702b102010-02-15 10:03:35 -0800280#ifdef CONFIG_REGULATOR
281
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500282static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800283 int vdd)
284{
285 struct omap_hsmmc_host *host =
286 platform_get_drvdata(to_platform_device(dev));
287 int ret = 0;
288
289 /*
290 * If we don't see a Vcc regulator, assume it's a fixed
291 * voltage always-on regulator.
292 */
293 if (!host->vcc)
294 return 0;
295
296 if (mmc_slot(host).before_set_reg)
297 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
298
Balaji T Ke99448f2014-02-19 20:26:40 +0530299 if (host->pbias) {
300 if (host->pbias_enabled == 1) {
301 ret = regulator_disable(host->pbias);
302 if (!ret)
303 host->pbias_enabled = 0;
304 }
305 regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
306 }
307
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800308 /*
309 * Assume Vcc regulator is used only to power the card ... OMAP
310 * VDDS is used to power the pins, optionally with a transceiver to
311 * support cards using voltages other than VDDS (1.8V nominal). When a
312 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
313 *
314 * In some cases this regulator won't support enable/disable;
315 * e.g. it's a fixed rail for a WLAN chip.
316 *
317 * In other cases vcc_aux switches interface power. Example, for
318 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
319 * chips/cards need an interface voltage rail too.
320 */
321 if (power_on) {
Balaji T K987fd492014-02-19 20:26:40 +0530322 if (host->vcc)
323 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800324 /* Enable interface voltage rail, if needed */
325 if (ret == 0 && host->vcc_aux) {
326 ret = regulator_enable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530327 if (ret < 0 && host->vcc)
Linus Walleij99fc5132010-09-29 01:08:27 -0400328 ret = mmc_regulator_set_ocr(host->mmc,
329 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800330 }
331 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400332 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800333 if (host->vcc_aux)
334 ret = regulator_disable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530335 if (host->vcc) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400336 /* Then proceed to shut down the local regulator */
337 ret = mmc_regulator_set_ocr(host->mmc,
338 host->vcc, 0);
339 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800340 }
341
Balaji T Ke99448f2014-02-19 20:26:40 +0530342 if (host->pbias) {
343 if (vdd <= VDD_165_195)
344 ret = regulator_set_voltage(host->pbias, VDD_1V8,
345 VDD_1V8);
346 else
347 ret = regulator_set_voltage(host->pbias, VDD_3V0,
348 VDD_3V0);
349 if (ret < 0)
350 goto error_set_power;
351
352 if (host->pbias_enabled == 0) {
353 ret = regulator_enable(host->pbias);
354 if (!ret)
355 host->pbias_enabled = 1;
356 }
357 }
358
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800359 if (mmc_slot(host).after_set_reg)
360 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
361
Balaji T Ke99448f2014-02-19 20:26:40 +0530362error_set_power:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800363 return ret;
364}
365
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800366static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
367{
368 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700369 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800370
Balaji T Kf2ddc1d2014-02-19 20:26:40 +0530371 reg = devm_regulator_get(host->dev, "vmmc");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800372 if (IS_ERR(reg)) {
Balaji T K987fd492014-02-19 20:26:40 +0530373 dev_err(host->dev, "unable to get vmmc regulator %ld\n",
374 PTR_ERR(reg));
NeilBrown1fdc90f2012-08-08 00:06:00 -0400375 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800376 } else {
377 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700378 ocr_value = mmc_regulator_get_ocrmask(reg);
379 if (!mmc_slot(host).ocr_mask) {
380 mmc_slot(host).ocr_mask = ocr_value;
381 } else {
382 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530383 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500384 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700385 mmc_slot(host).ocr_mask = 0;
386 return -EINVAL;
387 }
388 }
Balaji T K987fd492014-02-19 20:26:40 +0530389 }
390 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800391
Balaji T K987fd492014-02-19 20:26:40 +0530392 /* Allow an aux regulator */
393 reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
394 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800395
Balaji T Ke99448f2014-02-19 20:26:40 +0530396 reg = devm_regulator_get_optional(host->dev, "pbias");
397 host->pbias = IS_ERR(reg) ? NULL : reg;
398
Balaji T K987fd492014-02-19 20:26:40 +0530399 /* For eMMC do not power off when not in sleep state */
400 if (mmc_slot(host).no_regulator_off_init)
401 return 0;
402 /*
403 * To disable boot_on regulator, enable regulator
404 * to increase usecount and then disable it.
405 */
406 if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
407 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
408 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
Adrian Huntere840ce12011-05-06 12:14:10 +0300409
Balaji T K987fd492014-02-19 20:26:40 +0530410 mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
411 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800412 }
413
414 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800415}
416
417static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
418{
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800419 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800420}
421
Adrian Hunterb702b102010-02-15 10:03:35 -0800422static inline int omap_hsmmc_have_reg(void)
423{
424 return 1;
425}
426
427#else
428
429static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
430{
431 return -EINVAL;
432}
433
434static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
435{
436}
437
438static inline int omap_hsmmc_have_reg(void)
439{
440 return 0;
441}
442
443#endif
444
445static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
446{
447 int ret;
448
449 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800450 if (pdata->slots[0].cover)
451 pdata->slots[0].get_cover_state =
452 omap_hsmmc_get_cover_state;
453 else
454 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
455 pdata->slots[0].card_detect_irq =
456 gpio_to_irq(pdata->slots[0].switch_pin);
457 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
458 if (ret)
459 return ret;
460 ret = gpio_direction_input(pdata->slots[0].switch_pin);
461 if (ret)
462 goto err_free_sp;
463 } else
464 pdata->slots[0].switch_pin = -EINVAL;
465
466 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
467 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
468 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
469 if (ret)
470 goto err_free_cd;
471 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
472 if (ret)
473 goto err_free_wp;
474 } else
475 pdata->slots[0].gpio_wp = -EINVAL;
476
477 return 0;
478
479err_free_wp:
480 gpio_free(pdata->slots[0].gpio_wp);
481err_free_cd:
482 if (gpio_is_valid(pdata->slots[0].switch_pin))
483err_free_sp:
484 gpio_free(pdata->slots[0].switch_pin);
485 return ret;
486}
487
488static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
489{
490 if (gpio_is_valid(pdata->slots[0].gpio_wp))
491 gpio_free(pdata->slots[0].gpio_wp);
492 if (gpio_is_valid(pdata->slots[0].switch_pin))
493 gpio_free(pdata->slots[0].switch_pin);
494}
495
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100496/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300497 * Start clock to the card
498 */
499static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
500{
501 OMAP_HSMMC_WRITE(host->base, SYSCTL,
502 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
503}
504
505/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100506 * Stop clock to the card
507 */
Denis Karpov70a33412009-09-22 16:44:59 -0700508static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100509{
510 OMAP_HSMMC_WRITE(host->base, SYSCTL,
511 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
512 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900513 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100514}
515
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700516static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
517 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700518{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200519 u32 irq_mask = INT_EN_MASK;
520 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700521
522 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200523 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700524
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700525 /* Disable timeout for erases */
526 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530527 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700528
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200529 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700530 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
531 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200532
533 /* latch pending CIRQ, but don't signal MMC core */
534 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
535 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700536 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200537 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700538}
539
540static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
541{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200542 u32 irq_mask = 0;
543 unsigned long flags;
544
545 spin_lock_irqsave(&host->irq_lock, flags);
546 /* no transfer running but need to keep cirq if enabled */
547 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
548 irq_mask |= CIRQ_EN;
549 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
550 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700551 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200552 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700553}
554
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300555/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530556static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300557{
558 u16 dsor = 0;
559
560 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530561 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530562 if (dsor > CLKD_MAX)
563 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300564 }
565
566 return dsor;
567}
568
Andy Shevchenko5934df22011-05-06 12:14:06 +0300569static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
570{
571 struct mmc_ios *ios = &host->mmc->ios;
572 unsigned long regval;
573 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530574 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300575
Venkatraman S8986d312012-08-07 19:10:38 +0530576 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300577
578 omap_hsmmc_stop_clock(host);
579
580 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
581 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530582 clkdiv = calc_divisor(host, ios);
583 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300584 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
585 OMAP_HSMMC_WRITE(host->base, SYSCTL,
586 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
587
588 /* Wait till the ICS bit is set */
589 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
590 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
591 && time_before(jiffies, timeout))
592 cpu_relax();
593
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530594 /*
595 * Enable High-Speed Support
596 * Pre-Requisites
597 * - Controller should support High-Speed-Enable Bit
598 * - Controller should not be using DDR Mode
599 * - Controller should advertise that it supports High Speed
600 * in capabilities register
601 * - MMC/SD clock coming out of controller > 25MHz
602 */
603 if ((mmc_slot(host).features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900604 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530605 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
606 regval = OMAP_HSMMC_READ(host->base, HCTL);
607 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
608 regval |= HSPE;
609 else
610 regval &= ~HSPE;
611
612 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
613 }
614
Andy Shevchenko5934df22011-05-06 12:14:06 +0300615 omap_hsmmc_start_clock(host);
616}
617
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400618static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
619{
620 struct mmc_ios *ios = &host->mmc->ios;
621 u32 con;
622
623 con = OMAP_HSMMC_READ(host->base, CON);
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900624 if (ios->timing == MMC_TIMING_MMC_DDR52)
Balaji T K03b5d922012-04-09 12:08:33 +0530625 con |= DDR; /* configure in DDR mode */
626 else
627 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400628 switch (ios->bus_width) {
629 case MMC_BUS_WIDTH_8:
630 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
631 break;
632 case MMC_BUS_WIDTH_4:
633 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
634 OMAP_HSMMC_WRITE(host->base, HCTL,
635 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
636 break;
637 case MMC_BUS_WIDTH_1:
638 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
639 OMAP_HSMMC_WRITE(host->base, HCTL,
640 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
641 break;
642 }
643}
644
645static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
646{
647 struct mmc_ios *ios = &host->mmc->ios;
648 u32 con;
649
650 con = OMAP_HSMMC_READ(host->base, CON);
651 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
652 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
653 else
654 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
655}
656
Denis Karpov11dd62a2009-09-22 16:44:43 -0700657#ifdef CONFIG_PM
658
659/*
660 * Restore the MMC host context, if it was lost as result of a
661 * power state change.
662 */
Denis Karpov70a33412009-09-22 16:44:59 -0700663static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700664{
665 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400666 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700667 unsigned long timeout;
668
Tony Lindgren0a82e062013-10-21 00:25:19 +0530669 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
670 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
671 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
672 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
673 return 0;
674
675 host->context_loss++;
676
Balaji T Kc2200ef2012-03-07 09:55:30 -0500677 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700678 if (host->power_mode != MMC_POWER_OFF &&
679 (1 << ios->vdd) <= MMC_VDD_23_24)
680 hctl = SDVS18;
681 else
682 hctl = SDVS30;
683 capa = VS30 | VS18;
684 } else {
685 hctl = SDVS18;
686 capa = VS18;
687 }
688
689 OMAP_HSMMC_WRITE(host->base, HCTL,
690 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
691
692 OMAP_HSMMC_WRITE(host->base, CAPA,
693 OMAP_HSMMC_READ(host->base, CAPA) | capa);
694
695 OMAP_HSMMC_WRITE(host->base, HCTL,
696 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
697
698 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
699 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
700 && time_before(jiffies, timeout))
701 ;
702
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200703 OMAP_HSMMC_WRITE(host->base, ISE, 0);
704 OMAP_HSMMC_WRITE(host->base, IE, 0);
705 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700706
707 /* Do not initialize card-specific things if the power is off */
708 if (host->power_mode == MMC_POWER_OFF)
709 goto out;
710
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400711 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700712
Andy Shevchenko5934df22011-05-06 12:14:06 +0300713 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700714
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400715 omap_hsmmc_set_bus_mode(host);
716
Denis Karpov11dd62a2009-09-22 16:44:43 -0700717out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530718 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
719 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700720 return 0;
721}
722
723/*
724 * Save the MMC host context (store the number of power state changes so far).
725 */
Denis Karpov70a33412009-09-22 16:44:59 -0700726static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700727{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530728 host->con = OMAP_HSMMC_READ(host->base, CON);
729 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
730 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
731 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700732}
733
734#else
735
Denis Karpov70a33412009-09-22 16:44:59 -0700736static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700737{
738 return 0;
739}
740
Denis Karpov70a33412009-09-22 16:44:59 -0700741static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700742{
743}
744
745#endif
746
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100747/*
748 * Send init stream sequence to card
749 * before sending IDLE command
750 */
Denis Karpov70a33412009-09-22 16:44:59 -0700751static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100752{
753 int reg = 0;
754 unsigned long timeout;
755
Adrian Hunterb62f6222009-09-22 16:45:01 -0700756 if (host->protect_card)
757 return;
758
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100759 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700760
761 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100762 OMAP_HSMMC_WRITE(host->base, CON,
763 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
764 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
765
766 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530767 while ((reg != CC_EN) && time_before(jiffies, timeout))
768 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100769
770 OMAP_HSMMC_WRITE(host->base, CON,
771 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700772
773 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
774 OMAP_HSMMC_READ(host->base, STAT);
775
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100776 enable_irq(host->irq);
777}
778
779static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700780int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100781{
782 int r = 1;
783
Denis Karpov191d1f12009-09-22 16:44:55 -0700784 if (mmc_slot(host).get_cover_state)
785 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100786 return r;
787}
788
789static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700790omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100791 char *buf)
792{
793 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700794 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100795
Denis Karpov70a33412009-09-22 16:44:59 -0700796 return sprintf(buf, "%s\n",
797 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100798}
799
Denis Karpov70a33412009-09-22 16:44:59 -0700800static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100801
802static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700803omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100804 char *buf)
805{
806 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700807 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100808
Denis Karpov191d1f12009-09-22 16:44:55 -0700809 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100810}
811
Denis Karpov70a33412009-09-22 16:44:59 -0700812static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100813
814/*
815 * Configure the response type and send the cmd.
816 */
817static void
Denis Karpov70a33412009-09-22 16:44:59 -0700818omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100819 struct mmc_data *data)
820{
821 int cmdreg = 0, resptype = 0, cmdtype = 0;
822
Venkatraman S8986d312012-08-07 19:10:38 +0530823 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100824 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
825 host->cmd = cmd;
826
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700827 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100828
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200829 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100830 if (cmd->flags & MMC_RSP_PRESENT) {
831 if (cmd->flags & MMC_RSP_136)
832 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200833 else if (cmd->flags & MMC_RSP_BUSY) {
834 resptype = 3;
835 host->response_busy = 1;
836 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100837 resptype = 2;
838 }
839
840 /*
841 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
842 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
843 * a val of 0x3, rest 0x0.
844 */
845 if (cmd == host->mrq->stop)
846 cmdtype = 0x3;
847
848 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
849
Balaji T Ka2e77152014-01-21 19:54:42 +0530850 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
851 host->mrq->sbc) {
852 cmdreg |= ACEN_ACMD23;
853 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
854 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100855 if (data) {
856 cmdreg |= DP_SELECT | MSBS | BCE;
857 if (data->flags & MMC_DATA_READ)
858 cmdreg |= DDIR;
859 else
860 cmdreg &= ~(DDIR);
861 }
862
863 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530864 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100865
Adrian Hunterb4175772010-05-26 14:42:06 -0700866 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700867
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100868 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
869 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
870}
871
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200872static int
Denis Karpov70a33412009-09-22 16:44:59 -0700873omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200874{
875 if (data->flags & MMC_DATA_WRITE)
876 return DMA_TO_DEVICE;
877 else
878 return DMA_FROM_DEVICE;
879}
880
Russell Kingc5c98922012-04-13 12:14:39 +0100881static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
882 struct mmc_data *data)
883{
884 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
885}
886
Adrian Hunterb4175772010-05-26 14:42:06 -0700887static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
888{
889 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530890 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700891
Venkatraman S31463b12012-04-09 12:08:34 +0530892 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700893 host->req_in_progress = 0;
894 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530895 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700896
897 omap_hsmmc_disable_irq(host);
898 /* Do not complete the request if DMA is still in progress */
899 if (mrq->data && host->use_dma && dma_ch != -1)
900 return;
901 host->mrq = NULL;
902 mmc_request_done(host->mmc, mrq);
903}
904
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100905/*
906 * Notify the transfer complete to MMC core
907 */
908static void
Denis Karpov70a33412009-09-22 16:44:59 -0700909omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100910{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200911 if (!data) {
912 struct mmc_request *mrq = host->mrq;
913
Adrian Hunter23050102009-09-22 16:44:57 -0700914 /* TC before CC from CMD6 - don't know why, but it happens */
915 if (host->cmd && host->cmd->opcode == 6 &&
916 host->response_busy) {
917 host->response_busy = 0;
918 return;
919 }
920
Adrian Hunterb4175772010-05-26 14:42:06 -0700921 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200922 return;
923 }
924
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100925 host->data = NULL;
926
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100927 if (!data->error)
928 data->bytes_xfered += data->blocks * (data->blksz);
929 else
930 data->bytes_xfered = 0;
931
Balaji T Kbf129e12014-01-21 19:54:42 +0530932 if (data->stop && (data->error || !host->mrq->sbc))
933 omap_hsmmc_start_command(host, data->stop, NULL);
934 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700935 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100936}
937
938/*
939 * Notify the core about command completion
940 */
941static void
Denis Karpov70a33412009-09-22 16:44:59 -0700942omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100943{
Balaji T Kbf129e12014-01-21 19:54:42 +0530944 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +0530945 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +0530946 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +0530947 omap_hsmmc_start_dma_transfer(host);
948 omap_hsmmc_start_command(host, host->mrq->cmd,
949 host->mrq->data);
950 return;
951 }
952
Balaji T K2177fa92014-05-09 22:16:52 +0530953 host->cmd = NULL;
954
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100955 if (cmd->flags & MMC_RSP_PRESENT) {
956 if (cmd->flags & MMC_RSP_136) {
957 /* response type 2 */
958 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
959 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
960 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
961 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
962 } else {
963 /* response types 1, 1b, 3, 4, 5, 6 */
964 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
965 }
966 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700967 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530968 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100969}
970
971/*
972 * DMA clean up for command errors
973 */
Denis Karpov70a33412009-09-22 16:44:59 -0700974static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100975{
Adrian Hunterb4175772010-05-26 14:42:06 -0700976 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530977 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700978
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200979 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100980
Venkatraman S31463b12012-04-09 12:08:34 +0530981 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700982 dma_ch = host->dma_ch;
983 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530984 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700985
986 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100987 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
988
989 dmaengine_terminate_all(chan);
990 dma_unmap_sg(chan->device->dev,
991 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700992 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100993
Per Forlin053bf342011-11-07 21:55:11 +0530994 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100995 }
996 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100997}
998
999/*
1000 * Readable error output
1001 */
1002#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +03001003static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001004{
1005 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -07001006 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +03001007 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
1008 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
1009 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
1010 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001011 };
1012 char res[256];
1013 char *buf = res;
1014 int len, i;
1015
1016 len = sprintf(buf, "MMC IRQ 0x%x :", status);
1017 buf += len;
1018
Denis Karpov70a33412009-09-22 16:44:59 -07001019 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001020 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -07001021 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001022 buf += len;
1023 }
1024
Venkatraman S8986d312012-08-07 19:10:38 +05301025 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001026}
Adrian Hunter699b9582011-05-06 12:14:01 +03001027#else
1028static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1029 u32 status)
1030{
1031}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001032#endif /* CONFIG_MMC_DEBUG */
1033
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001034/*
1035 * MMC controller internal state machines reset
1036 *
1037 * Used to reset command or data internal state machines, using respectively
1038 * SRC or SRD bit of SYSCTL register
1039 * Can be called from interrupt context
1040 */
Denis Karpov70a33412009-09-22 16:44:59 -07001041static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1042 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001043{
1044 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301045 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001046
1047 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1048 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1049
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001050 /*
1051 * OMAP4 ES2 and greater has an updated reset logic.
1052 * Monitor a 0->1 transition first
1053 */
1054 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001055 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001056 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301057 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001058 }
1059 i = 0;
1060
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001061 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1062 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301063 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001064
1065 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1066 dev_err(mmc_dev(host->mmc),
1067 "Timeout waiting on controller reset in %s\n",
1068 __func__);
1069}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001070
Balaji T K25e18972012-11-19 21:59:55 +05301071static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1072 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301073{
Balaji T K25e18972012-11-19 21:59:55 +05301074 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301075 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301076 if (host->cmd)
1077 host->cmd->error = err;
1078 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301079
1080 if (host->data) {
1081 omap_hsmmc_reset_controller_fsm(host, SRD);
1082 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301083 } else if (host->mrq && host->mrq->cmd)
1084 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301085}
1086
Adrian Hunterb4175772010-05-26 14:42:06 -07001087static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001088{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001089 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001090 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301091 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001092
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001093 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301094 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001095
Venkatraman Sa7e96872012-11-19 22:00:01 +05301096 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001097 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001098
Venkatraman Sa7e96872012-11-19 22:00:01 +05301099 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301100 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301101 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301102 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301103 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301104 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1105
Balaji T Ka2e77152014-01-21 19:54:42 +05301106 if (status & ACE_EN) {
1107 u32 ac12;
1108 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1109 if (!(ac12 & ACNE) && host->mrq->sbc) {
1110 end_cmd = 1;
1111 if (ac12 & ACTO)
1112 error = -ETIMEDOUT;
1113 else if (ac12 & (ACCE | ACEB | ACIE))
1114 error = -EILSEQ;
1115 host->mrq->sbc->error = error;
1116 hsmmc_command_incomplete(host, error, end_cmd);
1117 }
1118 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1119 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301120 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301121 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301122 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001123 }
1124 }
1125
Francesco Lavra7472bab2013-06-29 08:25:12 +02001126 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301127 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001128 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301129 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001130 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001131}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001132
Adrian Hunterb4175772010-05-26 14:42:06 -07001133/*
1134 * MMC controller IRQ handler
1135 */
1136static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1137{
1138 struct omap_hsmmc_host *host = dev_id;
1139 int status;
1140
1141 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001142 while (status & (INT_EN_MASK | CIRQ_EN)) {
1143 if (host->req_in_progress)
1144 omap_hsmmc_do_irq(host, status);
1145
1146 if (status & CIRQ_EN)
1147 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301148
Adrian Hunterb4175772010-05-26 14:42:06 -07001149 /* Flush posted write */
1150 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301151 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001152
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001153 return IRQ_HANDLED;
1154}
1155
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001156static irqreturn_t omap_hsmmc_wake_irq(int irq, void *dev_id)
1157{
1158 struct omap_hsmmc_host *host = dev_id;
1159
1160 /* cirq is level triggered, disable to avoid infinite loop */
1161 spin_lock(&host->irq_lock);
1162 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
1163 disable_irq_nosync(host->wake_irq);
1164 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
1165 }
1166 spin_unlock(&host->irq_lock);
1167 pm_request_resume(host->dev); /* no use counter */
1168
1169 return IRQ_HANDLED;
1170}
1171
Denis Karpov70a33412009-09-22 16:44:59 -07001172static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001173{
1174 unsigned long i;
1175
1176 OMAP_HSMMC_WRITE(host->base, HCTL,
1177 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1178 for (i = 0; i < loops_per_jiffy; i++) {
1179 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1180 break;
1181 cpu_relax();
1182 }
1183}
1184
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001185/*
David Brownelleb250822009-02-17 14:49:01 -08001186 * Switch MMC interface voltage ... only relevant for MMC1.
1187 *
1188 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1189 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1190 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001191 */
Denis Karpov70a33412009-09-22 16:44:59 -07001192static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001193{
1194 u32 reg_val = 0;
1195 int ret;
1196
1197 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301198 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301199 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301200 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001201
1202 /* Turn the power off */
1203 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001204
1205 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001206 if (!ret)
1207 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1208 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301209 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301210 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301211 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001212
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001213 if (ret != 0)
1214 goto err;
1215
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001216 OMAP_HSMMC_WRITE(host->base, HCTL,
1217 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1218 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001219
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001220 /*
1221 * If a MMC dual voltage card is detected, the set_ios fn calls
1222 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001223 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001224 *
David Brownelleb250822009-02-17 14:49:01 -08001225 * Cope with a bit of slop in the range ... per data sheets:
1226 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1227 * but recommended values are 1.71V to 1.89V
1228 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1229 * but recommended values are 2.7V to 3.3V
1230 *
1231 * Board setup code shouldn't permit anything very out-of-range.
1232 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1233 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001234 */
David Brownelleb250822009-02-17 14:49:01 -08001235 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001236 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001237 else
1238 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001239
1240 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001241 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001242
1243 return 0;
1244err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301245 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001246 return ret;
1247}
1248
Adrian Hunterb62f6222009-09-22 16:45:01 -07001249/* Protect the card while the cover is open */
1250static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1251{
1252 if (!mmc_slot(host).get_cover_state)
1253 return;
1254
1255 host->reqs_blocked = 0;
1256 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1257 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301258 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001259 "card is now accessible\n",
1260 mmc_hostname(host->mmc));
1261 host->protect_card = 0;
1262 }
1263 } else {
1264 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301265 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001266 "card is now inaccessible\n",
1267 mmc_hostname(host->mmc));
1268 host->protect_card = 1;
1269 }
1270 }
1271}
1272
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001273/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001274 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001275 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001276static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001277{
NeilBrown7efab4f2011-12-30 12:35:13 +11001278 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001279 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001280 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001281
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001282 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001283
Denis Karpov191d1f12009-09-22 16:44:55 -07001284 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001285 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001286 else {
1287 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001288 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001289 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001290
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001291 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001292 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001293 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001294 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001295 return IRQ_HANDLED;
1296}
1297
Russell Kingc5c98922012-04-13 12:14:39 +01001298static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001299{
Russell Kingc5c98922012-04-13 12:14:39 +01001300 struct omap_hsmmc_host *host = param;
1301 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001302 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001303 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001304
Russell Kingc5c98922012-04-13 12:14:39 +01001305 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001306 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001307 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001308 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001309 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001310
Adrian Hunter770d7432011-05-06 12:14:11 +03001311 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001312 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001313 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001314 dma_unmap_sg(chan->device->dev,
1315 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001316 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001317
1318 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001319 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001320 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001321
1322 /* If DMA has finished after TC, complete the request */
1323 if (!req_in_progress) {
1324 struct mmc_request *mrq = host->mrq;
1325
1326 host->mrq = NULL;
1327 mmc_request_done(host->mmc, mrq);
1328 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001329}
1330
Per Forlin9782aff2011-07-01 18:55:23 +02001331static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1332 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001333 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001334 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001335{
1336 int dma_len;
1337
1338 if (!next && data->host_cookie &&
1339 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301340 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001341 " host->next_data.cookie %d\n",
1342 __func__, data->host_cookie, host->next_data.cookie);
1343 data->host_cookie = 0;
1344 }
1345
1346 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001347 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001348 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001349 omap_hsmmc_get_dma_dir(host, data));
1350
1351 } else {
1352 dma_len = host->next_data.dma_len;
1353 host->next_data.dma_len = 0;
1354 }
1355
1356
1357 if (dma_len == 0)
1358 return -EINVAL;
1359
1360 if (next) {
1361 next->dma_len = dma_len;
1362 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1363 } else
1364 host->dma_len = dma_len;
1365
1366 return 0;
1367}
1368
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001369/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001370 * Routine to configure and start DMA for the MMC card
1371 */
Balaji T K9d025332014-01-21 19:54:42 +05301372static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001373 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001374{
Russell King26b88522012-04-13 12:27:37 +01001375 struct dma_slave_config cfg;
1376 struct dma_async_tx_descriptor *tx;
1377 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001378 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001379 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001380
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001381 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001382 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001383 struct scatterlist *sgl;
1384
1385 sgl = data->sg + i;
1386 if (sgl->length % data->blksz)
1387 return -EINVAL;
1388 }
1389 if ((data->blksz % 4) != 0)
1390 /* REVISIT: The MMC buffer increments only when MSB is written.
1391 * Return error for blksz which is non multiple of four.
1392 */
1393 return -EINVAL;
1394
Adrian Hunterb4175772010-05-26 14:42:06 -07001395 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001396
Russell Kingc5c98922012-04-13 12:14:39 +01001397 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001398
Russell King26b88522012-04-13 12:27:37 +01001399 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1400 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1401 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1402 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1403 cfg.src_maxburst = data->blksz / 4;
1404 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001405
Russell King26b88522012-04-13 12:27:37 +01001406 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001407 if (ret)
1408 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001409
Russell King26b88522012-04-13 12:27:37 +01001410 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1411 if (ret)
1412 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001413
Russell King26b88522012-04-13 12:27:37 +01001414 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1415 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1416 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1417 if (!tx) {
1418 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1419 /* FIXME: cleanup */
1420 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001421 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001422
Russell King26b88522012-04-13 12:27:37 +01001423 tx->callback = omap_hsmmc_dma_callback;
1424 tx->callback_param = host;
1425
1426 /* Does not fail */
1427 dmaengine_submit(tx);
1428
1429 host->dma_ch = 1;
1430
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001431 return 0;
1432}
1433
Denis Karpov70a33412009-09-22 16:44:59 -07001434static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001435 unsigned int timeout_ns,
1436 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001437{
1438 unsigned int timeout, cycle_ns;
1439 uint32_t reg, clkd, dto = 0;
1440
1441 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1442 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1443 if (clkd == 0)
1444 clkd = 1;
1445
Balaji T K6e3076c2014-01-21 19:54:42 +05301446 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001447 timeout = timeout_ns / cycle_ns;
1448 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001449 if (timeout) {
1450 while ((timeout & 0x80000000) == 0) {
1451 dto += 1;
1452 timeout <<= 1;
1453 }
1454 dto = 31 - dto;
1455 timeout <<= 1;
1456 if (timeout && dto)
1457 dto += 1;
1458 if (dto >= 13)
1459 dto -= 13;
1460 else
1461 dto = 0;
1462 if (dto > 14)
1463 dto = 14;
1464 }
1465
1466 reg &= ~DTO_MASK;
1467 reg |= dto << DTO_SHIFT;
1468 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1469}
1470
Balaji T K9d025332014-01-21 19:54:42 +05301471static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1472{
1473 struct mmc_request *req = host->mrq;
1474 struct dma_chan *chan;
1475
1476 if (!req->data)
1477 return;
1478 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1479 | (req->data->blocks << 16));
1480 set_data_timeout(host, req->data->timeout_ns,
1481 req->data->timeout_clks);
1482 chan = omap_hsmmc_get_dma_chan(host, req->data);
1483 dma_async_issue_pending(chan);
1484}
1485
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001486/*
1487 * Configure block length for MMC/SD cards and initiate the transfer.
1488 */
1489static int
Denis Karpov70a33412009-09-22 16:44:59 -07001490omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001491{
1492 int ret;
1493 host->data = req->data;
1494
1495 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001496 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001497 /*
1498 * Set an arbitrary 100ms data timeout for commands with
1499 * busy signal.
1500 */
1501 if (req->cmd->flags & MMC_RSP_BUSY)
1502 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001503 return 0;
1504 }
1505
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001506 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301507 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001508 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301509 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001510 return ret;
1511 }
1512 }
1513 return 0;
1514}
1515
Per Forlin9782aff2011-07-01 18:55:23 +02001516static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1517 int err)
1518{
1519 struct omap_hsmmc_host *host = mmc_priv(mmc);
1520 struct mmc_data *data = mrq->data;
1521
Russell King26b88522012-04-13 12:27:37 +01001522 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001523 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001524
Russell King26b88522012-04-13 12:27:37 +01001525 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1526 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001527 data->host_cookie = 0;
1528 }
1529}
1530
1531static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1532 bool is_first_req)
1533{
1534 struct omap_hsmmc_host *host = mmc_priv(mmc);
1535
1536 if (mrq->data->host_cookie) {
1537 mrq->data->host_cookie = 0;
1538 return ;
1539 }
1540
Russell Kingc5c98922012-04-13 12:14:39 +01001541 if (host->use_dma) {
1542 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001543
Per Forlin9782aff2011-07-01 18:55:23 +02001544 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001545 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001546 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001547 }
Per Forlin9782aff2011-07-01 18:55:23 +02001548}
1549
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001550/*
1551 * Request function. for read/write operation
1552 */
Denis Karpov70a33412009-09-22 16:44:59 -07001553static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001554{
Denis Karpov70a33412009-09-22 16:44:59 -07001555 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001556 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001557
Adrian Hunterb4175772010-05-26 14:42:06 -07001558 BUG_ON(host->req_in_progress);
1559 BUG_ON(host->dma_ch != -1);
1560 if (host->protect_card) {
1561 if (host->reqs_blocked < 3) {
1562 /*
1563 * Ensure the controller is left in a consistent
1564 * state by resetting the command and data state
1565 * machines.
1566 */
1567 omap_hsmmc_reset_controller_fsm(host, SRD);
1568 omap_hsmmc_reset_controller_fsm(host, SRC);
1569 host->reqs_blocked += 1;
1570 }
1571 req->cmd->error = -EBADF;
1572 if (req->data)
1573 req->data->error = -EBADF;
1574 req->cmd->retries = 0;
1575 mmc_request_done(mmc, req);
1576 return;
1577 } else if (host->reqs_blocked)
1578 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001579 WARN_ON(host->mrq != NULL);
1580 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301581 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001582 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001583 if (err) {
1584 req->cmd->error = err;
1585 if (req->data)
1586 req->data->error = err;
1587 host->mrq = NULL;
1588 mmc_request_done(mmc, req);
1589 return;
1590 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301591 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301592 omap_hsmmc_start_command(host, req->sbc, NULL);
1593 return;
1594 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001595
Balaji T K9d025332014-01-21 19:54:42 +05301596 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001597 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001598}
1599
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001600/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001601static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001602{
Denis Karpov70a33412009-09-22 16:44:59 -07001603 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001604 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001605
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301606 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001607
Adrian Huntera3621462009-09-22 16:44:42 -07001608 if (ios->power_mode != host->power_mode) {
1609 switch (ios->power_mode) {
1610 case MMC_POWER_OFF:
1611 mmc_slot(host).set_power(host->dev, host->slot_id,
1612 0, 0);
1613 break;
1614 case MMC_POWER_UP:
1615 mmc_slot(host).set_power(host->dev, host->slot_id,
1616 1, ios->vdd);
1617 break;
1618 case MMC_POWER_ON:
1619 do_send_init_stream = 1;
1620 break;
1621 }
1622 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001623 }
1624
Denis Karpovdd498ef2009-09-22 16:44:49 -07001625 /* FIXME: set registers based only on changes to ios */
1626
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001627 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001628
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301629 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001630 /* Only MMC1 can interface at 3V without some flavor
1631 * of external transceiver; but they all handle 1.8V.
1632 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001633 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301634 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001635 /*
1636 * The mmc_select_voltage fn of the core does
1637 * not seem to set the power_mode to
1638 * MMC_POWER_UP upon recalculating the voltage.
1639 * vdd 1.8v.
1640 */
Denis Karpov70a33412009-09-22 16:44:59 -07001641 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1642 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001643 "Switch operation failed\n");
1644 }
1645 }
1646
Andy Shevchenko5934df22011-05-06 12:14:06 +03001647 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001648
Adrian Huntera3621462009-09-22 16:44:42 -07001649 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001650 send_init_stream(host);
1651
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001652 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001653
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301654 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001655}
1656
1657static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1658{
Denis Karpov70a33412009-09-22 16:44:59 -07001659 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001660
Denis Karpov191d1f12009-09-22 16:44:55 -07001661 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001662 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001663 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001664}
1665
1666static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1667{
Denis Karpov70a33412009-09-22 16:44:59 -07001668 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001669
Denis Karpov191d1f12009-09-22 16:44:55 -07001670 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001671 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001672 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001673}
1674
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001675static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1676{
1677 struct omap_hsmmc_host *host = mmc_priv(mmc);
1678
1679 if (mmc_slot(host).init_card)
1680 mmc_slot(host).init_card(card);
1681}
1682
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001683static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1684{
1685 struct omap_hsmmc_host *host = mmc_priv(mmc);
1686 u32 irq_mask;
1687 unsigned long flags;
1688
1689 spin_lock_irqsave(&host->irq_lock, flags);
1690
1691 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1692 if (enable) {
1693 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1694 irq_mask |= CIRQ_EN;
1695 } else {
1696 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1697 irq_mask &= ~CIRQ_EN;
1698 }
1699 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1700
1701 /*
1702 * if enable, piggy back detection on current request
1703 * but always disable immediately
1704 */
1705 if (!host->req_in_progress || !enable)
1706 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1707
1708 /* flush posted write */
1709 OMAP_HSMMC_READ(host->base, IE);
1710
1711 spin_unlock_irqrestore(&host->irq_lock, flags);
1712}
1713
1714static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1715{
1716 struct mmc_host *mmc = host->mmc;
1717 int ret;
1718
1719 /*
1720 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1721 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1722 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1723 * with functional clock disabled.
1724 */
1725 if (!host->dev->of_node || !host->wake_irq)
1726 return -ENODEV;
1727
1728 /* Prevent auto-enabling of IRQ */
1729 irq_set_status_flags(host->wake_irq, IRQ_NOAUTOEN);
1730 ret = devm_request_irq(host->dev, host->wake_irq, omap_hsmmc_wake_irq,
1731 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1732 mmc_hostname(mmc), host);
1733 if (ret) {
1734 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1735 goto err;
1736 }
1737
1738 /*
1739 * Some omaps don't have wake-up path from deeper idle states
1740 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1741 */
1742 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
1743 ret = -ENODEV;
1744 devm_free_irq(host->dev, host->wake_irq, host);
1745 goto err;
1746 }
1747
1748 return 0;
1749
1750err:
1751 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1752 host->wake_irq = 0;
1753 return ret;
1754}
1755
Denis Karpov70a33412009-09-22 16:44:59 -07001756static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001757{
1758 u32 hctl, capa, value;
1759
1760 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301761 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001762 hctl = SDVS30;
1763 capa = VS30 | VS18;
1764 } else {
1765 hctl = SDVS18;
1766 capa = VS18;
1767 }
1768
1769 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1770 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1771
1772 value = OMAP_HSMMC_READ(host->base, CAPA);
1773 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1774
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001775 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001776 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001777}
1778
Denis Karpov70a33412009-09-22 16:44:59 -07001779static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001780{
Denis Karpov70a33412009-09-22 16:44:59 -07001781 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001782
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301783 pm_runtime_get_sync(host->dev);
1784
Denis Karpovdd498ef2009-09-22 16:44:49 -07001785 return 0;
1786}
1787
Adrian Hunter907d2e72012-02-29 09:17:21 +02001788static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001789{
Denis Karpov70a33412009-09-22 16:44:59 -07001790 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001791
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301792 pm_runtime_mark_last_busy(host->dev);
1793 pm_runtime_put_autosuspend(host->dev);
1794
Denis Karpovdd498ef2009-09-22 16:44:49 -07001795 return 0;
1796}
1797
Denis Karpov70a33412009-09-22 16:44:59 -07001798static const struct mmc_host_ops omap_hsmmc_ops = {
1799 .enable = omap_hsmmc_enable_fclk,
1800 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001801 .post_req = omap_hsmmc_post_req,
1802 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001803 .request = omap_hsmmc_request,
1804 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001805 .get_cd = omap_hsmmc_get_cd,
1806 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001807 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001808 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001809};
1810
Denis Karpovd900f712009-09-22 16:44:38 -07001811#ifdef CONFIG_DEBUG_FS
1812
Denis Karpov70a33412009-09-22 16:44:59 -07001813static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001814{
1815 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001816 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001817
Tony Lindgren0a82e062013-10-21 00:25:19 +05301818 seq_printf(s, "mmc%d:\n ctx_loss:\t%d\n\nregs:\n",
1819 mmc->index, host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001820
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301821 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001822
Denis Karpovd900f712009-09-22 16:44:38 -07001823 seq_printf(s, "CON:\t\t0x%08x\n",
1824 OMAP_HSMMC_READ(host->base, CON));
1825 seq_printf(s, "HCTL:\t\t0x%08x\n",
1826 OMAP_HSMMC_READ(host->base, HCTL));
1827 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1828 OMAP_HSMMC_READ(host->base, SYSCTL));
1829 seq_printf(s, "IE:\t\t0x%08x\n",
1830 OMAP_HSMMC_READ(host->base, IE));
1831 seq_printf(s, "ISE:\t\t0x%08x\n",
1832 OMAP_HSMMC_READ(host->base, ISE));
1833 seq_printf(s, "CAPA:\t\t0x%08x\n",
1834 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001835
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301836 pm_runtime_mark_last_busy(host->dev);
1837 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001838
Denis Karpovd900f712009-09-22 16:44:38 -07001839 return 0;
1840}
1841
Denis Karpov70a33412009-09-22 16:44:59 -07001842static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001843{
Denis Karpov70a33412009-09-22 16:44:59 -07001844 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001845}
1846
1847static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001848 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001849 .read = seq_read,
1850 .llseek = seq_lseek,
1851 .release = single_release,
1852};
1853
Denis Karpov70a33412009-09-22 16:44:59 -07001854static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001855{
1856 if (mmc->debugfs_root)
1857 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1858 mmc, &mmc_regs_fops);
1859}
1860
1861#else
1862
Denis Karpov70a33412009-09-22 16:44:59 -07001863static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001864{
1865}
1866
1867#endif
1868
Rajendra Nayak46856a62012-03-12 20:32:37 +05301869#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001870static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1871 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1872 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1873};
1874
1875static const struct omap_mmc_of_data omap4_mmc_of_data = {
1876 .reg_offset = 0x100,
1877};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001878static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1879 .reg_offset = 0x100,
1880 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1881};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301882
1883static const struct of_device_id omap_mmc_of_match[] = {
1884 {
1885 .compatible = "ti,omap2-hsmmc",
1886 },
1887 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001888 .compatible = "ti,omap3-pre-es3-hsmmc",
1889 .data = &omap3_pre_es3_mmc_of_data,
1890 },
1891 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301892 .compatible = "ti,omap3-hsmmc",
1893 },
1894 {
1895 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001896 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301897 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001898 {
1899 .compatible = "ti,am33xx-hsmmc",
1900 .data = &am33xx_mmc_of_data,
1901 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301902 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001903};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301904MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1905
1906static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1907{
1908 struct omap_mmc_platform_data *pdata;
1909 struct device_node *np = dev->of_node;
Daniel Mackd8714e82012-10-15 21:35:06 +05301910 u32 bus_width, max_freq;
Jan Luebbedc642c22013-01-30 10:07:17 +01001911 int cd_gpio, wp_gpio;
1912
1913 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
1914 wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1915 if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
1916 return ERR_PTR(-EPROBE_DEFER);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301917
1918 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1919 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301920 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301921
1922 if (of_find_property(np, "ti,dual-volt", NULL))
1923 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1924
1925 /* This driver only supports 1 slot */
1926 pdata->nr_slots = 1;
Jan Luebbedc642c22013-01-30 10:07:17 +01001927 pdata->slots[0].switch_pin = cd_gpio;
1928 pdata->slots[0].gpio_wp = wp_gpio;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301929
1930 if (of_find_property(np, "ti,non-removable", NULL)) {
1931 pdata->slots[0].nonremovable = true;
1932 pdata->slots[0].no_regulator_off_init = true;
1933 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001934 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301935 if (bus_width == 4)
1936 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1937 else if (bus_width == 8)
1938 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1939
1940 if (of_find_property(np, "ti,needs-special-reset", NULL))
1941 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1942
Daniel Mackd8714e82012-10-15 21:35:06 +05301943 if (!of_property_read_u32(np, "max-frequency", &max_freq))
1944 pdata->max_freq = max_freq;
1945
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301946 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
1947 pdata->slots[0].features |= HSMMC_HAS_HSPE_SUPPORT;
1948
Daniel Mackc9ae64d2014-02-17 12:36:33 +01001949 if (of_find_property(np, "keep-power-in-suspend", NULL))
1950 pdata->slots[0].pm_caps |= MMC_PM_KEEP_POWER;
1951
1952 if (of_find_property(np, "enable-sdio-wakeup", NULL))
1953 pdata->slots[0].pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
1954
Rajendra Nayak46856a62012-03-12 20:32:37 +05301955 return pdata;
1956}
1957#else
1958static inline struct omap_mmc_platform_data
1959 *of_get_hsmmc_pdata(struct device *dev)
1960{
Balaji T K19df45b2014-02-28 19:08:18 +05301961 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301962}
1963#endif
1964
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001965static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001966{
1967 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1968 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001969 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001970 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001971 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301972 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001973 dma_cap_mask_t mask;
1974 unsigned tx_req, rx_req;
Daniel Mack46b76032012-10-15 21:35:05 +05301975 struct pinctrl *pinctrl;
Nishanth Menon59445b12014-02-13 23:45:48 -06001976 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05301977 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301978
1979 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1980 if (match) {
1981 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01001982
1983 if (IS_ERR(pdata))
1984 return PTR_ERR(pdata);
1985
Rajendra Nayak46856a62012-03-12 20:32:37 +05301986 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06001987 data = match->data;
1988 pdata->reg_offset = data->reg_offset;
1989 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301990 }
1991 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001992
1993 if (pdata == NULL) {
1994 dev_err(&pdev->dev, "Platform Data is missing\n");
1995 return -ENXIO;
1996 }
1997
1998 if (pdata->nr_slots == 0) {
1999 dev_err(&pdev->dev, "No Slots\n");
2000 return -ENXIO;
2001 }
2002
2003 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2004 irq = platform_get_irq(pdev, 0);
2005 if (res == NULL || irq < 0)
2006 return -ENXIO;
2007
Balaji T K77fae212014-05-09 22:16:51 +05302008 base = devm_ioremap_resource(&pdev->dev, res);
2009 if (IS_ERR(base))
2010 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002011
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002012 ret = omap_hsmmc_gpio_init(pdata);
2013 if (ret)
2014 goto err;
2015
Denis Karpov70a33412009-09-22 16:44:59 -07002016 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002017 if (!mmc) {
2018 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002019 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002020 }
2021
2022 host = mmc_priv(mmc);
2023 host->mmc = mmc;
2024 host->pdata = pdata;
2025 host->dev = &pdev->dev;
2026 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002027 host->dma_ch = -1;
2028 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002029 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05302030 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05302031 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08002032 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002033 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05302034 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002035
2036 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002037
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002038 if (pdev->dev.of_node)
2039 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2040
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302041 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002042
Daniel Mackd418ed82012-02-19 13:20:33 +01002043 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2044
2045 if (pdata->max_freq > 0)
2046 mmc->f_max = pdata->max_freq;
2047 else
2048 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002049
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002050 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002051
Balaji T K96181952014-05-09 22:16:48 +05302052 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002053 if (IS_ERR(host->fclk)) {
2054 ret = PTR_ERR(host->fclk);
2055 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002056 goto err1;
2057 }
2058
Paul Walmsley9b682562011-10-06 14:50:35 -06002059 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2060 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
2061 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
2062 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002063
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302064 pm_runtime_enable(host->dev);
2065 pm_runtime_get_sync(host->dev);
2066 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2067 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002068
Balaji T K92a3aeb2012-02-24 21:14:34 +05302069 omap_hsmmc_context_save(host);
2070
Balaji T K96181952014-05-09 22:16:48 +05302071 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302072 /*
2073 * MMC can still work without debounce clock.
2074 */
2075 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302076 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302077 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302078 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302079 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002080 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002081
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002082 /* Since we do only SG emulation, we can have as many segs
2083 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002084 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002085
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002086 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2087 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2088 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2089 mmc->max_seg_size = mmc->max_req_size;
2090
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002091 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002092 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002093
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002094 mmc->caps |= mmc_slot(host).caps;
2095 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002096 mmc->caps |= MMC_CAP_4_BIT_DATA;
2097
Denis Karpov191d1f12009-09-22 16:44:55 -07002098 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002099 mmc->caps |= MMC_CAP_NONREMOVABLE;
2100
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002101 mmc->pm_caps = mmc_slot(host).pm_caps;
2102
Denis Karpov70a33412009-09-22 16:44:59 -07002103 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002104
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302105 if (!pdev->dev.of_node) {
2106 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2107 if (!res) {
2108 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2109 ret = -ENXIO;
2110 goto err_irq;
2111 }
2112 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002113
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302114 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2115 if (!res) {
2116 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2117 ret = -ENXIO;
2118 goto err_irq;
2119 }
2120 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002121 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002122
Russell King26b88522012-04-13 12:27:37 +01002123 dma_cap_zero(mask);
2124 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002125
Matt Porterd272fbf2013-05-10 17:42:34 +05302126 host->rx_chan =
2127 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2128 &rx_req, &pdev->dev, "rx");
2129
Russell King26b88522012-04-13 12:27:37 +01002130 if (!host->rx_chan) {
2131 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002132 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002133 goto err_irq;
2134 }
2135
Matt Porterd272fbf2013-05-10 17:42:34 +05302136 host->tx_chan =
2137 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2138 &tx_req, &pdev->dev, "tx");
2139
Russell King26b88522012-04-13 12:27:37 +01002140 if (!host->tx_chan) {
2141 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002142 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002143 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002144 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002145
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002146 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302147 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002148 mmc_hostname(mmc), host);
2149 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302150 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002151 goto err_irq;
2152 }
2153
2154 if (pdata->init != NULL) {
2155 if (pdata->init(&pdev->dev) != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302156 dev_err(mmc_dev(host->mmc),
Denis Karpov70a33412009-09-22 16:44:59 -07002157 "Unable to configure MMC IRQs\n");
Balaji T Ke1538ed2014-05-09 22:16:49 +05302158 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002159 }
2160 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002161
Adrian Hunterb702b102010-02-15 10:03:35 -08002162 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002163 ret = omap_hsmmc_reg_get(host);
2164 if (ret)
2165 goto err_reg;
2166 host->use_reg = 1;
2167 }
2168
David Brownellb583f262009-05-28 14:04:03 -07002169 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002170
2171 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002172 if ((mmc_slot(host).card_detect_irq)) {
Balaji T K9fa0e052014-05-09 22:16:50 +05302173 ret = devm_request_threaded_irq(&pdev->dev,
2174 mmc_slot(host).card_detect_irq,
2175 NULL, omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08002176 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11002177 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002178 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302179 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002180 "Unable to grab MMC CD IRQ\n");
2181 goto err_irq_cd;
2182 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00002183 pdata->suspend = omap_hsmmc_suspend_cdirq;
2184 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002185 }
2186
Adrian Hunterb4175772010-05-26 14:42:06 -07002187 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002188
Daniel Mack46b76032012-10-15 21:35:05 +05302189 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
2190 if (IS_ERR(pinctrl))
2191 dev_warn(&pdev->dev,
2192 "pins are not configured from the driver\n");
2193
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002194 /*
2195 * For now, only support SDIO interrupt if we have a separate
2196 * wake-up interrupt configured from device tree. This is because
2197 * the wake-up interrupt is needed for idle state and some
2198 * platforms need special quirks. And we don't want to add new
2199 * legacy mux platform init code callbacks any longer as we
2200 * are moving to DT based booting anyways.
2201 */
2202 ret = omap_hsmmc_configure_wake_irq(host);
2203 if (!ret)
2204 mmc->caps |= MMC_CAP_SDIO_IRQ;
2205
Adrian Hunterb62f6222009-09-22 16:45:01 -07002206 omap_hsmmc_protect_card(host);
2207
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002208 mmc_add_host(mmc);
2209
Denis Karpov191d1f12009-09-22 16:44:55 -07002210 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002211 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2212 if (ret < 0)
2213 goto err_slot_name;
2214 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002215 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002216 ret = device_create_file(&mmc->class_dev,
2217 &dev_attr_cover_switch);
2218 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002219 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002220 }
2221
Denis Karpov70a33412009-09-22 16:44:59 -07002222 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302223 pm_runtime_mark_last_busy(host->dev);
2224 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002225
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002226 return 0;
2227
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002228err_slot_name:
2229 mmc_remove_host(mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002230err_irq_cd:
2231 if (host->use_reg)
2232 omap_hsmmc_reg_put(host);
2233err_reg:
2234 if (host->pdata->cleanup)
2235 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002236err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002237 if (host->tx_chan)
2238 dma_release_channel(host->tx_chan);
2239 if (host->rx_chan)
2240 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302241 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002242 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302243 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302244 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002245err1:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002246 mmc_free_host(mmc);
2247err_alloc:
2248 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002249err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002250 return ret;
2251}
2252
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002253static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002254{
Denis Karpov70a33412009-09-22 16:44:59 -07002255 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002256
Felipe Balbi927ce942012-03-14 11:18:27 +02002257 pm_runtime_get_sync(host->dev);
2258 mmc_remove_host(host->mmc);
2259 if (host->use_reg)
2260 omap_hsmmc_reg_put(host);
2261 if (host->pdata->cleanup)
2262 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002263
Russell Kingc5c98922012-04-13 12:14:39 +01002264 if (host->tx_chan)
2265 dma_release_channel(host->tx_chan);
2266 if (host->rx_chan)
2267 dma_release_channel(host->rx_chan);
2268
Felipe Balbi927ce942012-03-14 11:18:27 +02002269 pm_runtime_put_sync(host->dev);
2270 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302271 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302272 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002273
Balaji T K9ea28ec2012-10-15 21:35:08 +05302274 omap_hsmmc_gpio_free(host->pdata);
Balaji T K9d1f0282012-10-15 21:35:07 +05302275 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002276
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002277 return 0;
2278}
2279
2280#ifdef CONFIG_PM
Felipe Balbia48ce882012-11-19 21:59:59 +05302281static int omap_hsmmc_prepare(struct device *dev)
2282{
2283 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2284
2285 if (host->pdata->suspend)
2286 return host->pdata->suspend(dev, host->slot_id);
2287
2288 return 0;
2289}
2290
2291static void omap_hsmmc_complete(struct device *dev)
2292{
2293 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2294
2295 if (host->pdata->resume)
2296 host->pdata->resume(dev, host->slot_id);
2297
2298}
2299
Kevin Hilmana791daa2010-05-26 14:42:07 -07002300static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002301{
Felipe Balbi927ce942012-03-14 11:18:27 +02002302 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2303
2304 if (!host)
2305 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002306
Felipe Balbi927ce942012-03-14 11:18:27 +02002307 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002308
2309 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002310 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2311 OMAP_HSMMC_WRITE(host->base, IE, 0);
2312 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002313 OMAP_HSMMC_WRITE(host->base, HCTL,
2314 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2315 }
2316
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002317 /* do not wake up due to sdio irq */
2318 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2319 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2320 disable_irq(host->wake_irq);
2321
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302322 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302323 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002324
Eliad Peller31f9d462011-11-22 16:02:17 +02002325 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002326 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002327}
2328
2329/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002330static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002331{
Felipe Balbi927ce942012-03-14 11:18:27 +02002332 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2333
2334 if (!host)
2335 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002336
Felipe Balbi927ce942012-03-14 11:18:27 +02002337 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002338
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302339 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302340 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002341
Felipe Balbi927ce942012-03-14 11:18:27 +02002342 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2343 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002344
Felipe Balbi927ce942012-03-14 11:18:27 +02002345 omap_hsmmc_protect_card(host);
2346
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002347 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2348 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2349 enable_irq(host->wake_irq);
2350
Felipe Balbi927ce942012-03-14 11:18:27 +02002351 pm_runtime_mark_last_busy(host->dev);
2352 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002353 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002354}
2355
2356#else
Felipe Balbia48ce882012-11-19 21:59:59 +05302357#define omap_hsmmc_prepare NULL
2358#define omap_hsmmc_complete NULL
Denis Karpov70a33412009-09-22 16:44:59 -07002359#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302360#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002361#endif
2362
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302363static int omap_hsmmc_runtime_suspend(struct device *dev)
2364{
2365 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002366 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302367
2368 host = platform_get_drvdata(to_platform_device(dev));
2369 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002370 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302371
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002372 spin_lock_irqsave(&host->irq_lock, flags);
2373 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2374 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2375 /* disable sdio irq handling to prevent race */
2376 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2377 OMAP_HSMMC_WRITE(host->base, IE, 0);
2378 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2379
2380 WARN_ON(host->flags & HSMMC_WAKE_IRQ_ENABLED);
2381 enable_irq(host->wake_irq);
2382 host->flags |= HSMMC_WAKE_IRQ_ENABLED;
2383 }
2384 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302385 return 0;
2386}
2387
2388static int omap_hsmmc_runtime_resume(struct device *dev)
2389{
2390 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002391 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302392
2393 host = platform_get_drvdata(to_platform_device(dev));
2394 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002395 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302396
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002397 spin_lock_irqsave(&host->irq_lock, flags);
2398 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2399 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2400 /* sdio irq flag can't change while in runtime suspend */
2401 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
2402 disable_irq_nosync(host->wake_irq);
2403 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
2404 }
2405
2406 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2407 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2408 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2409 }
2410 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302411 return 0;
2412}
2413
Kevin Hilmana791daa2010-05-26 14:42:07 -07002414static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002415 .suspend = omap_hsmmc_suspend,
2416 .resume = omap_hsmmc_resume,
Felipe Balbia48ce882012-11-19 21:59:59 +05302417 .prepare = omap_hsmmc_prepare,
2418 .complete = omap_hsmmc_complete,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302419 .runtime_suspend = omap_hsmmc_runtime_suspend,
2420 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002421};
2422
2423static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002424 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002425 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002426 .driver = {
2427 .name = DRIVER_NAME,
2428 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002429 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302430 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002431 },
2432};
2433
Felipe Balbib7964502012-03-14 11:18:32 +02002434module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002435MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2436MODULE_LICENSE("GPL");
2437MODULE_ALIAS("platform:" DRIVER_NAME);
2438MODULE_AUTHOR("Texas Instruments Inc");